mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 21:51:21 +00:00
update get-page, cleanup types
This commit is contained in:
parent
bb321e2237
commit
ff0a468b02
1
framework/swell/api/endpoints/cart.ts
Normal file
1
framework/swell/api/endpoints/cart.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function (_commerce: any) {}
|
1
framework/swell/api/endpoints/catalog/products.ts
Normal file
1
framework/swell/api/endpoints/catalog/products.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function (_commerce: any) {}
|
1
framework/swell/api/endpoints/customer.ts
Normal file
1
framework/swell/api/endpoints/customer.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function (_commerce: any) {}
|
1
framework/swell/api/endpoints/login.ts
Normal file
1
framework/swell/api/endpoints/login.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function (_commerce: any) {}
|
1
framework/swell/api/endpoints/logout.ts
Normal file
1
framework/swell/api/endpoints/logout.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function (_commerce: any) {}
|
1
framework/swell/api/endpoints/signup.ts
Normal file
1
framework/swell/api/endpoints/signup.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function (_commerce: any) {}
|
1
framework/swell/api/endpoints/wishlist.ts
Normal file
1
framework/swell/api/endpoints/wishlist.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function (_commerce: any) {}
|
@ -1,6 +1,7 @@
|
|||||||
import { Page } from '../../schema'
|
import { Page } from '../../schema'
|
||||||
import { SwellConfig, Provider } from '..'
|
import { SwellConfig, Provider } from '..'
|
||||||
import { OperationContext } from '@commerce/api/operations'
|
import { OperationContext, OperationOptions } from '@commerce/api/operations'
|
||||||
|
import { GetPageOperation } from '../../types/page'
|
||||||
|
|
||||||
export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
|
export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
|
||||||
|
|
||||||
@ -11,33 +12,42 @@ export type PageVariables = {
|
|||||||
export default function getPageOperation({
|
export default function getPageOperation({
|
||||||
commerce,
|
commerce,
|
||||||
}: OperationContext<Provider>) {
|
}: OperationContext<Provider>) {
|
||||||
async function getPage(opts: {
|
async function getPage<T extends GetPageOperation>(opts: {
|
||||||
url?: string
|
variables: T['variables']
|
||||||
variables: PageVariables
|
|
||||||
config?: Partial<SwellConfig>
|
config?: Partial<SwellConfig>
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
}): Promise<GetPageResult>
|
}): Promise<T['data']>
|
||||||
|
|
||||||
async function getPage<T extends { page?: any }, V = any>(opts: {
|
async function getPage<T extends GetPageOperation>(
|
||||||
url: string
|
opts: {
|
||||||
variables: V
|
variables: T['variables']
|
||||||
config?: Partial<SwellConfig>
|
config?: Partial<SwellConfig>
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
}): Promise<GetPageResult<T>>
|
} & OperationOptions
|
||||||
|
): Promise<T['data']>
|
||||||
|
|
||||||
async function getPage({
|
async function getPage<T extends GetPageOperation>({
|
||||||
url,
|
|
||||||
variables,
|
variables,
|
||||||
config: cfg,
|
config,
|
||||||
preview,
|
|
||||||
}: {
|
}: {
|
||||||
url?: string
|
query?: string
|
||||||
variables: PageVariables
|
variables: T['variables']
|
||||||
config?: Partial<SwellConfig>
|
config?: Partial<SwellConfig>
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
}): Promise<GetPageResult> {
|
}): Promise<T['data']> {
|
||||||
const config = commerce.getConfig(cfg)
|
const { fetch, locale = 'en-US' } = commerce.getConfig(config)
|
||||||
return {}
|
const id = variables.id
|
||||||
|
const result = await fetch('content', 'get', ['pages', id])
|
||||||
|
const page = result
|
||||||
|
|
||||||
|
return {
|
||||||
|
page: page
|
||||||
|
? {
|
||||||
|
...page,
|
||||||
|
url: `/${locale}/${page.slug}`,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return getPage
|
return getPage
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
|
|
||||||
import { SwellConfig, getConfig } from '..'
|
|
||||||
|
|
||||||
export type SwellApiHandler<
|
|
||||||
T = any,
|
|
||||||
H extends SwellHandlers = {},
|
|
||||||
Options extends {} = {}
|
|
||||||
> = (
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse<SwellApiResponse<T>>,
|
|
||||||
config: SwellConfig,
|
|
||||||
handlers: H,
|
|
||||||
// Custom configs that may be used by a particular handler
|
|
||||||
options: Options
|
|
||||||
) => void | Promise<void>
|
|
||||||
|
|
||||||
export type SwellHandler<T = any, Body = null> = (options: {
|
|
||||||
req: NextApiRequest
|
|
||||||
res: NextApiResponse<SwellApiResponse<T>>
|
|
||||||
config: SwellConfig
|
|
||||||
body: Body
|
|
||||||
}) => void | Promise<void>
|
|
||||||
|
|
||||||
export type SwellHandlers<T = any> = {
|
|
||||||
[k: string]: SwellHandler<T, any>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type SwellApiResponse<T> = {
|
|
||||||
data: T | null
|
|
||||||
errors?: { message: string; code?: string }[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function createApiHandler<
|
|
||||||
T = any,
|
|
||||||
H extends SwellHandlers = {},
|
|
||||||
Options extends {} = {}
|
|
||||||
>(
|
|
||||||
handler: SwellApiHandler<T, H, Options>,
|
|
||||||
handlers: H,
|
|
||||||
defaultOptions: Options
|
|
||||||
) {
|
|
||||||
return function getApiHandler({
|
|
||||||
config,
|
|
||||||
operations,
|
|
||||||
options,
|
|
||||||
}: {
|
|
||||||
config?: SwellConfig
|
|
||||||
operations?: Partial<H>
|
|
||||||
options?: Options extends {} ? Partial<Options> : never
|
|
||||||
} = {}): NextApiHandler {
|
|
||||||
const ops = { ...operations, ...handlers }
|
|
||||||
const opts = { ...defaultOptions, ...options }
|
|
||||||
|
|
||||||
return function apiHandler(req, res) {
|
|
||||||
return handler(req, res, getConfig(config), ops, opts)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
import { getConfig, SwellConfig } from '../api'
|
|
||||||
import { Page } from './get-all-pages'
|
|
||||||
|
|
||||||
type Variables = {
|
|
||||||
id: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
|
|
||||||
|
|
||||||
const getPage = async (options: {
|
|
||||||
variables: Variables
|
|
||||||
config: SwellConfig
|
|
||||||
preview?: boolean
|
|
||||||
}): Promise<GetPageResult> => {
|
|
||||||
let { config, variables } = options ?? {}
|
|
||||||
|
|
||||||
config = getConfig(config)
|
|
||||||
const { locale } = config
|
|
||||||
const { id } = variables
|
|
||||||
const result = await config.fetch('content', 'get', ['pages', id])
|
|
||||||
const page = result
|
|
||||||
|
|
||||||
return {
|
|
||||||
page: page
|
|
||||||
? {
|
|
||||||
...page,
|
|
||||||
url: `/${locale}/${page.slug}`,
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default getPage
|
|
@ -47,10 +47,15 @@ export type SwellVariant = {
|
|||||||
__type?: 'MultipleChoiceOption' | undefined
|
__type?: 'MultipleChoiceOption' | undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SwellProductOptionValue {
|
||||||
|
id: string
|
||||||
|
label: string
|
||||||
|
hexColors?: string[]
|
||||||
|
}
|
||||||
|
|
||||||
export interface ProductOptionValue {
|
export interface ProductOptionValue {
|
||||||
label: string
|
label: string
|
||||||
hexColors?: string[]
|
hexColors?: string[]
|
||||||
id: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ProductOptions = {
|
export type ProductOptions = {
|
||||||
|
@ -10,6 +10,7 @@ import type {
|
|||||||
SwellImage,
|
SwellImage,
|
||||||
SwellVariant,
|
SwellVariant,
|
||||||
ProductOptionValue,
|
ProductOptionValue,
|
||||||
|
SwellProductOptionValue,
|
||||||
SwellCart,
|
SwellCart,
|
||||||
LineItem,
|
LineItem,
|
||||||
} from '../types'
|
} from '../types'
|
||||||
@ -73,7 +74,7 @@ const normalizeProductImages = (images: SwellImage[]) => {
|
|||||||
|
|
||||||
const normalizeProductVariants = (
|
const normalizeProductVariants = (
|
||||||
variants: SwellVariant[],
|
variants: SwellVariant[],
|
||||||
productOptions: normalizedProductOption[]
|
productOptions: swellProductOption[]
|
||||||
) => {
|
) => {
|
||||||
return variants?.map(
|
return variants?.map(
|
||||||
({ id, name, price, option_value_ids: optionValueIds = [] }) => {
|
({ id, name, price, option_value_ids: optionValueIds = [] }) => {
|
||||||
@ -84,12 +85,12 @@ const normalizeProductVariants = (
|
|||||||
const options = optionValueIds.map((id) => {
|
const options = optionValueIds.map((id) => {
|
||||||
const matchingOption = productOptions.find((option) => {
|
const matchingOption = productOptions.find((option) => {
|
||||||
return option.values.find(
|
return option.values.find(
|
||||||
(value: ProductOptionValue) => value.id == id
|
(value: SwellProductOptionValue) => value.id == id
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
return normalizeProductOption({
|
return normalizeProductOption({
|
||||||
id,
|
id,
|
||||||
name: matchingOption?.displayName ?? '',
|
name: matchingOption?.name ?? '',
|
||||||
values,
|
values,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -126,7 +127,7 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
|
|||||||
? options.map((o) => normalizeProductOption(o))
|
? options.map((o) => normalizeProductOption(o))
|
||||||
: []
|
: []
|
||||||
const productVariants = variants
|
const productVariants = variants
|
||||||
? normalizeProductVariants(variants, productOptions)
|
? normalizeProductVariants(variants, options)
|
||||||
: []
|
: []
|
||||||
|
|
||||||
const productImages = normalizeProductImages(images)
|
const productImages = normalizeProductImages(images)
|
||||||
|
@ -22,8 +22,8 @@
|
|||||||
"@components/*": ["components/*"],
|
"@components/*": ["components/*"],
|
||||||
"@commerce": ["framework/commerce"],
|
"@commerce": ["framework/commerce"],
|
||||||
"@commerce/*": ["framework/commerce/*"],
|
"@commerce/*": ["framework/commerce/*"],
|
||||||
"@framework": ["framework/swell"],
|
"@framework": ["framework/shopify"],
|
||||||
"@framework/*": ["framework/swell/*"]
|
"@framework/*": ["framework/shopify/*"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
|
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user