Skip to content

wolfdev1337/nextjs-route-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nextjs-route-utils

A lightweight, type-safe route builder utility for Next.js
Build dynamic and optional routes with ease and full TypeScript support.

X platform Github platform


Why use nextjs-route-utils?

In Next.js projects, especially with dynamic routes, managing route strings manually can lead to typos, inconsistencies, and bugs.
This package lets you define your route patterns once and then build URLs with typed parameters, ensuring correctness across your app.


Features

  • Define routes with dynamic (:param) and optional (:param?) segments
  • Type-safe parameter enforcement in TypeScript
  • Automatic URL encoding of parameter values
  • Removes optional params when not provided
  • Lightweight and zero dependencies

Installation

npm install nextjs-route-utils
# or
yarn add nextjs-route-utils

Examples

More examples in the test file

const user = createRoute<{ userId: string }>("/users/:userId");
user({ userId: "abc" }); // /users/abc
const post = createRoute<{ userId: string; postId: number }>(
  "/users/:userId/posts/:postId"
);
post({ userId: "john", postId: 42 }); // /users/john/posts/42

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published