1.0.0 • Published 6 years ago

max-router v1.0.0

Weekly downloads
139
License
ISC
Repository
github
Last release
6 years ago

max-router

An opinionated browser routing configuration. Basically a wrapper around 2 libraries History and Url Mapper. The purpose being to provide a slightly simpler API for a common use case but without any real flexibility.

Installation

With npm:

> npm install --save-dev max-router

Or yarn:

> yarn add -D max-router

Usage

import * as router from 'max-router'

router.route('/badger/:id', async ({ id }) => {
    const badger = await fetch(`example.com/badger/${id}`)
    // Render badger page...
})

router.onMissing(async path => {
    // Render 404
})

router.onError(async error => {
    // Render 500
})

// Listen for changes to the url (also immediately invokes for current url)
router.listen({ catchUnhandledErrors: true }, async location => {
    // Do middleware type stuff, like authentication...

    // Invoke the router
    router.onLocationChange(location)
})

// If you want to navigate somewhere
router.go('/badger/1')

The catchUnhandledErrors option will set window.onerror and window.onunhandledrejection to call the router's onError, so that unhandled errors can display the 500 page.