@lazy/service-worker-router
v1.0.0-rc.1
Published
A simple Service Worker router for the lazy developer.
Downloads
1
Readme
Lazy Service Worker Router
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>