From f2c79d07a7c040ca57e9e71f859a34292647cbeb Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Fri, 21 May 2021 18:49:11 -0500 Subject: [PATCH] Moved getSiteInfo --- framework/bigcommerce/api/index.ts | 3 +- .../api/operations/get-site-info.ts | 91 +++++++++++++++ framework/bigcommerce/common/get-site-info.ts | 106 ------------------ framework/bigcommerce/types/site.ts | 17 +++ framework/commerce/api/operations.ts | 19 +++- framework/commerce/types/site.ts | 15 +++ pages/index.tsx | 3 +- pages/search.tsx | 3 +- 8 files changed, 145 insertions(+), 112 deletions(-) create mode 100644 framework/bigcommerce/api/operations/get-site-info.ts delete mode 100644 framework/bigcommerce/common/get-site-info.ts create mode 100644 framework/bigcommerce/types/site.ts create mode 100644 framework/commerce/types/site.ts diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index 19a52ef37..6618b60fb 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -18,6 +18,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' +import getSiteInfo from './operations/get-site-info' export interface BigcommerceConfig extends CommerceAPIConfig { // Indicates if the returned metadata with translations should be applied to the @@ -113,7 +114,7 @@ const config2: BigcommerceConfig = { export const provider = { config: config2, - operations: { login, getAllPages, getPage }, + operations: { login, getAllPages, getPage, getSiteInfo }, } export type Provider = typeof provider diff --git a/framework/bigcommerce/api/operations/get-site-info.ts b/framework/bigcommerce/api/operations/get-site-info.ts new file mode 100644 index 000000000..cca11ed74 --- /dev/null +++ b/framework/bigcommerce/api/operations/get-site-info.ts @@ -0,0 +1,91 @@ +import type { + OperationContext, + OperationOptions, +} from '@commerce/api/operations' +import type { GetSiteInfoOperation } from '../../types/site' +import type { GetSiteInfoQuery } from '../../schema' +import type { RecursivePartial, RecursiveRequired } from '../utils/types' +import filterEdges from '../utils/filter-edges' +import type { BigcommerceConfig, Provider } from '..' +import { categoryTreeItemFragment } from '../fragments/category-tree' + +// Get 3 levels of categories +export const getSiteInfoQuery = /* GraphQL */ ` + query getSiteInfo { + site { + categoryTree { + ...categoryTreeItem + children { + ...categoryTreeItem + children { + ...categoryTreeItem + } + } + } + brands { + pageInfo { + startCursor + endCursor + } + edges { + cursor + node { + entityId + name + defaultImage { + urlOriginal + altText + } + pageTitle + metaDesc + metaKeywords + searchKeywords + path + } + } + } + } + } + ${categoryTreeItemFragment} +` + +export default function getSiteInfoOperation({ + commerce, +}: OperationContext) { + async function getSiteInfo(opts?: { + config?: BigcommerceConfig + preview?: boolean + }): Promise + + async function getSiteInfo( + opts: { + config?: BigcommerceConfig + preview?: boolean + } & OperationOptions + ): Promise + + async function getSiteInfo({ + query = getSiteInfoQuery, + config, + }: { + query?: 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 `query` + const { data } = await config.fetch>( + query + ) + const categories = data.site?.categoryTree + const brands = data.site?.brands?.edges + + return { + categories: (categories as RecursiveRequired) ?? [], + brands: filterEdges(brands as RecursiveRequired), + } + } + + return getSiteInfo +} diff --git a/framework/bigcommerce/common/get-site-info.ts b/framework/bigcommerce/common/get-site-info.ts deleted file mode 100644 index 80cde8d82..000000000 --- a/framework/bigcommerce/common/get-site-info.ts +++ /dev/null @@ -1,106 +0,0 @@ -import type { GetSiteInfoQuery, GetSiteInfoQueryVariables } from '../schema' -import type { RecursivePartial, RecursiveRequired } from '../api/utils/types' -import filterEdges from '../api/utils/filter-edges' -import { BigcommerceConfig, getConfig } from '../api' -import { categoryTreeItemFragment } from '../api/fragments/category-tree' - -// Get 3 levels of categories -export const getSiteInfoQuery = /* GraphQL */ ` - query getSiteInfo { - site { - categoryTree { - ...categoryTreeItem - children { - ...categoryTreeItem - children { - ...categoryTreeItem - } - } - } - brands { - pageInfo { - startCursor - endCursor - } - edges { - cursor - node { - entityId - name - defaultImage { - urlOriginal - altText - } - pageTitle - metaDesc - metaKeywords - searchKeywords - path - } - } - } - } - } - ${categoryTreeItemFragment} -` - -export type CategoriesTree = NonNullable< - GetSiteInfoQuery['site']['categoryTree'] -> - -export type BrandEdge = NonNullable< - NonNullable[0] -> - -export type Brands = BrandEdge[] - -export type GetSiteInfoResult< - T extends { categories: any[]; brands: any[] } = { - categories: CategoriesTree - brands: Brands - } -> = T - -async function getSiteInfo(opts?: { - variables?: GetSiteInfoQueryVariables - config?: BigcommerceConfig - preview?: boolean -}): Promise - -async function getSiteInfo< - T extends { categories: any[]; brands: any[] }, - V = any ->(opts: { - query: string - variables?: V - config?: BigcommerceConfig - preview?: boolean -}): Promise> - -async function getSiteInfo({ - query = getSiteInfoQuery, - variables, - config, -}: { - query?: string - variables?: GetSiteInfoQueryVariables - 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 `query` - const { data } = await config.fetch>( - query, - { variables } - ) - const categories = data.site?.categoryTree - const brands = data.site?.brands?.edges - - return { - categories: (categories as RecursiveRequired) ?? [], - brands: filterEdges(brands as RecursiveRequired), - } -} - -export default getSiteInfo diff --git a/framework/bigcommerce/types/site.ts b/framework/bigcommerce/types/site.ts new file mode 100644 index 000000000..2f67f7ffb --- /dev/null +++ b/framework/bigcommerce/types/site.ts @@ -0,0 +1,17 @@ +import * as Core from '@commerce/types/site' +import type { GetSiteInfoQuery, GetSiteInfoQueryVariables } from '../schema' + +export * from '@commerce/types/site' + +export type Category = NonNullable[0] + +export type Brand = NonNullable< + NonNullable[0] +> + +export type SiteTypes = { + category: Category + brand: Brand +} + +export type GetSiteInfoOperation = Core.GetSiteInfoOperation diff --git a/framework/commerce/api/operations.ts b/framework/commerce/api/operations.ts index caa4af20b..b6529f059 100644 --- a/framework/commerce/api/operations.ts +++ b/framework/commerce/api/operations.ts @@ -1,6 +1,7 @@ +import type { ServerResponse } from 'http' import type { LoginOperation } from '../types/login' import type { GetAllPagesOperation, GetPageOperation } from '../types/page' -import type { ServerResponse } from 'http' +import type { GetSiteInfoOperation } from '../types/site' import type { APIProvider, CommerceAPI } from '.' const noop = () => { @@ -32,6 +33,7 @@ export type Operations

= { } & OperationOptions ): Promise } + getAllPages: { (opts?: { config?: P['config'] @@ -45,6 +47,7 @@ export type Operations

= { } & OperationOptions ): Promise } + getPage: { (opts: { variables: T['variables'] @@ -60,6 +63,20 @@ export type Operations

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

= { diff --git a/framework/commerce/types/site.ts b/framework/commerce/types/site.ts new file mode 100644 index 000000000..da94c8b4e --- /dev/null +++ b/framework/commerce/types/site.ts @@ -0,0 +1,15 @@ +export type Category = any + +export type Brand = any + +export type SiteTypes = { + category: Category + brand: Brand +} + +export type GetSiteInfoOperation = { + data: { + categories: T['category'][] + brands: T['brand'][] + } +} diff --git a/pages/index.tsx b/pages/index.tsx index ac01a5dbe..d74a649fb 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -7,7 +7,6 @@ import type { GetStaticPropsContext, InferGetStaticPropsType } from 'next' import { getConfig } from '@framework/api' import getAllProducts from '@framework/product/get-all-products' -import getSiteInfo from '@framework/common/get-site-info' export async function getStaticProps({ preview, @@ -21,7 +20,7 @@ export async function getStaticProps({ preview, }) - const { categories, brands } = await getSiteInfo({ config, preview }) + const { categories, brands } = await commerce.getSiteInfo({ config, preview }) const { pages } = await commerce.getAllPages({ config, preview }) return { diff --git a/pages/search.tsx b/pages/search.tsx index 42c7f7af5..cc2bbd8d5 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -10,7 +10,6 @@ import { Container, Grid, Skeleton } from '@components/ui' import { getConfig } from '@framework/api' import useSearch from '@framework/product/use-search' -import getSiteInfo from '@framework/common/get-site-info' import commerce from '@lib/api/commerce' import rangeMap from '@lib/range-map' @@ -39,7 +38,7 @@ export async function getStaticProps({ }: GetStaticPropsContext) { const config = getConfig({ locale }) const { pages } = await commerce.getAllPages({ config, preview }) - const { categories, brands } = await getSiteInfo({ config, preview }) + const { categories, brands } = await commerce.getSiteInfo({ config, preview }) return { props: { pages,