url-fmt
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

url-fmt

Build Version Size License

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>]

Package Sidebar

Install

npm i url-fmt

Weekly Downloads

13

Version

0.1.4

License

MIT

Unpacked Size

5.33 kB

Total Files

5

Last publish

Collaborators

  • rzane