update get-page, cleanup types

This commit is contained in:
Greg Hoskin
2021-06-09 10:59:23 -07:00
parent bb321e2237
commit ff0a468b02
13 changed files with 50 additions and 118 deletions

View File

@@ -0,0 +1 @@
export default function (_commerce: any) {}

View File

@@ -0,0 +1 @@
export default function (_commerce: any) {}

View File

@@ -0,0 +1 @@
export default function (_commerce: any) {}

View File

@@ -0,0 +1 @@
export default function (_commerce: any) {}

View File

@@ -0,0 +1 @@
export default function (_commerce: any) {}

View File

@@ -0,0 +1 @@
export default function (_commerce: any) {}

View File

@@ -0,0 +1 @@
export default function (_commerce: any) {}

View File

@@ -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

View File

@@ -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)
}
}
}