From 1ba9d3bd6e79da1f0b05df2f1c0c1ca3632b6c16 Mon Sep 17 00:00:00 2001 From: Catalin Pinte <1243434+cond0r@users.noreply.github.com> Date: Wed, 7 Dec 2022 14:34:39 +0200 Subject: [PATCH] Update types --- packages/bigcommerce/src/lib/normalize.ts | 10 +-- packages/commerce/src/types/common.ts | 64 +++++++++++++ packages/commerce/src/types/product.ts | 89 +------------------ packages/shopify/src/utils/normalize.ts | 9 +- .../ProductCustomFields.tsx | 4 +- .../ProductMetafields/ProductMetafields.tsx | 4 +- 6 files changed, 79 insertions(+), 101 deletions(-) diff --git a/packages/bigcommerce/src/lib/normalize.ts b/packages/bigcommerce/src/lib/normalize.ts index aad68ac6b..fe0e579c1 100644 --- a/packages/bigcommerce/src/lib/normalize.ts +++ b/packages/bigcommerce/src/lib/normalize.ts @@ -1,8 +1,6 @@ import type { Page } from '@vercel/commerce/types/page' -import type { - Product, - ProductCustomField, -} from '@vercel/commerce/types/product' +import type { Product } from '@vercel/commerce/types/product' +import type { CustomField } from '@vercel/commerce/types/common' import type { Cart, LineItem } from '@vercel/commerce/types/cart' import type { Category, Brand } from '@vercel/commerce/types/site' import type { BigcommerceCart, BCCategory, BCBrand } from '../types' @@ -153,9 +151,7 @@ export function normalizeBrand(brand: BCBrand): Brand { } } -function normalizeCustomFieldsValue( - field: CustomFieldEdge -): ProductCustomField { +function normalizeCustomFieldsValue(field: CustomFieldEdge): CustomField { const { node: { entityId, name, value }, } = field diff --git a/packages/commerce/src/types/common.ts b/packages/commerce/src/types/common.ts index d63dfc0b9..111c2573b 100644 --- a/packages/commerce/src/types/common.ts +++ b/packages/commerce/src/types/common.ts @@ -34,3 +34,67 @@ export interface Image { */ height?: number } + +export interface CustomField { + /** + * The unique identifier for the custom field. + */ + id: string + /** + * The name of the custom field. + */ + name: string + /** + * The value of the custom field. + */ + value: string +} + +export interface Metafield { + /** + * The unique identifier for the metafield. + */ + key: string + + /** + * The namespace for the metafield, which is a container for a set of metadata. + * @example `rating` + */ + namespace: string + + /** + * The value of the metafield, usually a string that can be might parsed into JSON. + * @example `{"value": 5, "scale_max": 5}` + */ + value: any + + /** + * The value of the metafield, complete with HTML formatting. + */ + valueHtml?: string + + /** + * The type of the metafield, used to determine how the value should be interpreted. + * For example: `string`, `integer`, `boolean`, `json` ... + */ + type?: string + + /** + * The name of the metafield, that can be used as a label. + */ + name?: string +} + +export interface Metafields { + /** + * The namespace for the metafield, which is a container for a set of metadata. + * @example `reviews`, `specifications` + */ + [namespace: string]: { + /** + * The key of the metafield, which is the name of the metafield. + * @example `rating` + */ + [key: string]: Metafield + } +} diff --git a/packages/commerce/src/types/product.ts b/packages/commerce/src/types/product.ts index 8bb383962..bfe7136b5 100644 --- a/packages/commerce/src/types/product.ts +++ b/packages/commerce/src/types/product.ts @@ -1,4 +1,4 @@ -import { Image } from './common' +import { CustomField, Image, Metafields } from './common' export interface ProductPrice { /** @@ -84,89 +84,6 @@ export interface ProductVariant { image?: Image } -/** - * - * The product metafield object, which is a custom field attached to a product. It can be used to store additional information about the product in a structured format. - * @example `reviews` - */ -export interface ProductMetafield { - /** - * The unique identifier for the metafield. - */ - key: string - - /** - * The namespace for the metafield, which is a container for a set of metadata. - * @example `rating` - */ - namespace: string - - /** - * The value of the metafield, usually a string that can be might parsed into JSON. - * @example `{"value": 5, "scale_max": 5}` - */ - value: any - - /** - * The value of the metafield, complete with HTML formatting. - */ - valueHtml?: string - - /** - * The type of the metafield, used to determine how the value should be interpreted. - * For example: `string`, `integer`, `boolean`, `json` ... - */ - type?: string - - /** - * The name of the metafield, that can be used as a label. - */ - name?: string -} - -/** - * The product metafields are custom fields that can be added to a product. They are used to store additional information about the product. - * @example - * { - * // Namespace, the container for a set of metadata - * reviews: { - * // Key of the metafield - * rating: { - * key: 'rating', - * value: 5, - * // ... other metafield properties - * } - * } - */ -export interface ProductMetafields { - /** - * The namespace for the metafield, which is a container for a set of metadata. - * @example `reviews`, `specifications` - */ - [namespace: string]: { - /** - * The key of the metafield, which is the name of the metafield. - * @example `rating` - */ - [key: string]: ProductMetafield - } -} - -export interface ProductCustomField { - /** - * The unique identifier for the custom field. - */ - id: string - /** - * The name of the custom field. - */ - name: string - /** - * The value of the custom field. - */ - value: string -} - export interface Product { /** * The unique identifier for the product. @@ -209,7 +126,7 @@ export interface Product { * value: 'Aisle 3, Shelf 5, Bin 6' * }] */ - customFields?: ProductCustomField[] + customFields?: CustomField[] /** * Advanced custom fields that can be added to a product. They are used to store additional information about the product, in a structured format, grouped by namespaces. * @example @@ -226,7 +143,7 @@ export interface Product { * } * } */ - metafields?: ProductMetafields + metafields?: Metafields /** * List of the product’s variants. */ diff --git a/packages/shopify/src/utils/normalize.ts b/packages/shopify/src/utils/normalize.ts index d0dd1587f..710ba7423 100644 --- a/packages/shopify/src/utils/normalize.ts +++ b/packages/shopify/src/utils/normalize.ts @@ -1,5 +1,6 @@ import type { Page } from '@vercel/commerce/types/page' -import type { Product, ProductMetafield } from '@vercel/commerce/types/product' +import type { Product } from '@vercel/commerce/types/product' +import type { Metafield } from '@vercel/commerce/types/common' import type { Cart, LineItem } from '@vercel/commerce/types/cart' import type { Category } from '@vercel/commerce/types/site' import type { MetafieldType } from '../types/metafields' @@ -17,7 +18,7 @@ import type { PageEdge, Collection, Maybe, - Metafield, + Metafield as ShopifyMetafield, } from '../../schema' import { colorMap } from './colors' @@ -137,10 +138,10 @@ export function normalizeProduct( } export function normalizeMetafields( - metafields: Maybe[], + metafields: Maybe[], locale?: string ) { - const output: Record> = {} + const output: Record> = {} if (!metafields) return output diff --git a/site/components/product/ProductCustomFields/ProductCustomFields.tsx b/site/components/product/ProductCustomFields/ProductCustomFields.tsx index 5611e78a8..c0254bf17 100644 --- a/site/components/product/ProductCustomFields/ProductCustomFields.tsx +++ b/site/components/product/ProductCustomFields/ProductCustomFields.tsx @@ -1,7 +1,7 @@ -import { ProductCustomField } from '@commerce/types/product' +import type { CustomField } from '@commerce/types/common' interface Props { - customFields: ProductCustomField[] + customFields: CustomField[] } const ProductCustomFields: React.FC = ({ customFields }) => { diff --git a/site/components/product/ProductMetafields/ProductMetafields.tsx b/site/components/product/ProductMetafields/ProductMetafields.tsx index d619378a3..df8072cdd 100644 --- a/site/components/product/ProductMetafields/ProductMetafields.tsx +++ b/site/components/product/ProductMetafields/ProductMetafields.tsx @@ -1,9 +1,9 @@ import type { FC } from 'react' -import type { ProductMetafields as IProductMetafields } from '@commerce/types/product' +import type { Metafields } from '@commerce/types/common' import Text from '@components/ui/Text' interface Props { - metafields: IProductMetafields + metafields: Metafields /** * The namespace of the metafields to display. */