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

next-routes-typed

v0.1.0

Published

Generate type-safe route utilities for Next.js app router

Downloads

90

Readme

next-routes-typed

npm version License: MIT Downloads

Generate type-safe route utilities for Next.js app router. Automatically generate route constants and helper functions that match your app directory structure.

🌟 Features and Why next-routes-typed?

  • Type-safe route generation: Full TypeScript support with type inference
  • 🎯 App Router Support: Built for Next.js 13+ app directory
  • 🔄 Dynamic Routes: Support for [param], [...catchAll], and [[...optional]]
  • 📁 Route Groups: Support for route groups (folders in parentheses)
  • 🌍 i18n Ready: Perfect for internationalized applications
  • 🛠️ Zero Config: Works out of the box with your existing Next.js structure
  • 🎨 Developer Experience: Great autocomplete and type checking
  • 🔒 Type Safety: Catch routing errors at compile time
  • 🚀 Performance: Zero runtime overhead
  • 📦 Lightweight: No dependencies

🚀 Getting Started

npm install --save-dev next-routes-typed
# or
yarn add -D next-routes-typed
# or
pnpm add -D next-routes-typed

📘 Usage

Basic Usage

Run in your Next.js project root:

npx next-routes-typed

This will generate a routes.ts file in src/lib by default.

CLI Options

npx next-routes-typed --help

Options:
  -o, --output <path>       output directory (default: "src/lib")
  -f, --filename <name>     output filename (default: "routes.ts")
  --prettier-config <path>  path to prettier config
  --debug                   enable debug logging
  -h, --help               display help for command

Example

For a Next.js app structure like:

app/
├── page.tsx
├── about/
│   └── page.tsx
├── blog/
│   ├── page.tsx
│   └── [slug]/
│       └── page.tsx
└── products/
    └── [category]/
        └── [id]/
            └── page.tsx

next-routes-typed will generate:

export const routes = {
  home: {
    path: "/",
  },
  about: {
    path: "/about",
  },
  blog: {
    path: "/blog",
  },
  blogSlug: {
    path: "/blog/[slug]",
    params: {
      slug: "",
    },
  },
  productsCategoryId: {
    path: "/products/[category]/[id]",
    params: {
      category: "",
      id: "",
    },
  },
} as const;

export type AppRoutes = keyof typeof routes;

export function createUrl(
  route: AppRoutes,
  params?: Record<string, string>,
  query?: Record<string, string>
): string;

Using Generated Routes

import { createUrl } from "@/lib/routes";

// Simple route
const aboutUrl = createUrl("about");
// Result: /about

// Route with parameters
const blogPostUrl = createUrl("blogSlug", { slug: "hello-world" });
// Result: /blog/hello-world

// Route with parameters and query
const productUrl = createUrl(
  "productsCategoryId",
  { category: "electronics", id: "123" },
  { ref: "homepage" }
);
// Result: /products/electronics/123?ref=homepage

🎯 Supported Route Types

  • Basic routes: /about
  • Dynamic routes: /blog/[slug]
  • Catch-all routes: /docs/[...slug]
  • Optional catch-all routes: /docs/[[...slug]]
  • Route groups: (marketing)/blog
  • Parallel routes: @modal/login
  • Intercepting routes: (.)photo

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 License

This project is licensed under the MIT License.

📢 Support