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

View File

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

View File

@ -47,10 +47,15 @@ export type SwellVariant = {
__type?: 'MultipleChoiceOption' | undefined
}
export interface SwellProductOptionValue {
id: string
label: string
hexColors?: string[]
}
export interface ProductOptionValue {
label: string
hexColors?: string[]
id: string
}
export type ProductOptions = {

View File

@ -10,6 +10,7 @@ import type {
SwellImage,
SwellVariant,
ProductOptionValue,
SwellProductOptionValue,
SwellCart,
LineItem,
} from '../types'
@ -73,7 +74,7 @@ const normalizeProductImages = (images: SwellImage[]) => {
const normalizeProductVariants = (
variants: SwellVariant[],
productOptions: normalizedProductOption[]
productOptions: swellProductOption[]
) => {
return variants?.map(
({ id, name, price, option_value_ids: optionValueIds = [] }) => {
@ -84,12 +85,12 @@ const normalizeProductVariants = (
const options = optionValueIds.map((id) => {
const matchingOption = productOptions.find((option) => {
return option.values.find(
(value: ProductOptionValue) => value.id == id
(value: SwellProductOptionValue) => value.id == id
)
})
return normalizeProductOption({
id,
name: matchingOption?.displayName ?? '',
name: matchingOption?.name ?? '',
values,
})
})
@ -126,7 +127,7 @@ export function normalizeProduct(swellProduct: SwellProduct): Product {
? options.map((o) => normalizeProductOption(o))
: []
const productVariants = variants
? normalizeProductVariants(variants, productOptions)
? normalizeProductVariants(variants, options)
: []
const productImages = normalizeProductImages(images)

View File

@ -22,8 +22,8 @@
"@components/*": ["components/*"],
"@commerce": ["framework/commerce"],
"@commerce/*": ["framework/commerce/*"],
"@framework": ["framework/swell"],
"@framework/*": ["framework/swell/*"]
"@framework": ["framework/shopify"],
"@framework/*": ["framework/shopify/*"]
}
},
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],