api-def 0.7.4 → 0.8.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/cjs/Api.js +6 -2
- package/cjs/ApiTypes.d.ts +30 -10
- package/cjs/ApiTypes.js +2 -0
- package/cjs/ApiUtils.js +1 -1
- package/cjs/Endpoint.d.ts +3 -3
- package/cjs/Endpoint.js +13 -8
- package/cjs/QueryHandling.d.ts +3 -0
- package/cjs/QueryHandling.js +24 -0
- package/cjs/RequestConfig.d.ts +2 -0
- package/cjs/RequestConfig.js +74 -0
- package/cjs/RequestContext.d.ts +4 -4
- package/cjs/RequestContext.js +5 -24
- package/cjs/RequestError.js +1 -1
- package/cjs/Requester.d.ts +1 -1
- package/cjs/Requester.js +2 -2
- package/cjs/backend/AxiosRequestBackend.js +4 -4
- package/cjs/backend/FetchRequestBackend.js +4 -3
- package/cjs/backend/MockRequestBackend.js +3 -1
- package/esm/Api.js +34 -70
- package/esm/ApiConstants.js +5 -5
- package/esm/ApiTypes.d.ts +30 -10
- package/esm/ApiTypes.js +1 -1
- package/esm/ApiUtils.js +12 -13
- package/esm/Endpoint.d.ts +3 -3
- package/esm/Endpoint.js +42 -74
- package/esm/EndpointBuilder.js +14 -16
- package/esm/QueryHandling.d.ts +3 -0
- package/esm/QueryHandling.js +19 -0
- package/esm/RequestConfig.d.ts +2 -0
- package/esm/RequestConfig.js +56 -0
- package/esm/RequestContext.d.ts +4 -4
- package/esm/RequestContext.js +71 -148
- package/esm/RequestError.js +7 -7
- package/esm/Requester.d.ts +1 -1
- package/esm/Requester.js +182 -266
- package/esm/TextDecoding.js +20 -20
- package/esm/Utils.js +15 -15
- package/esm/backend/AxiosRequestBackend.js +34 -67
- package/esm/backend/FetchRequestBackend.js +66 -134
- package/esm/backend/MockRequestBackend.js +92 -136
- package/esm/cache/Caching.js +24 -66
- package/esm/cache/LocalForageCacheBackend.js +11 -13
- package/esm/cache/LocalStorageCacheBackend.js +19 -62
- package/esm/middleware/CacheMiddleware.js +44 -91
- package/esm/middleware/LoggingMiddleware.js +36 -39
- package/esm/util/retry/index.js +8 -8
- package/esm/util/retry/lib/retry.js +11 -22
- package/package.json +4 -2
package/esm/cache/Caching.js
CHANGED
|
@@ -7,75 +7,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (_) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
10
|
import LocalStorageCacheBackend from "./LocalStorageCacheBackend";
|
|
38
|
-
|
|
39
|
-
export
|
|
11
|
+
let cacheBackend = new LocalStorageCacheBackend();
|
|
12
|
+
export const setCacheBackend = (backend) => {
|
|
40
13
|
cacheBackend = backend;
|
|
41
14
|
};
|
|
42
|
-
export
|
|
43
|
-
export
|
|
15
|
+
export const DEFAULT_CACHE_EXPIRY = /* 15 mins */ 60 * 1000 * 15;
|
|
16
|
+
export const clearCache = () => {
|
|
44
17
|
return cacheBackend.clear();
|
|
45
18
|
};
|
|
46
|
-
export
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
var entry;
|
|
64
|
-
return __generator(this, function (_a) {
|
|
65
|
-
switch (_a.label) {
|
|
66
|
-
case 0: return [4 /*yield*/, cacheBackend.getItem(key)];
|
|
67
|
-
case 1:
|
|
68
|
-
entry = _a.sent();
|
|
69
|
-
if (!entry) {
|
|
70
|
-
return [2 /*return*/, undefined];
|
|
71
|
-
}
|
|
72
|
-
if (!(typeof entry.expiry === "number")) return [3 /*break*/, 3];
|
|
73
|
-
if (!(Date.now() >= entry.expiry)) return [3 /*break*/, 3];
|
|
74
|
-
return [4 /*yield*/, cacheBackend.removeItem(key)];
|
|
75
|
-
case 2:
|
|
76
|
-
_a.sent();
|
|
77
|
-
return [2 /*return*/, undefined];
|
|
78
|
-
case 3: return [2 /*return*/, entry.data];
|
|
19
|
+
export const setCachedItem = (key, value, expiry) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const entry = {
|
|
21
|
+
data: value,
|
|
22
|
+
expiry: expiry === undefined || isNaN(expiry) ? null : expiry,
|
|
23
|
+
};
|
|
24
|
+
yield cacheBackend.setItem(key, entry);
|
|
25
|
+
return value;
|
|
26
|
+
});
|
|
27
|
+
export const getCachedItem = (key) => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
+
const entry = yield cacheBackend.getItem(key);
|
|
29
|
+
if (!entry) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
if (typeof entry.expiry === "number") {
|
|
33
|
+
if (Date.now() >= entry.expiry) {
|
|
34
|
+
yield cacheBackend.removeItem(key);
|
|
35
|
+
return undefined;
|
|
79
36
|
}
|
|
80
|
-
}
|
|
81
|
-
|
|
37
|
+
}
|
|
38
|
+
return entry.data;
|
|
39
|
+
});
|
|
@@ -1,22 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
export default class LocalForageCacheBackend {
|
|
2
2
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
3
|
-
|
|
3
|
+
constructor(localforage) {
|
|
4
4
|
this.store = localforage.createInstance({
|
|
5
5
|
name: "requestCache",
|
|
6
6
|
});
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
clear() {
|
|
9
9
|
return this.store.clear();
|
|
10
|
-
}
|
|
11
|
-
|
|
10
|
+
}
|
|
11
|
+
getItem(key) {
|
|
12
12
|
return this.store.getItem(key);
|
|
13
|
-
}
|
|
14
|
-
|
|
13
|
+
}
|
|
14
|
+
removeItem(key) {
|
|
15
15
|
return this.store.removeItem(key);
|
|
16
|
-
}
|
|
17
|
-
|
|
16
|
+
}
|
|
17
|
+
setItem(key, value) {
|
|
18
18
|
return this.store.setItem(key, value);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
}());
|
|
22
|
-
export default LocalForageCacheBackend;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -7,69 +7,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (_) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
var LocalStorageCacheBackend = /** @class */ (function () {
|
|
38
|
-
function LocalStorageCacheBackend() {
|
|
39
|
-
}
|
|
40
|
-
LocalStorageCacheBackend.prototype.clear = function () {
|
|
41
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
42
|
-
return __generator(this, function (_a) {
|
|
43
|
-
localStorage.clear();
|
|
44
|
-
return [2 /*return*/];
|
|
45
|
-
});
|
|
10
|
+
export default class LocalStorageCacheBackend {
|
|
11
|
+
clear() {
|
|
12
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
+
localStorage.clear();
|
|
46
14
|
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
50
|
-
|
|
51
|
-
return
|
|
52
|
-
value = localStorage.getItem(key);
|
|
53
|
-
return [2 /*return*/, value ? JSON.parse(value) : null];
|
|
54
|
-
});
|
|
15
|
+
}
|
|
16
|
+
getItem(key) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
const value = localStorage.getItem(key);
|
|
19
|
+
return value ? JSON.parse(value) : null;
|
|
55
20
|
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
59
|
-
|
|
60
|
-
localStorage.removeItem(key);
|
|
61
|
-
return [2 /*return*/];
|
|
62
|
-
});
|
|
21
|
+
}
|
|
22
|
+
removeItem(key) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
localStorage.removeItem(key);
|
|
63
25
|
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
67
|
-
|
|
68
|
-
localStorage.setItem(key, JSON.stringify(value));
|
|
69
|
-
return [2 /*return*/];
|
|
70
|
-
});
|
|
26
|
+
}
|
|
27
|
+
setItem(key, value) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
71
30
|
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
}());
|
|
75
|
-
export default LocalStorageCacheBackend;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -7,99 +7,52 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (_) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
10
|
import * as Caching from "../cache/Caching";
|
|
38
11
|
import { CacheSource, EventResultType, RequestEvent, RequestMethod } from "../ApiConstants";
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (!(cache && shouldCache)) return [3 /*break*/, 2];
|
|
72
|
-
if (!cache) return [3 /*break*/, 2];
|
|
73
|
-
return [4 /*yield*/, Caching.getCachedItem(context.key)];
|
|
74
|
-
case 1:
|
|
75
|
-
cachedValue = _a.sent();
|
|
76
|
-
if (cachedValue) {
|
|
77
|
-
context.stats.cached = {
|
|
78
|
-
is: true,
|
|
79
|
-
by: CacheSource.Local,
|
|
80
|
-
};
|
|
81
|
-
context.cacheInfo.source = CacheSource.Local;
|
|
82
|
-
context.cacheInfo.cached = true;
|
|
83
|
-
return [2 /*return*/, {
|
|
84
|
-
type: EventResultType.Respond,
|
|
85
|
-
response: cachedValue,
|
|
86
|
-
}];
|
|
87
|
-
}
|
|
88
|
-
_a.label = 2;
|
|
89
|
-
case 2:
|
|
90
|
-
if (cache === false) {
|
|
91
|
-
context.updateQuery({
|
|
92
|
-
_bust: Math.floor(Math.random() * 9000) + 1000,
|
|
93
|
-
});
|
|
94
|
-
context.updateHeaders({
|
|
95
|
-
Pragma: "no-cache, no-store, must-revalidate",
|
|
96
|
-
"Cache-Control": "no-cache, no-store, must-revalidate",
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
return [2 /*return*/];
|
|
12
|
+
const CacheMiddleware = (options = {}) => {
|
|
13
|
+
return {
|
|
14
|
+
[RequestEvent.Success]: (context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
if (context.method !== RequestMethod.Get)
|
|
16
|
+
return;
|
|
17
|
+
const { cache } = context.computedConfig || {};
|
|
18
|
+
const shouldCache = !options.predicate || options.predicate();
|
|
19
|
+
if (cache && shouldCache) {
|
|
20
|
+
const expiry = typeof cache === "number" ? cache : (options.defaultExpiry || Caching.DEFAULT_CACHE_EXPIRY);
|
|
21
|
+
yield Caching.setCachedItem(context.key, context.response, expiry);
|
|
22
|
+
}
|
|
23
|
+
}),
|
|
24
|
+
[RequestEvent.BeforeSend]: (context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
if (context.method !== RequestMethod.Get)
|
|
26
|
+
return;
|
|
27
|
+
const { cache } = context.computedConfig || {};
|
|
28
|
+
const shouldCache = !options.predicate || options.predicate();
|
|
29
|
+
if (cache && shouldCache) {
|
|
30
|
+
if (cache) {
|
|
31
|
+
const cachedValue = yield Caching.getCachedItem(context.key);
|
|
32
|
+
if (cachedValue) {
|
|
33
|
+
context.stats.cached = {
|
|
34
|
+
is: true,
|
|
35
|
+
by: CacheSource.Local,
|
|
36
|
+
};
|
|
37
|
+
context.cacheInfo.source = CacheSource.Local;
|
|
38
|
+
context.cacheInfo.cached = true;
|
|
39
|
+
return {
|
|
40
|
+
type: EventResultType.Respond,
|
|
41
|
+
response: cachedValue,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
100
44
|
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
45
|
+
}
|
|
46
|
+
if (cache === false) {
|
|
47
|
+
context.updateQuery({
|
|
48
|
+
_bust: Math.floor(Math.random() * 9000) + 1000,
|
|
49
|
+
});
|
|
50
|
+
context.updateHeaders({
|
|
51
|
+
Pragma: "no-cache, no-store, must-revalidate",
|
|
52
|
+
"Cache-Control": "no-cache, no-store, must-revalidate",
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}),
|
|
56
|
+
};
|
|
104
57
|
};
|
|
105
58
|
export default CacheMiddleware;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
var _a;
|
|
2
1
|
import * as ApiUtils from "../ApiUtils";
|
|
3
2
|
import * as Utils from "../Utils";
|
|
4
3
|
import { RequestEvent } from "../ApiConstants";
|
|
@@ -9,79 +8,77 @@ var LogType;
|
|
|
9
8
|
LogType[LogType["Error"] = 2] = "Error";
|
|
10
9
|
LogType[LogType["Warn"] = 3] = "Warn";
|
|
11
10
|
})(LogType || (LogType = {}));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
const COLOR_MAP = {
|
|
12
|
+
[LogType.Error]: "#c8646c",
|
|
13
|
+
[LogType.Info]: "#85a6c7",
|
|
14
|
+
[LogType.Success]: "#a9c490",
|
|
15
|
+
[LogType.Warn]: "#d19a66",
|
|
16
|
+
};
|
|
17
|
+
const diagnoseError = (error) => {
|
|
19
18
|
if (!error.response) {
|
|
20
19
|
if (ApiUtils.isNetworkError(error)) {
|
|
21
|
-
return { message: "network issue", error
|
|
20
|
+
return { message: "network issue", error };
|
|
22
21
|
}
|
|
23
22
|
if (ApiUtils.isCancelledError(error)) {
|
|
24
|
-
return { message: "cancelled", error
|
|
23
|
+
return { message: "cancelled", error };
|
|
25
24
|
}
|
|
26
|
-
return { message: "client-side error", error
|
|
25
|
+
return { message: "client-side error", error };
|
|
27
26
|
}
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
const { status, data } = error.response;
|
|
28
|
+
const code = data === null || data === void 0 ? void 0 : data.code;
|
|
30
29
|
return {
|
|
31
|
-
message:
|
|
30
|
+
message: `responded with ${status}${code ? ` (${code})` : ""}`,
|
|
32
31
|
response: error.response,
|
|
33
32
|
};
|
|
34
33
|
};
|
|
35
|
-
|
|
36
|
-
return
|
|
34
|
+
const formatTime = (time) => {
|
|
35
|
+
return `${Utils.padNumber(time.getHours(), 2)}:${Utils.padNumber(time.getMinutes(), 2)}:${Utils.padNumber(time.getSeconds(), 2)}.${Utils.padNumber(time.getMilliseconds(), 3)}`;
|
|
37
36
|
};
|
|
38
|
-
|
|
37
|
+
const log = (context, type, message, config, objects) => {
|
|
39
38
|
if (typeof config.predicate === "function" && !config.predicate()) {
|
|
40
39
|
return;
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
const { computedPath } = context;
|
|
42
|
+
const color = COLOR_MAP[type];
|
|
43
|
+
const timestamp = formatTime(new Date());
|
|
44
|
+
const args = [
|
|
45
|
+
`%cnetwork %c[${context.api.name}] ${context.method.toUpperCase()} ${computedPath} %c${message} %c@ ${timestamp}`,
|
|
47
46
|
"color:gray",
|
|
48
47
|
"color:auto",
|
|
49
|
-
|
|
48
|
+
`color:${color}`,
|
|
50
49
|
"color:gray",
|
|
51
50
|
];
|
|
52
51
|
/* eslint-disable-next-line no-console */
|
|
53
|
-
console.groupCollapsed
|
|
52
|
+
console.groupCollapsed(...args);
|
|
54
53
|
/* eslint-disable-next-line no-console */
|
|
55
54
|
console.log(Utils.assign({ context: context }, objects || {}));
|
|
56
55
|
/* eslint-disable-next-line no-console */
|
|
57
56
|
console.groupEnd();
|
|
58
57
|
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return _a = {},
|
|
63
|
-
_a[RequestEvent.BeforeSend] = function (context) {
|
|
58
|
+
const LoggingMiddleware = (config = {}) => {
|
|
59
|
+
return {
|
|
60
|
+
[RequestEvent.BeforeSend]: (context) => {
|
|
64
61
|
log(context, LogType.Info, context.stats.attempt > 1 ? "retrying" : "sending", config);
|
|
65
62
|
},
|
|
66
|
-
|
|
63
|
+
[RequestEvent.Success]: (context) => {
|
|
67
64
|
var _a;
|
|
68
|
-
|
|
69
|
-
log(context, LogType.Success,
|
|
65
|
+
const cacheSource = context.cacheInfo.source;
|
|
66
|
+
log(context, LogType.Success, `responded with ${(_a = context.response) === null || _a === void 0 ? void 0 : _a.status}${cacheSource ? ` (cached by ${cacheSource})` : ""}`, config);
|
|
70
67
|
},
|
|
71
|
-
|
|
68
|
+
[RequestEvent.Error]: (context) => {
|
|
72
69
|
if (context.error) {
|
|
73
|
-
|
|
74
|
-
log(context, LogType.Warn,
|
|
70
|
+
const { error, message } = diagnoseError(context.error);
|
|
71
|
+
log(context, LogType.Warn, `error on attempt ${context.stats.attempt} - ${message}`, config, { error: error });
|
|
75
72
|
}
|
|
76
73
|
},
|
|
77
|
-
|
|
74
|
+
[RequestEvent.UnrecoverableError]: (context) => {
|
|
78
75
|
if (context.error) {
|
|
79
|
-
|
|
80
|
-
log(context, LogType.Error,
|
|
76
|
+
const { error, message } = diagnoseError(context.error);
|
|
77
|
+
log(context, LogType.Error, `failed - ${message}`, config, {
|
|
81
78
|
error: error,
|
|
82
79
|
});
|
|
83
80
|
}
|
|
84
81
|
},
|
|
85
|
-
|
|
82
|
+
};
|
|
86
83
|
};
|
|
87
84
|
export default LoggingMiddleware;
|
package/esm/util/retry/index.js
CHANGED
|
@@ -3,21 +3,21 @@ import * as retrier from "./lib/retry";
|
|
|
3
3
|
// https://www.npmjs.com/package/retry (lib), and
|
|
4
4
|
// https://www.npmjs.com/package/async-retry (RetryFunction)
|
|
5
5
|
// ---
|
|
6
|
-
|
|
7
|
-
return new Promise(
|
|
8
|
-
|
|
6
|
+
const retry = (fn, opts) => {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const options = opts || {};
|
|
9
9
|
if (!("randomize" in options)) {
|
|
10
10
|
options.randomize = true;
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
const op = retrier.operation(options);
|
|
13
13
|
// We allow the user to abort retrying
|
|
14
14
|
// this makes sense in the cases where
|
|
15
15
|
// knowledge is obtained that retrying
|
|
16
16
|
// would be futile (e.g.: auth errors)
|
|
17
|
-
|
|
17
|
+
const bail = (err) => {
|
|
18
18
|
reject(err || new Error("Aborted"));
|
|
19
19
|
};
|
|
20
|
-
|
|
20
|
+
const onError = (err, num) => {
|
|
21
21
|
if (err.bail) {
|
|
22
22
|
bail(err);
|
|
23
23
|
return;
|
|
@@ -29,8 +29,8 @@ var retry = function (fn, opts) {
|
|
|
29
29
|
options.onRetry(err, num);
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const runAttempt = (num) => {
|
|
33
|
+
let val;
|
|
34
34
|
try {
|
|
35
35
|
val = fn(bail, num);
|
|
36
36
|
}
|
|
@@ -1,42 +1,31 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
import RetryOperation from "./retryOperation";
|
|
13
|
-
export
|
|
14
|
-
|
|
2
|
+
export const operation = (options) => {
|
|
3
|
+
const timeouts = _timeouts(options);
|
|
15
4
|
return (new RetryOperation(timeouts, {
|
|
16
5
|
forever: options && (options.forever || options.retries === Infinity),
|
|
17
6
|
unref: options && options.unref,
|
|
18
7
|
maxRetryTime: options && options.maxRetryTime,
|
|
19
8
|
}));
|
|
20
9
|
};
|
|
21
|
-
|
|
10
|
+
const _timeouts = (options) => {
|
|
22
11
|
var _a;
|
|
23
|
-
|
|
24
|
-
|
|
12
|
+
const createTimeout = (attempt, opts) => {
|
|
13
|
+
const random = (opts.randomize)
|
|
25
14
|
? (Math.random() + 1)
|
|
26
15
|
: 1;
|
|
27
|
-
|
|
16
|
+
let timeout = Math.round(random * Math.max(opts.minTimeout, 1) * Math.pow(opts.factor, attempt));
|
|
28
17
|
timeout = Math.min(timeout, opts.maxTimeout);
|
|
29
18
|
return (timeout);
|
|
30
19
|
};
|
|
31
|
-
|
|
32
|
-
|
|
20
|
+
const defaultRetries = 10;
|
|
21
|
+
const opts = Object.assign({ retries: defaultRetries, factor: 2, minTimeout: 1 * 1000, maxTimeout: Infinity, randomize: false }, options);
|
|
33
22
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
34
23
|
if (opts.minTimeout > opts.maxTimeout) {
|
|
35
24
|
throw new Error("minTimeout is greater than maxTimeout");
|
|
36
25
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
for (
|
|
26
|
+
const timeouts = [];
|
|
27
|
+
const numRetries = (_a = opts.retries) !== null && _a !== void 0 ? _a : defaultRetries;
|
|
28
|
+
for (let i = 0; i < numRetries; i++) {
|
|
40
29
|
timeouts.push(createTimeout(i, opts));
|
|
41
30
|
}
|
|
42
31
|
if (options && options.forever && !timeouts.length) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-def",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Typed API definitions with middleware support",
|
|
5
5
|
"main": "cjs/index.js",
|
|
6
6
|
"types": "esm/index.d.ts",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"lint:strict": "npm run lint -- --max-warnings 0",
|
|
20
20
|
"cleanup": "rimraf esm && rimraf cjs",
|
|
21
21
|
"build": "npm run cleanup && npm run build:esm && npm run build:cjs",
|
|
22
|
-
"build:esm": "tsc --module es2015 --target
|
|
22
|
+
"build:esm": "tsc --module es2015 --target es2016 --outDir esm --preserveWatchOutput",
|
|
23
23
|
"build:cjs": "tsc --module commonjs --target es5 --outDir cjs --preserveWatchOutput",
|
|
24
24
|
"build:watch": "npm-run-all -p \"build:esm -- -w\" \"build:cjs -- -w\" \"lint:watch\"",
|
|
25
25
|
"website:dev": "cd website && npm run start",
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
"@esbuild-kit/cjs-loader": "2.4.0",
|
|
64
64
|
"@types/axios": "0.14.0",
|
|
65
65
|
"@types/node": "16.4.6",
|
|
66
|
+
"@types/qs": "6.9.8",
|
|
66
67
|
"@typescript-eslint/eslint-plugin": "4.28.5",
|
|
67
68
|
"@typescript-eslint/parser": "4.28.5",
|
|
68
69
|
"ava": "5.0.1",
|
|
@@ -71,6 +72,7 @@
|
|
|
71
72
|
"eslint": "7.31.0",
|
|
72
73
|
"eslint-watch": "7.0.0",
|
|
73
74
|
"npm-run-all": "4.1.5",
|
|
75
|
+
"qs": "6.11.2",
|
|
74
76
|
"typescript": "4.8.4"
|
|
75
77
|
}
|
|
76
78
|
}
|