https://github.com/dunkbing/route-path-builder
A small typescript library for building type-safe route paths with support for path and query parameters.
https://github.com/dunkbing/route-path-builder
Last synced: about 1 year ago
JSON representation
A small typescript library for building type-safe route paths with support for path and query parameters.
- Host: GitHub
- URL: https://github.com/dunkbing/route-path-builder
- Owner: dunkbing
- Created: 2024-07-31T04:15:03.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-31T04:54:37.000Z (almost 2 years ago)
- Last Synced: 2025-05-19T16:17:09.658Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/route-path-builder
- Size: 26.4 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TypeScript Route Builder
A small typescript library for building type-safe route paths with support for
path and query parameters.
## Installation
Using npm:
```bash
npm install route-path-builder
```
Using jsr:
```bash
npx jsr add @dunkbing/route-path-builder
```
Usage Creating a Single Route Path
```typescript
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(
{ category: "cloth", name: "shirt" },
{ param1: "test", param2: "test2" },
);
console.log(url2); // Output: /catalog/cloth/shirt?param1=test¶m2=test2
```
```typescript
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
```