source: node_modules/undici/docs/api/DispatchInterceptor.md@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1# Interface: DispatchInterceptor
2
3Extends: `Function`
4
5A function that can be applied to the `Dispatcher.Dispatch` function before it is invoked with a dispatch request.
6
7This allows one to write logic to intercept both the outgoing request, and the incoming response.
8
9### Parameter: `Dispatcher.Dispatch`
10
11The base dispatch function you are decorating.
12
13### ReturnType: `Dispatcher.Dispatch`
14
15A dispatch function that has been altered to provide additional logic
16
17### Basic Example
18
19Here is an example of an interceptor being used to provide a JWT bearer token
20
21```js
22'use strict'
23
24const insertHeaderInterceptor = dispatch => {
25 return function InterceptedDispatch(opts, handler){
26 opts.headers.push('Authorization', 'Bearer [Some token]')
27 return dispatch(opts, handler)
28 }
29}
30
31const client = new Client('https://localhost:3000', {
32 interceptors: { Client: [insertHeaderInterceptor] }
33})
34
35```
36
37### Basic Example 2
38
39Here is a contrived example of an interceptor stripping the headers from a response.
40
41```js
42'use strict'
43
44const clearHeadersInterceptor = dispatch => {
45 const { DecoratorHandler } = require('undici')
46 class ResultInterceptor extends DecoratorHandler {
47 onHeaders (statusCode, headers, resume) {
48 return super.onHeaders(statusCode, [], resume)
49 }
50 }
51 return function InterceptedDispatch(opts, handler){
52 return dispatch(opts, new ResultInterceptor(handler))
53 }
54}
55
56const client = new Client('https://localhost:3000', {
57 interceptors: { Client: [clearHeadersInterceptor] }
58})
59
60```
Note: See TracBrowser for help on using the repository browser.