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

@natsuneko-laboratory/nextpida

v1.0.1

Published

TypeScript friendly apis path generator for Next.js

Downloads

19

Readme

@natsuneko-laboratory/nextpida

nextpida is a package that TypeScript friendly apis path generator for Next.js

Install

# required
$ yarn add @natsuneko-laboratory/nextpida --dev

# optional for using default type definitions
$ yarn add @natsuneko-laboratory/nextpida-handler-types --dev

# optional for using default `withMethods` handler
$ yarn add @natsuneko-laboratory/nextpida-method-handler

Generate Type Definitions

# build
$ yarn run nextpida

# build with watch mode
$ yarn run nextpida --watch

$ yarn run nextpida --input ./src/

Type Detection

Nextpida generates routes and type definitions for a default exported handler with a typedefinition of the following form:

import type { NextApiRequest, NextApiResponse } from "next";

// default handler
type NextApiHandler<T = any> = (req: NextApiRequest, res: NextApiResponse<T>) => unknown;

// handler with request body
type NextApiRequestWithBody<T = any> = Omit<NextApiRequest, "body"> & { body: T };
type NextApiHandler<TResponse = any, TRequestBody = any> = (req: NextApiRequestWithBody<TRequestBody>, res: NextApiResponse<TResponse>) => unknown;

// handler with request query params
type NextApiRequestWithQuery<T = any> = Omit<NextApiRequest, "query"> & { query: T };
type NextApiHandler<TResponse = any, TRequestQueryParam = any> = (req: NextApiRequestWithQuery<TRequestQueryParam>, res: NextApiResponse<TResponse>) => unknown;

// handler with request body and query params
type NextApiRequestWithBodyAndQuery<TRequestBody = any, TQueryParameter = any> = Omit<NextApiRequest, "body" | "query"> & { body: TRequestBody, query: TQueryParameter };
type NextApiHandler<TResponse = any, TRequestBodyParam = any, TRequestQueryParam = any> = (req: NextApiRequestWithBodyAndQuery<TRequestBodyParam, TRequestQueryParam>, res: NextApiResponse<TResponse>) => unknown;

type HttpMethods = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH";
type HttpHandlers = { [TMethod in HttpMethods]?: NextApiHandler };
type NextpidaSatisfiedFunctionSignature = <THandlers extends HttpHandlers>(handlers: THandlers) => unknown | Promise<unknown>;

const handler: NextpidaSatisfiedFunctionSignature = /* ... */;

export default handler;

You must:

  • Response type must be specified in T of NextApiResponse<T>
  • Request body type must be replaced in body: any of NextApiRequest
    • If it is not replaced and remains any, no type definitions is generated for the request body
  • Request additional query params type must be replaced in query: Partial<{ [key: string]: string | string[]; }> of NextApiRequest
    • If it is not replaced and remains Partial<{ [key: string]: string | string[]; }>, no additional type definitions is generated for the request query params

Import Types

By default, nextpida write type definition file into lib/$apis.ts

import type { GetRequest, PostRequest } from "lib/$apis";

// GET /api/v1/users request and response typings

// full typing
type RequestBodyAndQueryParams = GetRequest["api/v1/users"];

// request body only
type RequestBody = GetRequest["api/v1/users"].body;

// request query params only
type RequestQueryParams = GetRequest["api/v1/users"].query;

// response body
type Response = GetResponse["api/v1/users"];


// POST /api/v1/users request and response typings
type Response = PostResponse["api/v1/users"];

Known Issues

  • if optional parameter is used, undefined is not given

License

MIT by @6jz