fetch-request-browser 1.0.1 → 1.0.3

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
@@ -24,9 +24,17 @@ $ npm install -S fetch-request-browser
24
24
  ## Usage
25
25
 
26
26
  ```typescript
27
- import { sendGET } from 'fetch-request-browser';
28
-
29
- await sendGET('https://httpbin.org/get');
27
+ import { send } from 'fetch-request-browser';
28
+
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,8 +1,9 @@
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
@@ -13,7 +14,7 @@ import { IRequestInput, IRequestMethod, IRequestOptions, IResponseDataType, IOpt
13
14
  * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
14
15
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
15
16
  */
16
- declare const send: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
17
+ declare const send: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
17
18
  /**
18
19
  * Builds and sends a GET HTTP Request based on the provided input and options.
19
20
  * IMPORTANT: The browser environment can be highly unreliable as the user can physically move
@@ -21,8 +22,7 @@ declare const send: (input: IRequestInput, options?: Partial<IOptions>) => Promi
21
22
  * worth retrying as they could fail temporarily and prevent a view from loading.
22
23
  * @param input
23
24
  * @param options?
24
- * @param retryAttempts? - the number of times it will retry the request on failure
25
- * @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
26
26
  * @returns Promise<IRequestResponse>
27
27
  * @throws
28
28
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
@@ -33,11 +33,12 @@ declare const send: (input: IRequestInput, options?: Partial<IOptions>) => Promi
33
33
  * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
34
34
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
35
35
  */
36
- 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>;
37
37
  /**
38
38
  * Builds and sends a POST HTTP Request based on the provided input and options.
39
39
  * @param input
40
40
  * @param options?
41
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
41
42
  * @returns Promise<IRequestResponse>
42
43
  * @throws
43
44
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
@@ -48,11 +49,12 @@ declare const sendGET: (input: IRequestInput, options?: Partial<IOptions>, retry
48
49
  * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
49
50
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
50
51
  */
51
- declare const sendPOST: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
52
+ declare const sendPOST: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
52
53
  /**
53
54
  * Builds and sends a PUT HTTP Request based on the provided input and options.
54
55
  * @param input
55
56
  * @param options?
57
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
56
58
  * @returns Promise<IRequestResponse>
57
59
  * @throws
58
60
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
@@ -63,11 +65,12 @@ declare const sendPOST: (input: IRequestInput, options?: Partial<IOptions>) => P
63
65
  * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
64
66
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
65
67
  */
66
- declare const sendPUT: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
68
+ declare const sendPUT: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
67
69
  /**
68
70
  * Builds and sends a PATCH HTTP Request based on the provided input and options.
69
71
  * @param input
70
72
  * @param options?
73
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
71
74
  * @returns Promise<IRequestResponse>
72
75
  * @throws
73
76
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
@@ -78,11 +81,12 @@ declare const sendPUT: (input: IRequestInput, options?: Partial<IOptions>) => Pr
78
81
  * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
79
82
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
80
83
  */
81
- declare const sendPATCH: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
84
+ declare const sendPATCH: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
82
85
  /**
83
86
  * Builds and sends a DELETE HTTP Request based on the provided input and options.
84
87
  * @param input
85
88
  * @param options?
89
+ * @param retryDelaySchedule? - list of seconds that will be applied to the delay before retrying
86
90
  * @returns Promise<IRequestResponse>
87
91
  * @throws
88
92
  * - INVALID_REQUEST_URL: if the provided input URL cannot be parsed
@@ -93,5 +97,5 @@ declare const sendPATCH: (input: IRequestInput, options?: Partial<IOptions>) =>
93
97
  * - CONTENT_TYPE_MISSMATCH: if the Content-Type Headers don't match
94
98
  * - INVALID_RESPONSE_DTYPE: if the data type is not supported by the Response Instance
95
99
  */
96
- declare const sendDELETE: (input: IRequestInput, options?: Partial<IOptions>) => Promise<IRequestResponse>;
100
+ declare const sendDELETE: (input: IRequestInput, options?: Partial<IOptions>, retryDelaySchedule?: number[]) => Promise<IRequestResponse>;
97
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{extractMessage}from"error-message-utils";import{buildOptions,buildRequest,extractResponseData,delay}from"./utils/utils.js";import{validateResponse}from"./validations/validations.js";const __executeSend=async(e,s)=>{const t=buildOptions(s),n=buildRequest(e,t.requestOptions),r=await fetch(n);return validateResponse(n,r,t),r.redirected&&console.warn(`The request sent to '${n.url}' was redirected. Please update the implementation to avoid future redirections.`),{code:r.status,headers:r.headers,data:await extractResponseData(r,t.responseDataType)}},send=async(e,s,t)=>{try{return await __executeSend(e,s)}catch(n){if(extractMessage(n).includes("429")||!Array.isArray(t)||0===t.length)throw n;return await delay(t[0]),send(e,s,t.slice(1))}},sendGET=async(e,s,t)=>send(e,{...s,requestOptions:{...s?.requestOptions,method:"GET"}},t),sendPOST=(e,s,t)=>send(e,{...s,requestOptions:{...s?.requestOptions,method:"POST"}},t),sendPUT=(e,s,t)=>send(e,{...s,requestOptions:{...s?.requestOptions,method:"PUT"}},t),sendPATCH=(e,s,t)=>send(e,{...s,requestOptions:{...s?.requestOptions,method:"PATCH"}},t),sendDELETE=(e,s,t)=>send(e,{...s,requestOptions:{...s?.requestOptions,method:"DELETE"}},t);export{send,sendGET,sendPOST,sendPUT,sendPATCH,sendDELETE};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetch-request-browser",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
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
  }