From f4e7cb51fa7fdd4994c89803667f941b0f1e14bd Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Fri, 21 May 2021 16:11:49 -0500 Subject: [PATCH] Moved getPage --- framework/bigcommerce/api/index.ts | 3 +- .../api/operations/get-all-pages.ts | 8 +-- .../bigcommerce/api/operations/get-page.ts | 53 +++++++++++++++++++ framework/bigcommerce/api/operations/login.ts | 6 +-- framework/bigcommerce/common/get-page.ts | 53 ------------------- framework/bigcommerce/types/page.ts | 8 ++- framework/commerce/api/operations.ts | 17 +++++- framework/commerce/types/page.ts | 13 ++++- pages/[...pages].tsx | 2 +- 9 files changed, 96 insertions(+), 67 deletions(-) create mode 100644 framework/bigcommerce/api/operations/get-page.ts delete mode 100644 framework/bigcommerce/common/get-page.ts diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index 35bd27fdb..19a52ef37 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -17,6 +17,7 @@ import type { SignupAPI } from './endpoints/signup' import login from './operations/login' import getAllPages from './operations/get-all-pages' +import getPage from './operations/get-page' export interface BigcommerceConfig extends CommerceAPIConfig { // Indicates if the returned metadata with translations should be applied to the @@ -112,7 +113,7 @@ const config2: BigcommerceConfig = { export const provider = { config: config2, - operations: { login, getAllPages }, + operations: { login, getAllPages, getPage }, } export type Provider = typeof provider diff --git a/framework/bigcommerce/api/operations/get-all-pages.ts b/framework/bigcommerce/api/operations/get-all-pages.ts index 845256245..d6b9f647a 100644 --- a/framework/bigcommerce/api/operations/get-all-pages.ts +++ b/framework/bigcommerce/api/operations/get-all-pages.ts @@ -2,11 +2,13 @@ import type { OperationContext, OperationOptions, } from '@commerce/api/operations' -import type { RecursivePartial, RecursiveRequired } from '../utils/types' import type { Page, GetAllPagesOperation } from '../../types/page' +import type { RecursivePartial, RecursiveRequired } from '../utils/types' import { BigcommerceConfig, Provider } from '..' -function getAllPagesOperation({ commerce }: OperationContext) { +export default function getAllPagesOperation({ + commerce, +}: OperationContext) { async function getAllPages(opts?: { config?: BigcommerceConfig preview?: boolean @@ -42,5 +44,3 @@ function getAllPagesOperation({ commerce }: OperationContext) { return getAllPages } - -export default getAllPagesOperation diff --git a/framework/bigcommerce/api/operations/get-page.ts b/framework/bigcommerce/api/operations/get-page.ts new file mode 100644 index 000000000..a5bca327c --- /dev/null +++ b/framework/bigcommerce/api/operations/get-page.ts @@ -0,0 +1,53 @@ +import type { + OperationContext, + OperationOptions, +} from '@commerce/api/operations' +import type { GetPageOperation, Page } from '../../types/page' +import type { RecursivePartial, RecursiveRequired } from '../utils/types' +import type { BigcommerceConfig, Provider } from '..' + +export default function getPageOperation({ + commerce, +}: OperationContext) { + async function getPage(opts: { + variables: T['variables'] + config?: BigcommerceConfig + preview?: boolean + }): Promise + + async function getPage( + opts: { + variables: T['variables'] + config?: BigcommerceConfig + preview?: boolean + } & OperationOptions + ): Promise + + async function getPage({ + url, + variables, + config, + preview, + }: { + url?: string + variables: T['variables'] + config?: BigcommerceConfig + preview?: boolean + }): Promise { + config = commerce.getConfig(config) + // RecursivePartial forces the method to check for every prop in the data, which is + // required in case there's a custom `url` + const { data } = await config.storeApiFetch< + RecursivePartial<{ data: Page[] }> + >(url || `/v3/content/pages?id=${variables.id}&include=body`) + const firstPage = data?.[0] + const page = firstPage as RecursiveRequired + + if (preview || page?.is_visible) { + return { page } + } + return {} + } + + return getPage +} diff --git a/framework/bigcommerce/api/operations/login.ts b/framework/bigcommerce/api/operations/login.ts index 275ea4a2c..f05924fc8 100644 --- a/framework/bigcommerce/api/operations/login.ts +++ b/framework/bigcommerce/api/operations/login.ts @@ -17,7 +17,9 @@ export const loginMutation = /* GraphQL */ ` } ` -function loginOperation({ commerce }: OperationContext) { +export default function loginOperation({ + commerce, +}: OperationContext) { async function login(opts: { variables: LoginOperation['variables'] config?: BigcommerceConfig @@ -75,5 +77,3 @@ function loginOperation({ commerce }: OperationContext) { return login } - -export default loginOperation diff --git a/framework/bigcommerce/common/get-page.ts b/framework/bigcommerce/common/get-page.ts deleted file mode 100644 index 932032efb..000000000 --- a/framework/bigcommerce/common/get-page.ts +++ /dev/null @@ -1,53 +0,0 @@ -import type { RecursivePartial, RecursiveRequired } from '../api/utils/types' -import { BigcommerceConfig, getConfig } from '../api' -import { definitions } from '../api/definitions/store-content' - -export type Page = definitions['page_Full'] - -export type GetPageResult = T - -export type PageVariables = { - id: number -} - -async function getPage(opts: { - url?: string - variables: PageVariables - config?: BigcommerceConfig - preview?: boolean -}): Promise - -async function getPage(opts: { - url: string - variables: V - config?: BigcommerceConfig - preview?: boolean -}): Promise> - -async function getPage({ - url, - variables, - config, - preview, -}: { - url?: string - variables: PageVariables - config?: BigcommerceConfig - preview?: boolean -}): Promise { - config = getConfig(config) - // RecursivePartial forces the method to check for every prop in the data, which is - // required in case there's a custom `url` - const { data } = await config.storeApiFetch< - RecursivePartial<{ data: Page[] }> - >(url || `/v3/content/pages?id=${variables.id}&include=body`) - const firstPage = data?.[0] - const page = firstPage as RecursiveRequired - - if (preview || page?.is_visible) { - return { page } - } - return {} -} - -export default getPage diff --git a/framework/bigcommerce/types/page.ts b/framework/bigcommerce/types/page.ts index b0be9867f..8f1adeb24 100644 --- a/framework/bigcommerce/types/page.ts +++ b/framework/bigcommerce/types/page.ts @@ -1,9 +1,13 @@ +import * as Core from '@commerce/types/page' import { definitions } from '../api/definitions/store-content' export * from '@commerce/types/page' export type Page = definitions['page_Full'] -export type GetAllPagesOperation = { - data: { pages: Page[] } +export type PageTypes = { + page: Page } + +export type GetAllPagesOperation = Core.GetAllPagesOperation +export type GetPageOperation = Core.GetPageOperation diff --git a/framework/commerce/api/operations.ts b/framework/commerce/api/operations.ts index 923cf55c6..caa4af20b 100644 --- a/framework/commerce/api/operations.ts +++ b/framework/commerce/api/operations.ts @@ -1,5 +1,5 @@ import type { LoginOperation } from '../types/login' -import type { GetAllPagesOperation } from '../types/page' +import type { GetAllPagesOperation, GetPageOperation } from '../types/page' import type { ServerResponse } from 'http' import type { APIProvider, CommerceAPI } from '.' @@ -45,6 +45,21 @@ export type Operations

= { } & OperationOptions ): Promise } + getPage: { + (opts: { + variables: T['variables'] + config?: P['config'] + preview?: boolean + }): Promise + + ( + opts: { + variables: T['variables'] + config?: P['config'] + preview?: boolean + } & OperationOptions + ): Promise + } } export type APIOperations

= { diff --git a/framework/commerce/types/page.ts b/framework/commerce/types/page.ts index ac674d993..334c81521 100644 --- a/framework/commerce/types/page.ts +++ b/framework/commerce/types/page.ts @@ -1,6 +1,15 @@ // TODO: define this type export type Page = any -export type GetAllPagesOperation = { - data: { pages: Page[] } +export type PageTypes = { + page: Page +} + +export type GetAllPagesOperation = { + data: { pages: T['page'][] } +} + +export type GetPageOperation = { + data: { page?: T['page'] } + variables: { id: string } } diff --git a/pages/[...pages].tsx b/pages/[...pages].tsx index de9bc90aa..91e4fb875 100644 --- a/pages/[...pages].tsx +++ b/pages/[...pages].tsx @@ -9,7 +9,7 @@ import commerce from '@lib/api/commerce' import getSlug from '@lib/get-slug' import { missingLocaleInPages } from '@lib/usage-warns' import { getConfig } from '@framework/api' -import getPage from '@framework/common/get-page' +import getPage from '@framework/api/operations/get-page' import { defaultPageProps } from '@lib/defaults' export async function getStaticProps({