Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add utility for router params (#120) #158

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Instead of adding helpers to `req` and `res`, h3 exposes them as composable util
- `setCookie(res, name, value, opts?)`
- `deleteCookie(res, name, opts?)`
- `useQuery(req)`
- `getRouterParams(event)`
- `send(res, data, type?)`
- `sendRedirect(res, location, code=302)`
- `getRequestHeaders(event, headers)` (alias: `getHeaders`)
Expand Down
11 changes: 11 additions & 0 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ export function getQuery (event: CompatibilityEvent) {
/** @deprecated Use `h3.getQuery` */
export const useQuery = getQuery

export function getRouterParams (event: CompatibilityEvent): CompatibilityEvent['context'] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please export it from src/router.ts or src/utils/router.ts?

// Fallback object needs to be returned in case router is not used (#149)
return event.context.params || {}
}
Comment on lines +13 to +16
Copy link
Member Author

@NozomuIkuta NozomuIkuta Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is just for GitHub to add a reference link to #149.


export function getRouterParam (event: CompatibilityEvent, name: string): CompatibilityEvent['context'][string] {
const params = getRouterParams(event)

return params[name]
}

export function getMethod (event: CompatibilityEvent, defaultMethod: HTTPMethod = 'GET'): HTTPMethod {
return (event.req.method || defaultMethod).toUpperCase() as HTTPMethod
}
Expand Down
72 changes: 71 additions & 1 deletion test/router.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import supertest, { SuperTest, Test } from 'supertest'
import { describe, it, expect, beforeEach } from 'vitest'
import { createApp, createRouter, App, Router } from '../src'
import { createApp, createRouter, App, Router, getRouterParams, getRouterParam } from '../src'

describe('router', () => {
let app: App
Expand Down Expand Up @@ -61,3 +61,73 @@ describe('router', () => {
expect(res.status).toEqual(405)
})
})

describe('getRouterParams', () => {
let app: App
let request: SuperTest<Test>

beforeEach(() => {
app = createApp({ debug: false })
request = supertest(app)
})

describe('with router', () => {
it('can return router params', async () => {
const router = createRouter().get('/test/params/:name', (request) => {
expect(getRouterParams(request)).toMatchObject({ name: 'string' })
return '200'
})
app.use(router)
const result = await request.get('/test/params/string')

expect(result.text).toBe('200')
})
})

describe('without router', () => {
it('can return an empty object if router is not used', async () => {
app.use('/', (request) => {
expect(getRouterParams(request)).toMatchObject({})
return '200'
})
const result = await request.get('/test/empty/params')

expect(result.text).toBe('200')
})
})
})

describe('getRouterParam', () => {
let app: App
let request: SuperTest<Test>

beforeEach(() => {
app = createApp({ debug: false })
request = supertest(app)
})

describe('with router', () => {
it('can return a value of router params corresponding to the given name', async () => {
const router = createRouter().get('/test/params/:name', (request) => {
expect(getRouterParam(request, 'name')).toEqual('string')
return '200'
})
app.use(router)
const result = await request.get('/test/params/string')

expect(result.text).toBe('200')
})
})

describe('without router', () => {
it('can return `undefined` for any keys', async () => {
app.use('/', (request) => {
expect(getRouterParam(request, 'name')).toEqual(undefined)
return '200'
})
const result = await request.get('/test/empty/params')

expect(result.text).toBe('200')
})
})
})