From 94084d35642cc01d61af58964a283f69e97bf03b Mon Sep 17 00:00:00 2001 From: Sam Slotsky Date: Wed, 8 Apr 2020 08:16:59 -0500 Subject: [PATCH] Accept RequestInit object in graphqlFetch (#80) --- src/components.d.ts | 4 +-- src/v0/graphqlFetch.ts | 61 +++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/components.d.ts b/src/components.d.ts index e1150c2..d9ebee7 100644 --- a/src/components.d.ts +++ b/src/components.d.ts @@ -17,7 +17,7 @@ export namespace Components { 'authToken'?: string; 'authType'?: 'manual' | 'oauth'; 'clientId'?: string; - 'env'?: 'stage' | 'prod'; + 'env'?: 'local' | 'stage' | 'prod'; 'initialize': (options: { element: HTMLElement; componentVersion: string; version: number; }) => Promise; } } @@ -48,7 +48,7 @@ declare namespace LocalJSX { 'authToken'?: string; 'authType'?: 'manual' | 'oauth'; 'clientId'?: string; - 'env'?: 'stage' | 'prod'; + 'env'?: 'local' | 'stage' | 'prod'; 'onManifold-auth-token-clear'?: (event: CustomEvent) => void; 'onManifold-auth-token-receive'?: (event: CustomEvent) => void; } diff --git a/src/v0/graphqlFetch.ts b/src/v0/graphqlFetch.ts index 48b3738..208a562 100644 --- a/src/v0/graphqlFetch.ts +++ b/src/v0/graphqlFetch.ts @@ -73,31 +73,36 @@ export function createGraphqlFetch({ analytics, waitTime = 15000, }: CreateGraphqlFetch): GraphqlFetch { - const options: RequestInit = { - method: 'POST', - headers: { - Connection: 'keep-alive', - 'Content-type': 'application/json', - 'x-mui-component': `${element.tagName}@${version}`, - 'x-manifold-manifold-init-version': '<@NPM_PACKAGE_VERSION@>', - }, - }; - - const token = getAuthToken(); - - if (clientId) { - options.headers['Manifold-Client-ID'] = clientId; - } - - if (token) { - /* eslint-disable-next-line dot-notation */ - options.headers['Authorization'] = `Bearer ${token}`; - } - async function graphqlFetch( args: GraphqlRequest, - attempts: number + attempts: number, + init?: RequestInit ): Promise> { + const opts = init || {}; + const headers = opts.headers || {}; + const options: RequestInit = { + ...opts, + method: 'POST', + headers: { + ...headers, + Connection: 'keep-alive', + 'Content-type': 'application/json', + 'x-mui-component': `${element.tagName}@${version}`, + 'x-manifold-manifold-init-version': '<@NPM_PACKAGE_VERSION@>', + }, + }; + + const token = getAuthToken(); + + if (clientId) { + options.headers['Manifold-Client-ID'] = clientId; + } + + if (token) { + /* eslint-disable-next-line dot-notation */ + options.headers['Authorization'] = `Bearer ${token}`; + } + const canRetry = attempts < retries; // Send Request @@ -109,7 +114,7 @@ export function createGraphqlFetch({ // Retry if (canRetry) { await wait(attempts ** 2 * 1000); - return graphqlFetch(args, attempts + 1); + return graphqlFetch(args, attempts + 1, init); } return Promise.reject(new ManifoldError({ type: ErrorType.NetworkError })); } @@ -127,7 +132,7 @@ export function createGraphqlFetch({ if (serverError) { if (canRetry) { await wait(attempts ** 2 * 1000); - return graphqlFetch(args, attempts + 1); + return graphqlFetch(args, attempts + 1, init); } return Promise.reject( new ManifoldError({ type: ErrorType.ServerError, message: response.statusText }) @@ -154,7 +159,9 @@ export function createGraphqlFetch({ try { clearAuthToken(); - return waitForAuthToken(getAuthToken, waitTime, () => graphqlFetch(args, attempts + 1)); + return waitForAuthToken(getAuthToken, waitTime, () => + graphqlFetch(args, attempts + 1, init) + ); } catch (e) { analytics.report({ message: e.message, @@ -168,7 +175,7 @@ export function createGraphqlFetch({ return body; } - return function(args: GraphqlRequest) { - return graphqlFetch(args, 0); + return function(args: GraphqlRequest, init?: RequestInit) { + return graphqlFetch(args, 0, init); }; }