@sapphire/fetch 2.4.1 β†’ 2.4.2-next.0220a57.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 CHANGED
@@ -136,7 +136,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
136
136
  <table>
137
137
  <tr>
138
138
  <td align="center"><a href="https://favware.tech/"><img src="https://avatars3.githubusercontent.com/u/4019718?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jeroen Claassens</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=favna" title="Code">πŸ’»</a> <a href="#infra-favna" title="Infrastructure (Hosting, Build-Tools, etc)">πŸš‡</a> <a href="#projectManagement-favna" title="Project Management">πŸ“†</a> <a href="https://github.com/sapphiredev/utilities/commits?author=favna" title="Documentation">πŸ“–</a> <a href="https://github.com/sapphiredev/utilities/commits?author=favna" title="Tests">⚠️</a></td>
139
- <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>
139
+ <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>Aura 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>
140
140
  <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>
141
141
  <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>
142
142
  <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>
@@ -146,7 +146,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
146
146
  <tr>
147
147
  <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>
148
148
  <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>
149
- <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>
149
+ <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> <a href="#tool-RealShadowNova" title="Tools">πŸ”§</a></td>
150
150
  <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>
151
151
  <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>
152
152
  <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>
@@ -181,6 +181,17 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
181
181
  </tr>
182
182
  <tr>
183
183
  <td align="center"><a href="https://github.com/MajesticString"><img src="https://avatars.githubusercontent.com/u/66224939?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Harry Allen</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=MajesticString" title="Documentation">πŸ“–</a></td>
184
+ <td align="center"><a href="https://github.com/EvolutionX-10"><img src="https://avatars.githubusercontent.com/u/85353424?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Evo</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=EvolutionX-10" title="Code">πŸ’»</a></td>
185
+ <td align="center"><a href="https://enes.ovh/"><img src="https://avatars.githubusercontent.com/u/61084101?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Enes GenΓ§</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=enxg" title="Code">πŸ’»</a></td>
186
+ <td align="center"><a href="https://github.com/muchnameless"><img src="https://avatars.githubusercontent.com/u/12682826?v=4?s=100" width="100px;" alt=""/><br /><sub><b>muchnameless</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=muchnameless" title="Code">πŸ’»</a></td>
187
+ <td align="center"><a href="https://github.com/r-priyam"><img src="https://avatars.githubusercontent.com/u/50884372?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Priyam</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=r-priyam" title="Code">πŸ’»</a></td>
188
+ <td align="center"><a href="https://github.com/legendhimslef"><img src="https://avatars.githubusercontent.com/u/69213593?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Voxelli</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=legendhimslef" title="Code">πŸ’»</a></td>
189
+ <td align="center"><a href="https://github.com/CitTheDev"><img src="https://avatars.githubusercontent.com/u/94020875?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cit The Dev</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=CitTheDev" title="Code">πŸ’»</a></td>
190
+ </tr>
191
+ <tr>
192
+ <td align="center"><a href="https://www.goestav.com/"><img src="https://avatars.githubusercontent.com/u/27970303?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Goestav</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=goestav" title="Code">πŸ’»</a></td>
193
+ <td align="center"><a href="https://github.com/didinele"><img src="https://avatars.githubusercontent.com/u/27137376?v=4?s=100" width="100px;" alt=""/><br /><sub><b>DD</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=didinele" title="Code">πŸ’»</a></td>
194
+ <td align="center"><a href="https://steamcommunity.com/id/06000208"><img src="https://avatars.githubusercontent.com/u/52764066?v=4?s=100" width="100px;" alt=""/><br /><sub><b>amber</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=06000208" title="Code">πŸ’»</a></td>
184
195
  </tr>
185
196
  </table>
186
197
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,312 @@
1
- export * from './lib/fetch';
2
- export * from './lib/QueryError';
3
- export * from './lib/types';
4
- //# sourceMappingURL=index.d.ts.map
1
+ /**
2
+ * The supported return types for the `fetch` method
3
+ */
4
+ declare enum FetchResultTypes {
5
+ /**
6
+ * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).
7
+ *
8
+ * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define
9
+ * the JSON structure, otherwise the result will be `unknown`.
10
+ */
11
+ JSON = "json",
12
+ /**
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!
16
+ */
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",
23
+ /**
24
+ * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).
25
+ */
26
+ Text = "text",
27
+ /**
28
+ * Returns the entire response and doesn't parse the `body` in any way.
29
+ */
30
+ Result = "result"
31
+ }
32
+ /**
33
+ * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
34
+ */
35
+ declare enum FetchMethods {
36
+ /**
37
+ * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.
38
+ * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)
39
+ */
40
+ Get = "GET",
41
+ /**
42
+ * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.
43
+ * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)
44
+ */
45
+ Head = "HEAD",
46
+ /**
47
+ * 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.
48
+ * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)
49
+ */
50
+ Post = "POST",
51
+ /**
52
+ * The `PUT` method replaces all current representations of the target resource with the request payload.
53
+ * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)
54
+ */
55
+ Put = "PUT",
56
+ /**
57
+ * The `DELETE` method deletes the specified resource.
58
+ * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)
59
+ */
60
+ Delete = "DELETE",
61
+ /**
62
+ * The `CONNECT` method establishes a tunnel to the server identified by the target resource
63
+ * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)
64
+ */
65
+ Connect = "CONNECT",
66
+ /**
67
+ * The `OPTIONS` method is used to describe the communication options for the target resource.
68
+ * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)
69
+ */
70
+ Options = "OPTIONS",
71
+ /**
72
+ * The `TRACE` method performs a message loop-back test along the path to the target resource.
73
+ * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)
74
+ */
75
+ Trace = "TRACE",
76
+ /**
77
+ * The `PATCH` method is used to apply partial modifications to a resource.
78
+ * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)
79
+ */
80
+ Patch = "PATCH"
81
+ }
82
+ /**
83
+ * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).
84
+ * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type
85
+ * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).
86
+ */
87
+ declare enum FetchMediaContentTypes {
88
+ /**
89
+ * The `audio/aac` media content type.
90
+ * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)
91
+ */
92
+ AudioAac = "audio/aac",
93
+ /**
94
+ * The `audio/mp4` media content type.
95
+ * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)
96
+ * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]
97
+ */
98
+ AudioMp4 = "audio/mp4",
99
+ /**
100
+ * The `audio/mpeg` media content type.
101
+ * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)
102
+ * @see [[RFC3003](https://www.iana.org/go/rfc3003)]
103
+ */
104
+ AudioMpeg = "audio/mpeg",
105
+ /**
106
+ * The `audio/ogg` media content type.
107
+ * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)
108
+ * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]
109
+ */
110
+ AudioOgg = "audio/ogg",
111
+ /**
112
+ * The `audio/opus` media content type.
113
+ * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)
114
+ * @see [[RFC7587](https://www.iana.org/go/rfc7587)]
115
+ */
116
+ AudioOpus = "audio/opus",
117
+ /**
118
+ * The `audio/vorbis` media content type.
119
+ * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)
120
+ * @see [[RFC5215](https://www.iana.org/go/rfc5215)]
121
+ */
122
+ AudioVorbis = "audio/vorbis",
123
+ /**
124
+ * The `audio/wav` media content type.
125
+ */
126
+ AudioWav = "audio/wav",
127
+ /**
128
+ * The `audio/webm` media content type.
129
+ */
130
+ AudioWebm = "audio/webm",
131
+ /**
132
+ * The `font/otf` media content type.
133
+ * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)
134
+ * @see [[RFC8081](https://www.iana.org/go/rfc8081)]
135
+ */
136
+ FontOtf = "font/otf",
137
+ /**
138
+ * The `font/ttf` media content type.
139
+ * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)
140
+ * @see [[RFC8081](https://www.iana.org/go/rfc8081)]
141
+ */
142
+ FontTtf = "font/ttf",
143
+ /**
144
+ * The `font/woff` media content type.
145
+ * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)
146
+ * @see [[RFC8081](https://www.iana.org/go/rfc8081)]
147
+ */
148
+ FontWoff = "font/woff",
149
+ /**
150
+ * The `font/woff2` media content type.
151
+ * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)
152
+ * @see [[RFC8081](https://www.iana.org/go/rfc8081)]
153
+ */
154
+ FontWoff2 = "font/woff2",
155
+ /**
156
+ * The `multipart/form-data` media content type.
157
+ * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)
158
+ * @see [[RFC7578](https://www.iana.org/go/rfc7578)]
159
+ */
160
+ FormData = "multipart/form-data",
161
+ /**
162
+ * The `x-www-form-urlencoded` media content type.
163
+ * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)
164
+ */
165
+ FormURLEncoded = "application/x-www-form-urlencoded",
166
+ /**
167
+ * The `image/apng` media content type.
168
+ * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)
169
+ */
170
+ ImageAPNG = "image/apng",
171
+ /**
172
+ * The `image/gif` media content type.
173
+ * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)
174
+ * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]
175
+ */
176
+ ImageGIF = "image/gif",
177
+ /**
178
+ * The `image/jpeg` media content type.
179
+ * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)
180
+ * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]
181
+ */
182
+ ImageJPEG = "image/jpeg",
183
+ /**
184
+ * The `image/png` media content type.
185
+ * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)
186
+ */
187
+ ImagePNG = "image/png",
188
+ /**
189
+ * The `image/webp` media content type.
190
+ * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)
191
+ */
192
+ ImageWEBP = "image/webp",
193
+ /**
194
+ * The `application/json` media content type.
195
+ * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)
196
+ * @see [[RFC8259](https://www.iana.org/go/rfc8259)]
197
+ */
198
+ JSON = "application/json",
199
+ /**
200
+ * The `application/javascript` media content type.
201
+ * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)
202
+ * @see [[RFC4329](https://www.iana.org/go/rfc4329)]
203
+ */
204
+ JavaScript = "application/javascript",
205
+ /**
206
+ * The `application/octet-stream` media content type.
207
+ * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)
208
+ * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]
209
+ */
210
+ OctetStream = "application/octet-stream",
211
+ /**
212
+ * The `text/css` media content type.
213
+ * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)
214
+ * @see [[RFC2318](https://www.iana.org/go/rfc2318)]
215
+ */
216
+ TextCSS = "text/css",
217
+ /**
218
+ * The `text/HTML` media content type.
219
+ * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)
220
+ */
221
+ TextHTML = "text/html",
222
+ /**
223
+ * The `text/plain` media content type.
224
+ * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)
225
+ * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]
226
+ */
227
+ TextPlain = "text/plain",
228
+ /**
229
+ * The `video/h264` media content type.
230
+ * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)
231
+ * @see [[RFC6184](https://www.iana.org/go/rfc6184)]
232
+ */
233
+ VideoH264 = "video/h264",
234
+ /**
235
+ * The `video/h265` media content type.
236
+ * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)
237
+ * @see [[RFC7798](https://www.iana.org/go/rfc7798)]
238
+ */
239
+ VideoH265 = "video/h265",
240
+ /**
241
+ * The `video/mp4` media content type.
242
+ * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)
243
+ * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]
244
+ */
245
+ VideoMp4 = "video/mp4",
246
+ /**
247
+ * The `video/ogg` media content type.
248
+ * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)
249
+ * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]
250
+ */
251
+ VideoOgg = "video/ogg",
252
+ /**
253
+ * The `video/webm` media content type.
254
+ */
255
+ VideoWebm = "video/webm",
256
+ /**
257
+ * The `application/xml` media content type.
258
+ * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)
259
+ * @see [[RFC7303](https://www.iana.org/go/rfc7303)]
260
+ */
261
+ XML = "application/xml"
262
+ }
263
+ interface RequestOptions extends Omit<RequestInit, 'body'> {
264
+ body?: BodyInit | Record<any, any>;
265
+ }
266
+
267
+ /// <reference lib="dom" />
268
+
269
+ /**
270
+ * Performs an HTTP(S) fetch
271
+ * @param url The URL to send the request to. Can be either a `string` or an `URL` object.
272
+ * `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`.
273
+ * @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}
274
+ * @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.
275
+ * @returns The return type is determined by the provided `type`.
276
+ * - 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.
277
+ * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).
278
+ * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
279
+ * - When using `FetchResultTypes.Text` the return type will be a `string`
280
+ * - 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)
281
+ */
282
+ declare function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;
283
+ declare function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;
284
+ declare function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;
285
+ declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;
286
+ declare function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;
287
+ declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;
288
+ declare function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;
289
+ declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;
290
+ declare function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;
291
+ declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;
292
+ declare function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;
293
+
294
+ /// <reference lib="dom" />
295
+ /**
296
+ * The QueryError class which is thrown by the `fetch` method
297
+ */
298
+ declare class QueryError extends Error {
299
+ #private;
300
+ /** The requested url. */
301
+ readonly url: string;
302
+ /** The HTTP status code. */
303
+ readonly code: number;
304
+ /** The returned response body as a string */
305
+ readonly body: string;
306
+ /** The original {@link Response} object */
307
+ readonly response: Response;
308
+ constructor(url: string, code: number, response: Response, body: string);
309
+ toJSON(): any;
310
+ }
311
+
312
+ export { FetchMediaContentTypes, FetchMethods, FetchResultTypes, QueryError, RequestOptions, fetch };
@@ -1,24 +1,9 @@
1
- "use strict";
2
- var SapphireFetch = (() => {
1
+ var SapphireFetch = (function (exports) {
2
+ 'use strict';
3
+
3
4
  var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
- var __export = (target, all) => {
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(to, key) && key !== except)
17
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
- }
19
- return to;
20
- };
21
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
7
  var __publicField = (obj, key, value) => {
23
8
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
9
  return value;
@@ -42,16 +27,6 @@ var SapphireFetch = (() => {
42
27
  return value;
43
28
  };
44
29
 
45
- // src/index.ts
46
- var src_exports = {};
47
- __export(src_exports, {
48
- FetchMediaContentTypes: () => FetchMediaContentTypes,
49
- FetchMethods: () => FetchMethods,
50
- FetchResultTypes: () => FetchResultTypes,
51
- QueryError: () => QueryError,
52
- fetch: () => fetch
53
- });
54
-
55
30
  // src/lib/QueryError.ts
56
31
  var _json;
57
32
  var QueryError = class extends Error {
@@ -166,6 +141,15 @@ var SapphireFetch = (() => {
166
141
  }
167
142
  }
168
143
  __name(fetch, "fetch");
169
- return __toCommonJS(src_exports);
170
- })();
144
+
145
+ exports.FetchMediaContentTypes = FetchMediaContentTypes;
146
+ exports.FetchMethods = FetchMethods;
147
+ exports.FetchResultTypes = FetchResultTypes;
148
+ exports.QueryError = QueryError;
149
+ exports.fetch = fetch;
150
+
151
+ return exports;
152
+
153
+ })({});
154
+ //# sourceMappingURL=out.js.map
171
155
  //# sourceMappingURL=index.global.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/lib/QueryError.ts","../src/lib/types.ts","../src/lib/fetch.ts"],"sourcesContent":["export * from './lib/fetch';\nexport * from './lib/QueryError';\nexport * from './lib/types';\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAMO,MAAM,aAAN,cAAyB,MAAM;AAAA,IAYrC,AAAO,YAAY,KAAa,MAAc,UAAoB,MAAc;AAC/E,YAAM,sBAAsB,kBAAkB,OAAO;AAXtD,0BAAgB;AAEhB,0BAAgB;AAEhB,0BAAgB;AAEhB,0BAAgB;AAEhB;AAIC,WAAK,MAAM;AACX,WAAK,OAAO;AACZ,WAAK,OAAO;AACZ,WAAK,WAAW;AAChB,yBAAK,OAAQ;AAAA,IACd;AAAA,IAEA,AAAO,SAAS;AACf,aAAO,mBAAK,UAAU,mBAAK,OAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,IACxD;AAAA,EACD;AAxBa;AAUZ;;;ACbM,MAAK,mBAAL,kBAAK,sBAAL;AAON,gCAAO;AAMP,kCAAS;AAKT,gCAAO;AAIP,gCAAO;AAIP,kCAAS;AA1BE;AAAA;AAgCL,MAAK,eAAL,kBAAK,kBAAL;AAKN,2BAAM;AAKN,4BAAO;AAKP,4BAAO;AAKP,2BAAM;AAKN,8BAAS;AAKT,+BAAU;AAKV,+BAAU;AAKV,6BAAQ;AAKR,6BAAQ;AA7CG;AAAA;AAqDL,MAAK,yBAAL,kBAAK,4BAAL;AAKN,0CAAW;AAMX,0CAAW;AAMX,2CAAY;AAMZ,0CAAW;AAMX,2CAAY;AAMZ,6CAAc;AAId,0CAAW;AAIX,2CAAY;AAMZ,yCAAU;AAMV,yCAAU;AAMV,0CAAW;AAMX,2CAAY;AAMZ,0CAAW;AAKX,gDAAiB;AAKjB,2CAAY;AAMZ,0CAAW;AAMX,2CAAY;AAKZ,0CAAW;AAKX,2CAAY;AAMZ,sCAAO;AAMP,4CAAa;AAMb,6CAAc;AAMd,yCAAU;AAKV,0CAAW;AAMX,2CAAY;AAMZ,2CAAY;AAMZ,2CAAY;AAMZ,0CAAW;AAMX,0CAAW;AAIX,2CAAY;AAMZ,qCAAM;AA9KK;AAAA;;;ACzDZ,uBAA4B,KAAmB,SAA6C,MAAyB;AACpH,QAAI,OAAO,YAAY,aAAa;AACnC,gBAAU,CAAC;AACX,aAAO;AAAA,IACR,WAAW,OAAO,YAAY,UAAU;AACvC,aAAO;AACP,gBAAU,CAAC;AAAA,IACZ,WAAW,OAAO,SAAS,aAAa;AACvC,aAAO;AAAA,IACR;AAEA,QAAI,EAAE,SAAS;AAEf,QAAI,QAAQ,OAAO,SAAS,UAAU;AACrC,aAAO,KAAK,UAAU,IAAI;AAAA,IAC3B;AAGA,UAAM,YAAY,OAAO,GAAG;AAE5B,UAAM,SAAmB,MAAM,WAAW,MAAM,WAAW,EAAE,GAAG,SAAS,KAAK,CAAC;AAC/E,QAAI,CAAC,OAAO;AAAI,YAAM,IAAI,WAAW,WAAW,OAAO,QAAQ,QAAQ,MAAM,OAAO,MAAM,EAAE,KAAK,CAAC;AAElG,YAAQ;AAAA,WACF;AACJ,eAAO;AAAA,WACH;AACJ,eAAO,OAAO,KAAK,MAAO,OAAM,OAAO,KAAK,GAAG,YAAY,CAAC;AAAA,WACxD;AACJ,eAAO,OAAO,KAAK;AAAA,WACf;AACJ,eAAO,OAAO,KAAK;AAAA,WACf;AACJ,eAAO,OAAO,KAAK;AAAA;AAEnB,cAAM,IAAI,MAAM,iBAAiB,OAAO;AAAA;AAAA,EAE3C;AArCsB;","names":[]}
1
+ {"version":3,"sources":["../src/lib/QueryError.ts","../src/lib/types.ts","../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":"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","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"]}
package/dist/index.js CHANGED
@@ -1,25 +1,9 @@
1
- "use strict";
2
- "use strict";
1
+ 'use strict';
2
+
3
3
  globalThis && globalThis.fetch ? globalThis.fetch : globalThis.fetch = require("cross-fetch");
4
4
  var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
- var __export = (target, all) => {
11
- for (var name in all)
12
- __defProp(target, name, { get: all[name], enumerable: true });
13
- };
14
- var __copyProps = (to, from, except, desc) => {
15
- if (from && typeof from === "object" || typeof from === "function") {
16
- for (let key of __getOwnPropNames(from))
17
- if (!__hasOwnProp.call(to, key) && key !== except)
18
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
- }
20
- return to;
21
- };
22
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
7
  var __publicField = (obj, key, value) => {
24
8
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
25
9
  return value;
@@ -43,17 +27,6 @@ var __privateSet = (obj, member, value, setter) => {
43
27
  return value;
44
28
  };
45
29
 
46
- // src/index.ts
47
- var src_exports = {};
48
- __export(src_exports, {
49
- FetchMediaContentTypes: () => FetchMediaContentTypes,
50
- FetchMethods: () => FetchMethods,
51
- FetchResultTypes: () => FetchResultTypes,
52
- QueryError: () => QueryError,
53
- fetch: () => fetch
54
- });
55
- module.exports = __toCommonJS(src_exports);
56
-
57
30
  // src/lib/QueryError.ts
58
31
  var _json;
59
32
  var QueryError = class extends Error {
@@ -168,12 +141,11 @@ async function fetch(url, options, type) {
168
141
  }
169
142
  }
170
143
  __name(fetch, "fetch");
171
- // Annotate the CommonJS export names for ESM import in node:
172
- 0 && (module.exports = {
173
- FetchMediaContentTypes,
174
- FetchMethods,
175
- FetchResultTypes,
176
- QueryError,
177
- fetch
178
- });
144
+
145
+ exports.FetchMediaContentTypes = FetchMediaContentTypes;
146
+ exports.FetchMethods = FetchMethods;
147
+ exports.FetchResultTypes = FetchResultTypes;
148
+ exports.QueryError = QueryError;
149
+ exports.fetch = fetch;
150
+ //# sourceMappingURL=out.js.map
179
151
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/lib/QueryError.ts","../src/lib/types.ts","../src/lib/fetch.ts"],"sourcesContent":["export * from './lib/fetch';\nexport * from './lib/QueryError';\nexport * from './lib/types';\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAMO,IAAM,aAAN,cAAyB,MAAM;AAAA,EAYrC,AAAO,YAAY,KAAa,MAAc,UAAoB,MAAc;AAC/E,UAAM,sBAAsB,kBAAkB,OAAO;AAXtD,wBAAgB;AAEhB,wBAAgB;AAEhB,wBAAgB;AAEhB,wBAAgB;AAEhB;AAIC,SAAK,MAAM;AACX,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,uBAAK,OAAQ;AAAA,EACd;AAAA,EAEA,AAAO,SAAS;AACf,WAAO,mBAAK,UAAU,mBAAK,OAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACxD;AACD;AAxBa;AAUZ;;;ACbM,IAAK,mBAAL,kBAAK,sBAAL;AAON,8BAAO;AAMP,gCAAS;AAKT,8BAAO;AAIP,8BAAO;AAIP,gCAAS;AA1BE;AAAA;AAgCL,IAAK,eAAL,kBAAK,kBAAL;AAKN,yBAAM;AAKN,0BAAO;AAKP,0BAAO;AAKP,yBAAM;AAKN,4BAAS;AAKT,6BAAU;AAKV,6BAAU;AAKV,2BAAQ;AAKR,2BAAQ;AA7CG;AAAA;AAqDL,IAAK,yBAAL,kBAAK,4BAAL;AAKN,wCAAW;AAMX,wCAAW;AAMX,yCAAY;AAMZ,wCAAW;AAMX,yCAAY;AAMZ,2CAAc;AAId,wCAAW;AAIX,yCAAY;AAMZ,uCAAU;AAMV,uCAAU;AAMV,wCAAW;AAMX,yCAAY;AAMZ,wCAAW;AAKX,8CAAiB;AAKjB,yCAAY;AAMZ,wCAAW;AAMX,yCAAY;AAKZ,wCAAW;AAKX,yCAAY;AAMZ,oCAAO;AAMP,0CAAa;AAMb,2CAAc;AAMd,uCAAU;AAKV,wCAAW;AAMX,yCAAY;AAMZ,yCAAY;AAMZ,yCAAY;AAMZ,wCAAW;AAMX,wCAAW;AAIX,yCAAY;AAMZ,mCAAM;AA9KK;AAAA;;;ACzDZ,qBAA4B,KAAmB,SAA6C,MAAyB;AACpH,MAAI,OAAO,YAAY,aAAa;AACnC,cAAU,CAAC;AACX,WAAO;AAAA,EACR,WAAW,OAAO,YAAY,UAAU;AACvC,WAAO;AACP,cAAU,CAAC;AAAA,EACZ,WAAW,OAAO,SAAS,aAAa;AACvC,WAAO;AAAA,EACR;AAEA,MAAI,EAAE,SAAS;AAEf,MAAI,QAAQ,OAAO,SAAS,UAAU;AACrC,WAAO,KAAK,UAAU,IAAI;AAAA,EAC3B;AAGA,QAAM,YAAY,OAAO,GAAG;AAE5B,QAAM,SAAmB,MAAM,WAAW,MAAM,WAAW,EAAE,GAAG,SAAS,KAAK,CAAC;AAC/E,MAAI,CAAC,OAAO;AAAI,UAAM,IAAI,WAAW,WAAW,OAAO,QAAQ,QAAQ,MAAM,OAAO,MAAM,EAAE,KAAK,CAAC;AAElG,UAAQ;AAAA,SACF;AACJ,aAAO;AAAA,SACH;AACJ,aAAO,OAAO,KAAK,MAAO,OAAM,OAAO,KAAK,GAAG,YAAY,CAAC;AAAA,SACxD;AACJ,aAAO,OAAO,KAAK;AAAA,SACf;AACJ,aAAO,OAAO,KAAK;AAAA,SACf;AACJ,aAAO,OAAO,KAAK;AAAA;AAEnB,YAAM,IAAI,MAAM,iBAAiB,OAAO;AAAA;AAE3C;AArCsB;","names":[]}
1
+ {"version":3,"sources":["../src/lib/QueryError.ts","../src/lib/types.ts","../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":"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","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"]}
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
- import crossFetch from "cross-fetch";
1
+ import crossFetch from 'cross-fetch';
2
+
2
3
  globalThis && globalThis.fetch ? globalThis.fetch : globalThis.fetch = crossFetch;
3
4
  var __defProp = Object.defineProperty;
4
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -140,11 +141,7 @@ async function fetch(url, options, type) {
140
141
  }
141
142
  }
142
143
  __name(fetch, "fetch");
143
- export {
144
- FetchMediaContentTypes,
145
- FetchMethods,
146
- FetchResultTypes,
147
- QueryError,
148
- fetch
149
- };
144
+
145
+ export { FetchMediaContentTypes, FetchMethods, FetchResultTypes, QueryError, fetch };
146
+ //# sourceMappingURL=out.js.map
150
147
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"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\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 enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAMO,IAAM,aAAN,cAAyB,MAAM;AAAA,EAYrC,AAAO,YAAY,KAAa,MAAc,UAAoB,MAAc;AAC/E,UAAM,sBAAsB,kBAAkB,OAAO;AAXtD,wBAAgB;AAEhB,wBAAgB;AAEhB,wBAAgB;AAEhB,wBAAgB;AAEhB;AAIC,SAAK,MAAM;AACX,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,uBAAK,OAAQ;AAAA,EACd;AAAA,EAEA,AAAO,SAAS;AACf,WAAO,mBAAK,UAAU,mBAAK,OAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACxD;AACD;AAxBa;AAUZ;;;ACbM,IAAK,mBAAL,kBAAK,sBAAL;AAON,8BAAO;AAMP,gCAAS;AAKT,8BAAO;AAIP,8BAAO;AAIP,gCAAS;AA1BE;AAAA;AAgCL,IAAK,eAAL,kBAAK,kBAAL;AAKN,yBAAM;AAKN,0BAAO;AAKP,0BAAO;AAKP,yBAAM;AAKN,4BAAS;AAKT,6BAAU;AAKV,6BAAU;AAKV,2BAAQ;AAKR,2BAAQ;AA7CG;AAAA;AAqDL,IAAK,yBAAL,kBAAK,4BAAL;AAKN,wCAAW;AAMX,wCAAW;AAMX,yCAAY;AAMZ,wCAAW;AAMX,yCAAY;AAMZ,2CAAc;AAId,wCAAW;AAIX,yCAAY;AAMZ,uCAAU;AAMV,uCAAU;AAMV,wCAAW;AAMX,yCAAY;AAMZ,wCAAW;AAKX,8CAAiB;AAKjB,yCAAY;AAMZ,wCAAW;AAMX,yCAAY;AAKZ,wCAAW;AAKX,yCAAY;AAMZ,oCAAO;AAMP,0CAAa;AAMb,2CAAc;AAMd,uCAAU;AAKV,wCAAW;AAMX,yCAAY;AAMZ,yCAAY;AAMZ,yCAAY;AAMZ,wCAAW;AAMX,wCAAW;AAIX,yCAAY;AAMZ,mCAAM;AA9KK;AAAA;;;ACzDZ,qBAA4B,KAAmB,SAA6C,MAAyB;AACpH,MAAI,OAAO,YAAY,aAAa;AACnC,cAAU,CAAC;AACX,WAAO;AAAA,EACR,WAAW,OAAO,YAAY,UAAU;AACvC,WAAO;AACP,cAAU,CAAC;AAAA,EACZ,WAAW,OAAO,SAAS,aAAa;AACvC,WAAO;AAAA,EACR;AAEA,MAAI,EAAE,SAAS;AAEf,MAAI,QAAQ,OAAO,SAAS,UAAU;AACrC,WAAO,KAAK,UAAU,IAAI;AAAA,EAC3B;AAGA,QAAM,YAAY,OAAO,GAAG;AAE5B,QAAM,SAAmB,MAAM,WAAW,MAAM,WAAW,EAAE,GAAG,SAAS,KAAK,CAAC;AAC/E,MAAI,CAAC,OAAO;AAAI,UAAM,IAAI,WAAW,WAAW,OAAO,QAAQ,QAAQ,MAAM,OAAO,MAAM,EAAE,KAAK,CAAC;AAElG,UAAQ;AAAA,SACF;AACJ,aAAO;AAAA,SACH;AACJ,aAAO,OAAO,KAAK,MAAO,OAAM,OAAO,KAAK,GAAG,YAAY,CAAC;AAAA,SACxD;AACJ,aAAO,OAAO,KAAK;AAAA,SACf;AACJ,aAAO,OAAO,KAAK;AAAA,SACf;AACJ,aAAO,OAAO,KAAK;AAAA;AAEnB,YAAM,IAAI,MAAM,iBAAiB,OAAO;AAAA;AAE3C;AArCsB;","names":[]}
1
+ {"version":3,"sources":["../src/lib/QueryError.ts","../src/lib/types.ts","../src/lib/fetch.ts"],"names":["FetchResultTypes","FetchMethods","FetchMediaContentTypes"],"mappings":"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","sourcesContent":["// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\n/**\n * The QueryError class which is thrown by the `fetch` method\n */\nexport class QueryError extends Error {\n\t/** The requested url. */\n\tpublic readonly url: string;\n\t/** The HTTP status code. */\n\tpublic readonly code: number;\n\t/** The returned response body as a string */\n\tpublic readonly body: string;\n\t/** The original {@link Response} object */\n\tpublic readonly response: Response;\n\n\t#json: unknown;\n\n\tpublic constructor(url: string, code: number, response: Response, body: string) {\n\t\tsuper(`Failed to request '${url}' with code ${code}.`);\n\t\tthis.url = url;\n\t\tthis.code = code;\n\t\tthis.body = body;\n\t\tthis.response = response;\n\t\tthis.#json = null;\n\t}\n\n\tpublic toJSON() {\n\t\treturn this.#json ?? (this.#json = JSON.parse(this.body));\n\t}\n}\n","/**\n * The supported return types for the `fetch` method\n */\nexport enum FetchResultTypes {\n\t/**\n\t * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).\n\t *\n\t * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define\n\t * the JSON structure, otherwise the result will be `unknown`.\n\t */\n\tJSON = 'json',\n\t/**\n\t * Returns only the body, as a [Buffer](https://nodejs.org/api/buffer.html).\n\t * @remark Does not work in a Browser environment. For browsers use {@link FetchResultTypes.Blob} instead.\n\t * If you use this type in a Browsers environment a {@link ReferenceError `ReferenceError: Buffer is not defined`} will be thrown!\n\t */\n\tBuffer = 'buffer',\n\t/**\n\t * Returns only the body, as a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n\t * @remark For NodeJS environment other `FetchResultTypes` are recommended, but you can use a Blob if you want to.\n\t */\n\tBlob = 'blob',\n\t/**\n\t * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).\n\t */\n\tText = 'text',\n\t/**\n\t * Returns the entire response and doesn't parse the `body` in any way.\n\t */\n\tResult = 'result'\n}\n\n/**\n * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n */\nexport enum FetchMethods {\n\t/**\n\t * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.\n\t * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)\n\t */\n\tGet = 'GET',\n\t/**\n\t * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.\n\t * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)\n\t */\n\tHead = 'HEAD',\n\t/**\n\t * The `POST` method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.\n\t * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)\n\t */\n\tPost = 'POST',\n\t/**\n\t * The `PUT` method replaces all current representations of the target resource with the request payload.\n\t * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)\n\t */\n\tPut = 'PUT',\n\t/**\n\t * The `DELETE` method deletes the specified resource.\n\t * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)\n\t */\n\tDelete = 'DELETE',\n\t/**\n\t * The `CONNECT` method establishes a tunnel to the server identified by the target resource\n\t * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)\n\t */\n\tConnect = 'CONNECT',\n\t/**\n\t * The `OPTIONS` method is used to describe the communication options for the target resource.\n\t * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)\n\t */\n\tOptions = 'OPTIONS',\n\t/**\n\t * The `TRACE` method performs a message loop-back test along the path to the target resource.\n\t * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)\n\t */\n\tTrace = 'TRACE',\n\t/**\n\t * The `PATCH` method is used to apply partial modifications to a resource.\n\t * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)\n\t */\n\tPatch = 'PATCH'\n}\n\n/**\n * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).\n * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type\n * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).\n */\nexport enum FetchMediaContentTypes {\n\t/**\n\t * The `audio/aac` media content type.\n\t * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)\n\t */\n\tAudioAac = 'audio/aac',\n\t/**\n\t * The `audio/mp4` media content type.\n\t * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tAudioMp4 = 'audio/mp4',\n\t/**\n\t * The `audio/mpeg` media content type.\n\t * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)\n\t * @see [[RFC3003](https://www.iana.org/go/rfc3003)]\n\t */\n\tAudioMpeg = 'audio/mpeg',\n\t/**\n\t * The `audio/ogg` media content type.\n\t * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tAudioOgg = 'audio/ogg',\n\t/**\n\t * The `audio/opus` media content type.\n\t * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)\n\t * @see [[RFC7587](https://www.iana.org/go/rfc7587)]\n\t */\n\tAudioOpus = 'audio/opus',\n\t/**\n\t * The `audio/vorbis` media content type.\n\t * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)\n\t * @see [[RFC5215](https://www.iana.org/go/rfc5215)]\n\t */\n\tAudioVorbis = 'audio/vorbis',\n\t/**\n\t * The `audio/wav` media content type.\n\t */\n\tAudioWav = 'audio/wav',\n\t/**\n\t * The `audio/webm` media content type.\n\t */\n\tAudioWebm = 'audio/webm',\n\t/**\n\t * The `font/otf` media content type.\n\t * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontOtf = 'font/otf',\n\t/**\n\t * The `font/ttf` media content type.\n\t * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontTtf = 'font/ttf',\n\t/**\n\t * The `font/woff` media content type.\n\t * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff = 'font/woff',\n\t/**\n\t * The `font/woff2` media content type.\n\t * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)\n\t * @see [[RFC8081](https://www.iana.org/go/rfc8081)]\n\t */\n\tFontWoff2 = 'font/woff2',\n\t/**\n\t * The `multipart/form-data` media content type.\n\t * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)\n\t * @see [[RFC7578](https://www.iana.org/go/rfc7578)]\n\t */\n\tFormData = 'multipart/form-data',\n\t/**\n\t * The `x-www-form-urlencoded` media content type.\n\t * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)\n\t */\n\tFormURLEncoded = 'application/x-www-form-urlencoded',\n\t/**\n\t * The `image/apng` media content type.\n\t * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)\n\t */\n\tImageAPNG = 'image/apng',\n\t/**\n\t * The `image/gif` media content type.\n\t * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageGIF = 'image/gif',\n\t/**\n\t * The `image/jpeg` media content type.\n\t * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]\n\t */\n\tImageJPEG = 'image/jpeg',\n\t/**\n\t * The `image/png` media content type.\n\t * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)\n\t */\n\tImagePNG = 'image/png',\n\t/**\n\t * The `image/webp` media content type.\n\t * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)\n\t */\n\tImageWEBP = 'image/webp',\n\t/**\n\t * The `application/json` media content type.\n\t * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)\n\t * @see [[RFC8259](https://www.iana.org/go/rfc8259)]\n\t */\n\tJSON = 'application/json',\n\t/**\n\t * The `application/javascript` media content type.\n\t * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)\n\t * @see [[RFC4329](https://www.iana.org/go/rfc4329)]\n\t */\n\tJavaScript = 'application/javascript',\n\t/**\n\t * The `application/octet-stream` media content type.\n\t * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)\n\t * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]\n\t */\n\tOctetStream = 'application/octet-stream',\n\t/**\n\t * The `text/css` media content type.\n\t * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)\n\t * @see [[RFC2318](https://www.iana.org/go/rfc2318)]\n\t */\n\tTextCSS = 'text/css',\n\t/**\n\t * The `text/HTML` media content type.\n\t * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)\n\t */\n\tTextHTML = 'text/html',\n\t/**\n\t * The `text/plain` media content type.\n\t * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)\n\t * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]\n\t */\n\tTextPlain = 'text/plain',\n\t/**\n\t * The `video/h264` media content type.\n\t * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)\n\t * @see [[RFC6184](https://www.iana.org/go/rfc6184)]\n\t */\n\tVideoH264 = 'video/h264',\n\t/**\n\t * The `video/h265` media content type.\n\t * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)\n\t * @see [[RFC7798](https://www.iana.org/go/rfc7798)]\n\t */\n\tVideoH265 = 'video/h265',\n\t/**\n\t * The `video/mp4` media content type.\n\t * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)\n\t * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]\n\t */\n\tVideoMp4 = 'video/mp4',\n\t/**\n\t * The `video/ogg` media content type.\n\t * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)\n\t * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]\n\t */\n\tVideoOgg = 'video/ogg',\n\t/**\n\t * The `video/webm` media content type.\n\t */\n\tVideoWebm = 'video/webm',\n\t/**\n\t * The `application/xml` media content type.\n\t * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)\n\t * @see [[RFC7303](https://www.iana.org/go/rfc7303)]\n\t */\n\tXML = 'application/xml'\n}\n\nexport interface RequestOptions extends Omit<RequestInit, 'body'> {\n\tbody?: BodyInit | Record<any, any>;\n}\n","// eslint-disable-next-line spaced-comment\n/// <reference lib=\"dom\" />\n\nimport { QueryError } from './QueryError';\nimport { FetchResultTypes, type RequestOptions } from './types';\n\n/**\n * Performs an HTTP(S) fetch\n * @param url The URL to send the request to. Can be either a `string` or an `URL` object.\n * `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.\n * @param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript) or one of the {@link FetchResultTypes}\n * @param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ({@link RequestInit} for TypeScript). One of the {@link FetchResultTypes} that will determine how the result is returned.\n * @returns The return type is determined by the provided `type`.\n * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.\n * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).\n * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).\n * - When using `FetchResultTypes.Text` the return type will be a `string`\n * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ({@link Response} in typescript)\n */\n\nexport async function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Buffer): Promise<Buffer>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;\nexport async function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;\nexport async function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;\nexport async function fetch(url: URL | string, options?: RequestOptions | FetchResultTypes, type?: FetchResultTypes) {\n\tif (typeof options === 'undefined') {\n\t\toptions = {};\n\t\ttype = FetchResultTypes.JSON;\n\t} else if (typeof options === 'string') {\n\t\ttype = options;\n\t\toptions = {};\n\t} else if (typeof type === 'undefined') {\n\t\ttype = FetchResultTypes.JSON;\n\t}\n\n\tlet { body } = options;\n\n\tif (body && typeof body === 'object') {\n\t\tbody = JSON.stringify(body);\n\t}\n\n\t// Transform the URL to a String, in case an URL object was passed\n\tconst stringUrl = String(url);\n\n\tconst result: Response = await globalThis.fetch(stringUrl, { ...options, body });\n\tif (!result.ok) throw new QueryError(stringUrl, result.status, result, await result.clone().text());\n\n\tswitch (type) {\n\t\tcase FetchResultTypes.Result:\n\t\t\treturn result;\n\t\tcase FetchResultTypes.Buffer:\n\t\t\treturn Buffer.from(await (await result.blob()).arrayBuffer());\n\t\tcase FetchResultTypes.Blob:\n\t\t\treturn result.blob();\n\t\tcase FetchResultTypes.JSON:\n\t\t\treturn result.json();\n\t\tcase FetchResultTypes.Text:\n\t\t\treturn result.text();\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown type \"${type}\"`);\n\t}\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/fetch",
3
- "version": "2.4.1",
3
+ "version": "2.4.2-next.0220a57.0",
4
4
  "description": "Tiny wrapper around cross-fetch (for NodeJS) and window.fetch (for Browsers) for improved TypeScript and data type support",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
@@ -19,7 +19,8 @@
19
19
  "scripts": {
20
20
  "test": "vitest run",
21
21
  "lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
22
- "build": "tsup && tsc -b src",
22
+ "build": "tsup",
23
+ "docs": "typedoc-json-parser",
23
24
  "prepack": "yarn build",
24
25
  "bump": "cliff-jumper",
25
26
  "check-update": "cliff-jumper --dry-run"
@@ -58,8 +59,12 @@
58
59
  "access": "public"
59
60
  },
60
61
  "devDependencies": {
61
- "@favware/cliff-jumper": "^1.8.5",
62
- "tsup": "^6.1.3",
63
- "typescript": "^4.7.4"
62
+ "@favware/cliff-jumper": "^1.9.0",
63
+ "@vitest/coverage-c8": "^0.25.7",
64
+ "tsup": "^6.5.0",
65
+ "typedoc": "^0.23.22",
66
+ "typedoc-json-parser": "^7.0.2",
67
+ "typescript": "^4.9.4",
68
+ "vitest": "^0.25.7"
64
69
  }
65
70
  }
@@ -1,18 +0,0 @@
1
- /// <reference lib="dom" />
2
- /**
3
- * The QueryError class which is thrown by the `fetch` method
4
- */
5
- export declare class QueryError extends Error {
6
- #private;
7
- /** The requested url. */
8
- readonly url: string;
9
- /** The HTTP status code. */
10
- readonly code: number;
11
- /** The returned response body as a string */
12
- readonly body: string;
13
- /** The original {@link Response} object */
14
- readonly response: Response;
15
- constructor(url: string, code: number, response: Response, body: string);
16
- toJSON(): any;
17
- }
18
- //# sourceMappingURL=QueryError.d.ts.map
@@ -1,28 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference lib="dom" />
3
- import { FetchResultTypes, type RequestOptions } from './types';
4
- /**
5
- * Performs an HTTP(S) fetch
6
- * @param url The URL to send the request to. Can be either a `string` or an `URL` object.
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`.
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.
10
- * @returns The return type is determined by the provided `type`.
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.
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
- */
17
- export declare function fetch<R>(url: URL | string, type?: FetchResultTypes.JSON): Promise<R>;
18
- export declare function fetch<R>(url: URL | string, options: RequestOptions, type?: FetchResultTypes.JSON): Promise<R>;
19
- export declare function fetch(url: URL | string, type: FetchResultTypes.Buffer): Promise<Buffer>;
20
- export declare function fetch(url: URL | string, options: RequestOptions, 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: RequestOptions, type: FetchResultTypes.Blob): Promise<Blob>;
23
- export declare function fetch(url: URL | string, type: FetchResultTypes.Text): Promise<string>;
24
- export declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Text): Promise<string>;
25
- export declare function fetch(url: URL | string, type: FetchResultTypes.Result): Promise<Response>;
26
- export declare function fetch(url: URL | string, options: RequestOptions, type: FetchResultTypes.Result): Promise<Response>;
27
- export declare function fetch<R>(url: URL | string, options: RequestOptions, type: FetchResultTypes): Promise<Response | Blob | Buffer | string | R>;
28
- //# sourceMappingURL=fetch.d.ts.map
@@ -1,266 +0,0 @@
1
- /**
2
- * The supported return types for the `fetch` method
3
- */
4
- export declare enum FetchResultTypes {
5
- /**
6
- * Returns only the body, as JSON. Similar to [`Body.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).
7
- *
8
- * You should provide your own type cast (either through the generic return type, or with `as <type>`) to the response to define
9
- * the JSON structure, otherwise the result will be `unknown`.
10
- */
11
- JSON = "json",
12
- /**
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!
16
- */
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",
23
- /**
24
- * Returns only the body, as plain text. Similar to [`Body.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Body/text).
25
- */
26
- Text = "text",
27
- /**
28
- * Returns the entire response and doesn't parse the `body` in any way.
29
- */
30
- Result = "result"
31
- }
32
- /**
33
- * The list of [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
34
- */
35
- export declare enum FetchMethods {
36
- /**
37
- * The `GET` method requests a representation of the specified resource. Requests using `GET` should only retrieve data.
38
- * @see [MDN / Web / HTTP / Methods / GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET)
39
- */
40
- Get = "GET",
41
- /**
42
- * The `HEAD` method asks for a response identical to that of a {@link FetchMethods.Get `GET`} request, but without the response body.
43
- * @see [MDN / Web / HTTP / Methods / HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)
44
- */
45
- Head = "HEAD",
46
- /**
47
- * 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.
48
- * @see [MDN / Web / HTTP / Methods / POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)
49
- */
50
- Post = "POST",
51
- /**
52
- * The `PUT` method replaces all current representations of the target resource with the request payload.
53
- * @see [MDN / Web / HTTP / Methods / PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT)
54
- */
55
- Put = "PUT",
56
- /**
57
- * The `DELETE` method deletes the specified resource.
58
- * @see [MDN / Web / HTTP / Methods / DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE)
59
- */
60
- Delete = "DELETE",
61
- /**
62
- * The `CONNECT` method establishes a tunnel to the server identified by the target resource
63
- * @see [MDN / Web / HTTP / Methods / CONNECT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT)
64
- */
65
- Connect = "CONNECT",
66
- /**
67
- * The `OPTIONS` method is used to describe the communication options for the target resource.
68
- * @see [MDN / Web / HTTP / Methods / OPTIONS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS)
69
- */
70
- Options = "OPTIONS",
71
- /**
72
- * The `TRACE` method performs a message loop-back test along the path to the target resource.
73
- * @see [MDN / Web / HTTP / Methods / TRACE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE)
74
- */
75
- Trace = "TRACE",
76
- /**
77
- * The `PATCH` method is used to apply partial modifications to a resource.
78
- * @see [MDN / Web / HTTP / Methods / PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)
79
- */
80
- Patch = "PATCH"
81
- }
82
- /**
83
- * A list of common [Media Content Types](https://www.iana.org/assignments/media-types/media-types.xhtml) as defined by the [IANA](https://www.iana.org/).
84
- * Media Content Types are also known as a Multipurpose Internet Mail Extensions or MIME type
85
- * Media Content Types are defined and standardized in IETF's [RFC 6838](https://datatracker.ietf.org/doc/html/rfc6838).
86
- */
87
- export declare enum FetchMediaContentTypes {
88
- /**
89
- * The `audio/aac` media content type.
90
- * @see [Media-Types / audio / aac](https://www.iana.org/assignments/media-types/audio/aac)
91
- */
92
- AudioAac = "audio/aac",
93
- /**
94
- * The `audio/mp4` media content type.
95
- * @see [Media-Types / audio / mp4](https://www.iana.org/assignments/media-types/audio/mp4)
96
- * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]
97
- */
98
- AudioMp4 = "audio/mp4",
99
- /**
100
- * The `audio/mpeg` media content type.
101
- * @see [Media-Types / audio / mpeg](https://www.iana.org/assignments/media-types/audio/mpeg)
102
- * @see [[RFC3003](https://www.iana.org/go/rfc3003)]
103
- */
104
- AudioMpeg = "audio/mpeg",
105
- /**
106
- * The `audio/ogg` media content type.
107
- * @see [Media-Types / audio / ogg](https://www.iana.org/assignments/media-types/audio/ogg)
108
- * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]
109
- */
110
- AudioOgg = "audio/ogg",
111
- /**
112
- * The `audio/opus` media content type.
113
- * @see [Media-Types / audio / opus](https://www.iana.org/assignments/media-types/audio/opus)
114
- * @see [[RFC7587](https://www.iana.org/go/rfc7587)]
115
- */
116
- AudioOpus = "audio/opus",
117
- /**
118
- * The `audio/vorbis` media content type.
119
- * @see [Media-Types / audio / vorbis](https://www.iana.org/assignments/media-types/audio/vorbis)
120
- * @see [[RFC5215](https://www.iana.org/go/rfc5215)]
121
- */
122
- AudioVorbis = "audio/vorbis",
123
- /**
124
- * The `audio/wav` media content type.
125
- */
126
- AudioWav = "audio/wav",
127
- /**
128
- * The `audio/webm` media content type.
129
- */
130
- AudioWebm = "audio/webm",
131
- /**
132
- * The `font/otf` media content type.
133
- * @see [Media-Types / font / otf](https://www.iana.org/assignments/media-types/font/otf)
134
- * @see [[RFC8081](https://www.iana.org/go/rfc8081)]
135
- */
136
- FontOtf = "font/otf",
137
- /**
138
- * The `font/ttf` media content type.
139
- * @see [Media-Types / font / ttf](https://www.iana.org/assignments/media-types/font/ttf)
140
- * @see [[RFC8081](https://www.iana.org/go/rfc8081)]
141
- */
142
- FontTtf = "font/ttf",
143
- /**
144
- * The `font/woff` media content type.
145
- * @see [Media-Types / font / woff](https://www.iana.org/assignments/media-types/font/woff)
146
- * @see [[RFC8081](https://www.iana.org/go/rfc8081)]
147
- */
148
- FontWoff = "font/woff",
149
- /**
150
- * The `font/woff2` media content type.
151
- * @see [Media-Types / font / woff2](https://www.iana.org/assignments/media-types/font/woff2)
152
- * @see [[RFC8081](https://www.iana.org/go/rfc8081)]
153
- */
154
- FontWoff2 = "font/woff2",
155
- /**
156
- * The `multipart/form-data` media content type.
157
- * @see [Media-Types / multipart / form-data](https://www.iana.org/assignments/media-types/multipart/form-data)
158
- * @see [[RFC7578](https://www.iana.org/go/rfc7578)]
159
- */
160
- FormData = "multipart/form-data",
161
- /**
162
- * The `x-www-form-urlencoded` media content type.
163
- * @see [Media-Types / application / x-www-form-urlencoded](https://www.iana.org/assignments/media-types/application/x-www-form-urlencoded)
164
- */
165
- FormURLEncoded = "application/x-www-form-urlencoded",
166
- /**
167
- * The `image/apng` media content type.
168
- * @see [Media-Types / image / apng](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#apng_animated_portable_network_graphics)
169
- */
170
- ImageAPNG = "image/apng",
171
- /**
172
- * The `image/gif` media content type.
173
- * @see [Media-Types / image / gif](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#gif_graphics_interchange_format)
174
- * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]
175
- */
176
- ImageGIF = "image/gif",
177
- /**
178
- * The `image/jpeg` media content type.
179
- * @see [Media-Types / image / jpeg](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#jpeg_joint_photographic_experts_group_image)
180
- * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046](https://www.iana.org/go/rfc2046)]
181
- */
182
- ImageJPEG = "image/jpeg",
183
- /**
184
- * The `image/png` media content type.
185
- * @see [Media-Types / image / png](https://www.iana.org/assignments/media-types/image/png)
186
- */
187
- ImagePNG = "image/png",
188
- /**
189
- * The `image/webp` media content type.
190
- * @see [Media-Types / image / webp](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image)
191
- */
192
- ImageWEBP = "image/webp",
193
- /**
194
- * The `application/json` media content type.
195
- * @see [Media-Types / application / json](https://www.iana.org/assignments/media-types/application/json)
196
- * @see [[RFC8259](https://www.iana.org/go/rfc8259)]
197
- */
198
- JSON = "application/json",
199
- /**
200
- * The `application/javascript` media content type.
201
- * @see [Media-Types / application / javascript](https://www.iana.org/assignments/media-types/application/javascript)
202
- * @see [[RFC4329](https://www.iana.org/go/rfc4329)]
203
- */
204
- JavaScript = "application/javascript",
205
- /**
206
- * The `application/octet-stream` media content type.
207
- * @see [Media-Types / application / octet-stream](https://www.iana.org/assignments/media-types/application/octet-stream)
208
- * @see [[RFC2045](https://www.iana.org/go/rfc2045)] [[RFC2046(https://www.iana.org/go/rfc2046)]
209
- */
210
- OctetStream = "application/octet-stream",
211
- /**
212
- * The `text/css` media content type.
213
- * @see [Media-Types / text / css](https://www.iana.org/assignments/media-types/text/css)
214
- * @see [[RFC2318](https://www.iana.org/go/rfc2318)]
215
- */
216
- TextCSS = "text/css",
217
- /**
218
- * The `text/HTML` media content type.
219
- * @see [Media-Types / text / html](https://www.iana.org/assignments/media-types/text/html)
220
- */
221
- TextHTML = "text/html",
222
- /**
223
- * The `text/plain` media content type.
224
- * @see [Media-Types / text / plain](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textplain)
225
- * @see [[RFC2046](https://www.iana.org/go/rfc2046)] [[RFC3676](https://www.iana.org/go/rfc3676)] [[RFC5147](https://www.iana.org/go/rfc5147)]
226
- */
227
- TextPlain = "text/plain",
228
- /**
229
- * The `video/h264` media content type.
230
- * @see [Media-Types / video / h264](https://www.iana.org/assignments/media-types/video/H264)
231
- * @see [[RFC6184](https://www.iana.org/go/rfc6184)]
232
- */
233
- VideoH264 = "video/h264",
234
- /**
235
- * The `video/h265` media content type.
236
- * @see [Media-Types / video / h265](https://www.iana.org/assignments/media-types/video/H265)
237
- * @see [[RFC7798](https://www.iana.org/go/rfc7798)]
238
- */
239
- VideoH265 = "video/h265",
240
- /**
241
- * The `video/mp4` media content type.
242
- * @see [Media-Types / video / mp4](https://www.iana.org/assignments/media-types/video/mp4)
243
- * @see [[RFC4337](https://www.iana.org/go/rfc4337)] [[RFC6381](https://www.iana.org/go/rfc6381)]
244
- */
245
- VideoMp4 = "video/mp4",
246
- /**
247
- * The `video/ogg` media content type.
248
- * @see [Media-Types / video / ogg](https://www.iana.org/assignments/media-types/video/ogg)
249
- * @see [[RFC5334](https://www.iana.org/go/rfc5334)] [[RFC7845](https://www.iana.org/go/rfc7845)]
250
- */
251
- VideoOgg = "video/ogg",
252
- /**
253
- * The `video/webm` media content type.
254
- */
255
- VideoWebm = "video/webm",
256
- /**
257
- * The `application/xml` media content type.
258
- * @see [Media-Types / application / xml](https://www.iana.org/assignments/media-types/application/xml)
259
- * @see [[RFC7303](https://www.iana.org/go/rfc7303)]
260
- */
261
- XML = "application/xml"
262
- }
263
- export interface RequestOptions extends Omit<RequestInit, 'body'> {
264
- body?: BodyInit | Record<any, any>;
265
- }
266
- //# sourceMappingURL=types.d.ts.map