diff --git a/framework/swell/api/endpoints/cart.ts b/framework/swell/api/endpoints/cart.ts new file mode 100644 index 000000000..d09c976c3 --- /dev/null +++ b/framework/swell/api/endpoints/cart.ts @@ -0,0 +1 @@ +export default function (_commerce: any) {} diff --git a/framework/swell/api/endpoints/catalog/products.ts b/framework/swell/api/endpoints/catalog/products.ts new file mode 100644 index 000000000..d09c976c3 --- /dev/null +++ b/framework/swell/api/endpoints/catalog/products.ts @@ -0,0 +1 @@ +export default function (_commerce: any) {} diff --git a/framework/swell/api/endpoints/customer.ts b/framework/swell/api/endpoints/customer.ts new file mode 100644 index 000000000..d09c976c3 --- /dev/null +++ b/framework/swell/api/endpoints/customer.ts @@ -0,0 +1 @@ +export default function (_commerce: any) {} diff --git a/framework/swell/api/endpoints/login.ts b/framework/swell/api/endpoints/login.ts new file mode 100644 index 000000000..d09c976c3 --- /dev/null +++ b/framework/swell/api/endpoints/login.ts @@ -0,0 +1 @@ +export default function (_commerce: any) {} diff --git a/framework/swell/api/endpoints/logout.ts b/framework/swell/api/endpoints/logout.ts new file mode 100644 index 000000000..d09c976c3 --- /dev/null +++ b/framework/swell/api/endpoints/logout.ts @@ -0,0 +1 @@ +export default function (_commerce: any) {} diff --git a/framework/swell/api/endpoints/signup.ts b/framework/swell/api/endpoints/signup.ts new file mode 100644 index 000000000..d09c976c3 --- /dev/null +++ b/framework/swell/api/endpoints/signup.ts @@ -0,0 +1 @@ +export default function (_commerce: any) {} diff --git a/framework/swell/api/endpoints/wishlist.ts b/framework/swell/api/endpoints/wishlist.ts new file mode 100644 index 000000000..d09c976c3 --- /dev/null +++ b/framework/swell/api/endpoints/wishlist.ts @@ -0,0 +1 @@ +export default function (_commerce: any) {} diff --git a/framework/swell/api/operations/get-page.ts b/framework/swell/api/operations/get-page.ts index 05646b71a..99fbac04d 100644 --- a/framework/swell/api/operations/get-page.ts +++ b/framework/swell/api/operations/get-page.ts @@ -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 @@ -11,33 +12,42 @@ export type PageVariables = { export default function getPageOperation({ commerce, }: OperationContext) { - async function getPage(opts: { - url?: string - variables: PageVariables + async function getPage(opts: { + variables: T['variables'] config?: Partial preview?: boolean - }): Promise + }): Promise - async function getPage(opts: { - url: string - variables: V - config?: Partial - preview?: boolean - }): Promise> + async function getPage( + opts: { + variables: T['variables'] + config?: Partial + preview?: boolean + } & OperationOptions + ): Promise - async function getPage({ - url, + async function getPage({ variables, - config: cfg, - preview, + config, }: { - url?: string - variables: PageVariables + query?: string + variables: T['variables'] config?: Partial preview?: boolean - }): Promise { - const config = commerce.getConfig(cfg) - return {} + }): Promise { + 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 diff --git a/framework/swell/api/utils/create-api-handler.ts b/framework/swell/api/utils/create-api-handler.ts deleted file mode 100644 index bd40a7ee4..000000000 --- a/framework/swell/api/utils/create-api-handler.ts +++ /dev/null @@ -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>, - config: SwellConfig, - handlers: H, - // Custom configs that may be used by a particular handler - options: Options -) => void | Promise - -export type SwellHandler = (options: { - req: NextApiRequest - res: NextApiResponse> - config: SwellConfig - body: Body -}) => void | Promise - -export type SwellHandlers = { - [k: string]: SwellHandler -} - -export type SwellApiResponse = { - data: T | null - errors?: { message: string; code?: string }[] -} - -export default function createApiHandler< - T = any, - H extends SwellHandlers = {}, - Options extends {} = {} ->( - handler: SwellApiHandler, - handlers: H, - defaultOptions: Options -) { - return function getApiHandler({ - config, - operations, - options, - }: { - config?: SwellConfig - operations?: Partial - options?: Options extends {} ? Partial : never - } = {}): NextApiHandler { - const ops = { ...operations, ...handlers } - const opts = { ...defaultOptions, ...options } - - return function apiHandler(req, res) { - return handler(req, res, getConfig(config), ops, opts) - } - } -} diff --git a/framework/swell/common/get-page.ts b/framework/swell/common/get-page.ts deleted file mode 100644 index dca317a19..000000000 --- a/framework/swell/common/get-page.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { getConfig, SwellConfig } from '../api' -import { Page } from './get-all-pages' - -type Variables = { - id: string -} - -export type GetPageResult = T - -const getPage = async (options: { - variables: Variables - config: SwellConfig - preview?: boolean -}): Promise => { - 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 diff --git a/framework/swell/types.ts b/framework/swell/types.ts index efd98cba6..ad8ee3b68 100644 --- a/framework/swell/types.ts +++ b/framework/swell/types.ts @@ -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 = { diff --git a/framework/swell/utils/normalize.ts b/framework/swell/utils/normalize.ts index fcc7ef4f3..2306f0662 100644 --- a/framework/swell/utils/normalize.ts +++ b/framework/swell/utils/normalize.ts @@ -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) diff --git a/tsconfig.json b/tsconfig.json index dec7c44b5..96e4359e5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"],