diff --git a/lib/bigcommerce/api/index.ts b/lib/bigcommerce/api/index.ts index f5dad43e4..683be5820 100644 --- a/lib/bigcommerce/api/index.ts +++ b/lib/bigcommerce/api/index.ts @@ -3,7 +3,7 @@ import { CommerceAPIOptions, CommerceAPIFetchOptions, } from 'lib/commerce/api'; -import { GetAllProductsQuery } from '../schema'; +import { GetAllProductsQuery, GetAllProductsQueryVariables } from '../schema'; import { getAllProductsQuery } from './operations/get-all-products'; type RecursivePartial = { @@ -16,19 +16,61 @@ export interface GetAllProductsResult { : unknown; } +export interface Images { + small?: ImageOptions; + medium?: ImageOptions; + large?: ImageOptions; + xl?: ImageOptions; +} + +export interface ImageOptions { + width: number; + height?: number; +} + +export interface BigcommerceAPIOptions extends CommerceAPIOptions { + images?: Images; +} + +export type ProductImageVariables = Pick< + GetAllProductsQueryVariables, + | 'imgSmallWidth' + | 'imgSmallHeight' + | 'imgMediumWidth' + | 'imgMediumHeight' + | 'imgLargeWidth' + | 'imgLargeHeight' + | 'imgXLWidth' + | 'imgXLHeight' +>; + +export type ProductVariables = Images & + Omit; + export default class BigcommerceAPI implements CommerceAPI { commerceUrl: string; apiToken: string; + imageVariables?: ProductImageVariables; - constructor({ commerceUrl, apiToken }: CommerceAPIOptions) { + constructor({ commerceUrl, apiToken, images }: BigcommerceAPIOptions) { this.commerceUrl = commerceUrl; this.apiToken = apiToken; + this.imageVariables = { + imgSmallWidth: images?.small?.width, + imgSmallHeight: images?.small?.height, + imgMediumWidth: images?.medium?.height, + imgMediumHeight: images?.medium?.height, + imgLargeWidth: images?.large?.height, + imgLargeHeight: images?.large?.height, + imgXLWidth: images?.xl?.height, + imgXLHeight: images?.xl?.height, + }; } - async fetch( + async fetch( query: string, - { variables, preview }: CommerceAPIFetchOptions = {} - ): Promise { + { variables, preview }: CommerceAPIFetchOptions = {} + ): Promise { const res = await fetch(this.commerceUrl + (preview ? '/preview' : ''), { method: 'POST', headers: { @@ -49,20 +91,33 @@ export default class BigcommerceAPI implements CommerceAPI { return json.data; } - async getAllProducts(opts: { + async getAllProducts(opts: { query: string; + variables?: V; }): Promise>; async getAllProducts(opts?: { query?: string; + variables?: ProductVariables; }): Promise>; async getAllProducts({ query = getAllProductsQuery, - }: { query?: string } = {}): Promise< + variables: vars, + }: { + query?: string; + variables?: ProductVariables; + } = {}): Promise< GetAllProductsResult> > { - const data = await this.fetch>(query); + const variables: GetAllProductsQueryVariables = { + ...this.imageVariables, + ...vars, + }; + const data = await this.fetch>( + query, + { variables } + ); return { products: data?.site?.products?.edges, diff --git a/lib/bigcommerce/api/operations/get-all-products.ts b/lib/bigcommerce/api/operations/get-all-products.ts index 37340703a..24b154571 100644 --- a/lib/bigcommerce/api/operations/get-all-products.ts +++ b/lib/bigcommerce/api/operations/get-all-products.ts @@ -1,16 +1,17 @@ -export const responsiveImageFragment = /* GraphQL */ ` - fragment responsiveImage on Image { - url320wide: url(width: 320) - url640wide: url(width: 640) - url960wide: url(width: 960) - url1280wide: url(width: 1280) - } -`; - export const getAllProductsQuery = /* GraphQL */ ` - query getAllProducts { + query getAllProducts( + $first: Int = 10 + $imgSmallWidth: Int = 320 + $imgSmallHeight: Int + $imgMediumWidth: Int = 640 + $imgMediumHeight: Int + $imgLargeWidth: Int = 960 + $imgLargeHeight: Int + $imgXLWidth: Int = 1280 + $imgXLHeight: Int + ) { site { - products(first: 4) { + products(first: $first) { pageInfo { startCursor endCursor @@ -38,7 +39,13 @@ export const getAllProductsQuery = /* GraphQL */ ` images { edges { node { - ...responsiveImage + urlSmall: url(width: $imgSmallWidth, height: $imgSmallHeight) + urlMedium: url( + width: $imgMediumWidth + height: $imgMediumHeight + ) + urlLarge: url(width: $imgLargeWidth, height: $imgLargeHeight) + urlXL: url(width: $imgXLWidth, height: $imgXLHeight) } } } @@ -47,7 +54,19 @@ export const getAllProductsQuery = /* GraphQL */ ` node { entityId defaultImage { - ...responsiveImage + urlSmall: url( + width: $imgSmallWidth + height: $imgSmallHeight + ) + urlMedium: url( + width: $imgMediumWidth + height: $imgMediumHeight + ) + urlLarge: url( + width: $imgLargeWidth + height: $imgLargeHeight + ) + urlXL: url(width: $imgXLWidth, height: $imgXLHeight) } } } @@ -74,6 +93,4 @@ export const getAllProductsQuery = /* GraphQL */ ` } } } - - ${responsiveImageFragment} `; diff --git a/lib/bigcommerce/schema.d.ts b/lib/bigcommerce/schema.d.ts index ab186f894..52c89ecbf 100644 --- a/lib/bigcommerce/schema.d.ts +++ b/lib/bigcommerce/schema.d.ts @@ -1649,12 +1649,17 @@ export enum CurrencyCode { -export type ResponsiveImageFragment = ( - { __typename?: 'Image' } - & { url320wide: Image['url'], url640wide: Image['url'], url960wide: Image['url'], url1280wide: Image['url'] } -); - -export type GetAllProductsQueryVariables = Exact<{ [key: string]: never; }>; +export type GetAllProductsQueryVariables = Exact<{ + first?: Maybe; + imgSmallWidth?: Maybe; + imgSmallHeight?: Maybe; + imgMediumWidth?: Maybe; + imgMediumHeight?: Maybe; + imgLargeWidth?: Maybe; + imgLargeHeight?: Maybe; + imgXLWidth?: Maybe; + imgXLHeight?: Maybe; +}>; export type GetAllProductsQuery = ( @@ -1690,7 +1695,7 @@ export type GetAllProductsQuery = ( { __typename?: 'ImageEdge' } & { node: ( { __typename?: 'Image' } - & ResponsiveImageFragment + & { urlSmall: Image['url'], urlMedium: Image['url'], urlLarge: Image['url'], urlXL: Image['url'] } ) } )>>> } ), variants: ( @@ -1702,7 +1707,7 @@ export type GetAllProductsQuery = ( & Pick & { defaultImage?: Maybe<( { __typename?: 'Image' } - & ResponsiveImageFragment + & { urlSmall: Image['url'], urlMedium: Image['url'], urlLarge: Image['url'], urlXL: Image['url'] } )> } ) } )>>> } diff --git a/lib/commerce/api/index.ts b/lib/commerce/api/index.ts index 7bd9f893c..a8978df33 100644 --- a/lib/commerce/api/index.ts +++ b/lib/commerce/api/index.ts @@ -3,8 +3,8 @@ export interface CommerceAPIOptions { apiToken: string; } -export interface CommerceAPIFetchOptions { - variables?: object; +export interface CommerceAPIFetchOptions { + variables?: V; preview?: boolean; } @@ -12,13 +12,10 @@ export interface CommerceAPI { commerceUrl: string; apiToken: string; - fetch(query: string, queryData?: CommerceAPIFetchOptions): Promise; + fetch( + query: string, + queryData?: CommerceAPIFetchOptions + ): Promise; getAllProducts(options?: { query: string }): Promise; } - -// export default class CommerceAPI { -// getAllProducts(query: string) { - -// } -// } diff --git a/pages/index.tsx b/pages/index.tsx index 89e476476..a78161c2f 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -3,7 +3,7 @@ import { commerce } from 'lib/commerce-api'; import { Layout } from '@components/core'; export async function getStaticProps({ preview }: GetStaticPropsContext) { - const products = await commerce.getAllProducts(); + const { products } = await commerce.getAllProducts(); return { props: { products },