About
Realization of request-kit engine interface, to process rest request. Extandable with plugins
Example use case
const engine = createRequestEngine({
plugins: [pluginA, pluginB],
});
engine.request({ endpoint: '/api/v1/url' });
API
engine.preset(configurationObject) – apply additional configuration, which overrides existent one if collide (acts like assign). Exception is for headers, only same headers will overwritten, new header would just added to existent headers object.
Note, in fact new engine will create on each preset call. So, it's possible to even use previous engine and preset one:
const legacyApiEngine = engine.preset({ apiVersion: 'v1' })
const apiEngine = engine.preset({ apiVersion: 'v2' })
engine.request(configurationObject) – execute request. Also may override configuration with same principles as preset, but those overriding not saved, and only applied to currently executing request
Configuration object options
fetchHandler – method to process request, by default window.fetch is used, but instead any third-party lib could be used. For example superagent or axios. It's could be convenient to use same lib that already used in other parts of project.
fetchAdapter - function to adapt request config generated by engine to signature of tool specified in fetchHandler. By default config is adapted to window.fetch. config.endpoint used as fetch first param, and rest config object just passed as second.
Request config is always plain object. Almost all of it properties is passed from project or generated in plugins, so it's up to project, how to configure adapter.
Note: All plugins from request-kit is made to have window.fetch compatible format
plugins - array of functions, where each could modify request config. Should be written like shown in example:
export default next => ({ endpoint, ...restOptions }) => next({
endpoint: typeof endpoint === 'string' ? endpoint : endpoint(restOptions),
...restOptions,
});