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-better-api

v0.1.5

Published

Utilities for safer, easier APIs with NextJS

Downloads

26

Readme

next-better-api ⚡️🔵 npm version

Opinionated TypeScript-first helpers for building better NextJS APIs, powered by Zod.

At a glance:

  • 🙅‍♀️ Hands-off Typescript type inference based on your Zod validation schemas for query params, request body, and your API response
  • ✨ Type inference helpers to use with react-query, fetch, and other client-side utilities
  • 🔌 Minimal and composable — bring your own request context, use existing middleware, etc
  • ☁ No additional dependencies (besides zod and next as peerDependencies, of course)
import { z } from 'zod';
import { endpoint, asHandler } from 'next-better-api';

const getUser = endpoint(
  {
    method: 'get',
    querySchema: z.object({
      id: z.string(),
    }),
    responseSchema: z.object({
      user: z.object({
        id: z.string(),
        name: z.string(),
        email: z.string(),
        active: z.boolean(),
      }),
    }),
  },
  async ({ query }) => {
    const user = await getUser(query.id);

    return {
      status: 200,
      body: {
        user,
      },
    };
  }
);

export default asHandler([getUser]);

Installation:

Skip to API reference

next-better-api requires zod for schema validation. You can install both libraries with yarn or npm on your existing NextJS project:

$ yarn add zod next-better-api

// OR

$ npm i -S zod next-better-api

And import it from your API definitions:

import { endpoint, asHandler } from 'next-better-api';
import { z } from 'zod'; // If you are defining schemas for your endpoints

Now simply define individual endpoints for each HTTP method in your existing API files, and export your endpoints as a single NextApiHandler:

// pages/api/users.ts
const getUsers = endpoint(
  {
    method: 'get',
  },
  async () => {
    const users = await getUsersFromDb();

    return {
      status: 200,
      body: {
        users,
      },
    };
  }
);

export default asHandler([getUsers]);

API Reference

See DOCS.md


Stuff

See license information under LICENSE.md.

Contributions are super welcome - in the form of bug reports, suggestions, or better yet, pull requests!