From 7f6a8498cd735265628aca770f622a5609992ed9 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Wed, 30 Sep 2020 00:41:42 -0500 Subject: [PATCH] Adding more types --- lib/bigcommerce/api/index.ts | 49 +++++++++++++++++-- .../get-all-products.ts | 0 lib/commerce/api/index.ts | 15 ++++++ tsconfig.json | 2 +- 4 files changed, 61 insertions(+), 5 deletions(-) rename lib/bigcommerce/api/{queries => operations}/get-all-products.ts (100%) diff --git a/lib/bigcommerce/api/index.ts b/lib/bigcommerce/api/index.ts index c760e9c1b..14c3a873a 100644 --- a/lib/bigcommerce/api/index.ts +++ b/lib/bigcommerce/api/index.ts @@ -1,11 +1,52 @@ -import { CommerceAPI } from 'lib/commerce/api'; +import { + CommerceAPI, + CommerceAPIOptions, + CommerceAPIFetchOptions, +} from 'lib/commerce/api'; import { GetAllProductsQuery } from '../schema'; -import { getAllProductsQuery } from './queries/get-all-products'; +import { getAllProductsQuery } from './operations/get-all-products'; + +type RecursivePartial = { + [P in keyof T]?: RecursivePartial; +}; export default class BigcommerceAPI implements CommerceAPI { - getAllProducts( + commerceUrl: string; + apiToken: string; + + constructor({ commerceUrl, apiToken }: CommerceAPIOptions) { + this.commerceUrl = commerceUrl; + this.apiToken = apiToken; + } + + async fetch( + query: string, + { variables, preview }: CommerceAPIFetchOptions = {} + ): Promise { + const res = await fetch(this.commerceUrl + (preview ? '/preview' : ''), { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.apiToken}`, + }, + body: JSON.stringify({ + query, + variables, + }), + }); + + const json = await res.json(); + if (json.errors) { + console.error(json.errors); + throw new Error('Failed to fetch API'); + } + return json.data; + } + + async getAllProducts( query: string = getAllProductsQuery ): Promise { - return null as any; + const data = await this.fetch>(query); + return data as T; } } diff --git a/lib/bigcommerce/api/queries/get-all-products.ts b/lib/bigcommerce/api/operations/get-all-products.ts similarity index 100% rename from lib/bigcommerce/api/queries/get-all-products.ts rename to lib/bigcommerce/api/operations/get-all-products.ts diff --git a/lib/commerce/api/index.ts b/lib/commerce/api/index.ts index 6309b752a..ce21b1f5c 100644 --- a/lib/commerce/api/index.ts +++ b/lib/commerce/api/index.ts @@ -1,4 +1,19 @@ +export interface CommerceAPIOptions { + commerceUrl: string; + apiToken: string; +} + +export interface CommerceAPIFetchOptions { + variables?: object; + preview?: boolean; +} + export interface CommerceAPI { + commerceUrl: string; + apiToken: string; + + fetch(query: string, queryData?: CommerceAPIFetchOptions): Promise; + getAllProducts(query: string): Promise; } diff --git a/tsconfig.json b/tsconfig.json index 371c18a6a..d8d1fa01c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, - "strict": false, + "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true,