UNPKG

4.15 kBMarkdownView Raw
1# Response Creators
2
3A collection of factory functions for [Fetch API](https://developer.mozilla.org/docs/Web/API/Response) `Response` types with pre-filled status and status-text headers for [well-known HTTP status codes](https://developer.mozilla.org/docs/Web/HTTP/Status).
4
5It is meant to be used in Service Workers and/or Cloudflare Workers.
6
7```js
8import { ok } from '@worker-tools/response-creators'
9
10self.addEventListener('fetch', event => event.respondWith(ok()))
11```
12
13For the most part, factory functions can be used like regular `Response` constructors, e.g.
14
15```js
16event.respondWith(
17 ok('Your custom body init', { headers: { 'Content-Type': 'text/plain' } })
18)
19```
20
21However, some provide a slightly different interface for enhanced usability. E.g. redirects (300, 301, 302, 303, 307, 308):
22
23```js
24event.respondWith(
25 seeOther(`/your-redirect-url`)
26)
27```
28
29(This will set the `Location` header to `/your-redirect-url`).
30
31**NOTE**: When using JSON response bodies, consider combining it with [`worker-tools/json-fetch`](https://github.com/worker-tools/json-fetch) like so:
32
33```js
34event.respondWith(
35 new JSONResponse({ error: '...' }, badRequest())
36)
37```
38
39Due to signature of the `Response` constructor, the opposite order (`badRequest(new JSONResponse({ error: '...' }))`) does not work!
40
41--------
42
43<p align="center"><a href="https://workers.tools"><img src="https://workers.tools/assets/img/logo.svg" width="100" height="100" /></a>
44<p align="center">This module is part of the Worker Tools collection<br/>⁕
45
46[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.
47
48If you liked this module, you might also like:
49
50- 🧭 [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers
51- πŸ”‹ [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support
52- πŸ“„ [__Worker HTML__][html] --- HTML templating and streaming response library
53- πŸ“¦ [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.
54- πŸ†— [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text
55- 🎏 [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...
56- πŸ₯ [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.
57- πŸ¦‘ [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.
58
59Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:
60- ✏️ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...
61- πŸ“ [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.
62- πŸ¦• [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Deno’s native HTTP server.
63
64[router]: https://workers.tools/router
65[middleware]: https://workers.tools/middleware
66[html]: https://workers.tools/html
67[kv-storage]: https://workers.tools/kv-storage
68[cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage
69[deno-kv-storage]: https://workers.tools/deno-kv-storage
70[kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill
71[response-creators]: https://workers.tools/response-creators
72[stream-response]: https://workers.tools/stream-response
73[json-fetch]: https://workers.tools/json-fetch
74[json-stream]: https://workers.tools/json-stream
75[request-cookie-store]: https://workers.tools/request-cookie-store
76[extendable-promise]: https://workers.tools/extendable-promise
77[html-rewriter]: https://workers.tools/html-rewriter
78[location-polyfill]: https://workers.tools/location-polyfill
79[deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter
80
81Fore more visit [workers.tools](https://workers.tools).