UNPKG

4.15 kBMarkdownView Raw
1# JSON Fetch
2
3A drop-in replacements for `fetch`, `Request`, and `Response` with first class support for JSON objects.
4
5Unlike other HTTP libraries, this one stays as close as possible to the original Fetch API,
6while improving the ergonomics the most common use case:
7
8Before:
9
10```ts
11const response = await fetch('/some', {
12 method: 'POST',
13 body: JSON.stringify(json),
14 headers: {
15 'Content-Type': 'application/json',
16 },
17});
18```
19
20After:
21
22```ts
23import { JSONRequest } from '@worker-tools/json-fetch';
24
25const response = await fetch(new JSONRequest('/some', {
26 method: 'POST',
27 body: json,
28}));
29```
30
31You can also use the updated `jsonFetch` function:
32
33```ts
34import { jsonFetch as fetch } from '@worker-tools/json-fetch';
35
36const response = await fetch('/some', { method: 'POST', body: data })
37```
38
39Note that previous use cases remain intact, i.e. posting `FormData`, `ReadableStream`, etc. as body works:
40
41```ts
42const response = await fetch(new JSONRequest('/some', {
43 method: 'POST',
44 body: new FromData(form),
45}))
46```
47
48This will send the body as form-data/multipart with correct content type header, as in the original Fetch API.
49Only difference is that the `Accept` header will be set to indicate preference for `application/json`, i.e. anticipating a JSON response from the server.
50
51--------
52
53<p align="center"><a href="https://workers.tools"><img src="https://workers.tools/assets/img/logo.svg" width="100" height="100" /></a>
54<p align="center">This module is part of the Worker Tools collection<br/>⁕
55
56[Worker Tools](https://workers.tools) are a collection of TypeScript libraries for writing web servers in [Worker Runtimes](https://workers.js.org) such as Cloudflare Workers, Deno Deploy and Service Workers in the browser.
57
58If you liked this module, you might also like:
59
60- 🧭 [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers
61- πŸ”‹ [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support
62- πŸ“„ [__Worker HTML__][html] --- HTML templating and streaming response library
63- πŸ“¦ [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.
64- πŸ†— [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text
65- 🎏 [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...
66- πŸ₯ [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.
67- πŸ¦‘ [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.
68
69Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:
70- ✏️ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...
71- πŸ“ [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.
72- πŸ¦• [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Deno’s native HTTP server.
73
74[router]: https://workers.tools/router
75[middleware]: https://workers.tools/middleware
76[html]: https://workers.tools/html
77[kv-storage]: https://workers.tools/kv-storage
78[cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage
79[deno-kv-storage]: https://workers.tools/deno-kv-storage
80[kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill
81[response-creators]: https://workers.tools/response-creators
82[stream-response]: https://workers.tools/stream-response
83[json-fetch]: https://workers.tools/json-fetch
84[json-stream]: https://workers.tools/json-stream
85[request-cookie-store]: https://workers.tools/request-cookie-store
86[extendable-promise]: https://workers.tools/extendable-promise
87[html-rewriter]: https://workers.tools/html-rewriter
88[location-polyfill]: https://workers.tools/location-polyfill
89[deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter
90
91Fore more visit [workers.tools](https://workers.tools).