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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
8 changes: 8 additions & 0 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ 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

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.


/** @deprecated Use `h3.getRouterParams` */
export const useRouterParams = getRouterParams

export function getMethod (event: CompatibilityEvent, defaultMethod: HTTPMethod = 'GET'): HTTPMethod {
return (event.req.method || defaultMethod).toUpperCase() as HTTPMethod
}
Expand Down
52 changes: 51 additions & 1 deletion test/utils.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, App, sendRedirect, useBase, useQuery, useMethod, assertMethod } from '../src'
import { createApp, createRouter, App, sendRedirect, useBase, useQuery, getRouterParams, useRouterParams, useMethod, assertMethod } from '../src'

describe('', () => {
let app: App
Expand Down Expand Up @@ -53,6 +53,56 @@ describe('', () => {
})
})

describe('getRouterParams', () => {
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 move it to test/router.test.ts ?

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

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

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

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

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

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

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

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

describe('useMethod', () => {
it('can get method', async () => {
app.use('/', req => useMethod(req))
Expand Down