Type-safe path utilities for Next.js App Router
Generate strongly typed path utilities from your Next.js App Router. This package automatically generates a paths.ts file that provides type-safe access to ALL your application's routes.
- π Automatic Scanning - Scans your Next.js App Router structure
- π Type Safety - Full TypeScript support with generated types
- π£οΈ Route Support - Handles dynamic routes and route groups
- π HTTP Methods - Includes GET, POST, PUT, DELETE handlers
- π― URL Utilities - Path, URL, and URL constructor utilities
- π Type Safety - Type-safe route parameters and query strings
- π¨ Naming - Supports both camelCase and snake_case
- π¦ Zero Dependencies - No runtime dependencies
- β‘οΈ Performance - Fast and efficient path generation
- π Declarative - Write routes in a natural, declarative way
# Or install globally
npm install -g nextjs-paths# Or install globally
yarn global add nextjs-paths# Or install globally
pnpm add -g nextjs-pathsnpm install nextjs-pathsAdd a script to your package.json:
{
"scripts": {
"generate:paths": "nextjs-paths generate"
}
}Then run:
npm run generate:pathsOr run directly:
npx nextjs-paths generateimport { paths } from "./paths";
// Simple navigation
export default function Navigation() {
return (
<nav>
<Link href={paths.blog.GET.path}>Blog</Link>
<Link href={paths.about.GET.path}>About</Link>
</nav>
);
}
// Dynamic routes
export function BlogPost({ slug }: { slug: string }) {
return <Link href={paths.blog.slug(slug).path}>Read Post</Link>;
}
// API calls
async function fetchBlogPost(slug: string) {
const response = await fetch(paths.blog.slug(slug).url);
return response.json();
}
// Form submissions
async function handleSubmit(data: FormData) {
await fetch(paths.blog.POST.url, {
method: "POST",
body: data,
});
}// TypeScript will ensure you provide all required parameters
const post = paths.blog.slug("2024/my-post");
const comment = paths.blog.slug("2024/my-post").comment("123");
// TypeScript error if you forget a parameter
const invalidPost = paths.blog.slug(); // Error: Missing required parameter// Build URLs with query parameters
const blogWithFilters = paths.blog.GET.URL();
blogWithFilters.searchParams.set("category", "tech");
blogWithFilters.searchParams.set("sort", "newest");
// Use in API calls
const response = await fetch(blogWithFilters.toString());| Option | Description | Default |
|---|---|---|
-d, --appDir <dir> |
App router root directory | src/app |
-e, --env <var> |
Environment variable for base URL | NEXT_PUBLIC_APP_BASE_URL |
-c, --caseStyle <style> |
Case style for path keys | camelCase |
-o, --outputDir <dir> |
Output directory for generated files | Same as appDir |
-f, --fileName <name> |
Output file name (must end with .ts) | paths.ts |
The following case styles are supported:
camelCase(default):blogPost,userProfilelowerSnake:blog_post,user_profileupperSnake:BLOG_POST,USER_PROFILEpascalCase:BlogPost,UserProfile
Given a Next.js app structure:
app/
βββ page.tsx
βββ about/
β βββ page.tsx
βββ blog/
β βββ page.tsx
β βββ [[...slug]]/
β β βββ page.tsx
β βββ route.ts
βββ api/
βββ hello/
βββ route.ts
The generated paths.ts provides type-safe access:
import { paths } from "./paths";
// Basic paths
paths.path; // "/"
paths.url; // "http://localhost:3000"
paths.URL(); // TS/JS URL class object
// Blog routes
paths.blog.GET.path; // "/blog"
paths.blog.GET.url; // "http://localhost:3000/blog"
paths.blog.GET.URL().toString(); // "http://localhost:3000/blog"
// Dynamic routes
const blogPost = paths.blog.slug("2024/my-post");
blogPost.path; // "/blog/2024/my-post"
blogPost.url; // "http://localhost:3000/blog/2024/my-post"
// Route handlers
paths.blog.GET.path; // "/blog"
paths.blog.POST.path; // "/blog"
paths.blog.PUT.path; // "/blog"
// URL with query params
const blogWithDraft = paths.blog.GET.URL();
blogWithDraft.searchParams.set("draft", "1");
blogWithDraft.toString(); // "http://localhost:3000/blog?draft=1"# Use custom environment variable
npx nextjs-paths generate --env NEXT_PUBLIC_SITE_URL# Generate files in custom directory with custom name
npx nextjs-paths generate --outputDir ./generated --fileName routes.ts# Use different case styles
npx nextjs-paths generate --caseStyle lowerSnake # blog_post
npx nextjs-paths generate --caseStyle upperSnake # BLOG_POST
npx nextjs-paths generate --caseStyle pascalCase # BlogPost
npx nextjs-paths generate --caseStyle camelCase # blogPost (default)# Generate from custom app directory
npx nextjs-paths generate --appDir ./app# Install dependencies
npm install
# Build the package
npm run build
# Run tests
npm test
# Generate test paths
npm run generate:testContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT