@isoden/url-dispatcher
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.0 • Public • Published

    @isoden/url-dispatcher

    minimal url dispatcher for client side.


    Installation

    $ npm install @isoden/url-dispatcher --save
    

    Usage

    // routing.ts
    import { UrlDispatcher } from '@isoden/url-dispatcher'
    
    export class Dispatcher extends UrlDispatcher {
      routes = {
        '/'             : this.indexAction.bind(this),
        '/about'        : this.aboutAction.bind(this),
        '/users/:userId': this.userAction.bind(this)
      }
    
      indexAction() {
        console.log('here is index')
      }
    
      aboutAction() {
        console.log('here is about')
      }
    
      userAction() {
        const userId = this.params('userId')
    
        console.log(`here is user => ${ userId }`)
      }
    }
    // main.ts
    
    import { Dispatcher } from './routing'
    
    const dispatcher = new Dispatcher()
    
    Dispatcher.dispatch('/').then(() => {
      // matched routing
      // called Dispatcher#indexAction
    })
    
    Dispatcher.dispatch('/404').catch(() => {
      // unmatched routing
    })

    Note

    The following implementation is required.

    • Object.entries
    • ES6 Promise
    • ES6 Map

    Please import alternate implementation as necessary.

    $ npm install core-js --save
    
    import 'core-js/fn/object/entries'
    import 'core-js/fn/promise'
    import 'core-js/fn/map'

    License

    Under The MIT License

    Keywords

    none

    Install

    npm i @isoden/url-dispatcher

    DownloadsWeekly Downloads

    5

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • isoden