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

next-api-simple-handler

v1.0.2

Published

[Demo](https://github.com/mikolertesx/next-api-handler-demo)

Downloads

8

Readme

Next Api Simple Handler

Demo

Badges

Build Status Maintainability codecov

Read The Docs (Documentation) NPM package

About

The Next Api Simple handler is a minimal guard that allows you to automatize asking for missing values or missing keys.

It's pretty simple in functionality, and allows you to shorten up the code you copy and paste everywhere else.

First steps

Preventing user from entering with wrong method.

import { apiHandler } from "next-api-simple-handler";

export async function handler(req, res) {
	return apiHandler(
		req,
		res,
		{
			methods: ["GET"],
		},
		async (req, res) => {
			return res.status(200).json("Hello world!");
		}
	);
}

export default handler

The snippet above prevents user entering with any other method that isn't get.

Available methods are:

  • "GET"
  • "POST"
  • "PUT"
  • "DELETE"
  • "PATCH"

Preventing user from sending wrong content type.

import { apiHandler } from "next-api-simple-handler";

export async function handler(req, res) {
	return apiHandler(
		req,
		res,
		{
			methods: ["POST"],
			contentType: "application/json",
		},
		async (req, res) => {
			return res.status(200).json("Hello world!");
		}
	);
}

export default handler

This snippet will prevent user from sending a request with a body that isn't made of "application/json".

Preventing user from sending unsufficient data.

import { apiHandler } from "next-api-simple-handler";

export async function handler(req, res) {
	return apiHandler(
		req,
		res,
		{
			methods: ["POST"],
			contentType: "application/json",
			requiredBody: ["username", "password"],
		},
		async (req, res) => {
			return res.status(200).json({ message: "Hello world!" });
		}
	);
}

export default handler;

The snippet above will make sure that req.body has a username, and a password key.

Telling the user what to send after failure.

import { apiHandler } from 'next-api-simple-handler'

const loginSchema = {
  username: '[String] The name of the user you are trying to log into.',
  password: '[String] The password.',
}

export default async function handler(req, res) {
	return apiHandler(
			req,
			res,
			{
				requiredBody: ['username', 'password'],
				methods: ['POST'],
				contentType: 'application/json',
				schema: loginSchema,
			},
			async (req, res) => {
				const {username, password} = req.body;
				return res.json({
					message: `Username is: ${username}, password is: ${password}`
				});
			}
	)
}

The snippet above, when the user does a request without a username, or without a password will tell the user that it was expecting this object.

{
  username: '[String] The name of the user you are trying to log into.',
  password: '[String] The password.',
}

The Schema provided here can be in any format you prefer, but this is what I thought was most useful.

Advanced usage

Chaining blockers

Let's say that, for example, we need to have a route that covers multiple different methods, a get, and a post route.

import { apiHandler } from 'next-api-simple-handler'

const handleGet = async (req, res) => {
	// No more checks are necessary in this example.
	return res.json({message: "This is a JSON message"})
}
const handlePost = async(req, res) => {
	// We know that we requrie a username, and a password, and that the application should be of application/json.

	return apiHandler(req, res, {
		requiredBody: ['username', 'password'],
		contentType: 'application/json',
		schema: {
			username: '[String] The name of the user you are trying to log into.',
			password: '[String] The password.',
		}
	}, async(req, res) => {
		return res.json({message: "Successfully registered"});
	});
}

export async function handler(req, res) {
	return apiHandler(req, res, {methods: ['GET', 'POST']}, async () => {
		if (req.method === 'GET') {
			return handleGet(req, res);
		}
		return handlePost(req, res);
	})
}

export default handler

Customizable error message

You can also define custom error messages and error codes, errorMessages and errorCodes are objects with the following keys:

  • "wrong-content-type"
  • "wrong-method"
  • "missing-body-key"

You can define inside the configuration object, any of this values and they will override the defaults.

Here is the declaration of the default configuration:

const defaultConfig: apiConfiguration = {
  errorCodes: {
    "missing-body-key": 422,
    "wrong-content-type": 422,
    "wrong-method": 422,
  },
  errorMessages: {
    "wrong-content-type": (expectedContentType, receivedContentType) =>
      `Expected '${expectedContentType}' content-type, not '${receivedContentType}' content-type`,
    "missing-body-key": (missingBodyKeys: string[]) =>
      `Missing required body keys: ${missingBodyKeys.join(", ")}`,
    "wrong-method": (allowedMethods: string[]) =>
      `Only methods ${allowedMethods.join(", ")} are permitted on this route.`,
  },
};

And here is an example of what I'd do in a login, register situation.

import { apiHandler } from "next-api-simple-handler";

const registerSchema = {
	username: "[String] The name of the user you are trying to log into.",
	password: "[String] The password.",
};

export async function handler(req, res) {
	return apiHandler(
		req,
		res,
		{
			methods: ["POST"],
			contentType: "application/json",
			requiredBody: ["username", "password"],
			errorMessages: {
				"missing-body-key": (missingKeys) =>
					`A username, and a password are required to register an account. You are missing ${missingKeys.join(
						", "
					)}`,
				"wrong-content-type": (expectedContentType, receivedContentType) =>
					`Only ${expectedContentType} is allowed on this route; You sent ${receivedContentType}`,
				"wrong-method": (allowedMethods) =>
					`Only the methods ${allowedMethods.join(
						", "
					)} can be done in this route.`,
			},
			schema: registerSchema,
		},
		(req, res) => {
			return res.json({ message: "User was successfully registered" });
		}
	);
}

export default handler;

The snippet above changes each of the errorMessages.

Mainteinance

The library was made with Typescript, so every piece here is typed and will give you hints on whether or not you are missusing it.