fetch-request-browser 1.0.0 → 1.0.2

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
@@ -3,7 +3,7 @@
3
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
4
 
5
5
 
6
-
6
+ If you are working on a node-based environment, make use of [fetch-request-node](https://github.com/jesusgraterol/fetch-request-node) instead.
7
7
 
8
8
 
9
9
  <br />
@@ -26,7 +26,15 @@ $ npm install -S fetch-request-browser
26
26
  ```typescript
27
27
  import { sendGET } from 'fetch-request-browser';
28
28
 
29
- await sendGET('https://httpbin.org/get');
29
+ await send(
30
+ 'https://httpbin.org/get',
31
+ {
32
+ requestOptions: {
33
+ method: 'GET'
34
+ }
35
+ },
36
+ [3, 5, 10]
37
+ );
30
38
  // {
31
39
  // code: 200,
32
40
  // headers: Headers {
@@ -69,7 +77,8 @@ Build and send an HTTP Request (any method):
69
77
  ```typescript
70
78
  send(
71
79
  input: IRequestInput,
72
- options?: Partial<IOptions>
80
+ options?: Partial<IOptions>,
81
+ retryDelaySchedule?: number[],
73
82
  ): Promise<IRequestResponse>
74
83
  ```
75
84
 
@@ -80,8 +89,7 @@ Build and send a `GET` HTTP Request:
80
89
  sendGET(
81
90
  input: IRequestInput,
82
91
  options?: Partial<IOptions>,
83
- retryAttempts?: number,
84
- retryDelaySeconds?: number
92
+ retryDelaySchedule?: number[],
85
93
  ): Promise<IRequestResponse>
86
94
  ```
87
95
 
@@ -91,7 +99,8 @@ Build and send a `POST` HTTP Request:
91
99
  ```typescript
92
100
  sendPOST(
93
101
  input: IRequestInput,
94
- options?: Partial<IOptions>
102
+ options?: Partial<IOptions>,
103
+ retryDelaySchedule?: number[],
95
104
  ): Promise<IRequestResponse>
96
105
  ```
97
106
 
@@ -101,7 +110,8 @@ Build and send a `PUT` HTTP Request:
101
110
  ```typescript
102
111
  sendPUT(
103
112
  input: IRequestInput,
104
- options?: Partial<IOptions>
113
+ options?: Partial<IOptions>,
114
+ retryDelaySchedule?: number[],
105
115
  ): Promise<IRequestResponse>
106
116
  ```
107
117
 
@@ -111,7 +121,8 @@ Build and send a `PATCH` HTTP Request:
111
121
  ```typescript
112
122
  sendPATCH(
113
123
  input: IRequestInput,
114
- options?: Partial<IOptions>
124
+ options?: Partial<IOptions>,
125
+ retryDelaySchedule?: number[],
115
126
  ): Promise<IRequestResponse>
116
127
  ```
117
128
 
@@ -121,7 +132,8 @@ Build and send a `DELETE` HTTP Request:
121
132
  ```typescript
122
133
  sendDELETE(
123
134
  input: IRequestInput,
124
- options?: Partial<IOptions>
135
+ options?: Partial<IOptions>,
136
+ retryDelaySchedule?: number[],
125
137
  ): Promise<IRequestResponse>
126
138
  ```
127
139
 
package/dist/index.d.ts CHANGED
@@ -1,18 +1,20 @@
1
1
  import { IRequestInput, IRequestMethod, IRequestOptions, IResponseDataType, IOptions, IRequestResponse } from './shared/types.js';
2
2
  /**
3
- * Builds and sends an HTTP Request based on the provided input and options.
3
+ * Attempts to send a HTTP request persistently (optionally).
4
4
  * @param input
5
5
  * @param options?
6
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
6
7
  * @returns Promise<IRequestResponse>
7
8
  * @throws
8
9
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
9
10
  * - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
10
11
  * - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
11
12
  * - 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_CONTENT_TYPE: if the res lacks the Content-Type Header or is an empty string
14
+ * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
13
15
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
14
16
  */
15
- declare const send: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
17
+ declare const send: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
16
18
  /**
17
19
  * Builds and sends a GET HTTP Request based on the provided input and options.
18
20
  * IMPORTANT: The browser environment can be highly unreliable as the user can physically move
@@ -20,72 +22,80 @@ declare const send: (input: IRequestInput, options?: Partial<IOptions>) => Promi
20
22
  * worth retrying as they could fail temporarily and prevent a view from loading.
21
23
  * @param input
22
24
  * @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
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
25
26
  * @returns Promise<IRequestResponse>
26
27
  * @throws
27
28
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
28
29
  * - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
29
30
  * - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
30
31
  * - 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_CONTENT_TYPE: if the res lacks the Content-Type Header or is an empty string
33
+ * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
32
34
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
33
35
  */
34
- declare const sendGET: (input: IRequestInput, options?: Partial<IOptions>, retryAttempts?: number, retryDelaySeconds?: number) => Promise<IRequestResponse>;
36
+ declare const sendGET: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
35
37
  /**
36
38
  * Builds and sends a POST HTTP Request based on the provided input and options.
37
39
  * @param input
38
40
  * @param options?
41
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
39
42
  * @returns Promise<IRequestResponse>
40
43
  * @throws
41
44
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
42
45
  * - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
43
46
  * - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
44
47
  * - 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
48
+ * - INVALID_RESPONSE_CONTENT_TYPE: if the res lacks the Content-Type Header or is an empty string
49
+ * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
46
50
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
47
51
  */
48
- declare const sendPOST: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
52
+ declare const sendPOST: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
49
53
  /**
50
54
  * Builds and sends a PUT HTTP Request based on the provided input and options.
51
55
  * @param input
52
56
  * @param options?
57
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
53
58
  * @returns Promise<IRequestResponse>
54
59
  * @throws
55
60
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
56
61
  * - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
57
62
  * - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
58
63
  * - 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
64
+ * - INVALID_RESPONSE_CONTENT_TYPE: if the res lacks the Content-Type Header or is an empty string
65
+ * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
60
66
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
61
67
  */
62
- declare const sendPUT: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
68
+ declare const sendPUT: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
63
69
  /**
64
70
  * Builds and sends a PATCH HTTP Request based on the provided input and options.
65
71
  * @param input
66
72
  * @param options?
73
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
67
74
  * @returns Promise<IRequestResponse>
68
75
  * @throws
69
76
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
70
77
  * - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
71
78
  * - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
72
79
  * - 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
80
+ * - INVALID_RESPONSE_CONTENT_TYPE: if the res lacks the Content-Type Header or is an empty string
81
+ * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
74
82
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
75
83
  */
76
- declare const sendPATCH: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
84
+ declare const sendPATCH: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
77
85
  /**
78
86
  * Builds and sends a DELETE HTTP Request based on the provided input and options.
79
87
  * @param input
80
88
  * @param options?
89
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
81
90
  * @returns Promise<IRequestResponse>
82
91
  * @throws
83
92
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
84
93
  * - INVALID_REQUEST_HEADERS: if invalid headers are passed in object format
85
94
  * - INVALID_REQUEST_OPTIONS: if the Request Instance cannot be instantiated due to the passed opts
86
95
  * - 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
96
+ * - INVALID_RESPONSE_CONTENT_TYPE: if the res lacks the Content-Type Header or is an empty string
97
+ * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
88
98
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
89
99
  */
90
- declare const sendDELETE: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
100
+ declare const sendDELETE: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
91
101
  export { IRequestInput, IRequestMethod, IRequestOptions, IResponseDataType, IOptions, IRequestResponse, send, sendGET, sendPOST, sendPUT, sendPATCH, sendDELETE, };
package/dist/index.js CHANGED
@@ -1 +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};
1
+ import{buildOptions,buildRequest,extractResponseData,delay}from"./utils/utils.js";import{validateResponse}from"./validations/validations.js";const __executeSend=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)}},send=async(e,t,s)=>{try{return await __executeSend(e,t)}catch(n){if(!Array.isArray(s)||0===s.length)throw n;return await delay(s[0]),send(e,t,s.slice(1))}},sendGET=async(e,t,s)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"GET"}},s),sendPOST=(e,t,s)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"POST"}},s),sendPUT=(e,t,s)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"PUT"}},s),sendPATCH=(e,t,s)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"PATCH"}},s),sendDELETE=(e,t,s)=>send(e,{...t,requestOptions:{...t?.requestOptions,method:"DELETE"}},s);export{send,sendGET,sendPOST,sendPUT,sendPATCH,sendDELETE};
@@ -4,6 +4,7 @@ declare enum ERRORS {
4
4
  INVALID_REQUEST_OPTIONS = "INVALID_REQUEST_OPTIONS",
5
5
  INVALID_RESPONSE_DTYPE = "INVALID_RESPONSE_DTYPE",
6
6
  UNEXPECTED_RESPONSE_STATUS_CODE = "UNEXPECTED_RESPONSE_STATUS_CODE",
7
+ INVALID_RESPONSE_CONTENT_TYPE = "INVALID_RESPONSE_CONTENT_TYPE",
7
8
  CONTENT_TYPE_MISSMATCH = "CONTENT_TYPE_MISSMATCH"
8
9
  }
9
10
  export { ERRORS, };
@@ -1 +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};
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.INVALID_RESPONSE_CONTENT_TYPE="INVALID_RESPONSE_CONTENT_TYPE",E.CONTENT_TYPE_MISSMATCH="CONTENT_TYPE_MISSMATCH"}(ERRORS||(ERRORS={}));export{ERRORS};
@@ -6,7 +6,8 @@ import { IOptions } from '../shared/types.js';
6
6
  * @param options
7
7
  * @throws
8
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
9
+ * - INVALID_RESPONSE_CONTENT_TYPE: if the res lacks the Content-Type Header or is an empty string
10
+ * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
10
11
  */
11
12
  declare const validateResponse: (req: Request, res: Response, options: IOptions) => void;
12
13
  export { validateResponse, };
@@ -1 +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};
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("string"!=typeof s||!s.length)throw new Error(encodeError(`The response's Content-Type Header is invalid. Received: '${s}'.`,ERRORS.INVALID_RESPONSE_CONTENT_TYPE));if(!s.includes(r))throw new Error(encodeError(`The request's Accept Header '${r}' is different to 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetch-request-browser",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
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
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -33,15 +33,15 @@
33
33
  },
34
34
  "homepage": "https://github.com/jesusgraterol/fetch-request-browser#readme",
35
35
  "devDependencies": {
36
- "@types/node": "^20.13.0",
37
- "@typescript-eslint/eslint-plugin": "^7.11.0",
38
- "@typescript-eslint/parser": "^7.11.0",
36
+ "@types/node": "^20.14.12",
37
+ "@typescript-eslint/eslint-plugin": "^7.17.0",
38
+ "@typescript-eslint/parser": "^7.17.0",
39
39
  "eslint-config-airbnb-typescript": "^18.0.0",
40
40
  "ts-lib-builder": "^1.0.3",
41
- "typescript": "^5.4.5",
41
+ "typescript": "^5.5.4",
42
42
  "vitest": "^1.6.0"
43
43
  },
44
44
  "dependencies": {
45
- "error-message-utils": "^1.1.0"
45
+ "error-message-utils": "^1.1.1"
46
46
  }
47
47
  }