JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4550
  • Score
    100M100P100Q134352F
  • License MIT

An assortment of delicious extras for the calorie-light itty-router.

Package Exports

  • itty-router-extras

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (itty-router-extras) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Itty Router

npm package minified + gzipped size Build Status Coverage Status Open Issues

An assortment of delicious extras for the calorie-light itty-router.

Installation

npm install itty-router itty-router-extras

Includes the following:

class

StatusError(statusCode: number, message: string): Error throw these to control HTTP status codes that itty responds with.

middleware (add inline as route handlers)

withContent safely parses and embeds content request bodies (e.g. text/json) as request.content

withCookies embeds cookies into request as request.cookies (object)

withParams embeds route params directly into request as a convenience

response

error(status: number, message: string): Response returns JSON-formatted Response with { error: message, status } and the matching status code on the response.

json(content: object, options: object): Response returns JSON-formatted Response with options passed to the Response (e.g. headers, status, etc)

status(status: number, message?: string): Response returns JSON-formatted Response with { message, status } and the matching status code on the response.

text(content: string, options: object): Response returns plaintext-formatted Response with options passed to the Response (e.g. headers, status, etc). This is simply a normal Response, but included for code-consistency with json()

routers

ThrowableRouter(options?: object): Proxy this is a convenience wrapper around itty-router that simply adds automatic exception handling, rather than requiring try/catch blocks within your middleware/handlers, or manually calling a .catch(error) on the router.handle.

Example

import { Router } from 'itty-router'
import {
  json,
  missing,
  error,
  withContent,
  withParams,
} from 'itty-router-extras'

const todos = [
  { id: '13', value: 'foo' },
  { id: '14', value: 'bar' },
  { id: '15', value: 'baz' },
]

// create a router
const router = Router({ base: '/todos' }) // this is a Proxy, not a class

// optional shortcut to avoid per-route calls
// router.all('*', withParams, withContent)

// GET collection index
router.get('/', () => asJSON(todos))

// GET item - only return if found
router.get('/:id', withParams, ({ id }) => {
  const todo = todos.find(t => t.id === id)
  if (todo) return json(todo)
})

// POST to the collection
router.post('/todos', withContent, ({ content }) => {
  return content
  ? json({
      created: 'todo',
      value: content,
    })
  : error(400, 'You probably need some content for that...')
)

// 404 for everything else
router.all('*', () => missing('Are you sure about that?'))

// attach the router "handle" to the event handler
addEventListener('fetch', event =>
  // router.handle expects a Request as the first param, then anything else gets passed along!
  event.respondWith(router.handle(event.request))
)

Contributors

These folks are the real heroes, making open source the powerhouse that it is! Help out and get your name added to this list! <3

Core, Concepts, and Codebase

  • @mvasigh - for constantly discussing these ridiculously-in-the-weeds topics with me. And then for writing the TS interfaces (or simply re-writing in TS), right Mehdi??