1.1.2 • Published 4 years ago

next-high-order-middleware v1.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

A utility that lets you add fake middlewares because Next Js currently doesn't support real ones.

💁 _Note: Requires browser support for Promises and ES6

Getting Started

npm i next-high-order-middleware

Examples

Basic Middleware setup

// middlewares/index.js

import { logger } from './logger';
import { bodyparser } from './bodyparser';

import apply from "next-high-order-middleware";

const applyBasicMiddlewares = apply([bodyparser, logger]);

export const applyBasicMiddlewares;

// next-js-project/pages/api/example

import { applyBasicMiddlewares } from '../../middlewares';

export default applyBasicMiddlewares(async (req, res) => {
    ...
})

Middlewares that can raise errors ( auth )

// middlewares/index.js

import { logger } from './logger';
import { bodyparser } from './bodyparser';
import { verifyAuthToken } from './auth';

import apply from "next-high-order-middleware";

const applyAuthMiddlewares = apply([bodyparser, logger, verifyAuthToken])

export const applyAuthMiddlewares;

//next-js-project/middlewares/auth
...
if(authFailed){
    return {
        ...,
        //this makes the execution flow stop for the api and this data object is sent to the client as a json
        _status:false
    }
}

// next-js-project/pages/api/example

import { applyBasicMiddlewares } from '../../middlewares';

export default applyBasicMiddlewares(async (req, res) => {
    ...
})
1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago