diff --git a/lib/bigcommerce/api/index.ts b/lib/bigcommerce/api/index.ts index 14c3a873a..457451326 100644 --- a/lib/bigcommerce/api/index.ts +++ b/lib/bigcommerce/api/index.ts @@ -10,6 +10,12 @@ type RecursivePartial = { [P in keyof T]?: RecursivePartial; }; +export interface GetAllProductsResult { + products: T extends GetAllProductsQuery + ? T['site']['products']['edges'] + : unknown; +} + export default class BigcommerceAPI implements CommerceAPI { commerceUrl: string; apiToken: string; @@ -43,10 +49,32 @@ export default class BigcommerceAPI implements CommerceAPI { return json.data; } - async getAllProducts( - query: string = getAllProductsQuery - ): Promise { + async getAllProducts(opts: { + query: string; + }): Promise>; + + async getAllProducts(opts?: { + query?: string; + }): Promise>; + + async getAllProducts({ + query = getAllProductsQuery, + }: { query?: string } = {}): Promise< + GetAllProductsResult> + > { const data = await this.fetch>(query); - return data as T; + + return { + products: data?.site?.products?.edges, + }; } } + +let h = new BigcommerceAPI({ apiToken: '', commerceUrl: '' }); + +async function yay() { + const x = await h.getAllProducts<{ custom: 'val' }>({ query: 'yes' }); + const y = await h.getAllProducts(); + + console.log(x.products); +} diff --git a/lib/commerce/api/index.ts b/lib/commerce/api/index.ts index ce21b1f5c..27fafd902 100644 --- a/lib/commerce/api/index.ts +++ b/lib/commerce/api/index.ts @@ -14,7 +14,7 @@ export interface CommerceAPI { fetch(query: string, queryData?: CommerceAPIFetchOptions): Promise; - getAllProducts(query: string): Promise; + getAllProducts(options?: { query?: string }): Promise; } // export default class CommerceAPI {