npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nextjs-13-api-middleware

v1.0.0

Published

Downloads

11

Readme

nextjs-13-api-middleware

For Nextjs 12, please use this packge nextjs-12-api-middleware

Sample


// nextjs 13 folder structure
//     Project folder
//         |   public
//         |   src
//             |   app
//                 |   page
//                 |   api
//                     |   route.js/ts


// @ route.js file

// Replace 
import { NextResponse } from 'next/server';
export function GET(req) {
    return NextResponse.json({})
}


// with
import { MethodRouter } from 'nextjs-13-api-middleware';
const router = new MethodRouter();

router.get(async (req, next, prop) => {
    // your code here...
    return NextResponse.json()
});

const handler = router.handler();
export const GET = handler.GET;
export const POST = handler.POST;
export const PUT = handler.PUT;
export const DELETE = handler.DELETE;
        

Constructor

import { MethodRouter } from 'nextjs-13-api-middleware';

// your middleware 1
const customMiddleware = (req, next, prop) => {
    
}

// your middleware 2
const customMiddleware2 = (req, next, prop) => {
    
}

// CONSTRUCTOR
const router = new MethodRouter({ 
    middlewares: [ 
        customMiddleware, // add middleware when creating router
        customMiddleware2 // add middleware when creating router
    ] 
});

router.get(async (req, next, prop) => {
    // your code here...
    return NextResponse.json()
});

// ... rest of your code

Predefined middlewares

import { ParamParser, PathParamParser, BodyParser } from 'nextjs-13-api-middleware';

const router = new MethodRouter({ 
    middlewares: [ ParamParser, PathParamParser, BodyParser ]
})

// ... your entire code

prop object

You will see prop in every middleware which is important.
prop object contain important data.\


// your middleware 1
const customMiddleware = (req, next, prop) => {
    /** 
     * 
     * `prop` object contain following properties
     * {
     *    // you can assign more data to `passdata` object from each middleware which allow to pass down data to every object
     *    passdata: {}, 
     * 
     *    // when calling next() in every middleware, the chain result will pass data to next middleware;
     *    // next('middleware1') => next middleware `chainResult` will be 'middleware1'
     *    // you can pass any data to next middleware by calling next() with param
     *    chainResult: undefined,
     * 
     *    // path param
     *    // this property will added if you use `PathParamParser` middleware 
     *    pathParams: { v1: [ 'v1fsd', 'fdsa', 'fdsa' ] },
     * 
     *    // query
     *    // this property will added if you use `ParamParser` middleware 
     *    query: { id: '1', name: 'aung aung' },
     * 
     *    // this property will added if you use `BodyParser` middleware
     *    body: {},
     * 
     *    // this is Next.js api default param from API
     *    nextOptions: { params: { v1: [Array] } }
     * }
     * 
    */
}

Middlewares


import { MethodRouter } from 'nextjs-13-api-middleware';
const router = new MethodRouter();


// add multiple middlewares
router.addMiddleware({ 
    method: 'GET', 
    callback: async (req, next, prop) => {

    }
})
router.addMiddleware({ 
    method: 'GET', 
    callback: async (req, next, prop) => {

    }
})

router.addMiddlewareByMultipleMethods({ 
    method: ['GET', 'POST', 'PUT'], 
    callback: async (req, next, prop) => {
        // this callback will called when request methods with
        // GET, POST, PUT
    }
})

router.get(async (req, next, prop) => {
    // your code here...
    return NextResponse.json()
});

const handler = router.handler();
export const GET = handler.GET;
export const POST = handler.POST;
export const PUT = handler.PUT;
export const DELETE = handler.DELETE;

use function



import { MethodRouter } from 'nextjs-13-api-middleware';
const router = new MethodRouter();


// add multiple middlewares
// ...


/** 
 * 
 * You can also call use instead of post() function
 * router.use('POST', (req, next, prop) => {})
 * router.post((req, next, prop) => {})
 * 
*/
router.use('POST', (req, next, prop) => {

});
router.get(async (req, next, prop) => {
    // your code here...
    // fallback callback will called if you also throw an error;
    return NextResponse.json()
});


// Fallback
// this callback will called if one of chain middlewares throw an errors;
router.fallback(async (req, error, prop) => {

})

const handler = router.handler();
export const GET = handler.GET;
export const POST = handler.POST;
export const PUT = handler.PUT;
export const DELETE = handler.DELETE;

HttpMethods

package support Http methods POST PUT DELETE GET PUT TRACE OPTIONS PATCH CONNECT HEAD



import { MethodRouter } from 'nextjs-13-api-middleware';
import { NextResponse } from 'next/server';
const router = new MethodRouter();


// add multiple middlewares
// ...

// SUPPORTED HTTP METHODS
router.get(async (req, next, prop) => {});
router.post(async (req, next, prop) => {});
router.put(async (req,  next) => {});
router.delete(async (req,  next) => {});
router.options(async (req,  next) => {});
router.patch(async (req,  next) => {});
router.head(async (req,  next) => {});
router.trace(async (req,  next) => {});
router.connect(async (req,  next) => {});


const handler = router.handler();
export const GET = handler.GET;
export const POST = handler.POST;
export const PUT = handler.PUT;
export const DELETE = handler.DELETE;
export const OPTIONS = handler.OPTIONS;
// ...
// ... also don't forget to add each method;

Fallback



import { MethodRouter } from 'nextjs-13-api-middleware';
const router = new MethodRouter();


// add multiple middlewares
// ...
router.addMiddleware({ 
    method: 'GET', 
    callback: async (req, next, prop) => {
        const user = {} // check db from user
        if(!user.allowed) {
            throw new Error('Your are not authorized')
        }
    }
})

router.get(async (req, next, prop) => {
    // your code here...
    // fallback callback will called if you also throw an error;
    return NextResponse.json()
});


// Fallback
// this callback will called if one of chain middlewares throw an errors;
router.fallback(async (req, error) => {

})

const handler = router.handler();
export const GET = handler.GET;
export const POST = handler.POST;
export const PUT = handler.PUT;
export const DELETE = handler.DELETE;