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-http-wrapper

v0.1.12

Published

<div align="center">

Downloads

6

Readme

Nextjs http method wrapper (nextjs-http-wrapper)

npm downloads npm npm

About

Next.js HTTP wrapper is a wrapper helper for Nextjs API routes. It encapsulates Next.js api routes with a single function and it supports authentication. It helps developers to be consistent, explicit, and not repeat them selfs. Let's look at some examples because the code explains better than words.

Features

  • Built for consistency, less boilerplate and do not repeat your self.
  • Supports Typescript.
  • Supports authentication handler.
  • Supports error handling and a custom error handler if needed.
  • Supports all supported HTTP methods and requires developer to be explicit about it.
  • Throws 404 Method not allowed error if none supported HTTP method is called.

Installation

$ npm install --save nextjs-http-wrapper
$ yarn add nextjs-http-wrapper

Setup

First, we will need to initialize our HTTP wrapper. You can initialize it with or without options.

Initialize setup without options.

import { initializeHttpWrapper } from 'nextjs-http-wrapper';

export const httpMethodWrapper = initializeHttpWrapper();

Initialize setup with options.

import { NextApiRequest } from 'next';
import { initializeHttpWrapper } from 'nextjs-http-wrapper';

// This can either be a normal function or a Promise.
const authHandler = (req: NextApiRequest) => {
  // Some authentication logic
  const isLoggedIn = req.user;

  return isLoggedIn;
};

const errorHandler = (error: Error) => {
  // Send error to services like Sentry, LogRocket, etc.
  console.log(error);
};

export const httpMethodWrapper = initializeHttpWrapper({
  authHandler,
  errorHandler,
});

Usage

Within your Next.js api handler, i.e. /pages/api/hello-world.ts.

import type { NextApiRequest, NextApiResponse } from 'next';
import { httpMethodWrapper } from '../../lib/httpMethodWrapper';

const getHandler = (_req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).send('Get');
};

const postHandler = (_req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).send('POST');
};

const deleteHandler = (_req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).send('DELETE');
};

const putHandler = (_req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).send('PUT');
};

export default httpMethodWrapper(
  {
    GET: getHandler,
    POST: postHandler,
    DELETE: deleteHandler,
    // Inline function
    PUT: (_req, res) => res.status(200).send('PUT')
    // Other supported http methods.
  },
  // If the initializeHttpWrapper is initialized with authHandler property,
  // then  authentication is on by default. To turn authentication off
  // for special hander you can provide the following object.
  {
    disableAuth: true
  }
);