diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index d33c5620b..542439cbb 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -21,6 +21,7 @@ import getAllPages from './operations/get-all-pages' import getPage from './operations/get-page' import getSiteInfo from './operations/get-site-info' import getCustomerWishlist from './operations/get-customer-wishlist' +import getAllProductPaths from './operations/get-all-product-paths' export interface BigcommerceConfig extends CommerceAPIConfig { // Indicates if the returned metadata with translations should be applied to the @@ -116,7 +117,14 @@ const config2: BigcommerceConfig = { export const provider = { config: config2, - operations: { login, getAllPages, getPage, getSiteInfo, getCustomerWishlist }, + operations: { + login, + getAllPages, + getPage, + getSiteInfo, + getCustomerWishlist, + getAllProductPaths, + }, } export type Provider = typeof provider diff --git a/framework/bigcommerce/api/operations/get-all-product-paths.ts b/framework/bigcommerce/api/operations/get-all-product-paths.ts new file mode 100644 index 000000000..302a4017b --- /dev/null +++ b/framework/bigcommerce/api/operations/get-all-product-paths.ts @@ -0,0 +1,66 @@ +import type { + OperationContext, + OperationOptions, +} from '@commerce/api/operations' +import type { GetAllProductPathsQuery } from '../../schema' +import type { GetAllProductPathsOperation } from '../../types/product' +import type { RecursivePartial, RecursiveRequired } from '../utils/types' +import filterEdges from '../utils/filter-edges' +import { BigcommerceConfig, Provider } from '..' + +export const getAllProductPathsQuery = /* GraphQL */ ` + query getAllProductPaths($first: Int = 100) { + site { + products(first: $first) { + edges { + node { + path + } + } + } + } + } +` + +export default function getAllProductPathsOperation({ + commerce, +}: OperationContext) { + async function getAllProductPaths< + T extends GetAllProductPathsOperation + >(opts?: { + variables?: T['variables'] + config?: BigcommerceConfig + }): Promise + + async function getAllProductPaths< + T extends GetAllProductPathsOperation + >(opts: { + variables?: T['variables'] + config?: BigcommerceConfig + } & OperationOptions): Promise + + async function getAllProductPaths({ + query = getAllProductPathsQuery, + variables, + config, + }: { + query?: string + variables?: T['variables'] + config?: BigcommerceConfig + } = {}): 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< + RecursivePartial + >(query, { variables }) + const products = data.site?.products?.edges + + return { + products: filterEdges(products as RecursiveRequired).map( + ({ node }) => node + ), + } + } + return getAllProductPaths +} diff --git a/framework/bigcommerce/product/get-all-product-paths.ts b/framework/bigcommerce/product/get-all-product-paths.ts deleted file mode 100644 index c1b23b38d..000000000 --- a/framework/bigcommerce/product/get-all-product-paths.ts +++ /dev/null @@ -1,71 +0,0 @@ -import type { - GetAllProductPathsQuery, - GetAllProductPathsQueryVariables, -} from '../schema' -import type { RecursivePartial, RecursiveRequired } from '../api/utils/types' -import filterEdges from '../api/utils/filter-edges' -import { BigcommerceConfig, getConfig } from '../api' - -export const getAllProductPathsQuery = /* GraphQL */ ` - query getAllProductPaths($first: Int = 100) { - site { - products(first: $first) { - edges { - node { - path - } - } - } - } - } -` - -export type ProductPath = NonNullable< - NonNullable[0] -> - -export type ProductPaths = ProductPath[] - -export type { GetAllProductPathsQueryVariables } - -export type GetAllProductPathsResult< - T extends { products: any[] } = { products: ProductPaths } -> = T - -async function getAllProductPaths(opts?: { - variables?: GetAllProductPathsQueryVariables - config?: BigcommerceConfig -}): Promise - -async function getAllProductPaths< - T extends { products: any[] }, - V = any ->(opts: { - query: string - variables?: V - config?: BigcommerceConfig -}): Promise> - -async function getAllProductPaths({ - query = getAllProductPathsQuery, - variables, - config, -}: { - query?: string - variables?: GetAllProductPathsQueryVariables - config?: BigcommerceConfig -} = {}): 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< - RecursivePartial - >(query, { variables }) - const products = data.site?.products?.edges - - return { - products: filterEdges(products as RecursiveRequired), - } -} - -export default getAllProductPaths diff --git a/framework/commerce/api/operations.ts b/framework/commerce/api/operations.ts index 2b8c9b77a..eebff74aa 100644 --- a/framework/commerce/api/operations.ts +++ b/framework/commerce/api/operations.ts @@ -3,6 +3,7 @@ import type { LoginOperation } from '../types/login' import type { GetAllPagesOperation, GetPageOperation } from '../types/page' import type { GetSiteInfoOperation } from '../types/site' import type { GetCustomerWishlistOperation } from '../types/wishlist' +import type { GetAllProductPathsOperation } from '../types/product' import type { APIProvider, CommerceAPI } from '.' const noop = () => { @@ -94,6 +95,20 @@ export type Operations

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

= { diff --git a/framework/commerce/types/product.ts b/framework/commerce/types/product.ts index d33cf3743..22a045f48 100644 --- a/framework/commerce/types/product.ts +++ b/framework/commerce/types/product.ts @@ -65,3 +65,10 @@ export type ProductsSchema = { } } } + +export type GetAllProductPathsOperation< + T extends ProductTypes = ProductTypes +> = { + data: { products: Pick[] } + variables: { first?: number } +}