From 6bcf9f9e7516135246a015ef76280f1ac9fc817a Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Thu, 20 May 2021 17:26:14 -0500 Subject: [PATCH] Added getAllPages operation --- framework/bigcommerce/api/index.ts | 4 +- .../api/operations/get-all-pages.ts | 46 +++++++++++++++++++ framework/bigcommerce/common/get-all-pages.ts | 43 ----------------- framework/bigcommerce/types/page.ts | 9 ++++ framework/commerce/api/operations.ts | 10 ++-- framework/commerce/types/page.ts | 6 +++ 6 files changed, 69 insertions(+), 49 deletions(-) create mode 100644 framework/bigcommerce/api/operations/get-all-pages.ts delete mode 100644 framework/bigcommerce/common/get-all-pages.ts create mode 100644 framework/bigcommerce/types/page.ts create mode 100644 framework/commerce/types/page.ts diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index 222ec0b06..35bd27fdb 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -14,7 +14,9 @@ import type { CustomerAPI } from './endpoints/customer' import type { LoginAPI } from './endpoints/login' import type { LogoutAPI } from './endpoints/logout' import type { SignupAPI } from './endpoints/signup' + import login from './operations/login' +import getAllPages from './operations/get-all-pages' export interface BigcommerceConfig extends CommerceAPIConfig { // Indicates if the returned metadata with translations should be applied to the @@ -110,7 +112,7 @@ const config2: BigcommerceConfig = { export const provider = { config: config2, - operations: { login }, + operations: { login, getAllPages }, } 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 new file mode 100644 index 000000000..845256245 --- /dev/null +++ b/framework/bigcommerce/api/operations/get-all-pages.ts @@ -0,0 +1,46 @@ +import type { + OperationContext, + OperationOptions, +} from '@commerce/api/operations' +import type { RecursivePartial, RecursiveRequired } from '../utils/types' +import type { Page, GetAllPagesOperation } from '../../types/page' +import { BigcommerceConfig, Provider } from '..' + +function getAllPagesOperation({ commerce }: OperationContext) { + async function getAllPages(opts?: { + config?: BigcommerceConfig + preview?: boolean + }): Promise + + async function getAllPages( + opts: { + config?: BigcommerceConfig + preview?: boolean + } & OperationOptions + ): Promise + + async function getAllPages({ + config, + preview, + }: { + url?: string + 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[] }> + >('/v3/content/pages') + const pages = (data as RecursiveRequired) ?? [] + + return { + pages: preview ? pages : pages.filter((p) => p.is_visible), + } + } + + return getAllPages +} + +export default getAllPagesOperation diff --git a/framework/bigcommerce/common/get-all-pages.ts b/framework/bigcommerce/common/get-all-pages.ts deleted file mode 100644 index dc5eb15a5..000000000 --- a/framework/bigcommerce/common/get-all-pages.ts +++ /dev/null @@ -1,43 +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 GetAllPagesResult< - T extends { pages: any[] } = { pages: Page[] } -> = T - -async function getAllPages(opts?: { - config?: BigcommerceConfig - preview?: boolean -}): Promise - -async function getAllPages(opts: { - url: string - config?: BigcommerceConfig - preview?: boolean -}): Promise> - -async function getAllPages({ - config, - preview, -}: { - url?: string - 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[] }> - >('/v3/content/pages') - const pages = (data as RecursiveRequired) ?? [] - - return { - pages: preview ? pages : pages.filter((p) => p.is_visible), - } -} - -export default getAllPages diff --git a/framework/bigcommerce/types/page.ts b/framework/bigcommerce/types/page.ts new file mode 100644 index 000000000..b0be9867f --- /dev/null +++ b/framework/bigcommerce/types/page.ts @@ -0,0 +1,9 @@ +import { definitions } from '../api/definitions/store-content' + +export * from '@commerce/types/page' + +export type Page = definitions['page_Full'] + +export type GetAllPagesOperation = { + data: { pages: Page[] } +} diff --git a/framework/commerce/api/operations.ts b/framework/commerce/api/operations.ts index e46b02e97..3cc030f4f 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 { GetAllPagesResult } from '../types/page' +import type { GetAllPagesOperation } from '../types/page' import type { ServerResponse } from 'http' import type { APIProvider, CommerceAPI, CommerceAPICore } from '.' @@ -33,17 +33,17 @@ export type Operations

= { ): Promise } getAllPages: { - (opts?: { + (opts?: { config?: P['config'] preview?: boolean - }): Promise + }): Promise - ( + ( opts: { config?: P['config'] preview?: boolean } & OperationOptions - ): Promise + ): Promise } } diff --git a/framework/commerce/types/page.ts b/framework/commerce/types/page.ts new file mode 100644 index 000000000..ac674d993 --- /dev/null +++ b/framework/commerce/types/page.ts @@ -0,0 +1,6 @@ +// TODO: define this type +export type Page = any + +export type GetAllPagesOperation = { + data: { pages: Page[] } +}