@lazy/service-worker-router
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-rc.1 • Public • Published

Lazy Service Worker Router

A simple Service Worker router for the lazy developer.


Table of Contents

Example

import { addRouter, response } from '@lazy/service-worker-router'

addRouter('https://api.example.com/api/v1/', {
  async 'GET /users'() {
    return response({ users: ['dfce5716-c722-4502-beb6-cfc52a6ef9f5'] })
  },
  async 'GET /users/:userId'(request, { userId }) {
    if (userId === 'dfce5716-c722-4502-beb6-cfc52a6ef9f5') {
      return response({ id: userId, username: 'Example' })
    }

    return response({}, 404)
  },
})

API

addRouter

Register the router to handle requests in a Service Worker.

Parameters

  • origin - string - The origin of the requests to intercept.
  • handlers - object - An object of function to handle each request.

Example

import { addRouter, response } from '@lazy/service-worker-router'

const removeRouter = addRouter('https://api.example.com/api/v1/', {
  async 'GET /'() {
    return response({})
  },
})

removeRouter()

Returns () => void

response

Create a Response object with some sensible defaults.

Parameters

  • body - object - An object to respond with.
  • status - number - The status code for the response.
  • headers - object - The headers for the response.

Example

import { response } from '@lazy/service-worker-router'

const r404 = response({}, 404)

Returns Response

router

Route an indivudual request to the respecitve handler.

Parameters

  • origin - string - The origin of the requests to intercept.
  • handlers - object - An object of function to handle each request.
  • request - Request - The request to route.

Example

import { router, response } from '@lazy/service-worker-router'

const res = router(
  'https://api.example.com/api/v1/',
  {
    async 'GET /'() {
      return response({})
    },
  },
  new Request('https://api.example.com/api/v1/')
)

Returns Promise<Response>

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.0-rc.10latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.0-rc.10

Package Sidebar

Install

npm i @lazy/service-worker-router

Weekly Downloads

0

Version

1.0.0-rc.1

License

MIT

Unpacked Size

7.23 kB

Total Files

5

Last publish

Collaborators

  • aidant