fetch-request-browser 1.0.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/LICENSE +21 -0
- package/README.md +205 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.js +1 -0
- package/dist/shared/errors.d.ts +9 -0
- package/dist/shared/errors.js +1 -0
- package/dist/shared/types.d.ts +61 -0
- package/dist/shared/types.js +1 -0
- package/dist/utils/utils.d.ts +34 -0
- package/dist/utils/utils.js +1 -0
- package/dist/validations/validations.d.ts +12 -0
- package/dist/validations/validations.js +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jesus Graterol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# Fetch Request Browser
|
|
2
|
+
|
|
3
|
+
The `fetch-request-browser` package makes working with external APIs simple and efficient. This intuitive wrapper leverages the power of the Fetch API, providing a clean and concise interface for your API interactions.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
<br />
|
|
10
|
+
|
|
11
|
+
## Getting Started
|
|
12
|
+
|
|
13
|
+
Install the package:
|
|
14
|
+
```bash
|
|
15
|
+
$ npm install -S fetch-request-browser
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
<br />
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { sendGET } from 'fetch-request-browser';
|
|
28
|
+
|
|
29
|
+
await sendGET('https://httpbin.org/get');
|
|
30
|
+
// {
|
|
31
|
+
// code: 200,
|
|
32
|
+
// headers: Headers {
|
|
33
|
+
// date: 'Tue, 04 Jun 2024 18:52:29 GMT',
|
|
34
|
+
// 'content-type': 'application/json',
|
|
35
|
+
// 'content-length': '407',
|
|
36
|
+
// connection: 'keep-alive',
|
|
37
|
+
// server: 'gunicorn/19.9.0',
|
|
38
|
+
// 'access-control-allow-origin': '*',
|
|
39
|
+
// 'access-control-allow-credentials': 'true'
|
|
40
|
+
// },
|
|
41
|
+
// data: {
|
|
42
|
+
// args: {},
|
|
43
|
+
// headers: {
|
|
44
|
+
// Accept: 'application/json',
|
|
45
|
+
// 'Accept-Encoding': 'br, gzip, deflate',
|
|
46
|
+
// 'Accept-Language': '*',
|
|
47
|
+
// 'Content-Type': 'application/json',
|
|
48
|
+
// Host: 'httpbin.org',
|
|
49
|
+
// 'Sec-Fetch-Mode': 'cors',
|
|
50
|
+
// 'User-Agent': 'node',
|
|
51
|
+
// 'X-Amzn-Trace-Id': '...'
|
|
52
|
+
// },
|
|
53
|
+
// origin: '...',
|
|
54
|
+
// url: 'https://httpbin.org/get'
|
|
55
|
+
// }
|
|
56
|
+
// }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
<br/>
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
Build and send an HTTP Request (any method):
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
send(
|
|
71
|
+
input: IRequestInput,
|
|
72
|
+
options?: Partial<IOptions>
|
|
73
|
+
): Promise<IRequestResponse>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
<br />
|
|
77
|
+
|
|
78
|
+
Build and send a `GET` HTTP Request:
|
|
79
|
+
```typescript
|
|
80
|
+
sendGET(
|
|
81
|
+
input: IRequestInput,
|
|
82
|
+
options?: Partial<IOptions>,
|
|
83
|
+
retryAttempts?: number,
|
|
84
|
+
retryDelaySeconds?: number
|
|
85
|
+
): Promise<IRequestResponse>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
<br />
|
|
89
|
+
|
|
90
|
+
Build and send a `POST` HTTP Request:
|
|
91
|
+
```typescript
|
|
92
|
+
sendPOST(
|
|
93
|
+
input: IRequestInput,
|
|
94
|
+
options?: Partial<IOptions>
|
|
95
|
+
): Promise<IRequestResponse>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
<br />
|
|
99
|
+
|
|
100
|
+
Build and send a `PUT` HTTP Request:
|
|
101
|
+
```typescript
|
|
102
|
+
sendPUT(
|
|
103
|
+
input: IRequestInput,
|
|
104
|
+
options?: Partial<IOptions>
|
|
105
|
+
): Promise<IRequestResponse>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
<br />
|
|
109
|
+
|
|
110
|
+
Build and send a `PATCH` HTTP Request:
|
|
111
|
+
```typescript
|
|
112
|
+
sendPATCH(
|
|
113
|
+
input: IRequestInput,
|
|
114
|
+
options?: Partial<IOptions>
|
|
115
|
+
): Promise<IRequestResponse>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
<br />
|
|
119
|
+
|
|
120
|
+
Build and send a `DELETE` HTTP Request:
|
|
121
|
+
```typescript
|
|
122
|
+
sendDELETE(
|
|
123
|
+
input: IRequestInput,
|
|
124
|
+
options?: Partial<IOptions>
|
|
125
|
+
): Promise<IRequestResponse>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
<br />
|
|
131
|
+
|
|
132
|
+
## Built With
|
|
133
|
+
|
|
134
|
+
- TypeScript
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
<br />
|
|
140
|
+
|
|
141
|
+
## Running the Tests
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
# Unit Tests
|
|
145
|
+
$ npm run test:unit
|
|
146
|
+
|
|
147
|
+
# Integration Tests
|
|
148
|
+
$ npm run test:integration
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
<br />
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
[MIT](https://choosealicense.com/licenses/mit/)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
<br />
|
|
166
|
+
|
|
167
|
+
## Acknowledgments
|
|
168
|
+
|
|
169
|
+
- [MDN](https://developer.mozilla.org/en-US/)
|
|
170
|
+
- [web.dev](https://web.dev/)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
<br />
|
|
177
|
+
|
|
178
|
+
## @TODOS
|
|
179
|
+
|
|
180
|
+
- [ ] Improve the docs
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
<br />
|
|
187
|
+
|
|
188
|
+
## Deployment
|
|
189
|
+
|
|
190
|
+
Install dependencies:
|
|
191
|
+
```bash
|
|
192
|
+
$ npm install
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
Build the library:
|
|
197
|
+
```bash
|
|
198
|
+
$ npm start
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
Publish to `npm`:
|
|
203
|
+
```bash
|
|
204
|
+
$ npm publish
|
|
205
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { IRequestInput, IRequestMethod, IRequestOptions, IResponseDataType, IOptions, IRequestResponse } from './shared/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Builds and sends an HTTP Request based on the provided input and options.
|
|
4
|
+
* @param input
|
|
5
|
+
* @param options?
|
|
6
|
+
* @returns Promise<IRequestResponse>
|
|
7
|
+
* @throws
|
|
8
|
+
* - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
|
|
9
|
+
* - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
|
|
10
|
+
* - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
|
|
11
|
+
* - UNEXPECTED_RESPONSE_STATUS_CODE: if the code doesn't meet the requirements set in the options
|
|
12
|
+
* - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers are not identical
|
|
13
|
+
* - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
|
|
14
|
+
*/
|
|
15
|
+
declare const send: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
|
|
16
|
+
/**
|
|
17
|
+
* Builds and sends a GET HTTP Request based on the provided input and options.
|
|
18
|
+
* IMPORTANT: The browser environment can be highly unreliable as the user can physically move
|
|
19
|
+
* around and suffer from an intermittent Internet connection. Therefore, some GET requests are
|
|
20
|
+
* worth retrying as they could fail temporarily and prevent a view from loading.
|
|
21
|
+
* @param input
|
|
22
|
+
* @param options?
|
|
23
|
+
* @param retryAttempts? - the number of times it will retry the request on failure
|
|
24
|
+
* @param retryDelaySeconds? - the # of secs it will wait before re-sending the req. Defaults to 3
|
|
25
|
+
* @returns Promise<IRequestResponse>
|
|
26
|
+
* @throws
|
|
27
|
+
* - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
|
|
28
|
+
* - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
|
|
29
|
+
* - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
|
|
30
|
+
* - UNEXPECTED_RESPONSE_STATUS_CODE: if the code doesn't meet the requirements set in the options
|
|
31
|
+
* - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers are not identical
|
|
32
|
+
* - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
|
|
33
|
+
*/
|
|
34
|
+
declare const sendGET: (input: IRequestInput, options?: Partial<IOptions>, retryAttempts?: number, retryDelaySeconds?: number) => Promise<IRequestResponse>;
|
|
35
|
+
/**
|
|
36
|
+
* Builds and sends a POST HTTP Request based on the provided input and options.
|
|
37
|
+
* @param input
|
|
38
|
+
* @param options?
|
|
39
|
+
* @returns Promise<IRequestResponse>
|
|
40
|
+
* @throws
|
|
41
|
+
* - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
|
|
42
|
+
* - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
|
|
43
|
+
* - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
|
|
44
|
+
* - UNEXPECTED_RESPONSE_STATUS_CODE: if the code doesn't meet the requirements set in the options
|
|
45
|
+
* - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers are not identical
|
|
46
|
+
* - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
|
|
47
|
+
*/
|
|
48
|
+
declare const sendPOST: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
|
|
49
|
+
/**
|
|
50
|
+
* Builds and sends a PUT HTTP Request based on the provided input and options.
|
|
51
|
+
* @param input
|
|
52
|
+
* @param options?
|
|
53
|
+
* @returns Promise<IRequestResponse>
|
|
54
|
+
* @throws
|
|
55
|
+
* - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
|
|
56
|
+
* - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
|
|
57
|
+
* - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
|
|
58
|
+
* - UNEXPECTED_RESPONSE_STATUS_CODE: if the code doesn't meet the requirements set in the options
|
|
59
|
+
* - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers are not identical
|
|
60
|
+
* - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
|
|
61
|
+
*/
|
|
62
|
+
declare const sendPUT: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
|
|
63
|
+
/**
|
|
64
|
+
* Builds and sends a PATCH HTTP Request based on the provided input and options.
|
|
65
|
+
* @param input
|
|
66
|
+
* @param options?
|
|
67
|
+
* @returns Promise<IRequestResponse>
|
|
68
|
+
* @throws
|
|
69
|
+
* - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
|
|
70
|
+
* - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
|
|
71
|
+
* - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
|
|
72
|
+
* - UNEXPECTED_RESPONSE_STATUS_CODE: if the code doesn't meet the requirements set in the options
|
|
73
|
+
* - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers are not identical
|
|
74
|
+
* - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
|
|
75
|
+
*/
|
|
76
|
+
declare const sendPATCH: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
|
|
77
|
+
/**
|
|
78
|
+
* Builds and sends a DELETE HTTP Request based on the provided input and options.
|
|
79
|
+
* @param input
|
|
80
|
+
* @param options?
|
|
81
|
+
* @returns Promise<IRequestResponse>
|
|
82
|
+
* @throws
|
|
83
|
+
* - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
|
|
84
|
+
* - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
|
|
85
|
+
* - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
|
|
86
|
+
* - UNEXPECTED_RESPONSE_STATUS_CODE: if the code doesn't meet the requirements set in the options
|
|
87
|
+
* - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers are not identical
|
|
88
|
+
* - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
|
|
89
|
+
*/
|
|
90
|
+
declare const sendDELETE: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
|
|
91
|
+
export { IRequestInput, IRequestMethod, IRequestOptions, IResponseDataType, IOptions, IRequestResponse, send, sendGET, sendPOST, sendPUT, sendPATCH, sendDELETE, };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{buildOptions,buildRequest,extractResponseData,delay}from"./utils/utils.js";import{validateResponse}from"./validations/validations.js";const send=async(e,t)=>{const s=buildOptions(t),n=buildRequest(e,s.requestOptions),d=await fetch(n);return validateResponse(n,d,s),d.redirected&&console.warn(`The request sent to '${n.url}' was redirected. Please update the implementation to avoid future redirections.`),{code:d.status,headers:d.headers,data:await extractResponseData(d,s.responseDataType)}},__executeSendGET=(e,t)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"GET"}}),sendGET=async(e,t,s,n)=>{try{return await __executeSendGET(e,t)}catch(d){if("number"==typeof s&&s>0)return await delay(n||3),sendGET(e,t,s-1,n);throw d}},sendPOST=(e,t)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"POST"}}),sendPUT=(e,t)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"PUT"}}),sendPATCH=(e,t)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"PATCH"}}),sendDELETE=(e,t)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"DELETE"}});export{send,sendGET,sendPOST,sendPUT,sendPATCH,sendDELETE};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare enum ERRORS {
|
|
2
|
+
INVALID_REQUEST_URL = "INVALID_REQUEST_URL",
|
|
3
|
+
INVALID_REQUEST_HEADERS = "INVALID_REQUEST_HEADERS",
|
|
4
|
+
INVALID_REQUEST_OPTIONS = "INVALID_REQUEST_OPTIONS",
|
|
5
|
+
INVALID_RESPONSE_DTYPE = "INVALID_RESPONSE_DTYPE",
|
|
6
|
+
UNEXPECTED_RESPONSE_STATUS_CODE = "UNEXPECTED_RESPONSE_STATUS_CODE",
|
|
7
|
+
CONTENT_TYPE_MISSMATCH = "CONTENT_TYPE_MISSMATCH"
|
|
8
|
+
}
|
|
9
|
+
export { ERRORS, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var ERRORS;!function(E){E.INVALID_REQUEST_URL="INVALID_REQUEST_URL",E.INVALID_REQUEST_HEADERS="INVALID_REQUEST_HEADERS",E.INVALID_REQUEST_OPTIONS="INVALID_REQUEST_OPTIONS",E.INVALID_RESPONSE_DTYPE="INVALID_RESPONSE_DTYPE",E.UNEXPECTED_RESPONSE_STATUS_CODE="UNEXPECTED_RESPONSE_STATUS_CODE",E.CONTENT_TYPE_MISSMATCH="CONTENT_TYPE_MISSMATCH"}(ERRORS||(ERRORS={}));export{ERRORS};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request Input
|
|
3
|
+
* The URL of the request's target.
|
|
4
|
+
*/
|
|
5
|
+
type IRequestInput = string | URL;
|
|
6
|
+
/**
|
|
7
|
+
* Request Options
|
|
8
|
+
* The options that can be applied when sending a Fetch Request.
|
|
9
|
+
* IMPORTANT: the reason RequestInit is extended is because in the original type, the body property
|
|
10
|
+
* does not accept plain objects. Even though this makes sense, the body is processed in the
|
|
11
|
+
* utilities so the Request's body is always instantiated with a string.
|
|
12
|
+
*/
|
|
13
|
+
interface IRequestOptions extends RequestInit {
|
|
14
|
+
method: IRequestMethod;
|
|
15
|
+
body: any;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Request Method
|
|
19
|
+
* The HTTP Methods supported by this library. To make use of a different one, pass the method name
|
|
20
|
+
* directly in the request options.
|
|
21
|
+
*/
|
|
22
|
+
type IRequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
23
|
+
/**
|
|
24
|
+
* Response Data Type
|
|
25
|
+
* The type of data that will be extracted from the HTTP Response body.
|
|
26
|
+
*/
|
|
27
|
+
type IResponseDataType = 'arrayBuffer' | 'blob' | 'formData' | 'json' | 'text';
|
|
28
|
+
/**
|
|
29
|
+
* Response Data
|
|
30
|
+
* The format of the data that can be extracted from the Response object.
|
|
31
|
+
*/
|
|
32
|
+
type IResponseData<T> = T extends 'arrayBuffer' ? ArrayBuffer : T extends 'blob' ? Blob : T extends 'formData' ? FormData : T extends 'json' ? any : T extends 'text' ? string : never;
|
|
33
|
+
/**
|
|
34
|
+
* Options
|
|
35
|
+
* The options object that can be passed and used for any request.
|
|
36
|
+
*/
|
|
37
|
+
interface IOptions {
|
|
38
|
+
requestOptions?: Partial<IRequestOptions>;
|
|
39
|
+
responseDataType: IResponseDataType;
|
|
40
|
+
/**
|
|
41
|
+
* Response Status Codes
|
|
42
|
+
* The request's response can be validated by providing a list of acceptable codes or a range
|
|
43
|
+
* object. Keep in mind that if the acceptableStatusCodes array is provided, it will only perform
|
|
44
|
+
* that validation and ignore the acceptableStatusCodesRange.
|
|
45
|
+
*/
|
|
46
|
+
acceptableStatusCodes?: number[];
|
|
47
|
+
acceptableStatusCodesRange: {
|
|
48
|
+
min: number;
|
|
49
|
+
max: number;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Request Response
|
|
54
|
+
* The object containing the result of the Request.
|
|
55
|
+
*/
|
|
56
|
+
interface IRequestResponse {
|
|
57
|
+
code: number;
|
|
58
|
+
headers: Headers;
|
|
59
|
+
data: any;
|
|
60
|
+
}
|
|
61
|
+
export type { IRequestInput, IRequestOptions, IRequestMethod, IResponseDataType, IResponseData, IOptions, IRequestResponse, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { IRequestInput, IResponseDataType, IResponseData, IRequestOptions, IOptions } from '../shared/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Builds the Request Instance based on given input and options.
|
|
4
|
+
* @param input
|
|
5
|
+
* @param options
|
|
6
|
+
* @returns Request
|
|
7
|
+
* @throws
|
|
8
|
+
* - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
|
|
9
|
+
* - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
|
|
10
|
+
* - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
|
|
11
|
+
*/
|
|
12
|
+
declare const buildRequest: (input: IRequestInput, options?: Partial<IRequestOptions>) => Request;
|
|
13
|
+
/**
|
|
14
|
+
* Extracts the data from the Response object based on the provided data type.
|
|
15
|
+
* @param res
|
|
16
|
+
* @param dType
|
|
17
|
+
* @returns Promise<IResponseData<T>>
|
|
18
|
+
* @throws
|
|
19
|
+
* - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
|
|
20
|
+
*/
|
|
21
|
+
declare const extractResponseData: <T extends IResponseDataType>(res: Response, dType: T) => Promise<IResponseData<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* Builds the main options object based on given args (if any).
|
|
24
|
+
* @param options
|
|
25
|
+
* @returns IOptions
|
|
26
|
+
*/
|
|
27
|
+
declare const buildOptions: (options?: Partial<IOptions>) => IOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an asynchronous delay that resolves once the provided seconds have passed.
|
|
30
|
+
* @param seconds
|
|
31
|
+
* @returns Promise<void>
|
|
32
|
+
*/
|
|
33
|
+
declare const delay: (seconds: number) => Promise<void>;
|
|
34
|
+
export { buildRequest, extractResponseData, buildOptions, delay, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{encodeError,isEncodedError}from"error-message-utils";import{ERRORS}from"../shared/errors.js";const __buildRequestInput=e=>{if(e instanceof URL)return e;try{return new URL(e)}catch(e){throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_URL))}},__buildRequestHeaders=e=>{let r;if(e&&"object"==typeof e)try{r=new Headers(e)}catch(e){throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_HEADERS))}else r=e instanceof Headers?e:new Headers({Accept:"application/json","Content-Type":"application/json"});return r.has("Accept")||r.append("Accept","application/json"),r.has("Content-Type")||r.append("Content-Type","application/json"),r},__buildRequestBody=e=>e?"object"==typeof e?JSON.stringify(e):e:null,__buildRequestOptions=(e={})=>{const r=e.method??"GET";return{method:r,mode:e.mode??"cors",cache:e.cache??"default",credentials:e.credentials??"same-origin",headers:__buildRequestHeaders(e.headers),priority:e.priority??"auto",redirect:e.redirect??"follow",referrer:e.referrer??"about:client",referrerPolicy:e.referrerPolicy??"no-referrer-when-downgrade",signal:e.signal,integrity:e.integrity||"",keepalive:e.keepalive??!1,body:"GET"===r?null:__buildRequestBody(e.body)}},buildRequest=(e,r)=>{try{return new Request(__buildRequestInput(e),__buildRequestOptions(r))}catch(e){if(isEncodedError(e))throw e;throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_OPTIONS))}},extractResponseData=async(e,r)=>{switch(r){case"arrayBuffer":return e.arrayBuffer();case"blob":return e.blob();case"formData":return e.formData();case"json":return e.json();case"text":return e.text();default:throw new Error(encodeError(`The provided response data type '${r}' is invalid.`,ERRORS.INVALID_RESPONSE_DTYPE))}},buildOptions=(e={})=>({requestOptions:e.requestOptions,responseDataType:e.responseDataType??"json",acceptableStatusCodes:e.acceptableStatusCodes,acceptableStatusCodesRange:e.acceptableStatusCodesRange??{min:200,max:299}}),delay=e=>new Promise((r=>{setTimeout(r,1e3*e)}));export{buildRequest,extractResponseData,buildOptions,delay};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IOptions } from '../shared/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates the Response's status code and the Content-Type Header.
|
|
4
|
+
* @param req
|
|
5
|
+
* @param res
|
|
6
|
+
* @param options
|
|
7
|
+
* @throws
|
|
8
|
+
* - UNEXPECTED_RESPONSE_STATUS_CODE: if the code doesn't meet the requirements set in the options
|
|
9
|
+
* - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers are not identical
|
|
10
|
+
*/
|
|
11
|
+
declare const validateResponse: (req: Request, res: Response, options: IOptions) => void;
|
|
12
|
+
export { validateResponse, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{encodeError}from"error-message-utils";import{ERRORS}from"../shared/errors.js";const __buildUnexpectedCodeErrorMessage=e=>encodeError(`Request Failed: received unexpected response code '${e.status}': ${e.statusText}`,ERRORS.UNEXPECTED_RESPONSE_STATUS_CODE),__validateStatusCode=(e,t)=>{if(Array.isArray(t.acceptableStatusCodes)&&t.acceptableStatusCodes.length){if(!t.acceptableStatusCodes.includes(e.status))throw new Error(__buildUnexpectedCodeErrorMessage(e))}else if(e.status<t.acceptableStatusCodesRange.min||e.status>t.acceptableStatusCodesRange.max)throw new Error(__buildUnexpectedCodeErrorMessage(e))},__validateContentType=(e,t)=>{const r=e.headers.get("Accept"),s=t.headers.get("Content-Type");if(r!==s)throw new Error(encodeError(`The request's Accept Header '${r}' is different from the Content-Type received in the response '${s}'.`,ERRORS.CONTENT_TYPE_MISSMATCH))},validateResponse=(e,t,r)=>{__validateStatusCode(t,r),__validateContentType(e,t)};export{validateResponse};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "fetch-request-browser",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The fetch-request-browser package makes working with external APIs simple and efficient. This intuitive wrapper leverages the power of the Fetch API, providing a clean and concise interface for your API interactions.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"start": "ts-lib-builder --tsconfig=tsconfig.build.json",
|
|
10
|
+
"test": "echo \"Error: tests are executed with npm run test:(integration|unit)\" && exit 1",
|
|
11
|
+
"test:integration": "vitest run --config vitest.test-integration.config.ts",
|
|
12
|
+
"test:unit": "vitest run --config vitest.test-unit.config.ts",
|
|
13
|
+
"watch-test:integration": "vitest --config vitest.test-integration.config.ts",
|
|
14
|
+
"watch-test:unit": "vitest --config vitest.test-unit.config.ts"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/jesusgraterol/fetch-request-browser.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"fetch",
|
|
22
|
+
"http",
|
|
23
|
+
"https",
|
|
24
|
+
"request",
|
|
25
|
+
"fetch-api",
|
|
26
|
+
"utils",
|
|
27
|
+
"utilities"
|
|
28
|
+
],
|
|
29
|
+
"author": "Jesus Graterol",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/jesusgraterol/fetch-request-browser/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/jesusgraterol/fetch-request-browser#readme",
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^20.13.0",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "^7.11.0",
|
|
38
|
+
"@typescript-eslint/parser": "^7.11.0",
|
|
39
|
+
"eslint-config-airbnb-typescript": "^18.0.0",
|
|
40
|
+
"ts-lib-builder": "^1.0.3",
|
|
41
|
+
"typescript": "^5.4.5",
|
|
42
|
+
"vitest": "^1.6.0"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"error-message-utils": "^1.1.0"
|
|
46
|
+
}
|
|
47
|
+
}
|