-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathclient.ts
350 lines (321 loc) · 11.7 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
*
* client
*
*/
import type { ExecutionResult } from 'graphql';
import { RequestParams, Sink } from './common';
import { isObject } from './utils';
/** This file is the entry point for browsers, re-export common elements. */
export * from './common';
/** @category Client */
export interface ClientOptions {
/**
* URL of the GraphQL over HTTP server to connect.
*
* If the option is a function, it will be called on each request.
* Returning a Promise is supported too and the request will stall until it
* resolves.
*
* A good use-case for having a function is when using the URL for authentication,
* where subsequent requests (due to auth) may have a refreshed identity token.
*
* Function receives the request params. Useful for example, to ease up debugging and DevTools
* navigation you might want to use the operation name in the URL's search params (`/graphql?MyQuery`).
*/
url: string | ((request: RequestParams) => Promise<string> | string);
/**
* Indicates whether the user agent should send cookies from the other domain in the case
* of cross-origin requests.
*
* Possible options are:
* - `omit`: Never send or receive cookies.
* - `same-origin`: Send user credentials (cookies, basic http auth, etc..) if the URL is on the same origin as the calling script.
* - `include`: Always send user credentials (cookies, basic http auth, etc..), even for cross-origin calls.
*
* @default same-origin
*/
credentials?: 'omit' | 'same-origin' | 'include';
/**
* A string specifying the referrer of the request. This can be a same-origin URL, about:client, or an empty string.
*
* @default undefined
*/
referrer?: string;
/**
* Specifies the referrer policy to use for the request.
*
* Possible options are:
* - `no-referrer`: Does not send referrer information along with requests to any origin.
* - `no-referrer-when-downgrade`: Sends full referrerURL for requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL.
* - `same-origin`: Sends full referrerURL as referrer information when making same-origin-referrer requests.
* - `origin`: Sends only the ASCII serialization of the request’s referrerURL when making both same-origin-referrer requests and cross-origin-referrer requests.
* - `strict-origin`: Sends the ASCII serialization of the origin of the referrerURL for requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL
* - `origin-when-cross-origin`: Sends full referrerURL when making same-origin-referrer requests, and only the ASCII serialization of the origin of the request’s referrerURL is sent when making cross-origin-referrer requests
* - `strict-origin-when-cross-origin`: Sends full referrerURL when making same-origin-referrer requests, and only the ASCII serialization of the origin of the request’s referrerURL when making cross-origin-referrer requests: whose referrerURL and current URL are both potentially trustworthy URLs, or whose referrerURL is a non-potentially trustworthy URL.
* - `unsafe-url`: Sends full referrerURL along for both same-origin-referrer requests and cross-origin-referrer requests.
*
* @default undefined
*/
referrerPolicy?:
| 'no-referrer'
| 'no-referrer-when-downgrade'
| 'same-origin'
| 'origin'
| 'strict-origin'
| 'origin-when-cross-origin'
| 'strict-origin-when-cross-origin'
| 'unsafe-url';
/**
* HTTP headers to pass along the request.
*
* If the option is a function, it will be called on each request.
* Returning a Promise is supported too and the request will stall until it
* resolves.
*
* A good use-case for having a function is when using the URL for authentication,
* where subsequent requests (due to auth) may have a refreshed identity token.
*/
headers?:
| Record<string, string>
| (() =>
| Promise<Record<string, string> | null | void>
| Record<string, string>
| null
| void);
/**
* Control whether the network request error should be retried.
*
* Please note that you can **only** control network errors, all other
* errors are considered fatal and will be reported immediately.
*
* You may implement your own waiting strategy by timing the resolution of the returned promise.
*
* Useful for retrying requests that failed because the service is temporarily unavailable.
*
* `retries` argument counts actual retries, so it will begin with
* 0 after the first failed request.
*
* Returning `false` will report the `err` argument; however, throwing a different error from
* the `err` argument, will report it instead.
*
* @default '() => false'
*/
shouldRetry?: (err: NetworkError, retries: number) => Promise<boolean>;
/**
* The Fetch function to use.
*
* For NodeJS environments consider using [`node-fetch`](https://github.com/node-fetch/node-fetch).
*
* @default global.fetch
*/
fetchFn?: unknown;
/**
* The AbortController implementation to use.
*
* For NodeJS environments before v15 consider using [`node-abort-controller`](https://github.com/southpolesteve/node-abort-controller).
*
* @default global.AbortController
*/
abortControllerImpl?: unknown;
}
/** @category Client */
export interface Client {
/**
* Subscribes to receive a response by making an HTTP request.
*
* It uses the `sink` to emit the received data or errors. Returns a _dispose_
* function used for canceling active requests and cleaning up.
*/
subscribe<Data = Record<string, unknown>, Extensions = unknown>(
request: RequestParams,
sink: Sink<ExecutionResult<Data, Extensions>>,
): () => void;
/**
* Dispose of the client, cancel all active requests and clean up resources.
*/
dispose: () => void;
}
/**
* Creates a disposable GraphQL over HTTP client to transmit
* GraphQL operation results.
*
* @category Client
*/
export function createClient(options: ClientOptions): Client {
const {
credentials = 'same-origin',
referrer,
referrerPolicy,
shouldRetry = () => false,
} = options;
const fetchFn = (options.fetchFn || fetch) as typeof fetch;
const AbortControllerImpl = (options.abortControllerImpl ||
AbortController) as typeof AbortController;
// we dont use yet another AbortController here because of
// node's max EventEmitters listeners being only 10
const client = (() => {
let disposed = false;
const listeners: (() => void)[] = [];
return {
get disposed() {
return disposed;
},
onDispose(cb: () => void) {
if (disposed) {
// empty the call stack and then call the cb
setTimeout(() => cb(), 0);
return () => {
// noop
};
}
listeners.push(cb);
return () => {
listeners.splice(listeners.indexOf(cb), 1);
};
},
dispose() {
if (disposed) return;
disposed = true;
// we copy the listeners so that onDispose unlistens dont "pull the rug under our feet"
for (const listener of [...listeners]) {
listener();
}
},
};
})();
return {
subscribe(request, sink) {
if (client.disposed) throw new Error('Client has been disposed');
const control = new AbortControllerImpl();
const unlisten = client.onDispose(() => {
unlisten();
control.abort();
});
(async () => {
let retryingErr: NetworkError | null = null,
retries = 0;
for (;;) {
if (retryingErr) {
const should = await shouldRetry(retryingErr, retries);
// requst might've been canceled while waiting for retry
if (control.signal.aborted) return;
if (!should) throw retryingErr;
retries++;
}
try {
const url =
typeof options.url === 'function'
? await options.url(https://github.com/request)
: options.url;
if (control.signal.aborted) return;
const headers =
typeof options.headers === 'function'
? await options.headers()
: options.headers ?? {};
if (control.signal.aborted) return;
let res;
try {
res = await fetchFn(url, {
signal: control.signal,
method: 'POST',
headers: {
...headers,
'content-type': 'application/json; charset=utf-8',
accept: 'application/graphql-response+json, application/json',
},
credentials,
referrer,
referrerPolicy,
body: JSON.stringify(request),
});
} catch (err) {
throw new NetworkError(err);
}
if (!res.ok) throw new NetworkError(res);
if (!res.body) throw new Error('Missing response body');
const contentType = res.headers.get('content-type');
if (!contentType) throw new Error('Missing response content-type');
if (
!contentType.includes('application/graphql-response+json') &&
!contentType.includes('application/json')
) {
throw new Error(
`Unsupported response content-type ${contentType}`,
);
}
const result = await res.json();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sink.next(result as any);
return control.abort();
} catch (err) {
if (control.signal.aborted) return;
// all non-network errors are worth reporting immediately
if (!(err instanceof NetworkError)) throw err;
// try again
retryingErr = err;
}
}
})()
.then(() => sink.complete())
.catch((err) => sink.error(err));
return () => control.abort();
},
dispose() {
client.dispose();
},
};
}
/**
* A network error caused by the client or an unexpected response from the server.
*
* To avoid bundling DOM typings (because the client can run in Node env too),
* you should supply the `Response` generic depending on your Fetch implementation.
*
* @category Client
*/
export class NetworkError<
Response extends ResponseLike = ResponseLike,
> extends Error {
/**
* The underlying response thats considered an error.
*
* Will be undefined when no response is received,
* instead an unexpected network error.
*/
public response: Response | undefined;
constructor(msgOrErrOrResponse: string | Error | Response) {
let message, response: Response | undefined;
if (isResponseLike(msgOrErrOrResponse)) {
response = msgOrErrOrResponse;
message =
'Server responded with ' +
msgOrErrOrResponse.status +
': ' +
msgOrErrOrResponse.statusText;
} else if (msgOrErrOrResponse instanceof Error)
message = msgOrErrOrResponse.message;
else message = String(msgOrErrOrResponse);
super(message);
this.name = this.constructor.name;
this.response = response;
}
}
/**
* Concrete interface a response needs to implement for the client.
*
* @category Client
*/
export interface ResponseLike {
readonly ok: boolean;
readonly status: number;
readonly statusText: string;
}
function isResponseLike(val: unknown): val is ResponseLike {
return (
isObject(val) &&
typeof val['ok'] === 'boolean' &&
typeof val['status'] === 'number' &&
typeof val['statusText'] === 'string'
);
}