@@ -68,9 +68,16 @@ const datasource = new (class MoviesAPI extends HTTPDataSource {
6868
6969 onRequest(request : Request ): void {
7070 // manipulate request before it is send
71+ // for example assign a AbortController signal to all requests and abort
72+
73+ request .signal = this .context .abortController .signal
74+
75+ setTimeout (() => {
76+ this .context .abortController .abort ()
77+ }, 3000 ).unref ()
7178 }
7279
73- onResponse<TResult = unknown >(request : Request , response : Response <TResult >): void {
80+ onResponse<TResult = unknown >(request : Request , response : Response <TResult >): Response < TResult > {
7481 // manipulate response or handle unsuccessful response in a different way
7582 return super .onResponse (request , response )
7683 }
@@ -84,6 +91,9 @@ const datasource = new (class MoviesAPI extends HTTPDataSource {
8491 query: {
8592 a: 1 ,
8693 },
94+ context: {
95+ tracingName: ' getMovie' ,
96+ },
8797 headers: {
8898 ' X-Foo' : ' bar' ,
8999 },
@@ -94,17 +104,14 @@ const datasource = new (class MoviesAPI extends HTTPDataSource {
94104 })
95105 }
96106})()
97-
98- // cancel all running requests e.g when the request is closed prematurely
99- datasource .abort ()
100107```
101108
102109## Hooks
103110
104111- ` onCacheKeyCalculation ` - Returns the cache key for request memoization.
105112- ` onRequest ` - Is executed before a request is made. This can be used to intercept requests (setting header, timeouts ...).
106113- ` onResponse ` - Is executed when a response has been received. This can be used to alter the response before it is passed to caller or to log errors.
107- - ` onError ` - Is executed for any error.
114+ - ` onError ` - Is executed for any request error.
108115
109116## Error handling
110117
0 commit comments