@gpa/extract-params-from-path
v1.0.1
Published
Extract path parameters from an express path specification using only the Typescript type system, to provide statically typed request parameters
Readme
@gpa/extract-params-from-path
Extract the parameters declared in an express path specification using the Typescript type system. Used to compute the list of available path parameters in a router in @gpa/type-safe-express.
The runtime path parameter parser of express is implemented in path-to-regexp, this library is compatible with:
| @gpa/extract-params-from-path version | express version | path-to-regexp version |
|:----------------------------------------|:------------------------------|:----------------------------------------|
| 0.x | 4.0-5.0.0-alpha.8 | 0.x |
| 1.x | 5.0.0-beta.1-5.0.0-beta.3 | 3.2.0 (through [email protected]) |
| 2.x | from 5.0.0 | 8.x (through [email protected]) |
Table of Contents
Installation
npm install @gpa/extract-params-from-pathRequirements
Node.js >= 18
typescript >= 5.0 (optional)
Usage
import { ExtractParamsFromPath } from '@gpa/extract-params-from-path';
function router<Path extends string>(path: Path, handler: (request: { params: ExtractParamsFromPath<Path> }) => void) {}
router('/path/:pathParam', (req) => {
// req.params is { pathParam: string }
});API
ExtractParamsFromPath<Path extends string>
<Path>
Type: string
The path to extract the parameters from.
