axios-morphism
Axios plugin to transform data requests/responses based on a schema.
This package is built upon Morphism
. Read the documentation here.
Getting started
Installation
npm install --save axios morphism # Axios and Morphism are defined as a peer dependencies in axios-morphism npm install --save axios-morphism
Example
;; ; ; ;applyclient, configuration; await client.get'/people/1';▼// {// name: "Luke Skywalker"// height: "172"// weight: "77"// } await client.get'/people';▼// [// {// name: "Luke Skywalker"// height: "172"// weight: "77"// },....// ]
Usage
Schema
Define a schema corresponding to the shape you expect to have after the transformation has been applied.
▶ Read About Morphism's Schema Capabilities
;
Interceptors Configuration
Create your configurations to be applied on Axios requests or responses.
Example:
;
Axios-Morphism Configuration
Property | Type | Description | Example |
---|---|---|---|
url | string |
Base URL to listen on | https://swapi.co/api |
interceptors | { responses: [], requests: []}; |
List of Responses and Requests Interceptors Configuration to register against Axios | |
interceptors.responses[].matcher | string RegExp Function |
Matcher used to detect on which response to apply the transformer | - 'people/:id' - /people$/i - (response: AxiosResponse) => response.config.method === 'POST' |
interceptors.requests[].matcher | string RegExp Function |
Matcher used to detect on which request to apply the transformer | - 'planets/:id' - /planets$/i - (request: AxiosRequestConfig) => request.url.includes('planets') |
interceptors.requests[].schema interceptors.responses[].schema | Schema StrictSchema |
A schema is an object-preserving map from one data structure to another. | Morphism Schema Examples |
interceptors.requests[].dataSelector interceptors.responses[].dataSelector | string |
A selector to access the data in the Axios returned data | With this Axios Response: { data: { results: [] }} . Pick the data with { dataSelector: 'results' } |
Apply Configurations
Apply your interceptors on your axios instance and there you go!
; ; ;applyclient, configuration; // Start making requests to see you data transformedawait client.get'/people';await client.get'/people/1';
Remove Configurations
Use the unsubscribe
method returned from the apply
function to opt-out from the interceptors
; ;axiosMorphism.unsubscribe; // Remove all registered interceptors
Combine Configurations
axios-morphism
provides the combine
function in order to help you merge multiple configurations under a baseURL
.
; ;; ;applyclient, combine'https://swapi.co/api/', peopleMorphism, planetMorphism; // Start making requests to see you data transformedawait client.get'/people';await client.get'/planets/1';
License
MIT © Yann Renaudin