Skip to content

Commit b97f8f6

Browse files
author
dustin deus
committed
pas requestOptions to onError and onResponse hook
1 parent 10267f6 commit b97f8f6

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,20 @@ const datasource = new (class MoviesAPI extends HTTPDataSource {
7070
// manipulate request before it is send
7171
}
7272

73-
onResponse<TResult = unknown>(request: Request, response: Response<TResult>): void {
73+
onResponse<TResult = unknown>(request: RequestOptions, response: Response<TResult>): void {
7474
// manipulate response or handle unsuccessful response in a different way
7575
return super.onResponse(request, response)
7676
}
7777

78-
onError(error: RequestError): void {
78+
onError(error: RequestError, request: RequestOptions): void {
7979
// log errors
8080
}
8181

8282
async getMovie(id) {
8383
return this.get(`/movies/${id}`, {
8484
headers: {
8585
'X-Foo': 'bar',
86-
}
86+
},
8787
})
8888
}
8989
})()
@@ -112,4 +112,4 @@ maxmemory 10mb
112112
maxmemory-policy allkeys-lru
113113
```
114114

115-
This will limit the cache to 10MB and removes the least recently used keys from the cache when the cache hits the limits.
115+
This will limit the cache to 10MB and removes the least recently used keys from the cache when the cache hits the limits.

src/http-data-source.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export type CacheTTLOptions = {
1919
}
2020
}
2121

22-
export type RequestOptions = Omit<DispatchOptions, 'origin' | 'path' | 'method'> & CacheTTLOptions
22+
export type ClientRequestOptions = Omit<DispatchOptions, 'origin' | 'path' | 'method'> & CacheTTLOptions
2323

24-
type InternalRequestOptions = DispatchOptions & CacheTTLOptions
24+
export type RequestOptions = DispatchOptions & CacheTTLOptions
2525

2626
export type Response<TResult> = {
2727
body: TResult
@@ -34,7 +34,7 @@ export interface LRUOptions {
3434

3535
export interface HTTPDataSourceOptions {
3636
pool?: Pool
37-
requestOptions?: RequestOptions
37+
requestOptions?: ClientRequestOptions
3838
clientOptions?: Client.Options
3939
lru?: Partial<LRUOptions>
4040
}
@@ -74,7 +74,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
7474
public context!: TContext
7575
private storageAdapter!: Keyv
7676
private readonly pool: Pool
77-
private readonly globalRequestOptions?: RequestOptions
77+
private readonly globalRequestOptions?: ClientRequestOptions
7878
private readonly abortController: AbortController
7979
private readonly memoizedResults: QuickLRU<string, Response<any>>
8080

@@ -112,7 +112,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
112112
}
113113

114114
protected isResponseCacheable<TResult = unknown>(
115-
requestOptions: InternalRequestOptions,
115+
requestOptions: RequestOptions,
116116
response: Response<TResult>,
117117
): boolean {
118118
return cacheableStatusCodes.indexOf(response.statusCode) > -1 && requestOptions.method === 'GET'
@@ -125,7 +125,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
125125
* @param request
126126
* @returns
127127
*/
128-
protected onCacheKeyCalculation(requestOptions: InternalRequestOptions): string {
128+
protected onCacheKeyCalculation(requestOptions: RequestOptions): string {
129129
return requestOptions.origin + requestOptions.path
130130
}
131131

@@ -135,16 +135,19 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
135135
*
136136
* @param request
137137
*/
138-
protected onRequest?(requestOptions: RequestOptions): void
138+
protected onRequest?(requestOptions: ClientRequestOptions): void
139139

140140
/**
141141
* onResponse is executed when a response has been received.
142142
* By default the implementation will throw for for unsuccessful responses.
143143
*
144-
* @param _error
145-
* @param _request
144+
* @param _requestOptions
145+
* @param response
146146
*/
147-
protected onResponse<TResult = unknown>(response: Response<TResult>): Response<TResult> {
147+
protected onResponse<TResult = unknown>(
148+
_requestOptions: RequestOptions,
149+
response: Response<TResult>,
150+
): Response<TResult> {
148151
if (this.isResponseOk(response.statusCode)) {
149152
return response
150153
}
@@ -155,11 +158,11 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
155158
)
156159
}
157160

158-
protected onError?(_error: Error): void
161+
protected onError?(_error: Error, requestOptions: RequestOptions): void
159162

160163
protected async get<TResult = unknown>(
161164
path: string,
162-
requestOptions?: RequestOptions,
165+
requestOptions?: ClientRequestOptions,
163166
): Promise<Response<TResult>> {
164167
return await this.request<TResult>({
165168
...requestOptions,
@@ -171,7 +174,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
171174

172175
protected async post<TResult = unknown>(
173176
path: string,
174-
requestOptions?: RequestOptions,
177+
requestOptions?: ClientRequestOptions,
175178
): Promise<Response<TResult>> {
176179
return await this.request<TResult>({
177180
...requestOptions,
@@ -183,7 +186,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
183186

184187
protected async delete<TResult = unknown>(
185188
path: string,
186-
requestOptions?: RequestOptions,
189+
requestOptions?: ClientRequestOptions,
187190
): Promise<Response<TResult>> {
188191
return await this.request<TResult>({
189192
...requestOptions,
@@ -195,7 +198,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
195198

196199
protected async put<TResult = unknown>(
197200
path: string,
198-
requestOptions?: RequestOptions,
201+
requestOptions?: ClientRequestOptions,
199202
): Promise<Response<TResult>> {
200203
return await this.request<TResult>({
201204
...requestOptions,
@@ -206,7 +209,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
206209
}
207210

208211
private async performRequest<TResult>(
209-
options: InternalRequestOptions,
212+
options: RequestOptions,
210213
cacheKey: string,
211214
): Promise<Response<TResult>> {
212215
this.onRequest?.(options)
@@ -230,7 +233,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
230233
body: json,
231234
}
232235

233-
this.onResponse<TResult>(response)
236+
this.onResponse<TResult>(options, response)
234237

235238
if (options.requestCache && this.isResponseCacheable<TResult>(options, response)) {
236239
this.storageAdapter.set(cacheKey, response, options.requestCache?.maxTtl)
@@ -243,7 +246,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
243246

244247
return response
245248
} catch (error) {
246-
this.onError?.(error)
249+
this.onError?.(error, options)
247250

248251
if (options.requestCache) {
249252
const hasFallback: Response<TResult> = await this.storageAdapter.get(
@@ -259,7 +262,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
259262
}
260263

261264
private async request<TResult = unknown>(
262-
requestOptions: InternalRequestOptions,
265+
requestOptions: RequestOptions,
263266
): Promise<Response<TResult>> {
264267
const cacheKey = this.onCacheKeyCalculation(requestOptions)
265268
const ttlCacheEnabled = requestOptions.requestCache

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
export { HTTPDataSource, HTTPDataSourceOptions, LRUOptions, RequestOptions, Response } from './http-data-source'
1+
export {
2+
HTTPDataSource,
3+
HTTPDataSourceOptions,
4+
LRUOptions,
5+
ClientRequestOptions,
6+
Response,
7+
RequestOptions,
8+
CacheTTLOptions,
9+
} from './http-data-source'

test/rest-data-source.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ test('Should be able to define a custom cache key for request memoization', asyn
271271
})
272272

273273
test('Should call onError on request error', async (t) => {
274-
t.plan(6)
274+
t.plan(7)
275275

276276
const path = '/'
277277

@@ -293,10 +293,11 @@ test('Should call onError on request error', async (t) => {
293293
super(baseURL)
294294
}
295295

296-
onResponse<TResult = any>(response: Response<TResult>) {
296+
onResponse<TResult = any>(requestOptions: RequestOptions, response: Response<TResult>) {
297+
t.truthy(requestOptions)
297298
t.truthy(response)
298299
t.pass('onResponse')
299-
return super.onResponse<TResult>(response)
300+
return super.onResponse<TResult>(requestOptions, response)
300301
}
301302

302303
onError(error: Error) {

0 commit comments

Comments
 (0)