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.
npm i url-route-handler
pnpm i url-route-handler
yarn add url-route-handler
import { Router } from 'url-route-handler';
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); // 12345
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 |
The following are methods for Router
router.use('/foo/bar/:barId', (url, someMap) => {
// someMap passed from handle method
...
});
const someMap = new Map();
...
const res = router.handle('https://domain.xyz/foo/bar/:barId', someMap);
// someMap will be passed to handler method
Response
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 |
- 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 😎
pnpm install
pnpm build
pnpm test