11cb0ef41Sopenharmony_ci# Interface: DispatchInterceptor
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciExtends: `Function`
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciA function that can be applied to the `Dispatcher.Dispatch` function before it is invoked with a dispatch request.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciThis allows one to write logic to intercept both the outgoing request, and the incoming response.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci### Parameter: `Dispatcher.Dispatch`
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciThe base dispatch function you are decorating.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci### ReturnType: `Dispatcher.Dispatch`
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciA dispatch function that has been altered to provide additional logic
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci### Basic Example
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciHere is an example of an interceptor being used to provide a JWT bearer token
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci```js
221cb0ef41Sopenharmony_ci'use strict'
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst insertHeaderInterceptor = dispatch => {
251cb0ef41Sopenharmony_ci  return function InterceptedDispatch(opts, handler){
261cb0ef41Sopenharmony_ci    opts.headers.push('Authorization', 'Bearer [Some token]')
271cb0ef41Sopenharmony_ci    return dispatch(opts, handler)
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst client = new Client('https://localhost:3000', {
321cb0ef41Sopenharmony_ci  interceptors: { Client: [insertHeaderInterceptor] }
331cb0ef41Sopenharmony_ci})
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci```
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci### Basic Example 2
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciHere is a contrived example of an interceptor stripping the headers from a response.
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci```js
421cb0ef41Sopenharmony_ci'use strict'
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciconst clearHeadersInterceptor = dispatch => {
451cb0ef41Sopenharmony_ci  const { DecoratorHandler } = require('undici')
461cb0ef41Sopenharmony_ci  class ResultInterceptor extends DecoratorHandler {
471cb0ef41Sopenharmony_ci    onHeaders (statusCode, headers, resume) {
481cb0ef41Sopenharmony_ci      return super.onHeaders(statusCode, [], resume)
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci  return function InterceptedDispatch(opts, handler){
521cb0ef41Sopenharmony_ci    return dispatch(opts, new ResultInterceptor(handler))
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciconst client = new Client('https://localhost:3000', {
571cb0ef41Sopenharmony_ci  interceptors: { Client: [clearHeadersInterceptor] }
581cb0ef41Sopenharmony_ci})
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci```
61