url-route-handler
v1.0.2
Published
The library provides a flexible and efficient way to map URL patterns to their respective handler methods. By defining a set of URL patterns and the corresponding handler functions, this package allows seamless routing, ensuring that each url is processed
Downloads
2,681
Maintainers
Readme
URL Handler
The library provides a flexible and efficient way to map URL patterns to their respective handler methods. By defining a set of URL patterns and the corresponding handler functions, this package allows seamless routing, ensuring that each url is processed by the correct function.
Installation
NPM
npm i url-route-handlerPNPM
pnpm i url-route-handlerYARN
yarn add url-route-handlerQuick Start
Import
import { Router } from 'url-route-handler';Usage
const router = new Router();
router.use('/foo/bar/:barId', (url: UrlOptions) => {
return url.params.barId;
});
...
...
const response = router.handle('https://domain.xyz/foo/bar/12345');
console.log(response.data); // 12345UrlOptions
| Parameter | Type | Description |
| :-------- | :------- | :------------------------- |
| url | string | contains href part of url send in router.handle method |
| host | any | host of the url passed. localhost in case if not provided |
| origin | string | origin of url |
| pathname | string | url path excluding host, query params and hash |
| protocol | string | http or https or etc |
| query | object | json object containing query params. key: value pair |
| params | object | path parameters json object. key: value pair |
| hash | string | contains hash string present in url |
Methods
The following are methods for Router
use(path: string, (url: UrlOptions [, ...args: any]) => any): void
router.use('/foo/bar/:barId', (url, someMap) => {
// someMap passed from handle method
...
});handle(url: string [, additionalData: any [, ...]]) => any): Response
const someMap = new Map();
...
const res = router.handle('https://domain.xyz/foo/bar/:barId', someMap);
// someMap will be passed to handler methodResponse
| Parameter | Type | Description |
| :-------- | :------- | :------------------------- |
| status | string | success/error |
| data | any | in case of success, data is returned from handler method |
| message | string | in case of error, error message returned |
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature - Add your changes:
git add . - Commit your changes:
git commit -m 'your commit message' - Push to the branch:
git push origin my-new-feature - Submit a pull request 😎
Development
Local Development
pnpm install
pnpm buildTest
pnpm test