diff --git a/CHANGELOG.md b/CHANGELOG.md index d34ed30..04f6e4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Changelog +- **v5.1.0** + - added: withParams is now an option in AutoRouter (set to no-op to bypass) - **v5.0.10** - fixed: response formatters in finally stage could still cross pollute headers in Node - **v5.0.9** diff --git a/src/AutoRouter.spec.ts b/src/AutoRouter.spec.ts index 2a5e37e..8b47605 100644 --- a/src/AutoRouter.spec.ts +++ b/src/AutoRouter.spec.ts @@ -55,6 +55,14 @@ describe(`SPECIFIC TESTS: AutoRouter`, () => { expect(response.status).toBe(418) }) + it('withParams: can replace the withParams middleware', async () => { + const handler = vi.fn(({ a, b, params }) => [a, b, params.a, params.b]) + const router = AutoRouter({ withParams: () => {} }).get('/:a/:b', handler) + + await router.fetch(toReq('/foo/bar')) + expect(handler).toHaveReturnedWith([undefined, undefined, 'foo', 'bar']) + }) + it('before: RouteHandler - adds upstream middleware', async () => { const handler = vi.fn(r => typeof r.date) const router = AutoRouter({ diff --git a/src/AutoRouter.ts b/src/AutoRouter.ts index e239920..ab53f8b 100644 --- a/src/AutoRouter.ts +++ b/src/AutoRouter.ts @@ -2,7 +2,7 @@ import { Router } from './Router' import { error } from './error' import { json } from './json' import { AutoRouterOptions, AutoRouterType, IRequest } from './types' -import { withParams } from './withParams' +import { withParams as wp } from './withParams' export const AutoRouter = < RequestType extends IRequest = IRequest, @@ -12,6 +12,7 @@ export const AutoRouter = < format = json, missing = () => error(404), finally: f = [], + withParams = wp, before = [], ...options }: AutoRouterOptions = {} ): AutoRouterType => Router({ diff --git a/src/websocket.ts b/src/websocket.ts index 452b6f3..94b9f30 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -1,7 +1,5 @@ -import { createResponse } from './createResponse' - -export const websocket = (client: WebSocket, options: object = {}) => - createResponse()(null, { +export const websocket = (client: WebSocket, options?: ResponseInit) => + new Response(null, { status: 101, webSocket: client, ...options, diff --git a/src/withParams.spec.ts b/src/withParams.spec.ts index 3f109ad..272a7e7 100644 --- a/src/withParams.spec.ts +++ b/src/withParams.spec.ts @@ -1,12 +1,13 @@ import { describe, expect, it, vi } from 'vitest' import { Router } from './Router' import { withParams } from './withParams' +import { toReq } from '../lib/index' describe('withParams (middleware)', () => { it('allows accessing route params from the request itself', async () => { const router = Router() const handler = vi.fn(({ id, method }) => ({ id, method })) - const request = { method: 'GET', url: 'https://foo.bar/baz' } + const request = toReq('/baz') await router.get('/:id', withParams, handler).fetch(request) @@ -36,7 +37,7 @@ describe('withParams (middleware)', () => { it('can be used as global upstream middleware', async () => { const router = Router() const handler = vi.fn(({ id, method }) => ({ id, method })) - const request = { method: 'GET', url: 'https://foo.bar/baz' } + const request = toReq('/baz') await router.all('*', withParams).get('/:id', handler).fetch(request) diff --git a/src/withParams.ts b/src/withParams.ts index 2388a88..8861b79 100644 --- a/src/withParams.ts +++ b/src/withParams.ts @@ -1,9 +1,10 @@ import { IRequest } from './types' export const withParams = (request: IRequest): void => { - request.proxy = new Proxy(request.proxy || request, { - get: (obj, prop) => obj[prop] !== undefined - ? obj[prop]?.bind?.(request) || obj[prop] - : obj?.params?.[prop] + request.proxy = new Proxy(request.proxy ?? request, { + get: (obj, prop) => + obj[prop]?.bind?.(request) // if prop exists (as function), return the function, bound to the original request + ?? obj[prop] // if prop exists, return it + ?? obj?.params?.[prop] // if no prop exists, try the params object }) }