Skip to content

Commit

Permalink
fix(HttpResponse): narrow "body" type to "JsonBodyType"
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 7, 2023
1 parent af9de76 commit f1d40d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/core/HttpResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ describe('HttpResponse.json()', () => {
})
})

it('creates a json response given an array', async () => {
const response = HttpResponse.json([1, 2, 3])

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.json()).toEqual([1, 2, 3])
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'application/json',
})
})

it('creates a json response given a plain string', async () => {
const response = HttpResponse.json(`"hello"`)

Expand Down Expand Up @@ -77,7 +89,10 @@ describe('HttpResponse.json()', () => {
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.json()).toEqual({ firstName: 'John' })
// A ReadableStream instance is not a valid body init
// for the "Response.json()" static method. It gets serialized
// into a plain object.
expect(await response.json()).toEqual({})
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'application/json',
})
Expand Down
6 changes: 3 additions & 3 deletions src/core/HttpResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DefaultBodyType } from './handlers/RequestHandler'
import type { DefaultBodyType, JsonBodyType } from './handlers/RequestHandler'
import {
decorateResponse,
normalizeResponseInit,
Expand Down Expand Up @@ -62,14 +62,14 @@ export class HttpResponse extends Response {
* HttpResponse.json({ firstName: 'John' })
* HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })
*/
static json<BodyType extends DefaultBodyType>(
static json<BodyType extends JsonBodyType>(
body?: BodyType | null,
init?: HttpResponseInit,
): StrictResponse<BodyType> {
const responseInit = normalizeResponseInit(init)
responseInit.headers.set('Content-Type', 'application/json')
return new HttpResponse(
body instanceof ReadableStream ? body : JSON.stringify(body),
JSON.stringify(body),
responseInit,
) as StrictResponse<BodyType>
}
Expand Down
8 changes: 8 additions & 0 deletions src/core/handlers/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export type DefaultBodyType =
| null
| undefined

export type JsonBodyType =
| Record<string, any>
| string
| number
| boolean
| null
| undefined

export interface RequestHandlerDefaultInfo {
header: string
}
Expand Down

0 comments on commit f1d40d6

Please sign in to comment.