url-route-handler
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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-handler

PNPM

pnpm i url-route-handler

YARN

yarn add url-route-handler

Quick 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); // 12345

UrlOptions

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 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

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Add your changes: git add .
  4. Commit your changes: git commit -m 'your commit message'
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request 😎

Development

Local Development

pnpm install
pnpm build

Test

pnpm test

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i url-route-handler

    Weekly Downloads

    16

    Version

    1.0.2

    License

    UNLICENSED

    Unpacked Size

    28.8 kB

    Total Files

    8

    Last publish

    Collaborators

    • satyam-bhadoria