UNPKG

3.33 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __exportStar = (this && this.__exportStar) || function(m, exports) {
14 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15};
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.jsonFetch = exports.JSONResponse = exports.JSONRequest = void 0;
18/**
19 * Tests is the argument is a Fetch API `BodyInit`.
20 * Assumed to be `JSONValue` otherwise.
21 */
22function isBodyInit(b) {
23 return (b == null ||
24 typeof b === 'string' ||
25 (typeof Blob !== 'undefined' && b instanceof Blob) ||
26 (typeof ArrayBuffer !== 'undefined' && (b instanceof ArrayBuffer || ArrayBuffer.isView(b))) ||
27 (typeof FormData !== 'undefined' && b instanceof FormData) ||
28 (typeof URLSearchParams !== 'undefined' && b instanceof URLSearchParams) ||
29 (typeof ReadableStream !== 'undefined' && b instanceof ReadableStream));
30}
31class JSONRequest extends Request {
32 constructor(input, init, replacer, space) {
33 const { headers: _headers, body: _body, ..._init } = init || {};
34 let isBI;
35 const body = (isBI = isBodyInit(_body))
36 ? _body
37 : JSON.stringify(_body, replacer, space);
38 const headers = new Headers(_headers);
39 if (!headers.has('Content-Type') && !isBI)
40 headers.set('Content-Type', JSONRequest.contentType);
41 if (!headers.has('Accept'))
42 headers.set('Accept', JSONRequest.accept);
43 super(input instanceof URL ? input.href : input, { headers, body, ..._init });
44 }
45}
46exports.JSONRequest = JSONRequest;
47Object.defineProperty(JSONRequest, "contentType", {
48 enumerable: true,
49 configurable: true,
50 writable: true,
51 value: 'application/json;charset=UTF-8'
52});
53Object.defineProperty(JSONRequest, "accept", {
54 enumerable: true,
55 configurable: true,
56 writable: true,
57 value: 'application/json, text/plain, */*'
58});
59class JSONResponse extends Response {
60 constructor(body, init, replacer, space) {
61 const { headers: _headers, ..._init } = init || {};
62 let isBI;
63 const _body = (isBI = isBodyInit(body))
64 ? body
65 : JSON.stringify(body, replacer, space);
66 const headers = new Headers(_headers);
67 if (!headers.has('Content-Type') && !isBI)
68 headers.set('Content-Type', JSONResponse.contentType);
69 super(_body, { headers, ..._init });
70 }
71}
72exports.JSONResponse = JSONResponse;
73Object.defineProperty(JSONResponse, "contentType", {
74 enumerable: true,
75 configurable: true,
76 writable: true,
77 value: 'application/json;charset=UTF-8'
78});
79function jsonFetch(...args) {
80 return fetch(new JSONRequest(...args));
81}
82exports.jsonFetch = jsonFetch;
83__exportStar(require("./search-params-url.js"), exports);
84//# sourceMappingURL=index.js.map
\No newline at end of file