Skip to content

Commit a7ff21b

Browse files
authored
docs(http-client): access Request via responseInterceptor (#1548)
Refs #1486
1 parent 03a46ca commit a7ff21b

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

docs/usage/http-client.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ const request = {
300300
method: 'GET',
301301
responseInterceptor: res => {
302302
res.arbitraryProp = 'arbitrary value';
303-
return req;
303+
return res;
304304
},
305305
};
306306

@@ -396,6 +396,34 @@ SwaggerClient.http(request);
396396
> *__Note:__ you can mutate or assign any property of `Response` object as long as your
397397
interceptor produces a valid `Response` object again.*
398398

399+
###### Accessing request in Response Interceptor
400+
401+
In order to access original `Request` in `responseInterceptor` you have to declare
402+
`responseInterceptor` either as [function declaration](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function) or [function expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function).
403+
By doing so, the interceptor will be bound to the original `Request`.
404+
405+
```js
406+
import SwaggerClient from 'swagger-client';
407+
408+
const request = {
409+
url: 'https://httpbin.org/',
410+
method: 'GET',
411+
responseInterceptor: function(res) {
412+
const request = this;
413+
console.log(request.url); // https://httpbin.org/
414+
console.log(request.method); // GET
415+
416+
res.arbitraryProp = 'arbitrary value';
417+
return res;
418+
},
419+
};
420+
421+
SwaggerClient.http(request);
422+
```
423+
424+
> *__Note:__ this will not work if [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) are used to define a `responseInterceptor`.*
425+
426+
399427
#### Response serialization
400428

401429
We detect the response `Content-Type` which identifies the payload of the `Response`.

0 commit comments

Comments
 (0)