From 6ad467df91ed01ba14e20e2444f57f06bcfc76ef Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta Date: Thu, 4 Aug 2022 02:34:45 +0900 Subject: [PATCH 1/4] feat: add utility for router params (#120) --- README.md | 1 + src/utils/request.ts | 8 +++++++ test/utils.test.ts | 52 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 87db7e30..ceac4597 100644 --- a/README.md +++ b/README.md @@ -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`) diff --git a/src/utils/request.ts b/src/utils/request.ts index 71b76956..0f64ec52 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -10,6 +10,14 @@ export function getQuery (event: CompatibilityEvent) { /** @deprecated Use `h3.getQuery` */ export const useQuery = getQuery +export function getRouterParams (event: CompatibilityEvent): CompatibilityEvent['context'] { + // Fallback object needs to be returned in case router is not used (#149) + return event.context.params || {} +} + +/** @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 } diff --git a/test/utils.test.ts b/test/utils.test.ts index bc1e4f16..fc2e503e 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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 @@ -53,6 +53,56 @@ describe('', () => { }) }) + describe('getRouterParams', () => { + 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)) From 63526114cdfe27d0a79e2be9bcaa3651d9b30ec0 Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta Date: Thu, 4 Aug 2022 03:24:25 +0900 Subject: [PATCH 2/4] feat: add one more utility for router params (#120) --- src/utils/request.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/utils/request.ts b/src/utils/request.ts index 0f64ec52..a905203a 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -18,6 +18,15 @@ export function getRouterParams (event: CompatibilityEvent): CompatibilityEvent[ /** @deprecated Use `h3.getRouterParams` */ export const useRouterParams = getRouterParams +export function getRouterParam (event: CompatibilityEvent, name: string): CompatibilityEvent['context'][string] { + const params = getRouterParams(event) + + return params[name] +} + +/** @deprecated Use `h3.getRouterParam` */ +export const useRouterParam = getRouterParam + export function getMethod (event: CompatibilityEvent, defaultMethod: HTTPMethod = 'GET'): HTTPMethod { return (event.req.method || defaultMethod).toUpperCase() as HTTPMethod } From b8f920dd74d0509db5e3c2a1cd53900c80ec8723 Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta Date: Thu, 4 Aug 2022 03:27:51 +0900 Subject: [PATCH 3/4] feat: reorganize unit tests (#120) --- test/router.test.ts | 76 ++++++++++++++++++++++++++++++++++++++++++++- test/utils.test.ts | 52 +------------------------------ 2 files changed, 76 insertions(+), 52 deletions(-) diff --git a/test/router.test.ts b/test/router.test.ts index bbdcea49..63b2472c 100644 --- a/test/router.test.ts +++ b/test/router.test.ts @@ -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, useRouterParams, getRouterParam, useRouterParam } from '../src' describe('router', () => { let app: App @@ -61,3 +61,77 @@ describe('router', () => { expect(res.status).toEqual(405) }) }) + +describe('getRouterParams / useRouterParams', () => { + let app: App + let request: SuperTest + + 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' }) + expect(useRouterParams(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({}) + expect(useRouterParams(request)).toMatchObject({}) + return '200' + }) + const result = await request.get('/test/empty/params') + + expect(result.text).toBe('200') + }) + }) +}) + +describe('getRouterParam / useRouterParam', () => { + let app: App + let request: SuperTest + + 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') + expect(useRouterParam(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) + expect(useRouterParam(request, 'name')).toEqual(undefined) + return '200' + }) + const result = await request.get('/test/empty/params') + + expect(result.text).toBe('200') + }) + }) +}) diff --git a/test/utils.test.ts b/test/utils.test.ts index fc2e503e..bc1e4f16 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -1,6 +1,6 @@ import supertest, { SuperTest, Test } from 'supertest' import { describe, it, expect, beforeEach } from 'vitest' -import { createApp, createRouter, App, sendRedirect, useBase, useQuery, getRouterParams, useRouterParams, useMethod, assertMethod } from '../src' +import { createApp, App, sendRedirect, useBase, useQuery, useMethod, assertMethod } from '../src' describe('', () => { let app: App @@ -53,56 +53,6 @@ describe('', () => { }) }) - describe('getRouterParams', () => { - 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)) From 819d1333dd26fd45f8dbc86117b148bbdca0e3a6 Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta Date: Thu, 4 Aug 2022 03:36:33 +0900 Subject: [PATCH 4/4] chore: delete redundant code (#120) --- src/utils/request.ts | 6 ------ test/router.test.ts | 10 +++------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index a905203a..1667c9e2 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -15,18 +15,12 @@ export function getRouterParams (event: CompatibilityEvent): CompatibilityEvent[ return event.context.params || {} } -/** @deprecated Use `h3.getRouterParams` */ -export const useRouterParams = getRouterParams - export function getRouterParam (event: CompatibilityEvent, name: string): CompatibilityEvent['context'][string] { const params = getRouterParams(event) return params[name] } -/** @deprecated Use `h3.getRouterParam` */ -export const useRouterParam = getRouterParam - export function getMethod (event: CompatibilityEvent, defaultMethod: HTTPMethod = 'GET'): HTTPMethod { return (event.req.method || defaultMethod).toUpperCase() as HTTPMethod } diff --git a/test/router.test.ts b/test/router.test.ts index 63b2472c..2ba07e93 100644 --- a/test/router.test.ts +++ b/test/router.test.ts @@ -1,6 +1,6 @@ import supertest, { SuperTest, Test } from 'supertest' import { describe, it, expect, beforeEach } from 'vitest' -import { createApp, createRouter, App, Router, getRouterParams, useRouterParams, getRouterParam, useRouterParam } from '../src' +import { createApp, createRouter, App, Router, getRouterParams, getRouterParam } from '../src' describe('router', () => { let app: App @@ -62,7 +62,7 @@ describe('router', () => { }) }) -describe('getRouterParams / useRouterParams', () => { +describe('getRouterParams', () => { let app: App let request: SuperTest @@ -75,7 +75,6 @@ describe('getRouterParams / useRouterParams', () => { it('can return router params', async () => { const router = createRouter().get('/test/params/:name', (request) => { expect(getRouterParams(request)).toMatchObject({ name: 'string' }) - expect(useRouterParams(request)).toMatchObject({ name: 'string' }) return '200' }) app.use(router) @@ -89,7 +88,6 @@ describe('getRouterParams / useRouterParams', () => { it('can return an empty object if router is not used', async () => { app.use('/', (request) => { expect(getRouterParams(request)).toMatchObject({}) - expect(useRouterParams(request)).toMatchObject({}) return '200' }) const result = await request.get('/test/empty/params') @@ -99,7 +97,7 @@ describe('getRouterParams / useRouterParams', () => { }) }) -describe('getRouterParam / useRouterParam', () => { +describe('getRouterParam', () => { let app: App let request: SuperTest @@ -112,7 +110,6 @@ describe('getRouterParam / useRouterParam', () => { 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') - expect(useRouterParam(request, 'name')).toEqual('string') return '200' }) app.use(router) @@ -126,7 +123,6 @@ describe('getRouterParam / useRouterParam', () => { it('can return `undefined` for any keys', async () => { app.use('/', (request) => { expect(getRouterParam(request, 'name')).toEqual(undefined) - expect(useRouterParam(request, 'name')).toEqual(undefined) return '200' }) const result = await request.get('/test/empty/params')