Skip to content

Commit fb93ce6

Browse files
committed
Fix thenable fallback in mapCompactResponse to avoid undefined set variable
- Replace mapResponse(x, set) with mapCompactResponse(x, request) in thenable handler - Fixes runtime crash when non-Promise thenables are returned (e.g., from ORMs like Drizzle) - Applied fix to both web-standard and bun adapters - Added regression test for custom thenable objects
1 parent 969fc76 commit fb93ce6

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/adapter/bun/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ export const mapCompactResponse = (
504504
// @ts-expect-error
505505
if (typeof response?.then === 'function')
506506
// @ts-expect-error
507-
return response.then((x) => mapResponse(x, set)) as any
507+
return response.then((x) => mapCompactResponse(x, request)) as any
508508

509509
// @ts-expect-error
510510
if (typeof response?.toResponse === 'function')

src/adapter/web-standard/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ export const mapCompactResponse = (
537537
// @ts-expect-error
538538
if (typeof response?.then === 'function')
539539
// @ts-expect-error
540-
return response.then((x) => mapResponse(x, set)) as any
540+
return response.then((x) => mapCompactResponse(x, request)) as any
541541

542542
// @ts-expect-error
543543
if (typeof response?.toResponse === 'function')

test/adapter/web-standard/map-compact-response.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,27 @@ describe('Web Standard - Map Compact Response', () => {
203203
expect(response.status).toBe(200)
204204
expect(await response.formData()).toBeInstanceOf(FormData)
205205
})
206+
207+
it('map custom thenable', async () => {
208+
// Custom thenable object (e.g., like some ORMs return such as Drizzle)
209+
// Using a class to avoid being caught by the 'Object' case
210+
class CustomThenable {
211+
then(onFulfilled: (value: any) => any) {
212+
const data = { name: 'Shiroko', id: 42 }
213+
return Promise.resolve(data).then(onFulfilled)
214+
}
215+
}
216+
217+
const customThenable = new CustomThenable()
218+
const responsePromise = mapCompactResponse(customThenable)
219+
expect(responsePromise).toBeInstanceOf(Promise)
220+
221+
const response = await responsePromise
222+
223+
expect(response).toBeInstanceOf(Response)
224+
const body = await response.text()
225+
const parsed = JSON.parse(body)
226+
expect(parsed).toEqual({ name: 'Shiroko', id: 42 })
227+
expect(response.status).toBe(200)
228+
})
206229
})

0 commit comments

Comments
 (0)