next-route-matcher
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

NextJS Router Matcher

Want to use nextjs-style routes without the next module? This module lets you easily match routes. This code was adapted directly from the NextJS implementation.

Installation

yarn add next-route-matcher

Usage

import nextRouteMatcher from "next-route-matcher"

const routeMatcher = nextRouteMatcher([
  "/health",
  "/api/nested",
  "/api/items/[item_name]",
  "/api/[someslug]/list",
  "/api/[slug1]/deepnest/[slug2]/image.png",
  "/api/[someslug]/greetings/[...anything]",
])

routeMatcher("/health")
// {
//   matchedRoute: "/health",
//   routeParams: {},
// }

routeMatcher("/api/nested")
// {
//   matchedRoute: "/api/nested",
//   routeParams: {},
// }

routeMatcher("/api/items/someitem")
// {
//   matchedRoute: "/api/items/[item_name]",
//   routeParams: { item_name: "someitem" },
// }

routeMatcher("/api/someslug/list")
// {
//   matchedRoute: "/api/[someslug]/list",
//   routeParams: { someslug: "someslug" },
// }

routeMatcher("/api/slug1/deepnest/slug2/image.png")
// {
//   matchedRoute: "/api/[slug1]/deepnest/[slug2]/image.png",
//   routeParams: {
//     slug1: "slug1"
//     slug2: "slug2",
//   }
// },
routeMatcher("/api/someslug/greetings/something/somethingelse.txt")
// {
//   matchedRoute: "/api/[someslug]/greetings/[...anything]"
//   routeParams: {
//     someslug: "someslug",
//     anything: ["something", "somethingelse.txt"],
//   },
// }

Dependents (4)

Package Sidebar

Install

npm i next-route-matcher

Weekly Downloads

3,783

Version

1.0.2

License

none

Unpacked Size

33.5 kB

Total Files

17

Last publish

Collaborators

  • seveibar