https://github.com/menduz/typed-url-params
TypeSystem based parser for url parameters
https://github.com/menduz/typed-url-params
Last synced: 5 months ago
JSON representation
TypeSystem based parser for url parameters
- Host: GitHub
- URL: https://github.com/menduz/typed-url-params
- Owner: menduz
- Created: 2021-01-05T16:04:19.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-08-12T23:46:57.000Z (almost 4 years ago)
- Last Synced: 2024-12-10T03:40:26.706Z (6 months ago)
- Language: TypeScript
- Size: 34.2 KB
- Stars: 85
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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(
app: Express.Application,
url: TypedUrl,
handler: (req: Express.Request & { params: ParseUrlParams }, res: Express.Response, next) => void
) {
// noop
}
```## More examples
```ts
import { ParseUrlParams } from "typed-url-params"
function assert(r: ParseUrlParams): asserts r is ParseUrlParams {}
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: "" })
```