Neurotoxin Powered Marketing

    This package has been deprecated

    Author message:

    It's bit easier to install and import it without @syndicode prefix, so moved

    @syndicode/route-maker

    1.1.3 • Public • Published

    route-maker

    For named routes, framework agnostic - just works with strings.

    Install

    npm install @syndicode/route-maker

    Defining routes

    // routes.js of your project
    
    import route from '@syndicode/route-maker'
    
    export default {
      root: route('/'),
      items: route('items'),
      item: route('items/:id')
    }

    Using routes

    Now in router code to get original route string, react for example:

    import routes from './routes'
    
    <Route path={routes.item()} />

    And for get path with id:

    import routes from './routes'
    
    <Link to={routes.item({id: 123})} />

    If param was not matched in string, it will become URI parameter:

    routes.items({search: 'phrase'}) //= '/items?search=phrase'

    Positional parameters

    Parameters can be passed as plain arguments

    import routes from './routes'
    
    routes.item(123) === '/items/123'
    routes.item(123, {param: 'value'}) === '/items/123?param=value'
    routes.item(123, {param: 'value'}, {prefix: 'api'}) === '/api/items/123?param=value'

    Positional parameters can be used with hash-provided parameters:

    import route from '@syndicode/route-maker'
    
    const url = route('path/:one/:two')
    url(1, {two: 2}) === '/path/1/2'

    Options with examples

    Options can be set when creating and calling route.

    Prefix example:

    import route from '@syndicode/route-maker'
    
    let path = route('path', {prefix: 'api'})
    path() === '/api/path'
    path({prefix: 'api/v2'}) === '/api/v2/path'

    Can be changed directly:

    import route from '@syndicode/route-maker'
    
    route.options.prefix = 'api'
    
    const path = route('path')
    path() === '/api/path'

    Imported constructor can be extended with config method:

    import route from '@syndicode/route-maker'
    
    const apiRoute = route.config({prefix: 'api'})
    let path = apiRoute('path')
    path() === '/api/path'
    
    let path = route('path')
    path() === '/path'

    Prefix and defaults example

    import route from '@syndicode/route-maker'
    
    const apiRoute = route.config({prefix: 'api/v:apiVersion', defaults: {apiVersion: 1}})
    let path = apiRoute('path')
    path === '/api/v1/path'
    
    let path = apiRoute('path')
    path({apiVersion: 2}) === '/api/v2/path'

    Default values can be dynamically calculated:

    import route from '@syndicode/route-maker'
    
    let currentUser = {}
    const userRoute = route.config({prefix: ':userType', defaults: {
      userType: () => currentUser.type
    }})
    
    let path = userRoute('path')
    
    currentUser.type = 'customer'
    path() === '/customer/path'
    
    currentUser.type = 'admin'
    path() === '/admin/path'

    Options list

    url string, full url if needed. Parameters inside are not allowed.

    prefix string, adds prefix to resulting strings. Parameters inside are allowed.

    prependSlash boolean, default true, adds slash at the begging of path if it is absent

    assign object, will call Object.assign on route instances to add them provided properties

    defaults object, default parameters. Works with parameters in prefix. If object value is function then it will be called, result will come to url.

    Install

    npm i @syndicode/route-maker

    DownloadsWeekly Downloads

    3

    Version

    1.1.3

    License

    ISC

    Unpacked Size

    15.6 kB

    Total Files

    10

    Last publish

    Collaborators

    • romikus
    • rusinov
    • rusinov-syndicode