forked from vercel/vercel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(edge): adds json() response helper (vercel#9081)
### 🔖 What's in there? Because Typescript's `libdom` does not have [`static Response.json()`](microsoft/TypeScript-DOM-lib-generator#1444) (which modern browsers and edge runtimes are supporting), Typescript users can't use easily use it. This helper fills the gap. ### 🧪 How to test? It's covered with unit tests
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Builds a response object from a serializable JavaScript object: | ||
* - sets the 'Content-Type' response header to 'application/json' | ||
* - sets the response body from provided data | ||
* | ||
* @see {@link https://fetch.spec.whatwg.org/#dom-response-json} | ||
* @param data serialized data | ||
* @param init optional custom response status, statusText and headers | ||
* | ||
* @example | ||
* <caption>Building a JSON response</caption> | ||
* | ||
* ```ts | ||
* import { json } from '@vercel/edge'; | ||
* | ||
* const response = json({ notification: { success: true, content: 'worked' } }, { headers: {'x-custom': '1' }}) | ||
* ``` | ||
*/ | ||
export function json(data: any, init?: ResponseInit): Response { | ||
// @ts-expect-error This is not in lib/dom right now, and we can't augment it. | ||
return Response.json(data, init); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @jest-environment @edge-runtime/jest-environment | ||
*/ | ||
|
||
import { json } from '../src/response'; | ||
|
||
describe('json', () => { | ||
it('returns a response with JSON content', async () => { | ||
const content = { foo: 'bar' }; | ||
const response = json(content); | ||
expect(response.headers.get('content-type')).toEqual('application/json'); | ||
expect(await response.json()).toEqual(content); | ||
}); | ||
|
||
it('can set response init', async () => { | ||
const content = { bar: 'baz' }; | ||
const status = 201; | ||
const statusText = 'it is in'; | ||
const customHeader = 'x-custom'; | ||
const customHeaderValue = '1'; | ||
const response = json(content, { | ||
status, | ||
statusText, | ||
headers: { [customHeader]: customHeaderValue }, | ||
}); | ||
expect(response).toMatchObject({ | ||
status, | ||
statusText, | ||
}); | ||
expect(response.headers.get('content-type')).toEqual('application/json'); | ||
expect(response.headers.get(customHeader)).toEqual(customHeaderValue); | ||
expect(await response.json()).toEqual(content); | ||
}); | ||
}); |