1.0.2 • Published 4 months ago

next-route-matcher v1.0.2

Weekly downloads
-
License
-
Repository
github
Last release
4 months ago

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"],
//   },
// }