Skip to content

Commit 4103918

Browse files
authored
Merge pull request #9 from ejnshtein/support-fetch-options
Add support for fetch options
2 parents a5fb41b + d09a9d3 commit 4103918

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test": "jest"
1818
},
1919
"dependencies": {
20-
"node-fetch": "^2.6.1",
20+
"node-fetch": "^2.6.2",
2121
"tslib": "^2.2.0"
2222
},
2323
"devDependencies": {
@@ -26,7 +26,7 @@
2626
"@rollup/plugin-node-resolve": "^11.2.1",
2727
"@types/jest": "^26.0.22",
2828
"@types/node": "^14.14.37",
29-
"@types/node-fetch": "^2.5.10",
29+
"@types/node-fetch": "2.5.12",
3030
"jest": "^26.6.3",
3131
"rollup": "^2.45.2",
3232
"rollup-plugin-dts": "^3.0.1",

src/client/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export type Middleware = (response: GraphQlResponse) => unknown;
1717

1818
export type RequestOptions = {
1919
endpoint: string,
20-
headers?: any
21-
};
20+
headers?: Record<string, string>
21+
} & Omit<RequestInit, 'method' | 'body' | 'headers'>;
2222

2323
export const defaultOptions: RequestOptions = {
2424
endpoint: process.env.GRAPHQL_ENDPOINT || '/graphql'
@@ -31,7 +31,7 @@ export class Client {
3131
this.options.endpoint = endpoint;
3232
};
3333

34-
setHeaders = (headers: any): void => {
34+
setHeaders = (headers: Record<string, string>): void => {
3535
this.options.headers = headers;
3636
};
3737

src/client/post.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { RequestOptions } from '.';
22
import { GraphQLDocument } from './prepare-document';
33

4-
if (typeof fetch === 'undefined') {
5-
var fetch = require('node-fetch');
6-
}
4+
const envAgnosticFetch: typeof window.fetch = typeof window === 'undefined'
5+
? require('node-fetch')
6+
: window.fetch
77

88
export const processHeaders = (headers: any, options: RequestOptions): any => {
99
const { headers: additionalHeaders = {} } = options;
@@ -18,9 +18,10 @@ export const postFetch = (
1818
query: string,
1919
variables: GraphQLDocument['variables'],
2020
options: RequestOptions
21-
): Promise<Response> => fetch(
21+
): Promise<Response> => envAgnosticFetch(
2222
options.endpoint,
2323
{
24+
...options,
2425
method: 'POST',
2526
body: JSON.stringify({ query, variables }),
2627
headers: processHeaders({

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,10 @@
646646
jest-diff "^26.0.0"
647647
pretty-format "^26.0.0"
648648

649-
"@types/node-fetch@^2.5.10":
650-
version "2.5.10"
651-
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.10.tgz#9b4d4a0425562f9fcea70b12cb3fcdd946ca8132"
652-
integrity sha512-IpkX0AasN44hgEad0gEF/V6EgR5n69VEqPEgnmoM8GsIGro3PowbWs4tR6IhxUTyPLpOn+fiGG6nrQhcmoCuIQ==
649+
"@types/node-fetch@2.5.12":
650+
version "2.5.12"
651+
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.12.tgz#8a6f779b1d4e60b7a57fb6fd48d84fb545b9cc66"
652+
integrity sha512-MKgC4dlq4kKNa/mYrwpKfzQMB5X3ee5U6fSprkKpToBqBmX4nFZL9cW5jl6sWn+xpRJ7ypWh2yyqqr8UUCstSw==
653653
dependencies:
654654
"@types/node" "*"
655655
form-data "^3.0.0"
@@ -2731,10 +2731,10 @@ nice-try@^1.0.4:
27312731
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
27322732
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
27332733

2734-
node-fetch@^2.6.1:
2735-
version "2.6.1"
2736-
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
2737-
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
2734+
node-fetch@^2.6.2:
2735+
version "2.6.2"
2736+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.2.tgz#986996818b73785e47b1965cc34eb093a1d464d0"
2737+
integrity sha512-aLoxToI6RfZ+0NOjmWAgn9+LEd30YCkJKFSyWacNZdEKTit/ZMcKjGkTRo8uWEsnIb/hfKecNPEbln02PdWbcA==
27382738

27392739
node-int64@^0.4.0:
27402740
version "0.4.0"

0 commit comments

Comments
 (0)