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

DRAFT: v5.1 - adds withParams option to AutoRouter #238

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**
Expand Down
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
5 changes: 3 additions & 2 deletions src/withParams.spec.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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)

Expand Down
9 changes: 5 additions & 4 deletions src/withParams.ts
Original file line number Diff line number Diff line change
@@ -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
})
}
Loading