Skip to content

Commit

Permalink
refactor!: move router params to event.context.params
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 7, 2022
1 parent b3ef52c commit 6fe32e2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const app = createApp()

const router = createRouter()
.get('/', () => 'Hello World!')
.get('/hello/:name', req => `Hello ${req.params.name}!`)
.get('/hello/:name', req => `Hello ${req.context.params.name}!`)

app.use(router)
```
Expand Down
2 changes: 1 addition & 1 deletion playground/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createApp, createRouter } from '../src'
const app = createApp()
const router = createRouter()
.get('/', () => 'Hello World!')
.get('/hello/:name', event => `Hello ${event.params.name}!`)
.get('/hello/:name', event => `Hello ${event.context.params.name}!`)

app.use(router)

Expand Down
8 changes: 5 additions & 3 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export interface H3Event {
event: H3Event
req: IncomingMessage
res: ServerResponse
/** Request params only filled with h3 Router handlers */
params?: Record<string, any>
context: Record<string, any>
}

export type CompatibilityEvent = H3Event | IncomingMessage
Expand Down Expand Up @@ -91,7 +90,8 @@ export function createEvent (req: http.IncomingMessage, res: http.ServerResponse
const event = {
__is_event__: true,
req,
res
res,
context: {}
} as H3Event

// Backward comatibility for interchangable usage of {event,req,res}.{req,res}
Expand All @@ -101,6 +101,8 @@ export function createEvent (req: http.IncomingMessage, res: http.ServerResponse
// @ts-ignore
req.event = event
// @ts-ignore
req.context = event.context
// @ts-ignore
req.req = req
// @ts-ignore
req.res = res
Expand Down
4 changes: 2 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ export function createRouter (): Router {
}

// Add params
event.event.params = matched.params || {}
event.event.context.params = matched.params || {}
// @ts-ignore Compatibility
event.req.params = event.event.params
event.req.context.params = event.event.params

// Call handler
return handler(event)
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import type { H3Event } from './event'

interface CompatibilityRequestProps {
event: H3Event
context: Record<string, any>
/** Only available with connect and press */
originalUrl?: string
/** Request params only filled with h3 Router handlers */
params?: Record<string, any>
}

export interface IncomingMessage extends http.IncomingMessage, CompatibilityRequestProps {
Expand Down

0 comments on commit 6fe32e2

Please sign in to comment.