1 | # JSON Fetch
|
2 |
|
3 | A drop-in replacements for `fetch`, `Request`, and `Response` with first class support for JSON objects.
|
4 |
|
5 | Unlike other HTTP libraries, this one stays as close as possible to the original Fetch API,
|
6 | while improving the ergonomics the most common use case:
|
7 |
|
8 | Before:
|
9 |
|
10 | ```ts
|
11 | const response = await fetch('/some', {
|
12 | method: 'POST',
|
13 | body: JSON.stringify(json),
|
14 | headers: {
|
15 | 'Content-Type': 'application/json',
|
16 | },
|
17 | });
|
18 | ```
|
19 |
|
20 | After:
|
21 |
|
22 | ```ts
|
23 | import { JSONRequest } from '@worker-tools/json-fetch';
|
24 |
|
25 | const response = await fetch(new JSONRequest('/some', {
|
26 | method: 'POST',
|
27 | body: json,
|
28 | }));
|
29 | ```
|
30 |
|
31 | You can also use the updated `jsonFetch` function:
|
32 |
|
33 | ```ts
|
34 | import { jsonFetch as fetch } from '@worker-tools/json-fetch';
|
35 |
|
36 | const response = await fetch('/some', { method: 'POST', body: data })
|
37 | ```
|
38 |
|
39 | Note that previous use cases remain intact, i.e. posting `FormData`, `ReadableStream`, etc. as body works:
|
40 |
|
41 | ```ts
|
42 | const response = await fetch(new JSONRequest('/some', {
|
43 | method: 'POST',
|
44 | body: new FromData(form),
|
45 | }))
|
46 | ```
|
47 |
|
48 | This will send the body as form-data/multipart with correct content type header, as in the original Fetch API.
|
49 | Only 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 |
|
58 | If 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 |
|
69 | Worker 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 |
|
91 | Fore more visit [workers.tools](https://workers.tools).
|