forked from crowetic/commerce
Merge branch 'master' of github.com:okbel/e-comm-example
This commit is contained in:
commit
3dc2c16cea
@ -10,6 +10,12 @@ type RecursivePartial<T> = {
|
|||||||
[P in keyof T]?: RecursivePartial<T[P]>;
|
[P in keyof T]?: RecursivePartial<T[P]>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface GetAllProductsResult<T> {
|
||||||
|
products: T extends GetAllProductsQuery
|
||||||
|
? T['site']['products']['edges']
|
||||||
|
: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
export default class BigcommerceAPI implements CommerceAPI {
|
export default class BigcommerceAPI implements CommerceAPI {
|
||||||
commerceUrl: string;
|
commerceUrl: string;
|
||||||
apiToken: string;
|
apiToken: string;
|
||||||
@ -43,10 +49,32 @@ export default class BigcommerceAPI implements CommerceAPI {
|
|||||||
return json.data;
|
return json.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllProducts<T = GetAllProductsQuery>(
|
async getAllProducts<T>(opts: {
|
||||||
query: string = getAllProductsQuery
|
query: string;
|
||||||
): Promise<T> {
|
}): Promise<GetAllProductsResult<T>>;
|
||||||
|
|
||||||
|
async getAllProducts(opts?: {
|
||||||
|
query?: string;
|
||||||
|
}): Promise<GetAllProductsResult<GetAllProductsQuery>>;
|
||||||
|
|
||||||
|
async getAllProducts({
|
||||||
|
query = getAllProductsQuery,
|
||||||
|
}: { query?: string } = {}): Promise<
|
||||||
|
GetAllProductsResult<RecursivePartial<GetAllProductsQuery>>
|
||||||
|
> {
|
||||||
const data = await this.fetch<RecursivePartial<GetAllProductsQuery>>(query);
|
const data = await this.fetch<RecursivePartial<GetAllProductsQuery>>(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);
|
||||||
|
}
|
||||||
|
@ -14,7 +14,7 @@ export interface CommerceAPI {
|
|||||||
|
|
||||||
fetch<T>(query: string, queryData?: CommerceAPIFetchOptions): Promise<T>;
|
fetch<T>(query: string, queryData?: CommerceAPIFetchOptions): Promise<T>;
|
||||||
|
|
||||||
getAllProducts(query: string): Promise<any>;
|
getAllProducts(options?: { query?: string }): Promise<any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// export default class CommerceAPI {
|
// export default class CommerceAPI {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user