diff --git a/src/AutoRouter.ts b/src/AutoRouter.ts index 4b99a048..5d61b671 100644 --- a/src/AutoRouter.ts +++ b/src/AutoRouter.ts @@ -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 diff --git a/src/IttyRouter.ts b/src/IttyRouter.ts index bef7cbe4..91fe8c63 100644 --- a/src/IttyRouter.ts +++ b/src/IttyRouter.ts @@ -39,7 +39,7 @@ export type RouteEntry = [ ] // this is the generic "Route", which allows per-route overrides -export type Route = ( +export type Route = ( path: string, ...handlers: RouteHandler[] ) => RT diff --git a/src/createResponse.spec.ts b/src/createResponse.spec.ts index 65748956..757500d0 100644 --- a/src/createResponse.spec.ts +++ b/src/createResponse.spec.ts @@ -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 () => { diff --git a/src/createResponse.ts b/src/createResponse.ts index f106792e..864fde78 100644 --- a/src/createResponse.ts +++ b/src/createResponse.ts @@ -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 + } diff --git a/tsconfig.json b/tsconfig.json index dde7b198..f8ca21c8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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,