@sapphire/fetch 1.0.5 → 2.0.0-next.0efe80e2.0
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 +11 -20
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +208 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +202 -6
- package/dist/index.mjs.map +1 -0
- package/dist/index.umd.js +212 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/lib/QueryError.d.ts +2 -4
- package/dist/lib/QueryError.d.ts.map +1 -1
- package/dist/lib/fetch.d.ts +10 -8
- package/dist/lib/fetch.d.ts.map +1 -1
- package/dist/lib/types.d.ts +8 -1
- package/dist/lib/types.d.ts.map +1 -1
- package/package.json +10 -9
- package/dist/lib/QueryError.js +0 -55
- package/dist/lib/QueryError.js.map +0 -1
- package/dist/lib/fetch.js +0 -37
- package/dist/lib/fetch.js.map +0 -1
- package/dist/lib/types.js +0 -80
- package/dist/lib/types.js.map +0 -1
package/README.md
CHANGED
|
@@ -4,13 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
# @sapphire/fetch
|
|
6
6
|
|
|
7
|
-
**Tiny wrapper around
|
|
7
|
+
**Tiny wrapper around cross-fetch for improved TypeScript and data type support**
|
|
8
8
|
|
|
9
9
|
[](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
|
|
10
10
|
[](https://codecov.io/gh/sapphiredev/utilities)
|
|
11
11
|
[](https://bundlephobia.com/result?p=@sapphire/fetch)
|
|
12
12
|
[](https://www.npmjs.com/package/@sapphire/fetch)
|
|
13
|
-
[](https://depfu.com/github/sapphiredev/utilities?project_id=15195)
|
|
14
13
|
|
|
15
14
|
</div>
|
|
16
15
|
|
|
@@ -20,18 +19,17 @@
|
|
|
20
19
|
- [Description](#description)
|
|
21
20
|
- [Features](#features)
|
|
22
21
|
- [Installation](#installation)
|
|
23
|
-
- [Node-fetch types](#node-fetch-types)
|
|
24
22
|
- [Usage](#usage)
|
|
25
|
-
- [`GET
|
|
26
|
-
- [`GET
|
|
27
|
-
- [`POST
|
|
23
|
+
- [`GET`ting JSON data](#getting-json-data)
|
|
24
|
+
- [`GET`ting Buffer data (images, etc.)](#getting-buffer-data-images-etc)
|
|
25
|
+
- [`POST`ing JSON data](#posting-json-data)
|
|
28
26
|
- [API Documentation](#api-documentation)
|
|
29
27
|
- [Buy us some doughnuts](#buy-us-some-doughnuts)
|
|
30
28
|
- [Contributors ✨](#contributors-%E2%9C%A8)
|
|
31
29
|
|
|
32
30
|
## Description
|
|
33
31
|
|
|
34
|
-
[
|
|
32
|
+
[cross-fetch] is already a great library for making API calls, but because it focuses solely on bringing the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to Node.js, it doesn't provide specific error messages and handling for different return types (JSON, Buffer, plain text, etc). This is where `@sapphire/fetch` comes in. The syntax is more restrictive than that of [cross-fetch], but that makes it consistent and easier to use in TypeScript.
|
|
35
33
|
|
|
36
34
|
## Features
|
|
37
35
|
|
|
@@ -40,6 +38,7 @@
|
|
|
40
38
|
- Exported `const enum` for the common return data types.
|
|
41
39
|
- Throws distinctive errors when the API returns a "not ok" status code to make them easier to understand.
|
|
42
40
|
- Enforces casting the return type when requesting JSON data, to ensure your return data is strictly typed.
|
|
41
|
+
- Uses [cross-fetch] so this package can be used in NodeJS (where it uses [node-fetch]) and browser (where it uses [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API))
|
|
43
42
|
|
|
44
43
|
## Installation
|
|
45
44
|
|
|
@@ -49,16 +48,6 @@ You can use the following command to install this package, or replace `npm insta
|
|
|
49
48
|
npm install @sapphire/fetch
|
|
50
49
|
```
|
|
51
50
|
|
|
52
|
-
### Node-fetch types
|
|
53
|
-
|
|
54
|
-
TypeScript users should also add `@types/node-fetch`:
|
|
55
|
-
|
|
56
|
-
```sh
|
|
57
|
-
npm install --save-dev @types/node-fetch
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
Alternatively, you can enable the [`skipLibChecks`](https://www.typescriptlang.org/tsconfig/#skipLibCheck) TypeScript compiler option
|
|
61
|
-
|
|
62
51
|
## Usage
|
|
63
52
|
|
|
64
53
|
**Note:** While this section uses `import`, it maps 1:1 with CommonJS' require syntax. For example, `import { fetch } from '@sapphire/fetch'` is the same as `const { fetch } = require('@sapphire/fetch')`.
|
|
@@ -155,7 +144,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
155
144
|
<td align="center"><a href="https://github.com/kyranet"><img src="https://avatars0.githubusercontent.com/u/24852502?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Antonio Román</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=kyranet" title="Code">💻</a> <a href="#projectManagement-kyranet" title="Project Management">📆</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3Akyranet" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/sapphiredev/utilities/commits?author=kyranet" title="Tests">⚠️</a></td>
|
|
156
145
|
<td align="center"><a href="https://github.com/PyroTechniac"><img src="https://avatars2.githubusercontent.com/u/39341355?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gryffon Bellish</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3APyroTechniac" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Tests">⚠️</a></td>
|
|
157
146
|
<td align="center"><a href="https://github.com/vladfrangu"><img src="https://avatars3.githubusercontent.com/u/17960496?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vlad Frangu</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3Avladfrangu" title="Bug reports">🐛</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3Avladfrangu" title="Reviewed Pull Requests">👀</a> <a href="#userTesting-vladfrangu" title="User Testing">📓</a> <a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Tests">⚠️</a></td>
|
|
158
|
-
<td align="center"><a href="https://github.com/
|
|
147
|
+
<td align="center"><a href="https://github.com/Stitch07"><img src="https://avatars0.githubusercontent.com/u/29275227?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stitch07</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Code">💻</a> <a href="#projectManagement-Stitch07" title="Project Management">📆</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Tests">⚠️</a></td>
|
|
159
148
|
<td align="center"><a href="https://github.com/apps/depfu"><img src="https://avatars3.githubusercontent.com/in/715?v=4?s=100" width="100px;" alt=""/><br /><sub><b>depfu[bot]</b></sub></a><br /><a href="#maintenance-depfu[bot]" title="Maintenance">🚧</a></td>
|
|
160
149
|
<td align="center"><a href="https://github.com/apps/allcontributors"><img src="https://avatars0.githubusercontent.com/in/23186?v=4?s=100" width="100px;" alt=""/><br /><sub><b>allcontributors[bot]</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=allcontributors[bot]" title="Documentation">📖</a></td>
|
|
161
150
|
</tr>
|
|
@@ -163,17 +152,18 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
163
152
|
<td align="center"><a href="https://github.com/Nytelife26"><img src="https://avatars1.githubusercontent.com/u/22531310?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tyler J Russell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Nytelife26" title="Documentation">📖</a></td>
|
|
164
153
|
<td align="center"><a href="https://github.com/Alcremie"><img src="https://avatars0.githubusercontent.com/u/54785334?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ivan Lieder</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Alcremie" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3AAlcremie" title="Bug reports">🐛</a></td>
|
|
165
154
|
<td align="center"><a href="https://github.com/RealShadowNova"><img src="https://avatars3.githubusercontent.com/u/46537907?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hezekiah Hendry</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=RealShadowNova" title="Code">💻</a></td>
|
|
166
|
-
<td align="center"><a href="https://github.com/Stitch07"><img src="https://avatars.githubusercontent.com/u/29275227?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stitch07</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Tests">⚠️</a></td>
|
|
167
155
|
<td align="center"><a href="https://github.com/Vetlix"><img src="https://avatars.githubusercontent.com/u/31412314?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vetlix</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Vetlix" title="Code">💻</a></td>
|
|
168
156
|
<td align="center"><a href="https://github.com/ethamitc"><img src="https://avatars.githubusercontent.com/u/27776796?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ethan Mitchell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=ethamitc" title="Documentation">📖</a></td>
|
|
169
157
|
<td align="center"><a href="https://github.com/noftaly"><img src="https://avatars.githubusercontent.com/u/34779161?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Elliot</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=noftaly" title="Code">💻</a></td>
|
|
158
|
+
<td align="center"><a href="https://jurien.dev"><img src="https://avatars.githubusercontent.com/u/5418114?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jurien Hamaker</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=jurienhamaker" title="Code">💻</a></td>
|
|
170
159
|
</tr>
|
|
171
160
|
<tr>
|
|
172
|
-
<td align="center"><a href="https://jurien.dev"><img src="https://avatars.githubusercontent.com/u/5418114?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jurien Hamaker</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=jurienhamaker" title="Code">💻</a></td>
|
|
173
161
|
<td align="center"><a href="https://fanoulis.dev/"><img src="https://avatars.githubusercontent.com/u/38255093?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Charalampos Fanoulis</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=cfanoulis" title="Documentation">📖</a></td>
|
|
174
162
|
<td align="center"><a href="https://github.com/apps/dependabot"><img src="https://avatars.githubusercontent.com/in/29110?v=4?s=100" width="100px;" alt=""/><br /><sub><b>dependabot[bot]</b></sub></a><br /><a href="#maintenance-dependabot[bot]" title="Maintenance">🚧</a></td>
|
|
175
163
|
<td align="center"><a href="https://kaname.netlify.app/"><img src="https://avatars.githubusercontent.com/u/56084970?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kaname</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=kaname-png" title="Code">💻</a></td>
|
|
176
164
|
<td align="center"><a href="https://github.com/nandhagk"><img src="https://avatars.githubusercontent.com/u/62976649?v=4?s=100" width="100px;" alt=""/><br /><sub><b>nandhagk</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/issues?q=author%3Anandhagk" title="Bug reports">🐛</a></td>
|
|
165
|
+
<td align="center"><a href="https://megatank58.me/"><img src="https://avatars.githubusercontent.com/u/51410502?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Megatank58</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Megatank58" title="Code">💻</a></td>
|
|
166
|
+
<td align="center"><a href="https://github.com/UndiedGamer"><img src="https://avatars.githubusercontent.com/u/84702365?v=4?s=100" width="100px;" alt=""/><br /><sub><b>UndiedGamer</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=UndiedGamer" title="Code">💻</a></td>
|
|
177
167
|
</tr>
|
|
178
168
|
</table>
|
|
179
169
|
|
|
@@ -187,3 +177,4 @@ This project follows the [all-contributors](https://github.com/all-contributors/
|
|
|
187
177
|
<!-- LINKS -->
|
|
188
178
|
|
|
189
179
|
[node-fetch]: https://github.com/node-fetch/node-fetch
|
|
180
|
+
[cross-fetch]: https://github.com/lquixada/cross-fetch
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,208 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var crossFetch = require('cross-fetch');
|
|
6
|
+
|
|
7
|
+
/*! *****************************************************************************
|
|
8
|
+
Copyright (c) Microsoft Corporation.
|
|
9
|
+
|
|
10
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
+
purpose with or without fee is hereby granted.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
+
***************************************************************************** */
|
|
21
|
+
|
|
22
|
+
function __classPrivateFieldGet(receiver, privateMap) {
|
|
23
|
+
if (!privateMap.has(receiver)) {
|
|
24
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
25
|
+
}
|
|
26
|
+
return privateMap.get(receiver);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function __classPrivateFieldSet(receiver, privateMap, value) {
|
|
30
|
+
if (!privateMap.has(receiver)) {
|
|
31
|
+
throw new TypeError("attempted to set private field on non-instance");
|
|
32
|
+
}
|
|
33
|
+
privateMap.set(receiver, value);
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// eslint-disable-next-line spaced-comment
|
|
38
|
+
/// <reference lib="dom" />
|
|
39
|
+
var _QueryError_json;
|
|
40
|
+
/**
|
|
41
|
+
* The QueryError class which is thrown by the `fetch` method
|
|
42
|
+
*/
|
|
43
|
+
class QueryError extends Error {
|
|
44
|
+
constructor(url, code, response, body) {
|
|
45
|
+
super(`Failed to request '${url}' with code ${code}.`);
|
|
46
|
+
/** The requested url. */
|
|
47
|
+
Object.defineProperty(this, "url", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: void 0
|
|
52
|
+
});
|
|
53
|
+
/** The HTTP status code. */
|
|
54
|
+
Object.defineProperty(this, "code", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: void 0
|
|
59
|
+
});
|
|
60
|
+
/** The returned response body as a string */
|
|
61
|
+
Object.defineProperty(this, "body", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: void 0
|
|
66
|
+
});
|
|
67
|
+
/** The original {@link Response} object */
|
|
68
|
+
Object.defineProperty(this, "response", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
|
|
75
|
+
_QueryError_json.set(this, void 0);
|
|
76
|
+
this.url = url;
|
|
77
|
+
this.code = code;
|
|
78
|
+
this.body = body;
|
|
79
|
+
this.response = response;
|
|
80
|
+
__classPrivateFieldSet(this, _QueryError_json, null);
|
|
81
|
+
}
|
|
82
|
+
toJSON() {
|
|
83
|
+
return __classPrivateFieldGet(this, _QueryError_json) ?? (__classPrivateFieldSet(this, _QueryError_json, JSON.parse(this.body)));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
_QueryError_json = new WeakMap();
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The supported return types for the `fetch` method
|
|
90
|
+
*/
|
|
91
|
+
exports.FetchResultTypes = void 0;
|
|
92
|
+
(function (FetchResultTypes) {
|
|
93
|
+
/**
|
|
94
|
+
* Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).
|
|
95
|
+
*
|
|
96
|
+
* You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define
|
|
97
|
+
* the JSON structure, otherwise the result will be `unknown`.
|
|
98
|
+
*/
|
|
99
|
+
FetchResultTypes["JSON"] = "json";
|
|
100
|
+
/**
|
|
101
|
+
* Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).
|
|
102
|
+
* @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.
|
|
103
|
+
* If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!
|
|
104
|
+
*/
|
|
105
|
+
FetchResultTypes["Buffer"] = "buffer";
|
|
106
|
+
/**
|
|
107
|
+
* Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
108
|
+
* @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.
|
|
109
|
+
*/
|
|
110
|
+
FetchResultTypes["Blob"] = "blob";
|
|
111
|
+
/**
|
|
112
|
+
* Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).
|
|
113
|
+
*/
|
|
114
|
+
FetchResultTypes["Text"] = "text";
|
|
115
|
+
/**
|
|
116
|
+
* Returns the entire response and doesn't parse the `body` in any way.
|
|
117
|
+
*/
|
|
118
|
+
FetchResultTypes["Result"] = "result";
|
|
119
|
+
})(exports.FetchResultTypes || (exports.FetchResultTypes = {}));
|
|
120
|
+
/**
|
|
121
|
+
* The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
|
|
122
|
+
*/
|
|
123
|
+
exports.FetchMethods = void 0;
|
|
124
|
+
(function (FetchMethods) {
|
|
125
|
+
/**
|
|
126
|
+
* The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.
|
|
127
|
+
* @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)
|
|
128
|
+
*/
|
|
129
|
+
FetchMethods["Get"] = "GET";
|
|
130
|
+
/**
|
|
131
|
+
* The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.
|
|
132
|
+
* @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)
|
|
133
|
+
*/
|
|
134
|
+
FetchMethods["Head"] = "HEAD";
|
|
135
|
+
/**
|
|
136
|
+
* 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.
|
|
137
|
+
* @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)
|
|
138
|
+
*/
|
|
139
|
+
FetchMethods["Post"] = "POST";
|
|
140
|
+
/**
|
|
141
|
+
* The `PUT` method replaces all current representations of the target resource with the request payload.
|
|
142
|
+
* @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)
|
|
143
|
+
*/
|
|
144
|
+
FetchMethods["Put"] = "PUT";
|
|
145
|
+
/**
|
|
146
|
+
* The `DELETE` method deletes the specified resource.
|
|
147
|
+
* @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)
|
|
148
|
+
*/
|
|
149
|
+
FetchMethods["Delete"] = "DELETE";
|
|
150
|
+
/**
|
|
151
|
+
* The `CONNECT` method establishes a tunnel to the server identified by the target resource
|
|
152
|
+
* @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)
|
|
153
|
+
*/
|
|
154
|
+
FetchMethods["Connect"] = "CONNECT";
|
|
155
|
+
/**
|
|
156
|
+
* The `OPTIONS` method is used to describe the communication options for the target resource.
|
|
157
|
+
* @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)
|
|
158
|
+
*/
|
|
159
|
+
FetchMethods["Options"] = "OPTIONS";
|
|
160
|
+
/**
|
|
161
|
+
* The `TRACE` method performs a message loop-back test along the path to the target resource.
|
|
162
|
+
* @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)
|
|
163
|
+
*/
|
|
164
|
+
FetchMethods["Trace"] = "TRACE";
|
|
165
|
+
/**
|
|
166
|
+
* The `PATCH` method is used to apply partial modifications to a resource.
|
|
167
|
+
* @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)
|
|
168
|
+
*/
|
|
169
|
+
FetchMethods["Patch"] = "PATCH";
|
|
170
|
+
})(exports.FetchMethods || (exports.FetchMethods = {}));
|
|
171
|
+
|
|
172
|
+
// eslint-disable-next-line spaced-comment
|
|
173
|
+
async function fetch(url, options, type) {
|
|
174
|
+
if (typeof options === 'undefined') {
|
|
175
|
+
options = {};
|
|
176
|
+
type = "json" /* JSON */;
|
|
177
|
+
}
|
|
178
|
+
else if (typeof options === 'string') {
|
|
179
|
+
type = options;
|
|
180
|
+
options = {};
|
|
181
|
+
}
|
|
182
|
+
else if (typeof type === 'undefined') {
|
|
183
|
+
type = "json" /* JSON */;
|
|
184
|
+
}
|
|
185
|
+
// Transform the URL to a String, in case an URL object was passed
|
|
186
|
+
const stringUrl = String(url);
|
|
187
|
+
const result = await crossFetch.fetch(stringUrl, options);
|
|
188
|
+
if (!result.ok)
|
|
189
|
+
throw new QueryError(stringUrl, result.status, result, await result.clone().text());
|
|
190
|
+
switch (type) {
|
|
191
|
+
case "result" /* Result */:
|
|
192
|
+
return result;
|
|
193
|
+
case "buffer" /* Buffer */:
|
|
194
|
+
return Buffer.from(await (await result.blob()).arrayBuffer());
|
|
195
|
+
case "blob" /* Blob */:
|
|
196
|
+
return result.blob();
|
|
197
|
+
case "json" /* JSON */:
|
|
198
|
+
return result.json();
|
|
199
|
+
case "text" /* Text */:
|
|
200
|
+
return result.text();
|
|
201
|
+
default:
|
|
202
|
+
throw new Error(`Unknown type "${type}"`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
exports.QueryError = QueryError;
|
|
207
|
+
exports.fetch = fetch;
|
|
208
|
+
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,sDAA4B;AAC5B,qCAA+C;AAAtC,gGAAA,KAAK,OAAW;AACzB,2DAAiC;AACjC,sDAA4B"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/lib/QueryError.ts","../src/lib/types.ts","../src/lib/fetch.ts"],"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\t// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility\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","/**\n * The supported return types for the `fetch` method\n */\nexport const 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 const 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","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { fetch as nodeFetch } from 'cross-fetch';\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes } 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 */\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestInit, 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: RequestInit, 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: RequestInit, 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: RequestInit, 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: RequestInit, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestInit, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestInit | 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\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 nodeFetch(stringUrl, options);\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"],"names":["FetchResultTypes","FetchMethods","nodeFetch"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AACA;;AAEA;;;MAGa,UAAW,SAAQ,KAAK;IAYpC,YAAmB,GAAW,EAAE,IAAY,EAAE,QAAkB,EAAE,IAAY;QAC7E,KAAK,CAAC,sBAAsB,GAAG,eAAe,IAAI,GAAG,CAAC,CAAC;;QAXxD;;;;;WAA4B;;QAE5B;;;;;WAA6B;;QAE7B;;;;;WAA6B;;QAE7B;;;;;WAAmC;;QAEnC,mCAAe;QAId,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,uBAAA,IAAI,oBAAS,KAAI,CAAC;KAClB;IAEM,MAAM;QACZ,OAAO,uBAAA,IAAI,mBAAM,KAAK,uBAAA,IAAI,oBAAS,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAC,CAAC,CAAC;KAC1D;CACD;;;AC9BD;;;AAGkBA;AAAlB,WAAkB,gBAAgB;;;;;;;IAOjC,iCAAa,CAAA;;;;;;IAMb,qCAAiB,CAAA;;;;;IAKjB,iCAAa,CAAA;;;;IAIb,iCAAa,CAAA;;;;IAIb,qCAAiB,CAAA;AAClB,CAAC,EA3BiBA,wBAAgB,KAAhBA,wBAAgB,QA2BjC;AAED;;;AAGkBC;AAAlB,WAAkB,YAAY;;;;;IAK7B,2BAAW,CAAA;;;;;IAKX,6BAAa,CAAA;;;;;IAKb,6BAAa,CAAA;;;;;IAKb,2BAAW,CAAA;;;;;IAKX,iCAAiB,CAAA;;;;;IAKjB,mCAAmB,CAAA;;;;;IAKnB,mCAAmB,CAAA;;;;;IAKnB,+BAAe,CAAA;;;;;IAKf,+BAAe,CAAA;AAChB,CAAC,EA9CiBA,oBAAY,KAAZA,oBAAY;;ACnC9B;AA+BO,eAAe,KAAK,CAAC,GAAiB,EAAE,OAAwC,EAAE,IAAuB;IAC/G,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;QACnC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,qBAAyB;KAC7B;SAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QACvC,IAAI,GAAG,OAAO,CAAC;QACf,OAAO,GAAG,EAAE,CAAC;KACb;SAAM,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;QACvC,IAAI,qBAAyB;KAC7B;;IAGD,MAAM,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IAE9B,MAAM,MAAM,GAAa,MAAMC,gBAAS,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC7D,IAAI,CAAC,MAAM,CAAC,EAAE;QAAE,MAAM,IAAI,UAAU,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;IAEpG,QAAQ,IAAI;QACX;YACC,OAAO,MAAM,CAAC;QACf;YACC,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,MAAM,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;QAC/D;YACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;QACtB;YACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;QACtB;YACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;QACtB;YACC,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,GAAG,CAAC,CAAC;KAC3C;AACF;;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,203 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { fetch as fetch$1 } from 'cross-fetch';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
/*! *****************************************************************************
|
|
4
|
+
Copyright (c) Microsoft Corporation.
|
|
5
|
+
|
|
6
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
7
|
+
purpose with or without fee is hereby granted.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
***************************************************************************** */
|
|
17
|
+
|
|
18
|
+
function __classPrivateFieldGet(receiver, privateMap) {
|
|
19
|
+
if (!privateMap.has(receiver)) {
|
|
20
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
21
|
+
}
|
|
22
|
+
return privateMap.get(receiver);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function __classPrivateFieldSet(receiver, privateMap, value) {
|
|
26
|
+
if (!privateMap.has(receiver)) {
|
|
27
|
+
throw new TypeError("attempted to set private field on non-instance");
|
|
28
|
+
}
|
|
29
|
+
privateMap.set(receiver, value);
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// eslint-disable-next-line spaced-comment
|
|
34
|
+
/// <reference lib="dom" />
|
|
35
|
+
var _QueryError_json;
|
|
36
|
+
/**
|
|
37
|
+
* The QueryError class which is thrown by the `fetch` method
|
|
38
|
+
*/
|
|
39
|
+
class QueryError extends Error {
|
|
40
|
+
constructor(url, code, response, body) {
|
|
41
|
+
super(`Failed to request '${url}' with code ${code}.`);
|
|
42
|
+
/** The requested url. */
|
|
43
|
+
Object.defineProperty(this, "url", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: void 0
|
|
48
|
+
});
|
|
49
|
+
/** The HTTP status code. */
|
|
50
|
+
Object.defineProperty(this, "code", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
/** The returned response body as a string */
|
|
57
|
+
Object.defineProperty(this, "body", {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
value: void 0
|
|
62
|
+
});
|
|
63
|
+
/** The original {@link Response} object */
|
|
64
|
+
Object.defineProperty(this, "response", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: void 0
|
|
69
|
+
});
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
|
|
71
|
+
_QueryError_json.set(this, void 0);
|
|
72
|
+
this.url = url;
|
|
73
|
+
this.code = code;
|
|
74
|
+
this.body = body;
|
|
75
|
+
this.response = response;
|
|
76
|
+
__classPrivateFieldSet(this, _QueryError_json, null);
|
|
77
|
+
}
|
|
78
|
+
toJSON() {
|
|
79
|
+
return __classPrivateFieldGet(this, _QueryError_json) ?? (__classPrivateFieldSet(this, _QueryError_json, JSON.parse(this.body)));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
_QueryError_json = new WeakMap();
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The supported return types for the `fetch` method
|
|
86
|
+
*/
|
|
87
|
+
var FetchResultTypes;
|
|
88
|
+
(function (FetchResultTypes) {
|
|
89
|
+
/**
|
|
90
|
+
* Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).
|
|
91
|
+
*
|
|
92
|
+
* You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define
|
|
93
|
+
* the JSON structure, otherwise the result will be `unknown`.
|
|
94
|
+
*/
|
|
95
|
+
FetchResultTypes["JSON"] = "json";
|
|
96
|
+
/**
|
|
97
|
+
* Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).
|
|
98
|
+
* @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.
|
|
99
|
+
* If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!
|
|
100
|
+
*/
|
|
101
|
+
FetchResultTypes["Buffer"] = "buffer";
|
|
102
|
+
/**
|
|
103
|
+
* Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
104
|
+
* @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.
|
|
105
|
+
*/
|
|
106
|
+
FetchResultTypes["Blob"] = "blob";
|
|
107
|
+
/**
|
|
108
|
+
* Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).
|
|
109
|
+
*/
|
|
110
|
+
FetchResultTypes["Text"] = "text";
|
|
111
|
+
/**
|
|
112
|
+
* Returns the entire response and doesn't parse the `body` in any way.
|
|
113
|
+
*/
|
|
114
|
+
FetchResultTypes["Result"] = "result";
|
|
115
|
+
})(FetchResultTypes || (FetchResultTypes = {}));
|
|
116
|
+
/**
|
|
117
|
+
* The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
|
|
118
|
+
*/
|
|
119
|
+
var FetchMethods;
|
|
120
|
+
(function (FetchMethods) {
|
|
121
|
+
/**
|
|
122
|
+
* The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.
|
|
123
|
+
* @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)
|
|
124
|
+
*/
|
|
125
|
+
FetchMethods["Get"] = "GET";
|
|
126
|
+
/**
|
|
127
|
+
* The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.
|
|
128
|
+
* @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)
|
|
129
|
+
*/
|
|
130
|
+
FetchMethods["Head"] = "HEAD";
|
|
131
|
+
/**
|
|
132
|
+
* 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.
|
|
133
|
+
* @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)
|
|
134
|
+
*/
|
|
135
|
+
FetchMethods["Post"] = "POST";
|
|
136
|
+
/**
|
|
137
|
+
* The `PUT` method replaces all current representations of the target resource with the request payload.
|
|
138
|
+
* @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)
|
|
139
|
+
*/
|
|
140
|
+
FetchMethods["Put"] = "PUT";
|
|
141
|
+
/**
|
|
142
|
+
* The `DELETE` method deletes the specified resource.
|
|
143
|
+
* @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)
|
|
144
|
+
*/
|
|
145
|
+
FetchMethods["Delete"] = "DELETE";
|
|
146
|
+
/**
|
|
147
|
+
* The `CONNECT` method establishes a tunnel to the server identified by the target resource
|
|
148
|
+
* @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)
|
|
149
|
+
*/
|
|
150
|
+
FetchMethods["Connect"] = "CONNECT";
|
|
151
|
+
/**
|
|
152
|
+
* The `OPTIONS` method is used to describe the communication options for the target resource.
|
|
153
|
+
* @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)
|
|
154
|
+
*/
|
|
155
|
+
FetchMethods["Options"] = "OPTIONS";
|
|
156
|
+
/**
|
|
157
|
+
* The `TRACE` method performs a message loop-back test along the path to the target resource.
|
|
158
|
+
* @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)
|
|
159
|
+
*/
|
|
160
|
+
FetchMethods["Trace"] = "TRACE";
|
|
161
|
+
/**
|
|
162
|
+
* The `PATCH` method is used to apply partial modifications to a resource.
|
|
163
|
+
* @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)
|
|
164
|
+
*/
|
|
165
|
+
FetchMethods["Patch"] = "PATCH";
|
|
166
|
+
})(FetchMethods || (FetchMethods = {}));
|
|
167
|
+
|
|
168
|
+
// eslint-disable-next-line spaced-comment
|
|
169
|
+
async function fetch(url, options, type) {
|
|
170
|
+
if (typeof options === 'undefined') {
|
|
171
|
+
options = {};
|
|
172
|
+
type = "json" /* JSON */;
|
|
173
|
+
}
|
|
174
|
+
else if (typeof options === 'string') {
|
|
175
|
+
type = options;
|
|
176
|
+
options = {};
|
|
177
|
+
}
|
|
178
|
+
else if (typeof type === 'undefined') {
|
|
179
|
+
type = "json" /* JSON */;
|
|
180
|
+
}
|
|
181
|
+
// Transform the URL to a String, in case an URL object was passed
|
|
182
|
+
const stringUrl = String(url);
|
|
183
|
+
const result = await fetch$1(stringUrl, options);
|
|
184
|
+
if (!result.ok)
|
|
185
|
+
throw new QueryError(stringUrl, result.status, result, await result.clone().text());
|
|
186
|
+
switch (type) {
|
|
187
|
+
case "result" /* Result */:
|
|
188
|
+
return result;
|
|
189
|
+
case "buffer" /* Buffer */:
|
|
190
|
+
return Buffer.from(await (await result.blob()).arrayBuffer());
|
|
191
|
+
case "blob" /* Blob */:
|
|
192
|
+
return result.blob();
|
|
193
|
+
case "json" /* JSON */:
|
|
194
|
+
return result.json();
|
|
195
|
+
case "text" /* Text */:
|
|
196
|
+
return result.text();
|
|
197
|
+
default:
|
|
198
|
+
throw new Error(`Unknown type "${type}"`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export { FetchMethods, FetchResultTypes, QueryError, fetch };
|
|
203
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/lib/QueryError.ts","../src/lib/types.ts","../src/lib/fetch.ts"],"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\t// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility\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","/**\n * The supported return types for the `fetch` method\n */\nexport const 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 const 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","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { fetch as nodeFetch } from 'cross-fetch';\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes } 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 */\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestInit, 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: RequestInit, 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: RequestInit, 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: RequestInit, 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: RequestInit, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestInit, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestInit | 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\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 nodeFetch(stringUrl, options);\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"],"names":["nodeFetch"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AACA;;AAEA;;;MAGa,UAAW,SAAQ,KAAK;IAYpC,YAAmB,GAAW,EAAE,IAAY,EAAE,QAAkB,EAAE,IAAY;QAC7E,KAAK,CAAC,sBAAsB,GAAG,eAAe,IAAI,GAAG,CAAC,CAAC;;QAXxD;;;;;WAA4B;;QAE5B;;;;;WAA6B;;QAE7B;;;;;WAA6B;;QAE7B;;;;;WAAmC;;QAEnC,mCAAe;QAId,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,uBAAA,IAAI,oBAAS,KAAI,CAAC;KAClB;IAEM,MAAM;QACZ,OAAO,uBAAA,IAAI,mBAAM,KAAK,uBAAA,IAAI,oBAAS,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAC,CAAC,CAAC;KAC1D;CACD;;;AC9BD;;;IAGkB;AAAlB,WAAkB,gBAAgB;;;;;;;IAOjC,iCAAa,CAAA;;;;;;IAMb,qCAAiB,CAAA;;;;;IAKjB,iCAAa,CAAA;;;;IAIb,iCAAa,CAAA;;;;IAIb,qCAAiB,CAAA;AAClB,CAAC,EA3BiB,gBAAgB,KAAhB,gBAAgB,QA2BjC;AAED;;;IAGkB;AAAlB,WAAkB,YAAY;;;;;IAK7B,2BAAW,CAAA;;;;;IAKX,6BAAa,CAAA;;;;;IAKb,6BAAa,CAAA;;;;;IAKb,2BAAW,CAAA;;;;;IAKX,iCAAiB,CAAA;;;;;IAKjB,mCAAmB,CAAA;;;;;IAKnB,mCAAmB,CAAA;;;;;IAKnB,+BAAe,CAAA;;;;;IAKf,+BAAe,CAAA;AAChB,CAAC,EA9CiB,YAAY,KAAZ,YAAY;;ACnC9B;AA+BO,eAAe,KAAK,CAAC,GAAiB,EAAE,OAAwC,EAAE,IAAuB;IAC/G,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;QACnC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,qBAAyB;KAC7B;SAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QACvC,IAAI,GAAG,OAAO,CAAC;QACf,OAAO,GAAG,EAAE,CAAC;KACb;SAAM,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;QACvC,IAAI,qBAAyB;KAC7B;;IAGD,MAAM,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IAE9B,MAAM,MAAM,GAAa,MAAMA,OAAS,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC7D,IAAI,CAAC,MAAM,CAAC,EAAE;QAAE,MAAM,IAAI,UAAU,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;IAEpG,QAAQ,IAAI;QACX;YACC,OAAO,MAAM,CAAC;QACf;YACC,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,MAAM,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;QAC/D;YACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;QACtB;YACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;QACtB;YACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;QACtB;YACC,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,GAAG,CAAC,CAAC;KAC3C;AACF;;;;"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('cross-fetch')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'cross-fetch'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SapphireFetch = {}, global.window));
|
|
5
|
+
})(this, (function (exports, crossFetch) { 'use strict';
|
|
6
|
+
|
|
7
|
+
/*! *****************************************************************************
|
|
8
|
+
Copyright (c) Microsoft Corporation.
|
|
9
|
+
|
|
10
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
+
purpose with or without fee is hereby granted.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
+
***************************************************************************** */
|
|
21
|
+
|
|
22
|
+
function __classPrivateFieldGet(receiver, privateMap) {
|
|
23
|
+
if (!privateMap.has(receiver)) {
|
|
24
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
25
|
+
}
|
|
26
|
+
return privateMap.get(receiver);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function __classPrivateFieldSet(receiver, privateMap, value) {
|
|
30
|
+
if (!privateMap.has(receiver)) {
|
|
31
|
+
throw new TypeError("attempted to set private field on non-instance");
|
|
32
|
+
}
|
|
33
|
+
privateMap.set(receiver, value);
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// eslint-disable-next-line spaced-comment
|
|
38
|
+
/// <reference lib="dom" />
|
|
39
|
+
var _QueryError_json;
|
|
40
|
+
/**
|
|
41
|
+
* The QueryError class which is thrown by the `fetch` method
|
|
42
|
+
*/
|
|
43
|
+
class QueryError extends Error {
|
|
44
|
+
constructor(url, code, response, body) {
|
|
45
|
+
super(`Failed to request '${url}' with code ${code}.`);
|
|
46
|
+
/** The requested url. */
|
|
47
|
+
Object.defineProperty(this, "url", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: void 0
|
|
52
|
+
});
|
|
53
|
+
/** The HTTP status code. */
|
|
54
|
+
Object.defineProperty(this, "code", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: void 0
|
|
59
|
+
});
|
|
60
|
+
/** The returned response body as a string */
|
|
61
|
+
Object.defineProperty(this, "body", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: void 0
|
|
66
|
+
});
|
|
67
|
+
/** The original {@link Response} object */
|
|
68
|
+
Object.defineProperty(this, "response", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
|
|
75
|
+
_QueryError_json.set(this, void 0);
|
|
76
|
+
this.url = url;
|
|
77
|
+
this.code = code;
|
|
78
|
+
this.body = body;
|
|
79
|
+
this.response = response;
|
|
80
|
+
__classPrivateFieldSet(this, _QueryError_json, null);
|
|
81
|
+
}
|
|
82
|
+
toJSON() {
|
|
83
|
+
return __classPrivateFieldGet(this, _QueryError_json) ?? (__classPrivateFieldSet(this, _QueryError_json, JSON.parse(this.body)));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
_QueryError_json = new WeakMap();
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The supported return types for the `fetch` method
|
|
90
|
+
*/
|
|
91
|
+
exports.FetchResultTypes = void 0;
|
|
92
|
+
(function (FetchResultTypes) {
|
|
93
|
+
/**
|
|
94
|
+
* Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).
|
|
95
|
+
*
|
|
96
|
+
* You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define
|
|
97
|
+
* the JSON structure, otherwise the result will be `unknown`.
|
|
98
|
+
*/
|
|
99
|
+
FetchResultTypes["JSON"] = "json";
|
|
100
|
+
/**
|
|
101
|
+
* Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).
|
|
102
|
+
* @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.
|
|
103
|
+
* If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!
|
|
104
|
+
*/
|
|
105
|
+
FetchResultTypes["Buffer"] = "buffer";
|
|
106
|
+
/**
|
|
107
|
+
* Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
108
|
+
* @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.
|
|
109
|
+
*/
|
|
110
|
+
FetchResultTypes["Blob"] = "blob";
|
|
111
|
+
/**
|
|
112
|
+
* Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).
|
|
113
|
+
*/
|
|
114
|
+
FetchResultTypes["Text"] = "text";
|
|
115
|
+
/**
|
|
116
|
+
* Returns the entire response and doesn't parse the `body` in any way.
|
|
117
|
+
*/
|
|
118
|
+
FetchResultTypes["Result"] = "result";
|
|
119
|
+
})(exports.FetchResultTypes || (exports.FetchResultTypes = {}));
|
|
120
|
+
/**
|
|
121
|
+
* The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
|
|
122
|
+
*/
|
|
123
|
+
exports.FetchMethods = void 0;
|
|
124
|
+
(function (FetchMethods) {
|
|
125
|
+
/**
|
|
126
|
+
* The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.
|
|
127
|
+
* @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)
|
|
128
|
+
*/
|
|
129
|
+
FetchMethods["Get"] = "GET";
|
|
130
|
+
/**
|
|
131
|
+
* The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.
|
|
132
|
+
* @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)
|
|
133
|
+
*/
|
|
134
|
+
FetchMethods["Head"] = "HEAD";
|
|
135
|
+
/**
|
|
136
|
+
* 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.
|
|
137
|
+
* @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)
|
|
138
|
+
*/
|
|
139
|
+
FetchMethods["Post"] = "POST";
|
|
140
|
+
/**
|
|
141
|
+
* The `PUT` method replaces all current representations of the target resource with the request payload.
|
|
142
|
+
* @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)
|
|
143
|
+
*/
|
|
144
|
+
FetchMethods["Put"] = "PUT";
|
|
145
|
+
/**
|
|
146
|
+
* The `DELETE` method deletes the specified resource.
|
|
147
|
+
* @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)
|
|
148
|
+
*/
|
|
149
|
+
FetchMethods["Delete"] = "DELETE";
|
|
150
|
+
/**
|
|
151
|
+
* The `CONNECT` method establishes a tunnel to the server identified by the target resource
|
|
152
|
+
* @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)
|
|
153
|
+
*/
|
|
154
|
+
FetchMethods["Connect"] = "CONNECT";
|
|
155
|
+
/**
|
|
156
|
+
* The `OPTIONS` method is used to describe the communication options for the target resource.
|
|
157
|
+
* @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)
|
|
158
|
+
*/
|
|
159
|
+
FetchMethods["Options"] = "OPTIONS";
|
|
160
|
+
/**
|
|
161
|
+
* The `TRACE` method performs a message loop-back test along the path to the target resource.
|
|
162
|
+
* @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)
|
|
163
|
+
*/
|
|
164
|
+
FetchMethods["Trace"] = "TRACE";
|
|
165
|
+
/**
|
|
166
|
+
* The `PATCH` method is used to apply partial modifications to a resource.
|
|
167
|
+
* @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)
|
|
168
|
+
*/
|
|
169
|
+
FetchMethods["Patch"] = "PATCH";
|
|
170
|
+
})(exports.FetchMethods || (exports.FetchMethods = {}));
|
|
171
|
+
|
|
172
|
+
// eslint-disable-next-line spaced-comment
|
|
173
|
+
async function fetch(url, options, type) {
|
|
174
|
+
if (typeof options === 'undefined') {
|
|
175
|
+
options = {};
|
|
176
|
+
type = "json" /* JSON */;
|
|
177
|
+
}
|
|
178
|
+
else if (typeof options === 'string') {
|
|
179
|
+
type = options;
|
|
180
|
+
options = {};
|
|
181
|
+
}
|
|
182
|
+
else if (typeof type === 'undefined') {
|
|
183
|
+
type = "json" /* JSON */;
|
|
184
|
+
}
|
|
185
|
+
// Transform the URL to a String, in case an URL object was passed
|
|
186
|
+
const stringUrl = String(url);
|
|
187
|
+
const result = await crossFetch.fetch(stringUrl, options);
|
|
188
|
+
if (!result.ok)
|
|
189
|
+
throw new QueryError(stringUrl, result.status, result, await result.clone().text());
|
|
190
|
+
switch (type) {
|
|
191
|
+
case "result" /* Result */:
|
|
192
|
+
return result;
|
|
193
|
+
case "buffer" /* Buffer */:
|
|
194
|
+
return Buffer.from(await (await result.blob()).arrayBuffer());
|
|
195
|
+
case "blob" /* Blob */:
|
|
196
|
+
return result.blob();
|
|
197
|
+
case "json" /* JSON */:
|
|
198
|
+
return result.json();
|
|
199
|
+
case "text" /* Text */:
|
|
200
|
+
return result.text();
|
|
201
|
+
default:
|
|
202
|
+
throw new Error(`Unknown type "${type}"`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
exports.QueryError = QueryError;
|
|
207
|
+
exports.fetch = fetch;
|
|
208
|
+
|
|
209
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
210
|
+
|
|
211
|
+
}));
|
|
212
|
+
//# sourceMappingURL=index.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/lib/QueryError.ts","../src/lib/types.ts","../src/lib/fetch.ts"],"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\t// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility\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","/**\n * The supported return types for the `fetch` method\n */\nexport const 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 const 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","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { fetch as nodeFetch } from 'cross-fetch';\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes } 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 */\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestInit, 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: RequestInit, 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: RequestInit, 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: RequestInit, 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: RequestInit, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestInit, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestInit | 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\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 nodeFetch(stringUrl, options);\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"],"names":["FetchResultTypes","FetchMethods","nodeFetch"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAA;IACA;;IAEA;;;UAGa,UAAW,SAAQ,KAAK;QAYpC,YAAmB,GAAW,EAAE,IAAY,EAAE,QAAkB,EAAE,IAAY;YAC7E,KAAK,CAAC,sBAAsB,GAAG,eAAe,IAAI,GAAG,CAAC,CAAC;;YAXxD;;;;;eAA4B;;YAE5B;;;;;eAA6B;;YAE7B;;;;;eAA6B;;YAE7B;;;;;eAAmC;;YAEnC,mCAAe;YAId,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;YACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;YACzB,uBAAA,IAAI,oBAAS,KAAI,CAAC;SAClB;QAEM,MAAM;YACZ,OAAO,uBAAA,IAAI,mBAAM,KAAK,uBAAA,IAAI,oBAAS,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAC,CAAC,CAAC;SAC1D;KACD;;;IC9BD;;;AAGkBA;IAAlB,WAAkB,gBAAgB;;;;;;;QAOjC,iCAAa,CAAA;;;;;;QAMb,qCAAiB,CAAA;;;;;QAKjB,iCAAa,CAAA;;;;QAIb,iCAAa,CAAA;;;;QAIb,qCAAiB,CAAA;IAClB,CAAC,EA3BiBA,wBAAgB,KAAhBA,wBAAgB,QA2BjC;IAED;;;AAGkBC;IAAlB,WAAkB,YAAY;;;;;QAK7B,2BAAW,CAAA;;;;;QAKX,6BAAa,CAAA;;;;;QAKb,6BAAa,CAAA;;;;;QAKb,2BAAW,CAAA;;;;;QAKX,iCAAiB,CAAA;;;;;QAKjB,mCAAmB,CAAA;;;;;QAKnB,mCAAmB,CAAA;;;;;QAKnB,+BAAe,CAAA;;;;;QAKf,+BAAe,CAAA;IAChB,CAAC,EA9CiBA,oBAAY,KAAZA,oBAAY;;ICnC9B;IA+BO,eAAe,KAAK,CAAC,GAAiB,EAAE,OAAwC,EAAE,IAAuB;QAC/G,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;YACnC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,qBAAyB;SAC7B;aAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;YACvC,IAAI,GAAG,OAAO,CAAC;YACf,OAAO,GAAG,EAAE,CAAC;SACb;aAAM,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;YACvC,IAAI,qBAAyB;SAC7B;;QAGD,MAAM,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAE9B,MAAM,MAAM,GAAa,MAAMC,gBAAS,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC7D,IAAI,CAAC,MAAM,CAAC,EAAE;YAAE,MAAM,IAAI,UAAU,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;QAEpG,QAAQ,IAAI;YACX;gBACC,OAAO,MAAM,CAAC;YACf;gBACC,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,MAAM,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;YAC/D;gBACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;YACtB;gBACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;YACtB;gBACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;YACtB;gBACC,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,GAAG,CAAC,CAAC;SAC3C;IACF;;;;;;;;;;;"}
|
package/dist/lib/QueryError.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
/// <reference
|
|
2
|
-
import type { Response } from 'node-fetch';
|
|
3
|
-
import type { URL } from 'url';
|
|
1
|
+
/// <reference lib="dom" />
|
|
4
2
|
/**
|
|
5
3
|
* The QueryError class which is thrown by the `fetch` method
|
|
6
4
|
*/
|
|
@@ -14,7 +12,7 @@ export declare class QueryError extends Error {
|
|
|
14
12
|
readonly body: string;
|
|
15
13
|
/** The original {@link Response} object */
|
|
16
14
|
readonly response: Response;
|
|
17
|
-
constructor(url: string
|
|
15
|
+
constructor(url: string, code: number, response: Response, body: string);
|
|
18
16
|
toJSON(): any;
|
|
19
17
|
}
|
|
20
18
|
//# sourceMappingURL=QueryError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QueryError.d.ts","sourceRoot":"","sources":["../../src/lib/QueryError.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"QueryError.d.ts","sourceRoot":"","sources":["../../src/lib/QueryError.ts"],"names":[],"mappings":";AAGA;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAK;;IACpC,yBAAyB;IACzB,SAAgB,GAAG,EAAE,MAAM,CAAC;IAC5B,4BAA4B;IAC5B,SAAgB,IAAI,EAAE,MAAM,CAAC;IAC7B,6CAA6C;IAC7C,SAAgB,IAAI,EAAE,MAAM,CAAC;IAC7B,2CAA2C;IAC3C,SAAgB,QAAQ,EAAE,QAAQ,CAAC;gBAIhB,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM;IASvE,MAAM;CAGb"}
|
package/dist/lib/fetch.d.ts
CHANGED
|
@@ -1,26 +1,28 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
|
|
3
|
-
import type { URL } from 'url';
|
|
2
|
+
/// <reference lib="dom" />
|
|
4
3
|
import { FetchResultTypes } from './types';
|
|
5
4
|
/**
|
|
6
5
|
* Performs an HTTP(S) fetch
|
|
7
6
|
* @param url The URL to send the request to. Can be either a `string` or an `URL` object.
|
|
8
7
|
* `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`.
|
|
9
|
-
* @param optionsOrType Either the [
|
|
10
|
-
* @param type Only needs to be provided if the second parameter are [
|
|
8
|
+
* @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}
|
|
9
|
+
* @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.
|
|
11
10
|
* @returns The return type is determined by the provided `type`.
|
|
12
11
|
* - 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.
|
|
13
|
-
* - When using `FetchResultTypes.Buffer` the return type
|
|
14
|
-
* - When using `FetchResultTypes.
|
|
15
|
-
* - When using `FetchResultTypes.
|
|
12
|
+
* - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).
|
|
13
|
+
* - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
14
|
+
* - When using `FetchResultTypes.Text` the return type will be a `string`
|
|
15
|
+
* - 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)
|
|
16
16
|
*/
|
|
17
17
|
export declare function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;
|
|
18
18
|
export declare function fetch<R>(url: URL | string, options: RequestInit, type?: FetchResultTypes.JSON): Promise<R>;
|
|
19
19
|
export declare function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;
|
|
20
20
|
export declare function fetch(url: URL | string, options: RequestInit, type: FetchResultTypes.Buffer): Promise<Buffer>;
|
|
21
|
+
export declare function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;
|
|
22
|
+
export declare function fetch(url: URL | string, options: RequestInit, type: FetchResultTypes.Blob): Promise<Blob>;
|
|
21
23
|
export declare function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;
|
|
22
24
|
export declare function fetch(url: URL | string, options: RequestInit, type: FetchResultTypes.Text): Promise<string>;
|
|
23
25
|
export declare function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;
|
|
24
26
|
export declare function fetch(url: URL | string, options: RequestInit, type: FetchResultTypes.Result): Promise<Response>;
|
|
25
|
-
export declare function fetch<R>(url: URL | string, options: RequestInit, type: FetchResultTypes): Promise<Response | Buffer | string | R>;
|
|
27
|
+
export declare function fetch<R>(url: URL | string, options: RequestInit, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;
|
|
26
28
|
//# sourceMappingURL=fetch.d.ts.map
|
package/dist/lib/fetch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/lib/fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/lib/fetch.ts"],"names":[],"mappings":";;AAKA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;;;;;GAYG;AACH,wBAAsB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,gBAAgB,CAAC,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAC5F,wBAAsB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,gBAAgB,CAAC,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAClH,wBAAsB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,IAAI,EAAE,gBAAgB,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC/F,wBAAsB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,gBAAgB,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AACrH,wBAAsB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,IAAI,EAAE,gBAAgB,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAC3F,wBAAsB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,gBAAgB,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AACjH,wBAAsB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,IAAI,EAAE,gBAAgB,CAAC,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC7F,wBAAsB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,gBAAgB,CAAC,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AACnH,wBAAsB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,IAAI,EAAE,gBAAgB,CAAC,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AACjG,wBAAsB,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,gBAAgB,CAAC,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AACvH,wBAAsB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC"}
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -10,9 +10,16 @@ export declare const enum FetchResultTypes {
|
|
|
10
10
|
*/
|
|
11
11
|
JSON = "json",
|
|
12
12
|
/**
|
|
13
|
-
* Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).
|
|
13
|
+
* Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).
|
|
14
|
+
* @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.
|
|
15
|
+
* If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!
|
|
14
16
|
*/
|
|
15
17
|
Buffer = "buffer",
|
|
18
|
+
/**
|
|
19
|
+
* Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
20
|
+
* @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.
|
|
21
|
+
*/
|
|
22
|
+
Blob = "blob",
|
|
16
23
|
/**
|
|
17
24
|
* Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).
|
|
18
25
|
*/
|
package/dist/lib/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,0BAAkB,gBAAgB;IACjC;;;;;OAKG;IACH,IAAI,SAAS;IACb
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,0BAAkB,gBAAgB;IACjC;;;;;OAKG;IACH,IAAI,SAAS;IACb;;;;OAIG;IACH,MAAM,WAAW;IACjB;;;OAGG;IACH,IAAI,SAAS;IACb;;OAEG;IACH,IAAI,SAAS;IACb;;OAEG;IACH,MAAM,WAAW;CACjB;AAED;;GAEG;AACH,0BAAkB,YAAY;IAC7B;;;OAGG;IACH,GAAG,QAAQ;IACX;;;OAGG;IACH,IAAI,SAAS;IACb;;;OAGG;IACH,IAAI,SAAS;IACb;;;OAGG;IACH,GAAG,QAAQ;IACX;;;OAGG;IACH,MAAM,WAAW;IACjB;;;OAGG;IACH,OAAO,YAAY;IACnB;;;OAGG;IACH,OAAO,YAAY;IACnB;;;OAGG;IACH,KAAK,UAAU;IACf;;;OAGG;IACH,KAAK,UAAU;CACf"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/fetch",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Tiny wrapper around
|
|
3
|
+
"version": "2.0.0-next.0efe80e2.0",
|
|
4
|
+
"description": "Tiny wrapper around cross-fetch for improved TypeScript and data type support",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"module": "dist/index.mjs",
|
|
9
|
+
"browser": "dist/index.umd.js",
|
|
10
|
+
"unpkg": "dist/index.umd.js",
|
|
9
11
|
"types": "dist/index.d.ts",
|
|
12
|
+
"typedocMain": "src/index.ts",
|
|
10
13
|
"exports": {
|
|
11
14
|
"import": "./dist/index.mjs",
|
|
12
15
|
"require": "./dist/index.js"
|
|
@@ -16,14 +19,12 @@
|
|
|
16
19
|
"scripts": {
|
|
17
20
|
"test": "jest",
|
|
18
21
|
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
|
|
19
|
-
"build": "
|
|
20
|
-
"postbuild": "gen-esm-wrapper dist/index.js dist/index.mjs",
|
|
22
|
+
"build": "rollup -c",
|
|
21
23
|
"start": "yarn build -w",
|
|
22
24
|
"prepublishOnly": "yarn build"
|
|
23
25
|
},
|
|
24
26
|
"dependencies": {
|
|
25
|
-
"
|
|
26
|
-
"tslib": "2.x"
|
|
27
|
+
"cross-fetch": "^3.1.4"
|
|
27
28
|
},
|
|
28
29
|
"repository": {
|
|
29
30
|
"type": "git",
|
|
@@ -35,8 +36,8 @@
|
|
|
35
36
|
"!dist/*.tsbuildinfo"
|
|
36
37
|
],
|
|
37
38
|
"engines": {
|
|
38
|
-
"node": ">=
|
|
39
|
-
"npm": ">=
|
|
39
|
+
"node": ">=v14.18.0",
|
|
40
|
+
"npm": ">=7.24.1"
|
|
40
41
|
},
|
|
41
42
|
"keywords": [
|
|
42
43
|
"@sapphire/fetch",
|
|
@@ -54,5 +55,5 @@
|
|
|
54
55
|
"publishConfig": {
|
|
55
56
|
"access": "public"
|
|
56
57
|
},
|
|
57
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "0efe80e2d454e7e468e822cc61dc532b8a6f7369"
|
|
58
59
|
}
|
package/dist/lib/QueryError.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var _QueryError_json;
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.QueryError = void 0;
|
|
5
|
-
const tslib_1 = require("tslib");
|
|
6
|
-
/**
|
|
7
|
-
* The QueryError class which is thrown by the `fetch` method
|
|
8
|
-
*/
|
|
9
|
-
class QueryError extends Error {
|
|
10
|
-
constructor(url, code, response, body) {
|
|
11
|
-
super(`Failed to request '${url}' with code ${code}.`);
|
|
12
|
-
/** The requested url. */
|
|
13
|
-
Object.defineProperty(this, "url", {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
configurable: true,
|
|
16
|
-
writable: true,
|
|
17
|
-
value: void 0
|
|
18
|
-
});
|
|
19
|
-
/** The HTTP status code. */
|
|
20
|
-
Object.defineProperty(this, "code", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
writable: true,
|
|
24
|
-
value: void 0
|
|
25
|
-
});
|
|
26
|
-
/** The returned response body as a string */
|
|
27
|
-
Object.defineProperty(this, "body", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: void 0
|
|
32
|
-
});
|
|
33
|
-
/** The original {@link Response} object */
|
|
34
|
-
Object.defineProperty(this, "response", {
|
|
35
|
-
enumerable: true,
|
|
36
|
-
configurable: true,
|
|
37
|
-
writable: true,
|
|
38
|
-
value: void 0
|
|
39
|
-
});
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
|
|
41
|
-
_QueryError_json.set(this, void 0);
|
|
42
|
-
this.url = typeof url === 'string' ? url : url.href;
|
|
43
|
-
this.code = code;
|
|
44
|
-
this.body = body;
|
|
45
|
-
this.response = response;
|
|
46
|
-
tslib_1.__classPrivateFieldSet(this, _QueryError_json, null, "f");
|
|
47
|
-
}
|
|
48
|
-
toJSON() {
|
|
49
|
-
var _a;
|
|
50
|
-
return (_a = tslib_1.__classPrivateFieldGet(this, _QueryError_json, "f")) !== null && _a !== void 0 ? _a : (tslib_1.__classPrivateFieldSet(this, _QueryError_json, JSON.parse(this.body), "f"));
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
exports.QueryError = QueryError;
|
|
54
|
-
_QueryError_json = new WeakMap();
|
|
55
|
-
//# sourceMappingURL=QueryError.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"QueryError.js","sourceRoot":"","sources":["../../src/lib/QueryError.ts"],"names":[],"mappings":";;;;;AAGA;;GAEG;AACH,MAAa,UAAW,SAAQ,KAAK;IAYpC,YAAmB,GAAiB,EAAE,IAAY,EAAE,QAAkB,EAAE,IAAY;QACnF,KAAK,CAAC,sBAAsB,GAAG,eAAe,IAAI,GAAG,CAAC,CAAC;QAZxD,yBAAyB;QACzB;;;;;WAA4B;QAC5B,4BAA4B;QAC5B;;;;;WAA6B;QAC7B,6CAA6C;QAC7C;;;;;WAA6B;QAC7B,2CAA2C;QAC3C;;;;;WAAmC;QACnC,4EAA4E;QAC5E,mCAAe;QAId,IAAI,CAAC,GAAG,GAAG,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC;QACpD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,+BAAA,IAAI,oBAAS,IAAI,MAAA,CAAC;IACnB,CAAC;IAEM,MAAM;;QACZ,OAAO,MAAA,+BAAA,IAAI,wBAAM,mCAAI,CAAC,+BAAA,IAAI,oBAAS,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAA,CAAC,CAAC;IAC3D,CAAC;CACD;AAxBD,gCAwBC"}
|
package/dist/lib/fetch.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetch = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
|
|
6
|
-
const QueryError_1 = require("./QueryError");
|
|
7
|
-
require("./types");
|
|
8
|
-
async function fetch(url, options, type) {
|
|
9
|
-
if (typeof options === 'undefined') {
|
|
10
|
-
options = {};
|
|
11
|
-
type = "json" /* JSON */;
|
|
12
|
-
}
|
|
13
|
-
else if (typeof options === 'string') {
|
|
14
|
-
type = options;
|
|
15
|
-
options = {};
|
|
16
|
-
}
|
|
17
|
-
else if (typeof type === 'undefined') {
|
|
18
|
-
type = "json" /* JSON */;
|
|
19
|
-
}
|
|
20
|
-
const result = await node_fetch_1.default(url, options);
|
|
21
|
-
if (!result.ok)
|
|
22
|
-
throw new QueryError_1.QueryError(url, result.status, result, await result.clone().text());
|
|
23
|
-
switch (type) {
|
|
24
|
-
case "result" /* Result */:
|
|
25
|
-
return result;
|
|
26
|
-
case "buffer" /* Buffer */:
|
|
27
|
-
return result.buffer();
|
|
28
|
-
case "json" /* JSON */:
|
|
29
|
-
return result.json();
|
|
30
|
-
case "text" /* Text */:
|
|
31
|
-
return result.text();
|
|
32
|
-
default:
|
|
33
|
-
throw new Error(`Unknown type "${type}"`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.fetch = fetch;
|
|
37
|
-
//# sourceMappingURL=fetch.js.map
|
package/dist/lib/fetch.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/lib/fetch.ts"],"names":[],"mappings":";;;;AAAA,oEAA8D;AAE9D,6CAA0C;AAC1C,mBAA2C;AAuBpC,KAAK,UAAU,KAAK,CAAC,GAAiB,EAAE,OAAwC,EAAE,IAAuB;IAC/G,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;QACnC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,oBAAwB,CAAC;KAC7B;SAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QACvC,IAAI,GAAG,OAAO,CAAC;QACf,OAAO,GAAG,EAAE,CAAC;KACb;SAAM,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;QACvC,IAAI,oBAAwB,CAAC;KAC7B;IAED,MAAM,MAAM,GAAa,MAAM,oBAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACvD,IAAI,CAAC,MAAM,CAAC,EAAE;QAAE,MAAM,IAAI,uBAAU,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;IAE9F,QAAQ,IAAI,EAAE;QACb;YACC,OAAO,MAAM,CAAC;QACf;YACC,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC;QACxB;YACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;QACtB;YACC,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;QACtB;YACC,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,GAAG,CAAC,CAAC;KAC3C;AACF,CAAC;AA1BD,sBA0BC"}
|
package/dist/lib/types.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FetchMethods = exports.FetchResultTypes = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* The supported return types for the `fetch` method
|
|
6
|
-
*/
|
|
7
|
-
var FetchResultTypes;
|
|
8
|
-
(function (FetchResultTypes) {
|
|
9
|
-
/**
|
|
10
|
-
* Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).
|
|
11
|
-
*
|
|
12
|
-
* You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define
|
|
13
|
-
* the JSON structure, otherwise the result will be `unknown`.
|
|
14
|
-
*/
|
|
15
|
-
FetchResultTypes["JSON"] = "json";
|
|
16
|
-
/**
|
|
17
|
-
* Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html). Similar to [`Body.blob()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/blob).
|
|
18
|
-
*/
|
|
19
|
-
FetchResultTypes["Buffer"] = "buffer";
|
|
20
|
-
/**
|
|
21
|
-
* Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).
|
|
22
|
-
*/
|
|
23
|
-
FetchResultTypes["Text"] = "text";
|
|
24
|
-
/**
|
|
25
|
-
* Returns the entire response and doesn't parse the `body` in any way.
|
|
26
|
-
*/
|
|
27
|
-
FetchResultTypes["Result"] = "result";
|
|
28
|
-
})(FetchResultTypes = exports.FetchResultTypes || (exports.FetchResultTypes = {}));
|
|
29
|
-
/**
|
|
30
|
-
* The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
|
|
31
|
-
*/
|
|
32
|
-
var FetchMethods;
|
|
33
|
-
(function (FetchMethods) {
|
|
34
|
-
/**
|
|
35
|
-
* The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.
|
|
36
|
-
* @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)
|
|
37
|
-
*/
|
|
38
|
-
FetchMethods["Get"] = "GET";
|
|
39
|
-
/**
|
|
40
|
-
* The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.
|
|
41
|
-
* @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)
|
|
42
|
-
*/
|
|
43
|
-
FetchMethods["Head"] = "HEAD";
|
|
44
|
-
/**
|
|
45
|
-
* 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.
|
|
46
|
-
* @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)
|
|
47
|
-
*/
|
|
48
|
-
FetchMethods["Post"] = "POST";
|
|
49
|
-
/**
|
|
50
|
-
* The `PUT` method replaces all current representations of the target resource with the request payload.
|
|
51
|
-
* @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)
|
|
52
|
-
*/
|
|
53
|
-
FetchMethods["Put"] = "PUT";
|
|
54
|
-
/**
|
|
55
|
-
* The `DELETE` method deletes the specified resource.
|
|
56
|
-
* @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)
|
|
57
|
-
*/
|
|
58
|
-
FetchMethods["Delete"] = "DELETE";
|
|
59
|
-
/**
|
|
60
|
-
* The `CONNECT` method establishes a tunnel to the server identified by the target resource
|
|
61
|
-
* @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)
|
|
62
|
-
*/
|
|
63
|
-
FetchMethods["Connect"] = "CONNECT";
|
|
64
|
-
/**
|
|
65
|
-
* The `OPTIONS` method is used to describe the communication options for the target resource.
|
|
66
|
-
* @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)
|
|
67
|
-
*/
|
|
68
|
-
FetchMethods["Options"] = "OPTIONS";
|
|
69
|
-
/**
|
|
70
|
-
* The `TRACE` method performs a message loop-back test along the path to the target resource.
|
|
71
|
-
* @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)
|
|
72
|
-
*/
|
|
73
|
-
FetchMethods["Trace"] = "TRACE";
|
|
74
|
-
/**
|
|
75
|
-
* The `PATCH` method is used to apply partial modifications to a resource.
|
|
76
|
-
* @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)
|
|
77
|
-
*/
|
|
78
|
-
FetchMethods["Patch"] = "PATCH";
|
|
79
|
-
})(FetchMethods = exports.FetchMethods || (exports.FetchMethods = {}));
|
|
80
|
-
//# sourceMappingURL=types.js.map
|
package/dist/lib/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,IAAkB,gBAoBjB;AApBD,WAAkB,gBAAgB;IACjC;;;;;OAKG;IACH,iCAAa,CAAA;IACb;;OAEG;IACH,qCAAiB,CAAA;IACjB;;OAEG;IACH,iCAAa,CAAA;IACb;;OAEG;IACH,qCAAiB,CAAA;AAClB,CAAC,EApBiB,gBAAgB,GAAhB,wBAAgB,KAAhB,wBAAgB,QAoBjC;AAED;;GAEG;AACH,IAAkB,YA8CjB;AA9CD,WAAkB,YAAY;IAC7B;;;OAGG;IACH,2BAAW,CAAA;IACX;;;OAGG;IACH,6BAAa,CAAA;IACb;;;OAGG;IACH,6BAAa,CAAA;IACb;;;OAGG;IACH,2BAAW,CAAA;IACX;;;OAGG;IACH,iCAAiB,CAAA;IACjB;;;OAGG;IACH,mCAAmB,CAAA;IACnB;;;OAGG;IACH,mCAAmB,CAAA;IACnB;;;OAGG;IACH,+BAAe,CAAA;IACf;;;OAGG;IACH,+BAAe,CAAA;AAChB,CAAC,EA9CiB,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QA8C7B"}
|