From a908ebadfdfd0a9bf1baaf0155e8c9f1915d885a Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Tue, 1 Jun 2021 01:03:44 -0500 Subject: [PATCH] Added category type and normalizer --- .../bigcommerce/api/operations/get-site-info.ts | 12 +++++------- framework/bigcommerce/lib/get-slug.ts | 5 +++++ framework/bigcommerce/lib/normalize.ts | 13 ++++++++++++- framework/bigcommerce/types/site.ts | 6 ++++-- framework/commerce/types/cart.ts | 9 --------- framework/commerce/types/site.ts | 7 ++++++- framework/shopify/utils/get-categories.ts | 12 ++++-------- framework/shopify/utils/normalize.ts | 9 +++++++++ 8 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 framework/bigcommerce/lib/get-slug.ts diff --git a/framework/bigcommerce/api/operations/get-site-info.ts b/framework/bigcommerce/api/operations/get-site-info.ts index afe5f3626..0dd94dd9d 100644 --- a/framework/bigcommerce/api/operations/get-site-info.ts +++ b/framework/bigcommerce/api/operations/get-site-info.ts @@ -4,10 +4,10 @@ import type { } 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' +import { normalizeCategory } from '../../lib/normalize' // Get 3 levels of categories export const getSiteInfoQuery = /* GraphQL */ ` @@ -73,15 +73,13 @@ export default function getSiteInfoOperation({ preview?: boolean } = {}): Promise { const cfg = 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 cfg.fetch>(query) - const categories = data.site?.categoryTree + const { data } = await cfg.fetch(query) + const categories = data.site.categoryTree.map(normalizeCategory) const brands = data.site?.brands?.edges return { - categories: (categories as RecursiveRequired) ?? [], - brands: filterEdges(brands as RecursiveRequired), + categories: categories ?? [], + brands: filterEdges(brands), } } diff --git a/framework/bigcommerce/lib/get-slug.ts b/framework/bigcommerce/lib/get-slug.ts new file mode 100644 index 000000000..329c5a27e --- /dev/null +++ b/framework/bigcommerce/lib/get-slug.ts @@ -0,0 +1,5 @@ +// Remove trailing and leading slash, usually included in nodes +// returned by the BigCommerce API +const getSlug = (path: string) => path.replace(/^\/|\/$/g, '') + +export default getSlug diff --git a/framework/bigcommerce/lib/normalize.ts b/framework/bigcommerce/lib/normalize.ts index 5e5ebc8e4..cd1c3ce5a 100644 --- a/framework/bigcommerce/lib/normalize.ts +++ b/framework/bigcommerce/lib/normalize.ts @@ -1,8 +1,10 @@ import type { Product } from '../types/product' import type { Cart, BigcommerceCart, LineItem } from '../types/cart' import type { Page } from '../types/page' -import update from './immutability' +import type { BCCategory, Category } from '../types/site' import { definitions } from '../api/definitions/store-content' +import update from './immutability' +import getSlug from './get-slug' function normalizeProductOption(productOption: any) { const { @@ -123,3 +125,12 @@ function normalizeLineItem(item: any): LineItem { })), } } + +export function normalizeCategory(category: BCCategory): Category { + return { + id: `${category.entityId}`, + name: category.name, + slug: getSlug(category.path), + path: category.path, + } +} diff --git a/framework/bigcommerce/types/site.ts b/framework/bigcommerce/types/site.ts index 2f67f7ffb..12dd7038c 100644 --- a/framework/bigcommerce/types/site.ts +++ b/framework/bigcommerce/types/site.ts @@ -3,14 +3,16 @@ import type { GetSiteInfoQuery, GetSiteInfoQueryVariables } from '../schema' export * from '@commerce/types/site' -export type Category = NonNullable[0] +export type BCCategory = NonNullable< + GetSiteInfoQuery['site']['categoryTree'] +>[0] export type Brand = NonNullable< NonNullable[0] > export type SiteTypes = { - category: Category + category: Core.Category brand: Brand } diff --git a/framework/commerce/types/cart.ts b/framework/commerce/types/cart.ts index 2ea866ad0..7826f9b2d 100644 --- a/framework/commerce/types/cart.ts +++ b/framework/commerce/types/cart.ts @@ -139,15 +139,6 @@ export type RemoveItemHook = { actionInput: { id: string } } -export type Category = { - id: string - name: string - slug: string - path: string -} - -export type Page = any - /** * API Schema */ diff --git a/framework/commerce/types/site.ts b/framework/commerce/types/site.ts index da94c8b4e..73c7dddd2 100644 --- a/framework/commerce/types/site.ts +++ b/framework/commerce/types/site.ts @@ -1,4 +1,9 @@ -export type Category = any +export type Category = { + id: string + name: string + slug: string + path: string +} export type Brand = any diff --git a/framework/shopify/utils/get-categories.ts b/framework/shopify/utils/get-categories.ts index aefa68b64..543ee2fa1 100644 --- a/framework/shopify/utils/get-categories.ts +++ b/framework/shopify/utils/get-categories.ts @@ -1,7 +1,8 @@ +import type { Category } from '../types/site' import { ShopifyConfig } from '../api' import { CollectionEdge } from '../schema' +import { normalizeCategory } from './normalize' import getSiteCollectionsQuery from './queries/get-all-collections-query' -import { Category } from '@commerce/types' const getCategories = async ({ fetch, @@ -24,13 +25,8 @@ const getCategories = async ({ ) return ( - data.collections?.edges?.map( - ({ node: { id, title: name, handle } }: CollectionEdge) => ({ - id, - name, - slug: handle, - path: `/${handle}`, - }) + data.collections?.edges?.map(({ node }: CollectionEdge) => + normalizeCategory(node) ) ?? [] ) } diff --git a/framework/shopify/utils/normalize.ts b/framework/shopify/utils/normalize.ts index 39a4cd00f..56764b95d 100644 --- a/framework/shopify/utils/normalize.ts +++ b/framework/shopify/utils/normalize.ts @@ -1,6 +1,7 @@ import type { Page } from '../types/page' import type { Product } from '../types/product' import type { Cart, LineItem } from '../types/cart' +import type { Category } from '../types/site' import { Product as ShopifyProduct, @@ -181,3 +182,11 @@ export const normalizePage = ( export const normalizePages = (edges: PageEdge[], locale: string): Page[] => edges?.map((edge) => normalizePage(edge.node, locale)) + +export const normalizeCategory = (category: any): Category => ({ + id: category.id, + name: category.name, + slug: category.handle, + path: `/${category.handle}`, +}) +