Skip to content

Files

Latest commit

48d9977 · Aug 28, 2023

History

History
175 lines (128 loc) · 4.68 KB

README.md

File metadata and controls

175 lines (128 loc) · 4.68 KB

DRF-Axios-Middleware

NPM Downloads

singularIT Logo

Website | Blog | Team

Features

  • 🦾 TypeScript support
  • 📦 CommonJS and ES Module support
  • ⚙️ Works with Axios and Django Rest Framework
  • 📝 Customizable
  • 📚 Well documented (Docs)

Installation

npm install @singularit/drf-axios-middleware

Usage

Basic

@singularit/drf-axios-middleware is a middleware for axios. It can be used with axios and django rest framework. It introduces a new parameter filterSet to the axios request config. This parameter is used to filter the request url. The filterSet is a object with the following structure:

{
  [field]: {
    [operator]: value
  }
}

By default, the operator can be one of the following:

const DefaultDRFFilters = ['in', 'exact', 'lt', 'gt', 'lte', 'gte', 'startswith', 'endswith']

You can use custom operators. See Customization for more information.

The final request url will be generated like: url?field__operator=value. Fields can be nested and will be generated like: url?field__nested__operator=value. See Nested Fields for more information.

Example

import axios from 'axios';
import applyDrfMiddleware from '@singularit/drf-axios-middleware';

const api = applyDrfMiddleware(axios.create());

// get all users with id >= 15
api.get('/api/v1/users/', {filterSet: {id: {gte: 15}}}).then((response) => {
    console.log(response.data);
});
// request: GET /api/v1/users/?id__gte=15

Nested Fields

Nested fields can be used to filter nested objects. The middleware will generate the correct url for you.

Examples

Basic Nested Field
import axios from 'axios';
import applyDrfMiddleware from '@singularit/drf-axios-middleware';

const api = applyDrfMiddleware(axios.create());

// get all users with id >= 15
api.get('/api/v1/users/', {filterSet: {profile: {id: {gte: 15}}}}).then((response) => {
    console.log(response.data);
});

// request: GET /api/v1/users/?profile__id__gte=15
Nested Field with multiple operators
import axios from 'axios';
import applyDrfMiddleware from '@singularit/drf-axios-middleware';

const api = applyDrfMiddleware(axios.create());

// get all users with id >= 15 and id <= 99
api.get('/api/v1/users/', {filterSet: {profile: {id: {gte: 15, lte: 99}}}}).then((response) => {
    console.log(response.data);
});

// request: GET /api/v1/users/?profile__id__gte=15&profile__id__lte=99

Customization

The middleware can be customized by passing a config object to the applyDrfMiddleware function.

Examples

Custom Filter Handler
import axios from 'axios';
import applyDrfMiddleware from '@singularit/drf-axios-middleware';

const api = applyDrfMiddleware(axios.create(), {
    filterHanlder: {
        notIn: (key, value) => {
            return [{key: 'notin', value: value}]
        }
    }
});

// get all users with id not 1, 2 or 3 
api.get('/api/v1/users/', {filterSet: {id: {notIn: [1, 2, 3]}}}).then((response) => {
    console.log(response.data);
});

// request: GET /api/v1/users/?id__notin=1,2,3 
// Note: depends on your array serializer

Note: notin is not a valid operator for django rest framework. You have to implement the filter on your own.

Custom Filter to multiple Parameters (between to gte and lte)
import axios from 'axios';
import applyDrfMiddleware from '@singularit/drf-axios-middleware';

const api = applyDrfMiddleware(axios.create(), {
    filterHanlder: {
        between: (key, value) => {
            return [
                {key: 'gte', value: value[0]},
                {key: 'lte', value: value[1]}
            ]
        }
    }
});

// get all users with id between 1 and 99
api.get('/api/v1/users/', {filterSet: {id: {between: [1, 99]}}}).then((response) => {
    console.log(response.data);
});

// request: GET /api/v1/users/?id__gte=1&id__lte=99

License

MIT License © 2023-PRESENT singularIT GmbH