dunkbing / route-path-builder

A small typescript library for building type-safe route paths with support for path and query parameters.

Home Page:https://www.npmjs.com/package/route-path-builder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript Route Builder

A small typescript library for building type-safe route paths with support for path and query parameters.

Installation

Using npm:

npm install route-path-builder

Using jsr:

npx jsr add @dunkbing/route-path-builder

Usage Creating a Single Route Path

import { createRoutePath } from "route-path-builder";

const productPath = createRoutePath("/catalog/:category/:name");

// Generate path with parameters
const url1 = productPath({ category: "cloth", name: "shirt" });
console.log(url1); // Output: /catalog/cloth/shirt

// Generate path with parameters and query parameters
type QueryParams = {
  param1: string;
  param2: string;
};
const url2 = productPath<QueryParams>(
  { category: "cloth", name: "shirt" },
  { param1: "test", param2: "test2" },
);
console.log(url2); // Output: /catalog/cloth/shirt?param1=test&param2=test2
import { createRoutePaths } from "route-path-builder";

const routes = createRoutePaths([
  { name: "product", path: "/catalog/:category/:name" },
  { name: "user", path: "/user/:id" },
]);
const productPath = routes.product({ category: "cloth", name: "shirt" });
console.log(productPath); // Output: /catalog/cloth/shirt

const userPath = routes.user({ id: "123" });
console.log(userPath); // Output: /user/123

About

A small typescript library for building type-safe route paths with support for path and query parameters.

https://www.npmjs.com/package/route-path-builder


Languages

Language:TypeScript 100.0%