From 9391865bb3af2aa67e129fbee64581ae789925e0 Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Mon, 8 Apr 2024 21:42:53 -0500 Subject: [PATCH] simplifies websocket, adds withParams option to AutoRouter --- src/AutoRouter.spec.ts | 8 ++++++++ src/AutoRouter.ts | 3 ++- src/websocket.ts | 6 ++---- src/withParams.spec.ts | 10 ---------- src/withParams.ts | 1 - 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/AutoRouter.spec.ts b/src/AutoRouter.spec.ts index 2a5e37ec..8b47605d 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 e239920f..ab53f8b3 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 452b6f37..94b9f30a 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 7b8f42d1..7f4d43d5 100644 --- a/src/withParams.spec.ts +++ b/src/withParams.spec.ts @@ -70,14 +70,4 @@ describe('withParams (middleware)', () => { testParam: 'testValue', }) }) - - it('downstream handlers can access original Request through request.raw', async () => { - const handler = vi.fn(r => r.raw) - const router = Router().get('/', withParams, handler) - const request = toReq('/') - - await router.fetch(request) - - expect(handler).toHaveReturnedWith(request) - }) }) diff --git a/src/withParams.ts b/src/withParams.ts index 588c645d..e96e123e 100644 --- a/src/withParams.ts +++ b/src/withParams.ts @@ -1,7 +1,6 @@ import { IRequest } from './types' export const withParams = (request: IRequest): void => { - request.raw = request.raw ?? request // places reference to request in request.raw if safe to do so request.proxy = new Proxy(request.proxy ?? request, { get: (obj, prop, receiver) => obj[prop]?.bind?.(request) // if prop exists (as function), return the function, bound to the original request