Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@daldalso/next-typed-route

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@daldalso/next-typed-route

Type-safe API call and routing library for Next.js

  • 0.9.1-1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@daldalso/next-typed-route

Type-safe API call and routing library for Next.js

Getting Started

  1. yarn add @daldalso/next-typed-route
  2. Open your Next.js config file and add NextTypedRoutePlugin like below:
     /** @type {import('next').NextConfig} */
     const nextConfig = {
       webpack: (config, context) => {
         if(context.isServer){
           config.plugins ??= [];
           config.plugins.push(new NextTypedRoutePlugin());
         }
         return config;
       }
     };
    
  3. yarn dev will run the plugin and make some type definitions.

Usage

Typed Page

Mark your page components with NextTypedPage for type-safe routing.

import { NextTypedPage } from "@daldalso/next-typed-route";

const MyPage:NextTypedPage<"/my-page/[foo]"> = ({ params }) => {
  return <div>Hello, {params.foo}!</div>; // Type-safe access to `params.foo`
};
export default Page;

Typed Route

Mark your API routes with NextTypedRoute for type-safe routing.

import type { NextTypedRoute } from "@daldalso/next-typed-route";
import { NextResponse } from "next/server";

export const GET:NextTypedRoute = () => new NextResponse();

Typed Search Parameters

You can use type-safe useSearchParams with a NextTypedPage.

import { NextTypedPage } from "@daldalso/next-typed-route";
import { useSearchParams } from "next/navigation";

const Page:NextTypedPage<"/", "foo"|"bar?"|"baz[]"> = () => {
  const searchParams = useSearchParams<typeof Page>();

  return <>
    {/* Fine */}
    {searchParams.get("foo").toString()}
    {/* This raises a type error. */}
    {searchParams.get("baar")}
    {/* This raises a type error; you should use `getAll` for an array. */}
    {searchParams.get("baz")}
  </>;
};
export default Page;

Typed Path Generation

You can use type-safe page function that returns a path string starting with /.

import { NextTypedPage, page } from "@daldalso/next-typed-route";
import { useRouter } from "next/navigation";

const Page:NextTypedPage<"/", "foo"|"bar?"> = ({ params }) => {
  const router = useRouter();

  return <button onClick={() => router.push(page("/shop"))}>
    Shop
  </button>;
};
export default Page;

Typed API Call

You can call your endpoints with type-safe callAPI function. The types of its parameters and return value are defined by the endpoint.

import callAPI, { NextTypedPage } from "@daldalso/next-typed-route";

const Page:NextTypedPage<"/"> = ({ params }) => {
  return <button onClick={() => callAPI("/api/foo")}>
    Submit
  </button>;
};
export default Page;

FAQs

Package last updated on 02 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc