mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
update get-page, cleanup types
This commit is contained in:
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 { 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
|
||||
|
||||
@@ -11,33 +12,42 @@ export type PageVariables = {
|
||||
export default function getPageOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function getPage(opts: {
|
||||
url?: string
|
||||
variables: PageVariables
|
||||
async function getPage<T extends GetPageOperation>(opts: {
|
||||
variables: T['variables']
|
||||
config?: Partial<SwellConfig>
|
||||
preview?: boolean
|
||||
}): Promise<GetPageResult>
|
||||
}): Promise<T['data']>
|
||||
|
||||
async function getPage<T extends { page?: any }, V = any>(opts: {
|
||||
url: string
|
||||
variables: V
|
||||
config?: Partial<SwellConfig>
|
||||
preview?: boolean
|
||||
}): Promise<GetPageResult<T>>
|
||||
async function getPage<T extends GetPageOperation>(
|
||||
opts: {
|
||||
variables: T['variables']
|
||||
config?: Partial<SwellConfig>
|
||||
preview?: boolean
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
|
||||
async function getPage({
|
||||
url,
|
||||
async function getPage<T extends GetPageOperation>({
|
||||
variables,
|
||||
config: cfg,
|
||||
preview,
|
||||
config,
|
||||
}: {
|
||||
url?: string
|
||||
variables: PageVariables
|
||||
query?: string
|
||||
variables: T['variables']
|
||||
config?: Partial<SwellConfig>
|
||||
preview?: boolean
|
||||
}): Promise<GetPageResult> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
return {}
|
||||
}): Promise<T['data']> {
|
||||
const { fetch, locale = 'en-US' } = commerce.getConfig(config)
|
||||
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
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user