-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathRESTDataSource.ts
298 lines (256 loc) · 7.91 KB
/
RESTDataSource.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
import {
Request,
RequestInit,
Response,
BodyInit,
Headers,
URL,
URLSearchParams,
URLSearchParamsInit,
fetch,
} from 'apollo-server-env';
import { ValueOrPromise } from 'apollo-server-types';
import { DataSource, DataSourceConfig } from 'apollo-datasource';
import { HTTPCache } from './HTTPCache';
import {
ApolloError,
AuthenticationError,
ForbiddenError,
} from 'apollo-server-errors';
declare module 'apollo-server-env/dist/fetch' {
interface RequestInit {
cacheOptions?:
| CacheOptions
| ((response: Response, request: Request) => CacheOptions | undefined);
}
}
export type RequestOptions = RequestInit & {
path: string;
params: URLSearchParams;
headers: Headers;
body?: Body;
};
export interface CacheOptions {
ttl?: number;
}
export type Body = BodyInit | object;
export { Request };
export abstract class RESTDataSource<TContext = any> extends DataSource {
httpCache!: HTTPCache;
context!: TContext;
memoizedResults = new Map<string, Promise<any>>();
constructor(private httpFetch?: typeof fetch) {
super();
}
initialize(config: DataSourceConfig<TContext>): void {
this.context = config.context;
this.httpCache = new HTTPCache(config.cache, this.httpFetch);
}
baseURL?: string;
// By default, we use the full request URL as the cache key.
// You can override this to remove query parameters or compute a cache key in any way that makes sense.
// For example, you could use this to take Vary header fields into account.
// Although we do validate header fields and don't serve responses from cache when they don't match,
// new reponses overwrite old ones with different vary header fields.
protected cacheKeyFor(request: Request): string {
return request.url;
}
protected willSendRequest?(request: RequestOptions): ValueOrPromise<void>;
protected resolveURL(request: RequestOptions): ValueOrPromise<URL> {
let path = request.path;
if (path.startsWith('/')) {
path = path.slice(1);
}
const baseURL = this.baseURL;
if (baseURL) {
const normalizedBaseURL = baseURL.endsWith('/')
? baseURL
: baseURL.concat('/');
return new URL(path, normalizedBaseURL);
} else {
return new URL(path);
}
}
protected cacheOptionsFor?(
response: Response,
request: Request,
): CacheOptions | undefined;
protected async didReceiveResponse<TResult = any>(
response: Response,
_request: Request,
): Promise<TResult> {
if (response.ok) {
return (this.parseBody(response) as any) as Promise<TResult>;
} else {
throw await this.errorFromResponse(response);
}
}
protected didEncounterError(error: Error, _request: Request) {
throw error;
}
protected parseBody(response: Response): Promise<object | string> {
const contentType = response.headers.get('Content-Type');
const contentLength = response.headers.get('Content-Length');
if (
// As one might expect, a "204 No Content" is empty! This means there
// isn't enough to `JSON.parse`, and trying will result in an error.
response.status !== 204 &&
contentLength !== '0' &&
contentType &&
(contentType.startsWith('application/json') ||
contentType.startsWith('application/hal+json'))
) {
return response.json();
} else {
return response.text();
}
}
protected async errorFromResponse(response: Response) {
const message = `${response.status}: ${response.statusText}`;
let error: ApolloError;
if (response.status === 401) {
error = new AuthenticationError(message);
} else if (response.status === 403) {
error = new ForbiddenError(message);
} else {
error = new ApolloError(message);
}
const body = await this.parseBody(response);
Object.assign(error.extensions, {
response: {
url: response.url,
status: response.status,
statusText: response.statusText,
body,
},
});
return error;
}
protected async get<TResult = any>(
path: string,
params?: URLSearchParamsInit,
init?: RequestInit,
): Promise<TResult> {
return this.fetch<TResult>(
Object.assign({ method: 'GET', path, params }, init),
);
}
protected async post<TResult = any>(
path: string,
body?: Body,
init?: RequestInit,
): Promise<TResult> {
return this.fetch<TResult>(
Object.assign({ method: 'POST', path, body }, init),
);
}
protected async patch<TResult = any>(
path: string,
body?: Body,
init?: RequestInit,
): Promise<TResult> {
return this.fetch<TResult>(
Object.assign({ method: 'PATCH', path, body }, init),
);
}
protected async put<TResult = any>(
path: string,
body?: Body,
init?: RequestInit,
): Promise<TResult> {
return this.fetch<TResult>(
Object.assign({ method: 'PUT', path, body }, init),
);
}
protected async delete<TResult = any>(
path: string,
params?: URLSearchParamsInit,
init?: RequestInit,
): Promise<TResult> {
return this.fetch<TResult>(
Object.assign({ method: 'DELETE', path, params }, init),
);
}
private async fetch<TResult>(
init: RequestInit & {
path: string;
params?: URLSearchParamsInit;
},
): Promise<TResult> {
if (!(init.params instanceof URLSearchParams)) {
init.params = new URLSearchParams(init.params);
}
if (!(init.headers && init.headers instanceof Headers)) {
init.headers = new Headers(init.headers || Object.create(null));
}
const options = init as RequestOptions;
if (this.willSendRequest) {
await this.willSendRequest(options);
}
const url = await this.resolveURL(options);
// Append params to existing params in the path
for (const [name, value] of options.params) {
url.searchParams.append(name, value);
}
// We accept arbitrary objects and arrays as body and serialize them as JSON
if (
options.body !== undefined &&
options.body !== null &&
(options.body.constructor === Object ||
Array.isArray(options.body) ||
((options.body as any).toJSON &&
typeof (options.body as any).toJSON === 'function'))
) {
options.body = JSON.stringify(options.body);
// If Content-Type header has not been previously set, set to application/json
if (!options.headers.get('Content-Type')) {
options.headers.set('Content-Type', 'application/json');
}
}
const request = new Request(String(url), options);
const cacheKey = this.cacheKeyFor(request);
const performRequest = async () => {
return this.trace(`${options.method || 'GET'} ${url}`, async () => {
const cacheOptions = options.cacheOptions
? options.cacheOptions
: this.cacheOptionsFor && this.cacheOptionsFor.bind(this);
try {
const response = await this.httpCache.fetch(request, {
cacheKey,
cacheOptions,
});
return await this.didReceiveResponse(response, request);
} catch (error) {
this.didEncounterError(error, request);
}
});
};
if (request.method === 'GET') {
let promise = this.memoizedResults.get(cacheKey);
if (promise) return promise;
promise = performRequest();
this.memoizedResults.set(cacheKey, promise);
return promise;
} else {
this.memoizedResults.delete(cacheKey);
return performRequest();
}
}
private async trace<TResult>(
label: string,
fn: () => Promise<TResult>,
): Promise<TResult> {
if (process && process.env && process.env.NODE_ENV === 'development') {
// We're not using console.time because that isn't supported on Cloudflare
const startTime = Date.now();
try {
return await fn();
} finally {
const duration = Date.now() - startTime;
console.log(`${label} (${duration}ms)`);
}
} else {
return fn();
}
}
}