Skip to content

Commit

Permalink
Accept RequestInit object in graphqlFetch (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
sslotsky authored Apr 8, 2020
1 parent de6df64 commit 94084d3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Connection>;
}
}
Expand Down Expand Up @@ -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<any>) => void;
'onManifold-auth-token-receive'?: (event: CustomEvent<string>) => void;
}
Expand Down
61 changes: 34 additions & 27 deletions src/v0/graphqlFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
args: GraphqlRequest,
attempts: number
attempts: number,
init?: RequestInit
): Promise<GraphqlResponseBody<T>> {
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
Expand All @@ -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 }));
}
Expand All @@ -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 })
Expand All @@ -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,
Expand All @@ -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);
};
}

0 comments on commit 94084d3

Please sign in to comment.