UNPKG

@lableb/javascript-sdk

Version:

Lableb cloud search client for javascript

124 lines (85 loc) 3.1 kB
# Lableb Interceptor Most developers are familiar with interceptors at http level, this library also provide passing many interceptors to manipulate the inner request which is sent to [Lableb](https://lableb.com) ## How to use? When creating Lableb's client, you pass an array of function, each represents an interceptors that can manipulate the requests parameters before being sent to Lableb like editing url(or part of it), adding more params, removing some params, adding a unique type of headers that the library don't provider normally and so on. ```js LablebClient({ APIKey: API_KEY, platformName: PLATFORM_NAME, interceptors: [ function appendToUrl(requestParams){ return { ...requestParams, url: requestParams.url + '/demo' } } ] }); ``` Adding `appendToUrl` interceptor function will append the `/demo` to all url(s) used in Lableb's client. ---------------------------- ## Many interceptors working together You can add many interceptors functions, each complete the job of another forming the final wanted goal ```js LablebClient({ APIKey: API_KEY, platformName: PLATFORM_NAME, interceptors: [ function appendToUrl(requestParams){ return { ...requestParams, url: requestParams.url + '/demo' } }, function addCustomHeader(requestParams){ return { ...requestParams, headers:{ ...requestParams.headers, 'Accept-Language': 'fr' } } }, function appendCustomParamForAllRequests(requestParams){ return { ...requestParams, params: { ...requestParams.params, type: 'posts' } } }, ] }); ``` > Note: if order of interceptor is important for you, you have to know that we iterate over interceptors in orders from the 1st to the last element. ---------------------------- ## Interceptor API ### Interceptors Arguments | field | type | description | | ------- | ------ | ----------- | | url | string | request url | | params | object | request query params as js object | | headers | object | request headers as js object | | method | string | used http method | | body | object | request body if it has one | ### Interceptors Return are the same of interceptors arguments. > Note: interceptor will skip returning any new property that is not listed ### Request parameters type use the exported `InterceptorParams` interface to help you write your interceptor ```js import { InterceptorParams } from '@lableb/javascript-sdk'; LablebClient({ APIKey: API_KEY, platformName: PLATFORM_NAME, interceptors: [ function appendToUrl(requestParams: InterceptorParams){ return { ...requestParams, url: requestParams.url + '/demo' } } ] }); ```