UNPKG

1.95 kBJavaScriptView Raw
1// deno-lint-ignore-file no-explicit-any
2import { StreamResponse, StreamRequest } from "@worker-tools/stream-response";
3import { asyncIterToStream } from 'whatwg-stream-to-async-iter';
4import { JSONStringifyReadable, isAsyncIterable } from './json-stringify.js';
5const toBody = (x) => x instanceof ReadableStream
6 ? x
7 : isAsyncIterable(x)
8 ? asyncIterToStream(x)
9 : new JSONStringifyReadable(x);
10export class JSONStreamRequest extends StreamRequest {
11 constructor(input, init) {
12 const { headers: _headers, body: _body, ...rest } = init || {};
13 const body = toBody(_body);
14 const headers = new Headers(_headers);
15 if (!headers.has('Content-Type') && _body != null)
16 headers.set('Content-Type', JSONStreamRequest.contentType);
17 if (!headers.has('Accept'))
18 headers.set('Accept', JSONStreamRequest.accept);
19 super(input instanceof URL ? input.href : input, { headers, body, ...rest });
20 }
21}
22Object.defineProperty(JSONStreamRequest, "contentType", {
23 enumerable: true,
24 configurable: true,
25 writable: true,
26 value: 'application/json;charset=UTF-8'
27});
28Object.defineProperty(JSONStreamRequest, "accept", {
29 enumerable: true,
30 configurable: true,
31 writable: true,
32 value: 'application/json, text/plain, */*'
33});
34export class JSONStreamResponse extends StreamResponse {
35 constructor(body, init) {
36 const { headers: _headers, ...rest } = init || {};
37 const _body = toBody(body);
38 const headers = new Headers(_headers);
39 if (!headers.has('Content-Type') && body != null)
40 headers.set('Content-Type', JSONStreamResponse.contentType);
41 super(_body, { headers, ...rest });
42 }
43}
44Object.defineProperty(JSONStreamResponse, "contentType", {
45 enumerable: true,
46 configurable: true,
47 writable: true,
48 value: 'application/json;charset=UTF-8'
49});
50//# sourceMappingURL=json-fetch.js.map
\No newline at end of file