# typed-url-params

> Types your URL parameters in compliance with Express.js router and https://github.com/pillarjs/path-to-regexp

Latest version **1.0.1** (published 2021-04-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install typed-url-params
pnpm add typed-url-params
yarn add typed-url-params
bun add typed-url-params
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-04-08 |
| First published | 2021-01-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 38.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 84 |
| Maintainers | menduz |

## Links

- npm: https://www.npmjs.com/package/typed-url-params
- Repository: https://github.com/menduz/typed-url-params
- Homepage: https://github.com/menduz/typed-url-params#readme
- Issues: https://github.com/menduz/typed-url-params/issues
- npm.io page: https://npm.io/package/typed-url-params

## Recent versions

- 1.0.1 (latest) — 2021-04-08
- 1.0.0-20210105203632.commit-6fc4309 (next) — 2021-01-05
- 1.0.0-20210105161750.commit-12a4e67 — 2021-01-05

## README

# typed-url-params

Types your URL parameters in compliance with Express.js router and https://github.com/pillarjs/path-to-regexp

## Install

```
npm install typed-url-params
```

## Usage

```ts
type Params = ParseUrlParams<"/test"> // { }
type Params = ParseUrlParams<"/:abc"> // { abc: string }
type Params = ParseUrlParams<"/:abc+"> // { abc: string | string[] }
type Params = ParseUrlParams<"/:abc*"> // { abc?: string | string[] }
type Params = ParseUrlParams<"/users/:userId"> // { userId: string }
type Params = ParseUrlParams<"/flights/:from-:to"> // { from: string, to: string }
```

## Usage with Express

This library only offers typings. You must write your own wrapper for Express. You can do it like this:

```typescript
function handleGet<TypedUrl extends string>(
  app: Express.Application,
  url: TypedUrl,
  handler: (req: Express.Request & { params: ParseUrlParams<TypedUrl> }, res: Express.Response, next) => void
) {
  // noop
}
```

## More examples

```ts

import { ParseUrlParams } from "typed-url-params"

function assert<T extends string = string>(r: ParseUrlParams<T>): asserts r is ParseUrlParams<T> {}

assert<"/:asd/b">({ asd: "" })
assert<"/xxx/:asd/bbb:dsa">({ asd: "", dsa: "" })
assert<"/xxx/:asd/bbb/:dsa">({ asd: "", dsa: "" })
assert<"/xxx/:asd/bbb/:dsa">({ asd: "", dsa: "" })
assert<"/:test*-bar">({ test: [] })
assert<"/:test*-bar">({})
assert<"/:test*-bar">({ test: "asd" })
assert<"/:test*-bar">({ test: ["asd"] })
assert<"/:test+-bar">({ test: [""] })
assert<"/:test*">({ test: [] })
assert<"/:test+">({ test: "" })
assert<"/:test+">({ test: [""] })
assert<"/:test?-bar">({ test: "" })
assert<"/:test?">({ test: "" })
assert<"/:test(\\d+)">({ test: "" })
assert<"/:test(\\d+)+">({ test: "" })
assert<"/:test(\\d+)+">({ test: [""] })
assert<"/route.:ext(json|xml)?">({})
assert<"/route.:ext(json|xml)?">({ ext: "" })
assert<"/route.:ext(json|xml)">({ ext: "" })
assert<"/route.:ext(json|xml)+">({ ext: "" })
assert<"/route.:ext(json|xml)+">({ ext: [""] })
assert<"/route.:ext([a-z]+)*/:asd">({ ext: [""], asd: "" })
assert<"/route.:ext([a-z]+)*/:asd">({ asd: "" })
assert<"/route.:ext([a-z]+)*">({ ext: [""] })
assert<"/route.:ext([a-z]+)/:asd">({ ext: "", asd: "" })
assert<"/:test(.*)">({ test: "" })
assert<"/route\\(\\\\(\\d+\\\\)\\)">({})
assert<"/{apple-}?icon-:res(\\d+).png">({ res: "" })
assert<"/:foo/:bar">({ foo: "", bar: "" })
assert<"/users/:user_id/:asd+">({ user_id: "", asd: [] })
assert<"/flights/:from-:to">({ from: "", to: "" })
```

---
_Source: https://npm.io/package/typed-url-params · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
