@sapphire/fetch 3.0.3-next.f4635450.0 → 3.0.3-next.f63a9a11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -16
- package/dist/cjs/index.cjs +26 -28
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +107 -10
- package/dist/esm/index.d.mts +107 -10
- package/dist/esm/index.mjs +26 -28
- package/dist/esm/index.mjs.map +1 -1
- package/dist/iife/index.global.js +26 -28
- package/dist/iife/index.global.js.map +1 -1
- package/package.json +13 -12
package/README.md
CHANGED
|
@@ -15,16 +15,15 @@
|
|
|
15
15
|
|
|
16
16
|
**Table of Contents**
|
|
17
17
|
|
|
18
|
-
- [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
- [
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
- [Contributors ✨](#contributors-%E2%9C%A8)
|
|
18
|
+
- [Description](#description)
|
|
19
|
+
- [Features](#features)
|
|
20
|
+
- [Installation](#installation)
|
|
21
|
+
- [Usage](#usage)
|
|
22
|
+
- [`GET`ting JSON data](#getting-json-data)
|
|
23
|
+
- [`GET`ting Buffer data (images, etc.)](#getting-buffer-data-images-etc)
|
|
24
|
+
- [`POST`ing JSON data](#posting-json-data)
|
|
25
|
+
- [Buy us some doughnuts](#buy-us-some-doughnuts)
|
|
26
|
+
- [Contributors](#contributors)
|
|
28
27
|
|
|
29
28
|
## Description
|
|
30
29
|
|
|
@@ -49,9 +48,31 @@ npm install @sapphire/fetch
|
|
|
49
48
|
|
|
50
49
|
## Usage
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
> [!NOTE]
|
|
52
|
+
> While this section uses `import`, it maps 1:1 with CommonJS' require syntax. For example,
|
|
53
|
+
>
|
|
54
|
+
> ```ts
|
|
55
|
+
> import { fetch } from '@sapphire/fetch';
|
|
56
|
+
> ```
|
|
57
|
+
>
|
|
58
|
+
> is the same as
|
|
59
|
+
>
|
|
60
|
+
> ```ts
|
|
61
|
+
> const { fetch } = require('@sapphire/fetch');
|
|
62
|
+
> ```
|
|
63
|
+
|
|
64
|
+
> [!IMPORTANT]
|
|
65
|
+
> When providing a serializable object to the `body` option, `@sapphire/fetch` will automatically call `JSON.stringify` on the object. This means you can pass an object directly to the `body` option without having to call `JSON.stringify` yourself.
|
|
66
|
+
> If the body is _not_ serializable (such as a `File`, `Buffer`, or `Blob`), the body will be sent as-is.
|
|
67
|
+
> Serializability is calculated based on:
|
|
68
|
+
>
|
|
69
|
+
> - If the body is `null`
|
|
70
|
+
> - If the body's `.constructor` property is `undefined`
|
|
71
|
+
> - If the body's `.constructor.name` property is `Object`
|
|
72
|
+
> - If the body has a function property named `toJSON`
|
|
73
|
+
|
|
74
|
+
> [!WARNING]
|
|
75
|
+
> Because `@sapphire/fetch` aims to be as close to global fetch as possible, it doesn't support proxy options that a library like undici does. If you want to use a proxy, you should use undici directly.
|
|
55
76
|
|
|
56
77
|
### `GET`ting JSON data
|
|
57
78
|
|
|
@@ -100,9 +121,7 @@ const responseData = await fetch(
|
|
|
100
121
|
headers: {
|
|
101
122
|
'Content-Type': 'application/json'
|
|
102
123
|
},
|
|
103
|
-
body:
|
|
104
|
-
name: 'John Doe'
|
|
105
|
-
})
|
|
124
|
+
body: { name: 'John Doe' }
|
|
106
125
|
},
|
|
107
126
|
FetchResultTypes.JSON
|
|
108
127
|
);
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,30 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
+
var __typeError = (msg) => {
|
|
5
|
+
throw TypeError(msg);
|
|
6
|
+
};
|
|
4
7
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
8
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
-
var __publicField = (obj, key, value) =>
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var
|
|
11
|
-
if (!member.has(obj))
|
|
12
|
-
throw TypeError("Cannot " + msg);
|
|
13
|
-
};
|
|
14
|
-
var __privateGet = (obj, member, getter) => {
|
|
15
|
-
__accessCheck(obj, member, "read from private field");
|
|
16
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
17
|
-
};
|
|
18
|
-
var __privateAdd = (obj, member, value) => {
|
|
19
|
-
if (member.has(obj))
|
|
20
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
21
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
22
|
-
};
|
|
23
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
24
|
-
__accessCheck(obj, member, "write to private field");
|
|
25
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
26
|
-
return value;
|
|
27
|
-
};
|
|
9
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
10
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
11
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
12
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
28
14
|
|
|
29
15
|
// src/lib/QueryError.ts
|
|
30
16
|
var _json;
|
|
@@ -39,7 +25,7 @@ var _QueryError = class _QueryError extends Error {
|
|
|
39
25
|
__publicField(this, "body");
|
|
40
26
|
/** The original {@link Response} object */
|
|
41
27
|
__publicField(this, "response");
|
|
42
|
-
__privateAdd(this, _json
|
|
28
|
+
__privateAdd(this, _json);
|
|
43
29
|
this.url = url;
|
|
44
30
|
this.code = code;
|
|
45
31
|
this.body = body;
|
|
@@ -122,13 +108,15 @@ async function fetch(url, options, type) {
|
|
|
122
108
|
type = "json" /* JSON */;
|
|
123
109
|
}
|
|
124
110
|
let { body } = options;
|
|
125
|
-
if (body
|
|
111
|
+
if (shouldJsonStringify(body)) {
|
|
126
112
|
body = JSON.stringify(body);
|
|
127
113
|
}
|
|
128
114
|
const stringUrl = String(url);
|
|
129
|
-
const result = await globalThis.fetch(stringUrl, {
|
|
130
|
-
|
|
131
|
-
|
|
115
|
+
const result = await globalThis.fetch(stringUrl, {
|
|
116
|
+
...options,
|
|
117
|
+
body
|
|
118
|
+
});
|
|
119
|
+
if (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());
|
|
132
120
|
switch (type) {
|
|
133
121
|
case "result" /* Result */:
|
|
134
122
|
return result;
|
|
@@ -145,11 +133,21 @@ async function fetch(url, options, type) {
|
|
|
145
133
|
}
|
|
146
134
|
}
|
|
147
135
|
__name(fetch, "fetch");
|
|
136
|
+
function shouldJsonStringify(value) {
|
|
137
|
+
if (typeof value !== "object") return false;
|
|
138
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(value)) return false;
|
|
139
|
+
if (value === null) return true;
|
|
140
|
+
if (value.constructor === void 0) return true;
|
|
141
|
+
if (value.constructor === Object) return true;
|
|
142
|
+
if ("toJSON" in value && typeof value.toJSON === "function") return true;
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
__name(shouldJsonStringify, "shouldJsonStringify");
|
|
148
146
|
|
|
149
147
|
exports.FetchMediaContentTypes = FetchMediaContentTypes;
|
|
150
148
|
exports.FetchMethods = FetchMethods;
|
|
151
149
|
exports.FetchResultTypes = FetchResultTypes;
|
|
152
150
|
exports.QueryError = QueryError;
|
|
153
151
|
exports.fetch = fetch;
|
|
154
|
-
//# sourceMappingURL=
|
|
152
|
+
//# sourceMappingURL=index.cjs.map
|
|
155
153
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/QueryError.ts","../../src/lib/types.ts","../../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAMO,IAAM,cAAN,MAAM,oBAAmB,MAAM;AAAA,EAY9B,YAAY,KAAa,MAAc,UAAoB,MAAc;AAC/E,UAAM,sBAAsB,GAAG,eAAe,IAAI,GAAG;AAXtD;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAIC,SAAK,MAAM;AACX,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,uBAAK,OAAQ;AAAA,EACd;AAAA,EAEO,SAAS;AACf,WAAO,mBAAK,UAAU,mBAAK,OAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACxD;AACD;AAdC;AAVqC;AAA/B,IAAM,aAAN;;;ACAA,IAAK,mBAAL,kBAAKA,sBAAL;AAON,EAAAA,kBAAA,UAAO;AAMP,EAAAA,kBAAA,YAAS;AAKT,EAAAA,kBAAA,UAAO;AAIP,EAAAA,kBAAA,UAAO;AAIP,EAAAA,kBAAA,YAAS;AA1BE,SAAAA;AAAA,GAAA;AAgCL,IAAK,eAAL,kBAAKC,kBAAL;AAKN,EAAAA,cAAA,SAAM;AAKN,EAAAA,cAAA,UAAO;AAKP,EAAAA,cAAA,UAAO;AAKP,EAAAA,cAAA,SAAM;AAKN,EAAAA,cAAA,YAAS;AAKT,EAAAA,cAAA,aAAU;AAKV,EAAAA,cAAA,aAAU;AAKV,EAAAA,cAAA,WAAQ;AAKR,EAAAA,cAAA,WAAQ;AA7CG,SAAAA;AAAA,GAAA;AAqDL,IAAK,yBAAL,kBAAKC,4BAAL;AAKN,EAAAA,wBAAA,cAAW;A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,WAAW,EAAE,GAAG,SAAS,KAAK,CAAC;AAC/E,MAAI,CAAC,OAAO;AAAI,UAAM,IAAI,WAAW,WAAW,OAAO,QAAQ,QAAQ,MAAM,OAAO,MAAM,EAAE,KAAK,CAAC;AAElG,UAAQ,MAAM;AAAA,IACb;AACC,aAAO;AAAA,IACR;AACC,aAAO,OAAO,KAAK,OAAO,MAAM,OAAO,KAAK,GAAG,YAAY,CAAC;AAAA,IAC7D;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,YAAM,IAAI,MAAM,iBAAiB,IAAI,GAAG;AAAA,EAC1C;AACD;AArCsB","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/lib/QueryError.ts","../../src/lib/types.ts","../../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":";;;;;;;;;;;;;;;AAAA,IAAA,KAAA,CAAA;AAMO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmB,KAAM,CAAA;AAAA,EAY9B,WAAY,CAAA,GAAA,EAAa,IAAc,EAAA,QAAA,EAAoB,IAAc,EAAA;AAC/E,IAAA,KAAA,CAAM,CAAsB,mBAAA,EAAA,GAAG,CAAe,YAAA,EAAA,IAAI,CAAG,CAAA,CAAA,CAAA,CAAA;AAXtD;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CAAA;AAEhB;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAEhB;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAEhB;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,UAAA,CAAA,CAAA;AAEhB,IAAA,YAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CAAA;AAIC,IAAA,IAAA,CAAK,GAAM,GAAA,GAAA,CAAA;AACX,IAAA,IAAA,CAAK,IAAO,GAAA,IAAA,CAAA;AACZ,IAAA,IAAA,CAAK,IAAO,GAAA,IAAA,CAAA;AACZ,IAAA,IAAA,CAAK,QAAW,GAAA,QAAA,CAAA;AAChB,IAAA,YAAA,CAAA,IAAA,EAAK,KAAQ,EAAA,IAAA,CAAA,CAAA;AAAA,GACd;AAAA,EAEO,MAAS,GAA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AAmB,OAAA,IAAA,CAAA;AAGpE,EAAO,OAAA,KAAA,CAAA;AACR,CAAA;AAjBS,MAAA,CAAA,mBAAA,EAAA,qBAAA,CAAA","file":"index.cjs","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: RequestInit['body'] | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.\n */\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.\n */\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The response body as a raw `string`.\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The response body as a raw `string`.\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be {@linkplain https://nodejs.org/api/buffer.html `Buffer`}.\n * - When using `FetchResultTypes.Blob` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`}.\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript)\n */\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be {@linkplain https://nodejs.org/api/buffer.html `Buffer`}.\n * - When using `FetchResultTypes.Blob` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`}.\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript)\n */\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (shouldJsonStringify(body)) {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, {\n\t\t...options,\n\t\tbody: body as RequestInit['body']\n\t});\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n\n/**\n * Determines whether a value should be stringified as JSON.\n *\n * @param value - The value to check.\n * @returns A boolean indicating whether the value should be stringified as JSON.\n */\nfunction shouldJsonStringify(value: unknown) {\n\t// If the value is not an object, it should not be stringified\n\tif (typeof value !== 'object') return false;\n\t// Buffers should not be stringified\n\tif (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) return false;\n\n\t// null object\n\tif (value === null) return true;\n\t// Object.create(null)\n\tif (value.constructor === undefined) return true;\n\t// Plain objects\n\tif (value.constructor === Object) return true;\n\t// Has toJSON method\n\tif ('toJSON' in value && typeof value.toJSON === 'function') return true;\n\n\t// Anything else (such as streams or unserializables)\n\treturn false;\n}\n"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -262,34 +262,131 @@ declare enum FetchMediaContentTypes {
|
|
|
262
262
|
XML = "application/xml"
|
|
263
263
|
}
|
|
264
264
|
interface RequestOptions extends Omit<RequestInit, 'body'> {
|
|
265
|
-
body?:
|
|
265
|
+
body?: RequestInit['body'] | Record<any, any>;
|
|
266
266
|
}
|
|
267
267
|
|
|
268
268
|
/// <reference lib="dom" />
|
|
269
269
|
|
|
270
270
|
/**
|
|
271
271
|
* Performs an HTTP(S) fetch
|
|
272
|
-
*
|
|
272
|
+
*
|
|
273
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
273
274
|
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
274
|
-
* @param
|
|
275
|
-
*
|
|
276
|
-
* @returns
|
|
277
|
-
* - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.
|
|
278
|
-
* - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).
|
|
279
|
-
* - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
280
|
-
* - When using `FetchResultTypes.Text` the return type will be a `string`
|
|
281
|
-
* - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)
|
|
275
|
+
* @param type - The {@link FetchResultTypes}
|
|
276
|
+
*
|
|
277
|
+
* @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.
|
|
282
278
|
*/
|
|
283
279
|
declare function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;
|
|
280
|
+
/**
|
|
281
|
+
* Performs an HTTP(S) fetch
|
|
282
|
+
*
|
|
283
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
284
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
285
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
286
|
+
* @param type - The {@link FetchResultTypes}
|
|
287
|
+
*
|
|
288
|
+
* @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.
|
|
289
|
+
*/
|
|
284
290
|
declare function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;
|
|
291
|
+
/**
|
|
292
|
+
* Performs an HTTP(S) fetch
|
|
293
|
+
*
|
|
294
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
295
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
296
|
+
* @param type - The {@link FetchResultTypes}
|
|
297
|
+
*
|
|
298
|
+
* @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body
|
|
299
|
+
*/
|
|
285
300
|
declare function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;
|
|
301
|
+
/**
|
|
302
|
+
* Performs an HTTP(S) fetch
|
|
303
|
+
*
|
|
304
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
305
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
306
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
307
|
+
* @param type - The {@link FetchResultTypes}
|
|
308
|
+
*
|
|
309
|
+
* @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body
|
|
310
|
+
*/
|
|
286
311
|
declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;
|
|
312
|
+
/**
|
|
313
|
+
* Performs an HTTP(S) fetch
|
|
314
|
+
*
|
|
315
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
316
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
317
|
+
* @param type - The {@link FetchResultTypes}
|
|
318
|
+
*
|
|
319
|
+
* @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body
|
|
320
|
+
*/
|
|
287
321
|
declare function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;
|
|
322
|
+
/**
|
|
323
|
+
* Performs an HTTP(S) fetch
|
|
324
|
+
*
|
|
325
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
326
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
327
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
328
|
+
* @param type - The {@link FetchResultTypes}
|
|
329
|
+
*
|
|
330
|
+
* @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body
|
|
331
|
+
*/
|
|
288
332
|
declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;
|
|
333
|
+
/**
|
|
334
|
+
* Performs an HTTP(S) fetch
|
|
335
|
+
*
|
|
336
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
337
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
338
|
+
* @param type - The {@link FetchResultTypes}
|
|
339
|
+
*
|
|
340
|
+
* @returns The response body as a raw `string`.
|
|
341
|
+
*/
|
|
289
342
|
declare function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;
|
|
343
|
+
/**
|
|
344
|
+
* Performs an HTTP(S) fetch
|
|
345
|
+
*
|
|
346
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
347
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
348
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
349
|
+
* @param type - The {@link FetchResultTypes}
|
|
350
|
+
*
|
|
351
|
+
* @returns The response body as a raw `string`.
|
|
352
|
+
*/
|
|
290
353
|
declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;
|
|
354
|
+
/**
|
|
355
|
+
* Performs an HTTP(S) fetch
|
|
356
|
+
*
|
|
357
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
358
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
359
|
+
* @param type - The {@link FetchResultTypes}
|
|
360
|
+
*
|
|
361
|
+
* @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).
|
|
362
|
+
*/
|
|
291
363
|
declare function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;
|
|
364
|
+
/**
|
|
365
|
+
* Performs an HTTP(S) fetch
|
|
366
|
+
*
|
|
367
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
368
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
369
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
370
|
+
* @param type - The {@link FetchResultTypes}
|
|
371
|
+
*
|
|
372
|
+
* @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).
|
|
373
|
+
*/
|
|
292
374
|
declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;
|
|
375
|
+
/**
|
|
376
|
+
* Performs an HTTP(S) fetch
|
|
377
|
+
*
|
|
378
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
379
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
380
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
381
|
+
* @param type - The {@link FetchResultTypes}
|
|
382
|
+
*
|
|
383
|
+
* @returns The return type is determined by the provided `type`.
|
|
384
|
+
* - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.
|
|
385
|
+
* - When using `FetchResultTypes.Buffer` the return type will be {@linkplain https://nodejs.org/api/buffer.html `Buffer`}.
|
|
386
|
+
* - When using `FetchResultTypes.Blob` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`}.
|
|
387
|
+
* - When using `FetchResultTypes.Text` the return type will be a `string`
|
|
388
|
+
* - When using `FetchResultTypes.Result` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript)
|
|
389
|
+
*/
|
|
293
390
|
declare function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;
|
|
294
391
|
|
|
295
392
|
/// <reference lib="dom" />
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -262,34 +262,131 @@ declare enum FetchMediaContentTypes {
|
|
|
262
262
|
XML = "application/xml"
|
|
263
263
|
}
|
|
264
264
|
interface RequestOptions extends Omit<RequestInit, 'body'> {
|
|
265
|
-
body?:
|
|
265
|
+
body?: RequestInit['body'] | Record<any, any>;
|
|
266
266
|
}
|
|
267
267
|
|
|
268
268
|
/// <reference lib="dom" />
|
|
269
269
|
|
|
270
270
|
/**
|
|
271
271
|
* Performs an HTTP(S) fetch
|
|
272
|
-
*
|
|
272
|
+
*
|
|
273
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
273
274
|
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
274
|
-
* @param
|
|
275
|
-
*
|
|
276
|
-
* @returns
|
|
277
|
-
* - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.
|
|
278
|
-
* - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).
|
|
279
|
-
* - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
280
|
-
* - When using `FetchResultTypes.Text` the return type will be a `string`
|
|
281
|
-
* - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)
|
|
275
|
+
* @param type - The {@link FetchResultTypes}
|
|
276
|
+
*
|
|
277
|
+
* @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.
|
|
282
278
|
*/
|
|
283
279
|
declare function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;
|
|
280
|
+
/**
|
|
281
|
+
* Performs an HTTP(S) fetch
|
|
282
|
+
*
|
|
283
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
284
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
285
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
286
|
+
* @param type - The {@link FetchResultTypes}
|
|
287
|
+
*
|
|
288
|
+
* @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.
|
|
289
|
+
*/
|
|
284
290
|
declare function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;
|
|
291
|
+
/**
|
|
292
|
+
* Performs an HTTP(S) fetch
|
|
293
|
+
*
|
|
294
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
295
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
296
|
+
* @param type - The {@link FetchResultTypes}
|
|
297
|
+
*
|
|
298
|
+
* @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body
|
|
299
|
+
*/
|
|
285
300
|
declare function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;
|
|
301
|
+
/**
|
|
302
|
+
* Performs an HTTP(S) fetch
|
|
303
|
+
*
|
|
304
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
305
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
306
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
307
|
+
* @param type - The {@link FetchResultTypes}
|
|
308
|
+
*
|
|
309
|
+
* @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body
|
|
310
|
+
*/
|
|
286
311
|
declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;
|
|
312
|
+
/**
|
|
313
|
+
* Performs an HTTP(S) fetch
|
|
314
|
+
*
|
|
315
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
316
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
317
|
+
* @param type - The {@link FetchResultTypes}
|
|
318
|
+
*
|
|
319
|
+
* @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body
|
|
320
|
+
*/
|
|
287
321
|
declare function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;
|
|
322
|
+
/**
|
|
323
|
+
* Performs an HTTP(S) fetch
|
|
324
|
+
*
|
|
325
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
326
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
327
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
328
|
+
* @param type - The {@link FetchResultTypes}
|
|
329
|
+
*
|
|
330
|
+
* @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body
|
|
331
|
+
*/
|
|
288
332
|
declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;
|
|
333
|
+
/**
|
|
334
|
+
* Performs an HTTP(S) fetch
|
|
335
|
+
*
|
|
336
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
337
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
338
|
+
* @param type - The {@link FetchResultTypes}
|
|
339
|
+
*
|
|
340
|
+
* @returns The response body as a raw `string`.
|
|
341
|
+
*/
|
|
289
342
|
declare function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;
|
|
343
|
+
/**
|
|
344
|
+
* Performs an HTTP(S) fetch
|
|
345
|
+
*
|
|
346
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
347
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
348
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
349
|
+
* @param type - The {@link FetchResultTypes}
|
|
350
|
+
*
|
|
351
|
+
* @returns The response body as a raw `string`.
|
|
352
|
+
*/
|
|
290
353
|
declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;
|
|
354
|
+
/**
|
|
355
|
+
* Performs an HTTP(S) fetch
|
|
356
|
+
*
|
|
357
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
358
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
359
|
+
* @param type - The {@link FetchResultTypes}
|
|
360
|
+
*
|
|
361
|
+
* @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).
|
|
362
|
+
*/
|
|
291
363
|
declare function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;
|
|
364
|
+
/**
|
|
365
|
+
* Performs an HTTP(S) fetch
|
|
366
|
+
*
|
|
367
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
368
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
369
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
370
|
+
* @param type - The {@link FetchResultTypes}
|
|
371
|
+
*
|
|
372
|
+
* @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).
|
|
373
|
+
*/
|
|
292
374
|
declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;
|
|
375
|
+
/**
|
|
376
|
+
* Performs an HTTP(S) fetch
|
|
377
|
+
*
|
|
378
|
+
* @param url - The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
379
|
+
* `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
|
380
|
+
* @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)
|
|
381
|
+
* @param type - The {@link FetchResultTypes}
|
|
382
|
+
*
|
|
383
|
+
* @returns The return type is determined by the provided `type`.
|
|
384
|
+
* - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.
|
|
385
|
+
* - When using `FetchResultTypes.Buffer` the return type will be {@linkplain https://nodejs.org/api/buffer.html `Buffer`}.
|
|
386
|
+
* - When using `FetchResultTypes.Blob` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`}.
|
|
387
|
+
* - When using `FetchResultTypes.Text` the return type will be a `string`
|
|
388
|
+
* - When using `FetchResultTypes.Result` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript)
|
|
389
|
+
*/
|
|
293
390
|
declare function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;
|
|
294
391
|
|
|
295
392
|
/// <reference lib="dom" />
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,28 +1,14 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
+
var __typeError = (msg) => {
|
|
3
|
+
throw TypeError(msg);
|
|
4
|
+
};
|
|
2
5
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
6
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
var __publicField = (obj, key, value) =>
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
if (!member.has(obj))
|
|
10
|
-
throw TypeError("Cannot " + msg);
|
|
11
|
-
};
|
|
12
|
-
var __privateGet = (obj, member, getter) => {
|
|
13
|
-
__accessCheck(obj, member, "read from private field");
|
|
14
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
15
|
-
};
|
|
16
|
-
var __privateAdd = (obj, member, value) => {
|
|
17
|
-
if (member.has(obj))
|
|
18
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
19
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
20
|
-
};
|
|
21
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
22
|
-
__accessCheck(obj, member, "write to private field");
|
|
23
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
24
|
-
return value;
|
|
25
|
-
};
|
|
7
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
9
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
10
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
11
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
26
12
|
|
|
27
13
|
// src/lib/QueryError.ts
|
|
28
14
|
var _json;
|
|
@@ -37,7 +23,7 @@ var _QueryError = class _QueryError extends Error {
|
|
|
37
23
|
__publicField(this, "body");
|
|
38
24
|
/** The original {@link Response} object */
|
|
39
25
|
__publicField(this, "response");
|
|
40
|
-
__privateAdd(this, _json
|
|
26
|
+
__privateAdd(this, _json);
|
|
41
27
|
this.url = url;
|
|
42
28
|
this.code = code;
|
|
43
29
|
this.body = body;
|
|
@@ -120,13 +106,15 @@ async function fetch(url, options, type) {
|
|
|
120
106
|
type = "json" /* JSON */;
|
|
121
107
|
}
|
|
122
108
|
let { body } = options;
|
|
123
|
-
if (body
|
|
109
|
+
if (shouldJsonStringify(body)) {
|
|
124
110
|
body = JSON.stringify(body);
|
|
125
111
|
}
|
|
126
112
|
const stringUrl = String(url);
|
|
127
|
-
const result = await globalThis.fetch(stringUrl, {
|
|
128
|
-
|
|
129
|
-
|
|
113
|
+
const result = await globalThis.fetch(stringUrl, {
|
|
114
|
+
...options,
|
|
115
|
+
body
|
|
116
|
+
});
|
|
117
|
+
if (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());
|
|
130
118
|
switch (type) {
|
|
131
119
|
case "result" /* Result */:
|
|
132
120
|
return result;
|
|
@@ -143,7 +131,17 @@ async function fetch(url, options, type) {
|
|
|
143
131
|
}
|
|
144
132
|
}
|
|
145
133
|
__name(fetch, "fetch");
|
|
134
|
+
function shouldJsonStringify(value) {
|
|
135
|
+
if (typeof value !== "object") return false;
|
|
136
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(value)) return false;
|
|
137
|
+
if (value === null) return true;
|
|
138
|
+
if (value.constructor === void 0) return true;
|
|
139
|
+
if (value.constructor === Object) return true;
|
|
140
|
+
if ("toJSON" in value && typeof value.toJSON === "function") return true;
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
__name(shouldJsonStringify, "shouldJsonStringify");
|
|
146
144
|
|
|
147
145
|
export { FetchMediaContentTypes, FetchMethods, FetchResultTypes, QueryError, fetch };
|
|
148
|
-
//# sourceMappingURL=
|
|
146
|
+
//# sourceMappingURL=index.mjs.map
|
|
149
147
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/QueryError.ts","../../src/lib/types.ts","../../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAMO,IAAM,cAAN,MAAM,oBAAmB,MAAM;AAAA,EAY9B,YAAY,KAAa,MAAc,UAAoB,MAAc;AAC/E,UAAM,sBAAsB,GAAG,eAAe,IAAI,GAAG;AAXtD;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAIC,SAAK,MAAM;AACX,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,uBAAK,OAAQ;AAAA,EACd;AAAA,EAEO,SAAS;AACf,WAAO,mBAAK,UAAU,mBAAK,OAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACxD;AACD;AAdC;AAVqC;AAA/B,IAAM,aAAN;;;ACAA,IAAK,mBAAL,kBAAKA,sBAAL;AAON,EAAAA,kBAAA,UAAO;AAMP,EAAAA,kBAAA,YAAS;AAKT,EAAAA,kBAAA,UAAO;AAIP,EAAAA,kBAAA,UAAO;AAIP,EAAAA,kBAAA,YAAS;AA1BE,SAAAA;AAAA,GAAA;AAgCL,IAAK,eAAL,kBAAKC,kBAAL;AAKN,EAAAA,cAAA,SAAM;AAKN,EAAAA,cAAA,UAAO;AAKP,EAAAA,cAAA,UAAO;AAKP,EAAAA,cAAA,SAAM;AAKN,E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AAO,SAAS,aAAa;AACvC;AAAA,EACD;AAEA,MAAI,EAAE,KAAK,IAAI;AAEf,MAAI,QAAQ,OAAO,SAAS,UAAU;AACrC,WAAO,KAAK,UAAU,IAAI;AAAA,EAC3B;AAGA,QAAM,YAAY,OAAO,GAAG;AAE5B,QAAM,SAAmB,MAAM,WAAW,MAAM,WAAW,EAAE,GAAG,SAAS,KAAK,CAAC;AAC/E,MAAI,CAAC,OAAO;AAAI,UAAM,IAAI,WAAW,WAAW,OAAO,QAAQ,QAAQ,MAAM,OAAO,MAAM,EAAE,KAAK,CAAC;AAElG,UAAQ,MAAM;AAAA,IACb;AACC,aAAO;AAAA,IACR;AACC,aAAO,OAAO,KAAK,OAAO,MAAM,OAAO,KAAK,GAAG,YAAY,CAAC;AAAA,IAC7D;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,YAAM,IAAI,MAAM,iBAAiB,IAAI,GAAG;AAAA,EAC1C;AACD;AArCsB","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/lib/QueryError.ts","../../src/lib/types.ts","../../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":";;;;;;;;;;;;;AAAA,IAAA,KAAA,CAAA;AAMO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmB,KAAM,CAAA;AAAA,EAY9B,WAAY,CAAA,GAAA,EAAa,IAAc,EAAA,QAAA,EAAoB,IAAc,EAAA;AAC/E,IAAA,KAAA,CAAM,CAAsB,mBAAA,EAAA,GAAG,CAAe,YAAA,EAAA,IAAI,CAAG,CAAA,CAAA,CAAA,CAAA;AAXtD;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CAAA;AAEhB;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAEhB;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAEhB;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,UAAA,CAAA,CAAA;AAEhB,IAAA,YAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CAAA;AAIC,IAAA,IAAA,CAAK,GAAM,GAAA,GAAA,CAAA;AACX,IAAA,IAAA,CAAK,IAAO,GAAA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AA,KAAA,CAAM,WAAgB,KAAA,KAAA,CAAA,EAAkB,OAAA,IAAA,CAAA;AAE5C,EAAI,IAAA,KAAA,CAAM,WAAgB,KAAA,MAAA,EAAe,OAAA,IAAA,CAAA;AAEzC,EAAA,IAAI,YAAY,KAAS,IAAA,OAAO,KAAM,CAAA,MAAA,KAAW,YAAmB,OAAA,IAAA,CAAA;AAGpE,EAAO,OAAA,KAAA,CAAA;AACR,CAAA;AAjBS,MAAA,CAAA,mBAAA,EAAA,qBAAA,CAAA","file":"index.mjs","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: RequestInit['body'] | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.\n */\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.\n */\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The response body as a raw `string`.\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The response body as a raw `string`.\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be {@linkplain https://nodejs.org/api/buffer.html `Buffer`}.\n * - When using `FetchResultTypes.Blob` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`}.\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript)\n */\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be {@linkplain https://nodejs.org/api/buffer.html `Buffer`}.\n * - When using `FetchResultTypes.Blob` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`}.\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript)\n */\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (shouldJsonStringify(body)) {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, {\n\t\t...options,\n\t\tbody: body as RequestInit['body']\n\t});\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n\n/**\n * Determines whether a value should be stringified as JSON.\n *\n * @param value - The value to check.\n * @returns A boolean indicating whether the value should be stringified as JSON.\n */\nfunction shouldJsonStringify(value: unknown) {\n\t// If the value is not an object, it should not be stringified\n\tif (typeof value !== 'object') return false;\n\t// Buffers should not be stringified\n\tif (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) return false;\n\n\t// null object\n\tif (value === null) return true;\n\t// Object.create(null)\n\tif (value.constructor === undefined) return true;\n\t// Plain objects\n\tif (value.constructor === Object) return true;\n\t// Has toJSON method\n\tif ('toJSON' in value && typeof value.toJSON === 'function') return true;\n\n\t// Anything else (such as streams or unserializables)\n\treturn false;\n}\n"]}
|
|
@@ -2,30 +2,16 @@ var SapphireFetch = (function (exports) {
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
|
+
var __typeError = (msg) => {
|
|
6
|
+
throw TypeError(msg);
|
|
7
|
+
};
|
|
5
8
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
9
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
-
var __publicField = (obj, key, value) =>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var
|
|
12
|
-
if (!member.has(obj))
|
|
13
|
-
throw TypeError("Cannot " + msg);
|
|
14
|
-
};
|
|
15
|
-
var __privateGet = (obj, member, getter) => {
|
|
16
|
-
__accessCheck(obj, member, "read from private field");
|
|
17
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
18
|
-
};
|
|
19
|
-
var __privateAdd = (obj, member, value) => {
|
|
20
|
-
if (member.has(obj))
|
|
21
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
22
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
23
|
-
};
|
|
24
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
25
|
-
__accessCheck(obj, member, "write to private field");
|
|
26
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
27
|
-
return value;
|
|
28
|
-
};
|
|
10
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
11
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
12
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
13
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
14
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
29
15
|
|
|
30
16
|
// src/lib/QueryError.ts
|
|
31
17
|
var _json;
|
|
@@ -40,7 +26,7 @@ var SapphireFetch = (function (exports) {
|
|
|
40
26
|
__publicField(this, "body");
|
|
41
27
|
/** The original {@link Response} object */
|
|
42
28
|
__publicField(this, "response");
|
|
43
|
-
__privateAdd(this, _json
|
|
29
|
+
__privateAdd(this, _json);
|
|
44
30
|
this.url = url;
|
|
45
31
|
this.code = code;
|
|
46
32
|
this.body = body;
|
|
@@ -123,13 +109,15 @@ var SapphireFetch = (function (exports) {
|
|
|
123
109
|
type = "json" /* JSON */;
|
|
124
110
|
}
|
|
125
111
|
let { body } = options;
|
|
126
|
-
if (body
|
|
112
|
+
if (shouldJsonStringify(body)) {
|
|
127
113
|
body = JSON.stringify(body);
|
|
128
114
|
}
|
|
129
115
|
const stringUrl = String(url);
|
|
130
|
-
const result = await globalThis.fetch(stringUrl, {
|
|
131
|
-
|
|
132
|
-
|
|
116
|
+
const result = await globalThis.fetch(stringUrl, {
|
|
117
|
+
...options,
|
|
118
|
+
body
|
|
119
|
+
});
|
|
120
|
+
if (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());
|
|
133
121
|
switch (type) {
|
|
134
122
|
case "result" /* Result */:
|
|
135
123
|
return result;
|
|
@@ -146,6 +134,16 @@ var SapphireFetch = (function (exports) {
|
|
|
146
134
|
}
|
|
147
135
|
}
|
|
148
136
|
__name(fetch, "fetch");
|
|
137
|
+
function shouldJsonStringify(value) {
|
|
138
|
+
if (typeof value !== "object") return false;
|
|
139
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(value)) return false;
|
|
140
|
+
if (value === null) return true;
|
|
141
|
+
if (value.constructor === void 0) return true;
|
|
142
|
+
if (value.constructor === Object) return true;
|
|
143
|
+
if ("toJSON" in value && typeof value.toJSON === "function") return true;
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
__name(shouldJsonStringify, "shouldJsonStringify");
|
|
149
147
|
|
|
150
148
|
exports.FetchMediaContentTypes = FetchMediaContentTypes;
|
|
151
149
|
exports.FetchMethods = FetchMethods;
|
|
@@ -156,5 +154,5 @@ var SapphireFetch = (function (exports) {
|
|
|
156
154
|
return exports;
|
|
157
155
|
|
|
158
156
|
})({});
|
|
159
|
-
//# sourceMappingURL=
|
|
157
|
+
//# sourceMappingURL=index.global.js.map
|
|
160
158
|
//# sourceMappingURL=index.global.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/lib/QueryError.ts","../../src/lib/types.ts","../../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAMO,IAAM,cAAN,MAAM,oBAAmB,MAAM;AAAA,EAY9B,YAAY,KAAa,MAAc,UAAoB,MAAc;AAC/E,UAAM,sBAAsB,GAAG,eAAe,IAAI,GAAG;AAXtD;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAAA,wBAAgB;AAEhB;AAIC,SAAK,MAAM;AACX,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,uBAAK,OAAQ;AAAA,EACd;AAAA,EAEO,SAAS;AACf,WAAO,mBAAK,UAAU,mBAAK,OAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACxD;AACD;AAdC;AAVqC;AAA/B,IAAM,aAAN;;;ACAA,IAAK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A,eAAY;AAMZ,EAAAA,wBAAA,cAAW;AAMX,EAAAA,wBAAA,cAAW;AAIX,EAAAA,wBAAA,eAAY;AAMZ,EAAAA,wBAAA,SAAM;AA9KK,SAAAA;AAAA,GAAA;;;AC5DZ,eAAsB,MAAM,KAAmB,SAA6C,MAAyB;AACpH,MAAI,OAAO,YAAY,aAAa;AACnC,cAAU,CAAC;AACX;AAAA,EACD,WAAW,OAAO,YAAY,UAAU;AACvC,WAAO;AACP,cAAU,CAAC;AAAA,EACZ,WAAW,OAAO,SAAS,aAAa;AACvC;AAAA,EACD;AAEA,MAAI,EAAE,KAAK,IAAI;AAEf,MAAI,QAAQ,OAAO,SAAS,UAAU;AACrC,WAAO,KAAK,UAAU,IAAI;AAAA,EAC3B;AAGA,QAAM,YAAY,OAAO,GAAG;AAE5B,QAAM,SAAmB,MAAM,WAAW,MAAM,WAAW,EAAE,GAAG,SAAS,KAAK,CAAC;AAC/E,MAAI,CAAC,OAAO;AAAI,UAAM,IAAI,WAAW,WAAW,OAAO,QAAQ,QAAQ,MAAM,OAAO,MAAM,EAAE,KAAK,CAAC;AAElG,UAAQ,MAAM;AAAA,IACb;AACC,aAAO;AAAA,IACR;AACC,aAAO,OAAO,KAAK,OAAO,MAAM,OAAO,KAAK,GAAG,YAAY,CAAC;AAAA,IAC7D;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,aAAO,OAAO,KAAK;AAAA,IACpB;AACC,YAAM,IAAI,MAAM,iBAAiB,IAAI,GAAG;AAAA,EAC1C;AACD;AArCsB","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/lib/QueryError.ts","../../src/lib/types.ts","../../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":";;;;;;;;;;;;;;;;EAAA,IAAA,KAAA,CAAA;EAMO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmB,KAAM,CAAA;EAAA,EAY9B,WAAY,CAAA,GAAA,EAAa,IAAc,EAAA,QAAA,EAAoB,IAAc,EAAA;EAC/E,IAAA,KAAA,CAAM,CAAsB,mBAAA,EAAA,GAAG,CAAe,YAAA,EAAA,IAAI,CAAG,CAAA,CAAA,CAAA,CAAA;EAXtD;EAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CA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AAA;EAxCsB,MAAA,CAAA,KAAA,EAAA,OAAA,CAAA,CAAA;EAgDtB,SAAS,oBAAoB,KAAgB,EAAA;EAE5C,EAAI,IAAA,OAAO,KAAU,KAAA,QAAA,EAAiB,OAAA,KAAA,CAAA;EAEtC,EAAA,IAAI,OAAO,MAAW,KAAA,WAAA,IAAe,OAAO,QAAS,CAAA,KAAK,GAAU,OAAA,KAAA,CAAA;EAGpE,EAAI,IAAA,KAAA,KAAU,MAAa,OAAA,IAAA,CAAA;EAE3B,EAAI,IAAA,KAAA,CAAM,WAAgB,KAAA,KAAA,CAAA,EAAkB,OAAA,IAAA,CAAA;EAE5C,EAAI,IAAA,KAAA,CAAM,WAAgB,KAAA,MAAA,EAAe,OAAA,IAAA,CAAA;EAEzC,EAAA,IAAI,YAAY,KAAS,IAAA,OAAO,KAAM,CAAA,MAAA,KAAW,YAAmB,OAAA,IAAA,CAAA;EAGpE,EAAO,OAAA,KAAA,CAAA;EACR,CAAA;EAjBS,MAAA,CAAA,mBAAA,EAAA,qBAAA,CAAA","file":"index.global.js","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: RequestInit['body'] | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.\n */\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A JSON of the response body. Defaults to `unknown`, with the type set by passing the generic type of this function, or casting the result.\n */\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://nodejs.org/api/buffer.html `Buffer`} of the response body\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns A {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`} of the response body\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The response body as a raw `string`.\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The response body as a raw `string`.\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).\n */\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The raw {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript).\n */\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be {@linkplain https://nodejs.org/api/buffer.html `Buffer`}.\n * - When using `FetchResultTypes.Blob` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`}.\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript)\n */\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\n/**\n * Performs an HTTP(S) fetch\n *\n * @param url - The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param options - The {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Request `Request`} ({@link RequestInit} for TypeScript)\n * @param type - The {@link FetchResultTypes}\n *\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be {@linkplain https://nodejs.org/api/buffer.html `Buffer`}.\n * - When using `FetchResultTypes.Blob` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Blob `Blob`}.\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a {@linkplain https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} ({@link Response} in typescript)\n */\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (shouldJsonStringify(body)) {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, {\n\t\t...options,\n\t\tbody: body as RequestInit['body']\n\t});\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n\n/**\n * Determines whether a value should be stringified as JSON.\n *\n * @param value - The value to check.\n * @returns A boolean indicating whether the value should be stringified as JSON.\n */\nfunction shouldJsonStringify(value: unknown) {\n\t// If the value is not an object, it should not be stringified\n\tif (typeof value !== 'object') return false;\n\t// Buffers should not be stringified\n\tif (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) return false;\n\n\t// null object\n\tif (value === null) return true;\n\t// Object.create(null)\n\tif (value.constructor === undefined) return true;\n\t// Plain objects\n\tif (value.constructor === Object) return true;\n\t// Has toJSON method\n\tif ('toJSON' in value && typeof value.toJSON === 'function') return true;\n\n\t// Anything else (such as streams or unserializables)\n\treturn false;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/fetch",
|
|
3
|
-
"version": "3.0.3-next.
|
|
3
|
+
"version": "3.0.3-next.f63a9a11",
|
|
4
4
|
"description": "Tiny wrapper around globalThis.fetch for improved TypeScript and data type support",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,12 +25,13 @@
|
|
|
25
25
|
"scripts": {
|
|
26
26
|
"test": "vitest run",
|
|
27
27
|
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
|
|
28
|
-
"build": "tsup && yarn build:rename-cjs-index",
|
|
29
|
-
"build:rename-cjs-index": "tsx
|
|
28
|
+
"build": "yarn gen-index && tsup && yarn build:rename-cjs-index",
|
|
29
|
+
"build:rename-cjs-index": "tsx ../../scripts/rename-cjs-index.cts",
|
|
30
30
|
"docs": "typedoc-json-parser",
|
|
31
31
|
"prepack": "yarn build",
|
|
32
32
|
"bump": "cliff-jumper",
|
|
33
|
-
"check-update": "cliff-jumper --dry-run"
|
|
33
|
+
"check-update": "cliff-jumper --dry-run",
|
|
34
|
+
"gen-index": "tsx ../../scripts/gen-index.cts -w fetch"
|
|
34
35
|
},
|
|
35
36
|
"repository": {
|
|
36
37
|
"type": "git",
|
|
@@ -60,13 +61,13 @@
|
|
|
60
61
|
"access": "public"
|
|
61
62
|
},
|
|
62
63
|
"devDependencies": {
|
|
63
|
-
"@favware/cliff-jumper": "^
|
|
64
|
-
"@vitest/coverage-v8": "^
|
|
65
|
-
"tsup": "^8.
|
|
66
|
-
"tsx": "^4.
|
|
67
|
-
"typedoc": "^0.25.
|
|
68
|
-
"typedoc-json-parser": "^
|
|
69
|
-
"typescript": "
|
|
70
|
-
"vitest": "^
|
|
64
|
+
"@favware/cliff-jumper": "^4.0.3",
|
|
65
|
+
"@vitest/coverage-v8": "^2.0.5",
|
|
66
|
+
"tsup": "^8.2.4",
|
|
67
|
+
"tsx": "^4.17.0",
|
|
68
|
+
"typedoc": "^0.25.13",
|
|
69
|
+
"typedoc-json-parser": "^10.0.0",
|
|
70
|
+
"typescript": "~5.4.5",
|
|
71
|
+
"vitest": "^2.0.5"
|
|
71
72
|
}
|
|
72
73
|
}
|