Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow RequestInit options in gateway.post #81

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/v0/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ export function createGateway({
baseUrl = () => 'https://api.manifold.co/graphql',
retries = 3,
}): Gateway {
async function post<Req extends {}, Resp>(path: string, body: Req, attempts: number) {
async function post<Req extends {}, Resp>(
path: string,
body: Req,
attempts: number,
init?: RequestInit
) {
const opts = init || {};
const headers = opts.headers || {};
const options: RequestInit = {
...opts,
method: 'POST',
body: JSON.stringify(body),
headers: {
...headers,
Connection: 'keep-alive',
'Content-type': 'application/json',
},
Expand All @@ -30,7 +39,7 @@ export function createGateway({
} catch (e) {
if (canRetry) {
await wait(attempts ** 2 * 1000);
return post(path, body, attempts + 1);
return post(path, body, attempts + 1, init);
}
}

Expand All @@ -53,7 +62,7 @@ export function createGateway({
if (serverError) {
if (canRetry) {
await wait(attempts ** 2 * 1000);
return post(path, body, attempts + 1);
return post(path, body, attempts + 1, init);
}
return Promise.reject(
new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText })
Expand All @@ -63,5 +72,5 @@ export function createGateway({
return (await resp.json()) as Resp;
}

return { post: <Req>(path: string, body: Req) => post(path, body, 0) };
return { post: <Req>(path: string, body: Req, init?: RequestInit) => post(path, body, 0, init) };
}