Skip to content

Commit 9155465

Browse files
committed
make onRequest hook async
1 parent 20bcaa4 commit 9155465

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const datasource = new (class MoviesAPI extends HTTPDataSource {
6666
// return different key based on request options
6767
}
6868

69-
onRequest(request: Request): void {
69+
async onRequest(request: Request): Promise<void> {
7070
// manipulate request before it is send
7171
// for example assign a AbortController signal to all requests and abort
7272

src/http-data-source.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
138138
*
139139
* @param request
140140
*/
141-
protected onRequest?(request: Request): void
141+
protected async onRequest?(request: Request): Promise<void>
142142

143143
/**
144144
* onResponse is executed when a response has been received.
@@ -229,19 +229,19 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
229229
}
230230

231231
private async performRequest<TResult>(
232-
options: Request,
232+
request: Request,
233233
cacheKey: string,
234234
): Promise<Response<TResult>> {
235-
this.onRequest?.(options)
235+
await this.onRequest?.(request)
236236

237237
try {
238238
const responseData = await this.pool.request({
239-
method: options.method,
240-
origin: options.origin,
241-
path: options.path,
242-
body: options.body,
243-
headers: options.headers,
244-
signal: options.signal,
239+
method: request.method,
240+
origin: request.origin,
241+
path: request.path,
242+
body: request.body,
243+
headers: request.headers,
244+
signal: request.signal,
245245
})
246246

247247
responseData.body.setEncoding('utf8')
@@ -262,29 +262,29 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
262262
body: json,
263263
}
264264

265-
this.onResponse<TResult>(options, response)
265+
this.onResponse<TResult>(request, response)
266266

267267
// let's see if we can fill the shared cache
268-
if (options.requestCache && this.isResponseCacheable<TResult>(options, response)) {
269-
response.maxTtl = Math.max(options.requestCache.maxTtl, options.requestCache.maxTtlIfError)
268+
if (request.requestCache && this.isResponseCacheable<TResult>(request, response)) {
269+
response.maxTtl = Math.max(request.requestCache.maxTtl, request.requestCache.maxTtlIfError)
270270
const cachedResponse = JSON.stringify(response)
271271
this.cache
272272
.set(cacheKey, cachedResponse, {
273-
ttl: options.requestCache?.maxTtl,
273+
ttl: request.requestCache?.maxTtl,
274274
})
275275
.catch((err) => this.logger?.error(err))
276276
this.cache
277277
.set(`staleIfError:${cacheKey}`, cachedResponse, {
278-
ttl: options.requestCache?.maxTtlIfError,
278+
ttl: request.requestCache?.maxTtlIfError,
279279
})
280280
.catch((err) => this.logger?.error(err))
281281
}
282282

283283
return response
284284
} catch (error) {
285-
this.onError?.(error, options)
285+
this.onError?.(error, request)
286286

287-
if (options.requestCache) {
287+
if (request.requestCache) {
288288
const cacheItem = await this.cache.get(`staleIfError:${cacheKey}`)
289289
if (cacheItem) {
290290
const response: Response<TResult> = sjson.parse(cacheItem)

test/http-data-source.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ test('Should be able to modify request in willSendRequest', async (t) => {
588588
constructor() {
589589
super(baseURL)
590590
}
591-
onRequest(request: Request) {
591+
async onRequest(request: Request) {
592592
request.headers = {
593593
'X-Foo': 'bar',
594594
}

0 commit comments

Comments
 (0)