@rendomnet/apiservice 1.3.3 → 1.3.5
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 +35 -0
- package/dist/HttpClient.js +4 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -1
- package/dist/types.d.ts +1 -0
- package/package.json +12 -10
package/README.md
CHANGED
|
@@ -194,6 +194,41 @@ const result = await api.call({
|
|
|
194
194
|
|
|
195
195
|
If no accountId is provided, ApiService automatically uses 'default' as the account ID.
|
|
196
196
|
|
|
197
|
+
## Request Cancellation
|
|
198
|
+
|
|
199
|
+
ApiService supports request cancellation using the standard `AbortController` API. This allows you to cancel ongoing API requests, including those that are being retried.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// Create an AbortController
|
|
203
|
+
const controller = new AbortController();
|
|
204
|
+
|
|
205
|
+
// Make an API call with the controller's signal
|
|
206
|
+
const apiPromise = api.call({
|
|
207
|
+
method: 'GET',
|
|
208
|
+
route: '/users',
|
|
209
|
+
abortSignal: controller.signal
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Cancel the request at any time
|
|
213
|
+
controller.abort();
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
await apiPromise;
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (error.name === 'AbortError' || error.message === 'Request aborted') {
|
|
219
|
+
console.log('Request was successfully aborted');
|
|
220
|
+
} else {
|
|
221
|
+
console.error('An error occurred:', error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The abort functionality works with all features of ApiService:
|
|
227
|
+
- Cancels ongoing requests immediately
|
|
228
|
+
- Prevents retry attempts from starting
|
|
229
|
+
- Works with cached and non-cached requests
|
|
230
|
+
- Compatible with all authentication providers
|
|
231
|
+
|
|
197
232
|
## AuthProvider Interface
|
|
198
233
|
|
|
199
234
|
```typescript
|
package/dist/HttpClient.js
CHANGED
|
@@ -15,7 +15,7 @@ class HttpClient {
|
|
|
15
15
|
* Make an HTTP request
|
|
16
16
|
*/
|
|
17
17
|
async makeRequest(apiParams, authToken) {
|
|
18
|
-
const { accountId, method, route, base, body, data, headers, queryParams, contentType = 'application/json', accessToken: forcedAccessToken, useAuth = true, files, } = apiParams;
|
|
18
|
+
const { accountId, method, route, base, body, data, headers, queryParams, contentType = 'application/json', accessToken: forcedAccessToken, useAuth = true, files, abortSignal, } = apiParams;
|
|
19
19
|
// Build URL and request body
|
|
20
20
|
const url = this.buildUrl(base, route, queryParams);
|
|
21
21
|
const requestBody = body || data;
|
|
@@ -32,6 +32,7 @@ class HttpClient {
|
|
|
32
32
|
forcedAccessToken,
|
|
33
33
|
useAuth,
|
|
34
34
|
headers,
|
|
35
|
+
abortSignal,
|
|
35
36
|
});
|
|
36
37
|
// Make the request
|
|
37
38
|
try {
|
|
@@ -71,10 +72,11 @@ class HttpClient {
|
|
|
71
72
|
/**
|
|
72
73
|
* Build fetch options for request
|
|
73
74
|
*/
|
|
74
|
-
buildFetchOptions({ method, body, formData, contentType, authToken, forcedAccessToken, useAuth, headers, }) {
|
|
75
|
+
buildFetchOptions({ method, body, formData, contentType, authToken, forcedAccessToken, useAuth, headers, abortSignal, }) {
|
|
75
76
|
const allowedMethods = ['POST', 'PUT', 'PATCH'];
|
|
76
77
|
return {
|
|
77
78
|
method,
|
|
79
|
+
signal: abortSignal,
|
|
78
80
|
headers: {
|
|
79
81
|
...(useAuth && {
|
|
80
82
|
Authorization: `Bearer ${forcedAccessToken || authToken.access_token}`
|
package/dist/index.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ declare class ApiService {
|
|
|
42
42
|
*/
|
|
43
43
|
call(apiCallParams: Omit<ApiCallParams, 'accountId'> & {
|
|
44
44
|
accountId?: string;
|
|
45
|
+
abortSignal?: AbortSignal;
|
|
45
46
|
}): Promise<any>;
|
|
46
47
|
/**
|
|
47
48
|
* Legacy method for backward compatibility
|
|
@@ -49,6 +50,7 @@ declare class ApiService {
|
|
|
49
50
|
*/
|
|
50
51
|
makeApiCall(apiCallParams: Omit<ApiCallParams, 'accountId'> & {
|
|
51
52
|
accountId?: string;
|
|
53
|
+
abortSignal?: AbortSignal;
|
|
52
54
|
}): Promise<any>;
|
|
53
55
|
/**
|
|
54
56
|
* Make a request with retry capability
|
package/dist/index.js
CHANGED
|
@@ -135,7 +135,7 @@ class ApiService {
|
|
|
135
135
|
*/
|
|
136
136
|
async makeRequestWithRetry(apiCallParams) {
|
|
137
137
|
var _a;
|
|
138
|
-
const { accountId } = apiCallParams;
|
|
138
|
+
const { accountId, abortSignal } = apiCallParams;
|
|
139
139
|
let attempts = 0;
|
|
140
140
|
const statusRetries = {};
|
|
141
141
|
let currentParams = { ...apiCallParams };
|
|
@@ -150,6 +150,9 @@ class ApiService {
|
|
|
150
150
|
}
|
|
151
151
|
// Main retry loop
|
|
152
152
|
while (attempts < this.maxAttempts) {
|
|
153
|
+
if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) {
|
|
154
|
+
throw new Error('Request aborted');
|
|
155
|
+
}
|
|
153
156
|
attempts++;
|
|
154
157
|
try {
|
|
155
158
|
// Get authentication headers if needed
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rendomnet/apiservice",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.5",
|
|
4
4
|
"description": "A robust TypeScript API service framework for making authenticated API calls",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist"
|
|
9
9
|
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"clean": "rimraf dist",
|
|
13
|
+
"prebuild": "npm run clean",
|
|
14
|
+
"prepublishOnly": "npm run build",
|
|
15
|
+
"test": "jest",
|
|
16
|
+
"test:watch": "jest --watch",
|
|
17
|
+
"test:coverage": "jest --coverage",
|
|
18
|
+
"prepare": "npm run build"
|
|
19
|
+
},
|
|
10
20
|
"keywords": [
|
|
11
21
|
"api",
|
|
12
22
|
"service",
|
|
@@ -40,13 +50,5 @@
|
|
|
40
50
|
},
|
|
41
51
|
"publishConfig": {
|
|
42
52
|
"access": "public"
|
|
43
|
-
},
|
|
44
|
-
"scripts": {
|
|
45
|
-
"build": "tsc",
|
|
46
|
-
"clean": "rimraf dist",
|
|
47
|
-
"prebuild": "npm run clean",
|
|
48
|
-
"test": "jest",
|
|
49
|
-
"test:watch": "jest --watch",
|
|
50
|
-
"test:coverage": "jest --coverage"
|
|
51
53
|
}
|
|
52
|
-
}
|
|
54
|
+
}
|