url-fmt
v0.1.4
Published
Format URL strings with template parameters
Downloads
1
Maintainers
Readme
Functions that can be used to generate fully-typed URL helper methods.
This package uses Typescript's template literals to parse the URL and generate a type definition for the parameters that will be used to generate the URL.
Install
$ yarn add url-fmt
Usage
format
import { format } from "url-fmt";
format("/users");
format("/users/:id", { id: 1 });
format("/users/:id?");
format("/users/:id?", { id: 1 });
format("/users/:id"); // error
format("/users/:id", {}); // error
createNamedRoutes
import { createNamedRoutes } from "url-fmt";
const routes = {
users: "/users",
user: "/users/:id",
} as const;
const route = createNamedRoutes(routes);
route("users");
route("user", { id: 1 });
Syntax
| Syntax | Type |
| --------- | -------------------------------------------------------------------- |
| :param
| string \| number
|
| :param?
| string \| number \| undefined
|
| :param*
| string \| number \| undefined \| Array<string \| number>
|
| :param+
| string \| number \| [string \| number, ...Array<string \| number>]
|