UNPKG

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