Skip to content

Commit

Permalink
simplifies websocket, adds withParams option to AutoRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Apr 9, 2024
1 parent 2d1a274 commit 9391865
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/AutoRouter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion src/AutoRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,6 +12,7 @@ export const AutoRouter = <
format = json,
missing = () => error(404),
finally: f = [],
withParams = wp,
before = [],
...options }: AutoRouterOptions = {}
): AutoRouterType<RequestType, Args, ResponseType> => Router({
Expand Down
6 changes: 2 additions & 4 deletions src/websocket.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
10 changes: 0 additions & 10 deletions src/withParams.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
1 change: 0 additions & 1 deletion src/withParams.ts
Original file line number Diff line number Diff line change
@@ -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) =>

Check failure on line 5 in src/withParams.ts

View workflow job for this annotation

GitHub Actions / build

'receiver' is defined but never used
obj[prop]?.bind?.(request) // if prop exists (as function), return the function, bound to the original request
Expand Down

0 comments on commit 9391865

Please sign in to comment.