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

@kienleholdings/nextjs-api-utils

v0.1.0

Published

Tooling to build better, more secure APIs with Next.js

Downloads

1

Readme

NextJS API Utilities

Tooling to build better, more secure APIs with Next.js

Installation

yarn add @kienleholdings/nextjs-api-utils

Usage

This package exposes several modules and scripts, the following documentation should cover most use cases

Building API Handlers

import { createHandler } from '@kienleholdings/nextjs-api-utils';

// Available methods include get, post, patch, put, and delete
const handler = createHandler({
  get: (req, res) => {
    // TODO: Write handler
  },
});

export default handler;

Using Standardized Responses

import { apiResponse } from '@kienleholdings/nextjs-api-utils';

...

const get = (req, res) => {
  // Sends a 200 response with a JSON body { "data": "foo" }
  apiResponse.ok(res, { data: 'foo' });
};

...

Using Standardized Middleware Stack

This includes a body parser, body validator, and CORS middleware (unconfigured by default)

import { withMiddleware } from '@kienleholdings/nextjs-api-utils';

...

export default withMiddleware()()(handler);

Configuring Middleware

Body Parser

import { withMiddleware } from '@kienleholdings/nextjs-api-utils';

...

export default withMiddleware({
  bodyParserConfig: ['application/json'] // Only allow application/json to be submitted,
})()(handler);

Body Validator

import { withMiddleware } from '@kienleholdings/nextjs-api-utils';
import * as yup from 'yup';

const postSchema = Yup.object().shape({
  foo: Yup.string().required(),
  bar: Yup.bool().required()
});

...

// This enforces that `foo` is a required string and `bar` is a required boolean. The API will
// return a 422 (unprocessable entity) if the schema is not satisfied
export default withMiddleware({
  bodyValidatorConfig: {
    post: postSchema
  }
})()(handler);

CORS

import { withMiddleware } from '@kienleholdings/nextjs-api-utils';

...

// See https://www.npmjs.com/package/cors for more complete configuration options
export default withMiddleware({
  corsConfig: {
    origin: 'http://example.com',
  }
})()(handler);

Generating OpenAPI Specifications

Any good API needs good documentation, but maintaining one giant OpenAPI specification can be a nightmare for backend developers, especially when mege conflicts are involved. Luckily, the package contains an easy utility that turns json files that live alongside your API endpoints into OpenAPI (or Swagger) specifications! Here's how to do it:

  1. In the root of your API's package, create a new file called openapi-base.json
  2. Populate that file with your OpenAPI spec's info, components, .etc
  3. In the same directory as your API endpoint, create a .json file. As an example, if my endpoint was /api/users and contained a file named users.js, I'd create a file named users.json
  4. In your newly-created file, populate it with OpenAPI definitions for each exposed method
  5. Once you've documented all of your API endpoints, run yarn run generate-openapi-spec

Need your files named something different? Need to pull definitions from a different directory? Run generate-openapi-slec --help for full command info

Not sure how to write OpenAPI definitions? The Swagger Editor and PetStore example are both great places to get started

Local Development

  1. Clone the repo
  2. Open the folder as a container with VSCode
  3. Wait for dependencies to install
  4. Run yarn build
  5. Note the output in ./lib