Skip to content

Commit

Permalink
createResponse simplified and safe against using as ResponseHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Mar 20, 2024
1 parent 1f82479 commit 3ad0615
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 23 deletions.
6 changes: 2 additions & 4 deletions src/AutoRouter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { RouteHandler } from 'IttyRouter'
import { ResponseHandler, Router, RouterOptions } from './Router'
import { error } from './error'
import { json } from './json'
import { withParams } from './withParams'
import { Router, RouterOptions} from './Router'
import { RouteHandler } from 'IttyRouter'
import { ResponseFormatter } from './createResponse'
import { ResponseHandler } from './Router'

type AutoRouterOptions = {
missing?: RouteHandler
Expand Down
2 changes: 1 addition & 1 deletion src/IttyRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type RouteEntry = [
]

// this is the generic "Route", which allows per-route overrides
export type Route = <RequestType = IRequest, Args extends any[] = any[], RT = RouterType>(
export type Route = <RequestType = IRequest, Args extends any[] = any[], RT = IttyRouterType>(
path: string,
...handlers: RouteHandler<RequestType, Args>[]
) => RT
Expand Down
5 changes: 3 additions & 2 deletions src/createResponse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ describe('createResponse(mimeType: string, transform?: Function)', () => {
expect(body).toBe('***')
})

it('will ignore a Response, to allow downstream use', async () => {
it('will ignore a Response, to allow downstream use (will not modify headers)', async () => {
const r1 = json({ foo: 'bar' })
const r2 = json(r1)
const r2 = text(r1)

expect(r2).toBe(r1)
expect(r2.headers.get('content-type')?.includes('text')).toBe(false)
})

it('will ignore an undefined body', async () => {
Expand Down
22 changes: 8 additions & 14 deletions src/createResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ export interface BodyTransformer {
(body: any): string
}

export const createResponse =
export const createResponse =
(
format = 'text/plain; charset=utf-8',
transform?: BodyTransformer
): ResponseFormatter =>
(body, { ...options } = {}) =>
body === undefined || body instanceof Response // skip function if undefined or an existing Response
? body
: new Response(transform ? transform(body) : body, {
...options,
headers: {
'content-type': format,
...options.headers?.entries
// @ts-expect-error - foul
? Object.fromEntries(options.headers)
: options.headers
},
})
(body, { ...options } = {}) => {
if (body === undefined || body instanceof Response) return body

const response = new Response(transform?.(body) ?? body, options)
response.headers.set('content-type', format)
return response
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"lib": ["esnext", "dom", "dom.iterable"],
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "nodeNext",
"noFallthroughCasesInSwitch": true,
"pretty": true,
"resolveJsonModule": true,
// "moduleResolution": "nodeNext", // disabled to be compatible with module: "esnext"
// "resolveJsonModule": true, // disabled to be compatible with module: "esnext"
"rootDir": "src",
"skipLibCheck": true,
"strict": true,
Expand Down

0 comments on commit 3ad0615

Please sign in to comment.