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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nathanhoad/next-api

v1.2.2

Published

Some API helpers for Next.js

Readme

@nathanhoad/next-api

Some API helpers for Next.js.

npm i @nathanhoad/next-api

Controllers

User controllers to help define simple CRUD API endpoints. Note, that the D here is destroy, not delete as delete is a reserved word in JavaScript and this makes it easier to pass around.

import { Controller } from "@nathanhoad/next-api";
import Things from "../models/Things";

class ThingsController extends Controller {
  async index(query: any) {
    return await Things.all();
  }

  async create(body: any) {
    return Things.create(body);
  }

  async read(query: any) {
    return await Things.find(query.id);
  }

  async update(query: any, body: any) {
    const thing = await Things.find(query.id);
    return await Things.save({ ...thing, body });
  }

  async destroy(query: any) {
    const thing = await Things.find(query.id);
    return await Things.destroy(thing);
  }
}

export default new ThingsController();

Then you can use them in your index.ts and [id].ts API route handlers:

// index.ts
import thingsController from "../../../controllers/ThingsController";

export default thingsController.handleCollection();
// [id].ts
import thingsController from "../../../controllers/ThingsController";

export default thingsController.handItem();

API client

The API client is best used in conjunction with something like SWR.

First, make sure to set a URL in your public runtime config.

If you are wanting to use the JWT session handling then make sure to set a SESSION_NAME too (just your app's name should be fine).

Then you can use create, read, update, and destroy in your pages and components.

Something like:

// [id].tsx
import { NextPage } from "next/page";
import useSWR, { mutate } from "swr";
import { read, update } from "@nathanhoad/next-api";

import { IThing } from "../../types";

interface Props {
  thing: IThing;
}

const ThingPage: NextPage<Props> = props => {
  const { data: thing } = useSWR<IThing>(`/things/${props.thing.id}`, read, { initialData: props.thing });

  async function addOne() {
    const updatedThing = { ...thing, counter: thing.counter + 1 };
    // Send the change to the api
    update(`/things/${thing.id}`, updatedThing);
    // Update the local cache immediately (without revalidating)
    mutate(`/things/${props.thing.id}`, updatedThing, false);
    mutate(`/things`);
  }

  return (
    <main>
      <h1>{thing.name}</h1>
      <div>This has been clicked {thing.counter} times</div>
      <button onClick={() => addOne()}>Click it again!</button>
    </main>
  );
};

ThingPage.getInitialProps = async ({ query }) => {
  return {
    thing: await read(`/things/${query.id}`)
  };
};

export default ThingPage;