jrf0110 / 8track

A service worker router with async middleware and neato type-inference inspired by Koa
56 stars 6 forks source link

13 router mounting #50

Closed jrf0110 closed 3 years ago

jrf0110 commented 3 years ago

Sub Router Mounting

import { Router } from '8track'

const router = new Router()
const usersRouter = new Router()

usersRouter.get`/`.handle(async ctx => {
  // List users
})

usersRouter.post`/`.handle(async ctx => {
  // Create user
})

usersRouter.get`/${'userId'}`.handle(async ctx => {
  // Get user by ID
})

usersRouter.put`/${'userId'}`.handle(async ctx => {
  // Update user by ID
})

usersRouter.del`/${'userId'}`.handle(async ctx => {
  // Delete user by ID
})

router.all`/organization/${'organizationId'}/users`.use(usersRouter)

.url property on Context

Middlewares frequently need to parse the URL and we end up with a ton of new URL(event.request.url) calls littered throughout the application. No more! Now you can use ctx.url to access a pre-parsed URL instance