/axios-middleware

Simple axios middleware service

Primary LanguageJavaScriptMIT LicenseMIT

axios-middleware

Build Status Dependency Status npm version

Simple axios HTTP middleware service.

Explore the documentation.

Installation

npm install --save axios-middleware

How to use

A simple example using the simplified middleware syntax.

import axios from 'axios';
import { HttpMiddlewareService } from 'axios-middleware';

// Create a new service instance
const service = new HttpMiddlewareService(axios);

// Then register your middleware instances.
service.register({
    onRequest(config) {
        // handle the request config
        return config;
    },
    onSync(promise) {
        // handle the promsie
        return promise;
    },
    onResponse(response) {
        // handle the response
        return response;
    }
});

// We're good to go!
export default { service };

A common use-case would be to expose an instance of the service which consumes an axios instance configured for an API. It's then possible to register middlewares for this API at different stages of the initialization process of an application.