GithubHelp home page GithubHelp logo

singularit-de / drf-axios-middleware Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 363 KB

@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.

Home Page: https://singularit-de.github.io/drf-axios-middleware/

License: MIT License

JavaScript 0.92% TypeScript 99.08%
axios axios-interceptor axios-middleware django django-rest-framework jest singularit typescript

drf-axios-middleware's Introduction

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

drf-axios-middleware's People

Contributors

orbisk avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

moerlin1337

drf-axios-middleware's Issues

Typescript Fehler, wenn ein Attribut des Filtersets eine Union aus Typen einschließlich Objekt ist.

Describe the bug

Wenn der Typ eines Attributes eines FilterSets eine Union aus einem Objekt und einem weiteren Datentypen ist, bekommt man einen Fehler, wenn man nach den Attributen des Objekts filtern möchte.

Reproduction

interface Example {a: {b: number} | number}; type FilterSet = FilterSetConfig; const filterSet: FilterSet = {a: {b: 1}}

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1255U
    Memory: 2.12 GB / 15.71 GB
Binaries:
    Node: 18.15.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.5.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.3.1 - C:\Program Files\nodejs\pnpm.CMD
Browsers:
    Edge: Chromium (118.0.2088.76)
    Internet Explorer: 11.0.22621.1
npmPackages:
    @singularit/drf-axios-middleware: ^1.0.0-beta.1 => 1.0.0-beta.1
    @singularit/eslint-config-vue: ^1.2.0 => 1.2.0
    @singularit/kurz-components: ^1.0.0-beta.10 => 1.0.0-beta.10
    axios: ^1.4.0 => 1.4.0

Used Package Manager

npm

Validations

TypeScript Fehler beim Zugriff auf Filter-Attribute des Filtersets

Describe the bug

Beim Zugriff auf ein Filter-Attribut des Filtersets bekommt man TypeScript Error TS2339: Property ... does not exist on type

Reproduction

type FilterSet = FilterSetConfig<{attribute: any}>; const filterSet: FilterSet = {attribute: {value: 1}}; filterSet.attribute.value = 2

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1255U
    Memory: 2.12 GB / 15.71 GB
Binaries:
    Node: 18.15.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.5.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.3.1 - C:\Program Files\nodejs\pnpm.CMD
Browsers:
    Edge: Chromium (118.0.2088.76)
    Internet Explorer: 11.0.22621.1
npmPackages:
    @singularit/drf-axios-middleware: ^1.0.0-beta.1 => 1.0.0-beta.1
    @singularit/eslint-config-vue: ^1.2.0 => 1.2.0
    @singularit/kurz-components: ^1.0.0-beta.10 => 1.0.0-beta.10
    axios: ^1.4.0 => 1.4.0

Used Package Manager

npm

Validations

Das standardmäßige Verhalten von 'in' wird verworfen, wenn es im FilterSetMapping aufgelistet wird

Describe the bug

Beim Filter eines Objektes nach einem Attribut mit dem 'in'-Filter wird standardmäßig ein Array erwartet. Gibt man jedoch ein FilterSetMapping an, zu dem auch der 'in'-Filter gehört, dann nicht mehr.

Reproduction

FilterSetConfig<{attribute: any}, {attribute: 'in'}>

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1255U
    Memory: 2.12 GB / 15.71 GB
Binaries:
    Node: 18.15.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.5.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.3.1 - C:\Program Files\nodejs\pnpm.CMD
Browsers:
    Edge: Chromium (118.0.2088.76)
    Internet Explorer: 11.0.22621.1
npmPackages:
    @singularit/drf-axios-middleware: ^1.0.0-beta.1 => 1.0.0-beta.1
    @singularit/eslint-config-vue: ^1.2.0 => 1.2.0
    @singularit/kurz-components: ^1.0.0-beta.10 => 1.0.0-beta.10
    axios: ^1.4.0 => 1.4.0

Used Package Manager

npm

Validations

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.