api-def 0.12.0 → 0.13.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/README.md +3 -0
- package/bin/index.js +312 -313
- package/cjs/Api.d.ts +6 -6
- package/cjs/Api.js +99 -143
- package/cjs/ApiTypes.d.ts +1 -1
- package/cjs/ApiUtils.d.ts +2 -1
- package/cjs/ApiUtils.js +41 -33
- package/cjs/Endpoint.d.ts +1 -1
- package/cjs/Endpoint.js +112 -175
- package/cjs/EndpointBuilder.js +23 -32
- package/cjs/QueryHandling.js +12 -13
- package/cjs/RequestConfig.js +54 -36
- package/cjs/RequestContext.d.ts +1 -1
- package/cjs/RequestContext.js +125 -180
- package/cjs/RequestError.js +16 -19
- package/cjs/Requester.js +230 -312
- package/cjs/TextDecoding.js +31 -31
- package/cjs/Utils.js +20 -23
- package/cjs/backend/AxiosRequestBackend.d.ts +1 -1
- package/cjs/backend/AxiosRequestBackend.js +65 -172
- package/cjs/backend/FetchRequestBackend.d.ts +1 -1
- package/cjs/backend/FetchRequestBackend.js +108 -154
- package/cjs/backend/MockRequestBackend.d.ts +1 -1
- package/cjs/backend/MockRequestBackend.js +146 -208
- package/cjs/cache/ClientCaching.js +26 -74
- package/cjs/cache/LocalForageClientCacheBackend.js +15 -83
- package/cjs/cache/LocalStorageClientCacheBackend.js +14 -73
- package/cjs/index.d.ts +11 -11
- package/cjs/index.js +24 -21
- package/cjs/middleware/ClientCacheMiddleware.js +80 -103
- package/cjs/middleware/LoggingMiddleware.js +71 -42
- package/cjs/util/retry/index.js +42 -9
- package/cjs/util/retry/lib/retry.js +25 -27
- package/cjs/util/retry/lib/retryOperation.d.ts +1 -26
- package/cjs/util/retry/lib/retryOperation.js +21 -23
- package/esm/Api.d.ts +11 -11
- package/esm/Api.js +12 -23
- package/esm/ApiConstants.d.ts +1 -1
- package/esm/ApiTypes.d.ts +5 -5
- package/esm/ApiUtils.d.ts +4 -3
- package/esm/ApiUtils.js +15 -7
- package/esm/Endpoint.d.ts +6 -6
- package/esm/Endpoint.js +22 -28
- package/esm/EndpointBuilder.d.ts +4 -4
- package/esm/EndpointBuilder.js +2 -2
- package/esm/MockingTypes.d.ts +1 -1
- package/esm/QueryHandling.d.ts +1 -1
- package/esm/QueryHandling.js +2 -3
- package/esm/RequestConfig.d.ts +1 -1
- package/esm/RequestConfig.js +6 -7
- package/esm/RequestContext.d.ts +7 -7
- package/esm/RequestContext.js +15 -27
- package/esm/RequestError.d.ts +3 -3
- package/esm/RequestError.js +2 -3
- package/esm/Requester.d.ts +2 -2
- package/esm/Requester.js +44 -50
- package/esm/UtilTypes.d.ts +1 -1
- package/esm/Utils.js +2 -1
- package/esm/Validation.d.ts +1 -1
- package/esm/backend/AxiosRequestBackend.d.ts +4 -4
- package/esm/backend/AxiosRequestBackend.js +47 -84
- package/esm/backend/FetchRequestBackend.d.ts +5 -5
- package/esm/backend/FetchRequestBackend.js +49 -63
- package/esm/backend/MockRequestBackend.d.ts +4 -4
- package/esm/backend/MockRequestBackend.js +105 -136
- package/esm/backend/RequestBackend.d.ts +2 -2
- package/esm/cache/ClientCaching.d.ts +1 -1
- package/esm/cache/ClientCaching.js +8 -17
- package/esm/cache/LocalForageClientCacheBackend.d.ts +1 -1
- package/esm/cache/LocalForageClientCacheBackend.js +8 -25
- package/esm/cache/LocalStorageClientCacheBackend.d.ts +1 -1
- package/esm/cache/LocalStorageClientCacheBackend.js +9 -26
- package/esm/index.d.ts +16 -16
- package/esm/index.js +15 -15
- package/esm/middleware/ClientCacheMiddleware.d.ts +1 -1
- package/esm/middleware/ClientCacheMiddleware.js +8 -17
- package/esm/middleware/LoggingMiddleware.d.ts +1 -1
- package/esm/middleware/LoggingMiddleware.js +5 -6
- package/esm/util/retry/index.js +1 -1
- package/esm/util/retry/lib/retry.d.ts +1 -1
- package/esm/util/retry/lib/retry.js +13 -7
- package/esm/util/retry/lib/retryOperation.d.ts +1 -26
- package/esm/util/retry/lib/retryOperation.js +1 -1
- package/package.json +66 -28
|
@@ -1,226 +1,164 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
36
7
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
51
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
36
|
+
const ApiUtils_1 = require("../ApiUtils");
|
|
37
|
+
const RequestError_1 = require("../RequestError");
|
|
38
|
+
const Utils = __importStar(require("../Utils"));
|
|
39
|
+
const Utils_1 = require("../Utils");
|
|
40
|
+
class MockRequestBackend {
|
|
41
|
+
constructor() {
|
|
58
42
|
this.id = "mock";
|
|
59
43
|
}
|
|
60
|
-
|
|
61
|
-
return
|
|
62
|
-
|
|
63
|
-
|
|
44
|
+
async convertResponse(context, response, error) {
|
|
45
|
+
return response;
|
|
46
|
+
}
|
|
47
|
+
async extractResponseFromError(error) {
|
|
48
|
+
if ("response" in error) {
|
|
49
|
+
const fetchError = error;
|
|
50
|
+
return fetchError.response ? fetchError.response : null;
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
async runRequest(context) {
|
|
55
|
+
const mockingFunc = context.mocking?.handler;
|
|
56
|
+
if (!mockingFunc) {
|
|
57
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
58
|
+
error: new Error("[api-def] Attempted to run mocked request without mocking function"),
|
|
59
|
+
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
60
|
+
context,
|
|
64
61
|
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
62
|
+
}
|
|
63
|
+
const req = {
|
|
64
|
+
body: context.getParsedBody(),
|
|
65
|
+
params: context.requestConfig.params ?? {},
|
|
66
|
+
query: context.requestConfig.queryObject,
|
|
67
|
+
headers: context.requestConfig.headers ?? {},
|
|
68
|
+
url: context.requestUrl.toString(),
|
|
69
|
+
state: context.requestConfig.state,
|
|
70
|
+
};
|
|
71
|
+
const res = {
|
|
72
|
+
statusCode: -1,
|
|
73
|
+
headers: {},
|
|
74
|
+
response: undefined,
|
|
75
|
+
status(statusCode) {
|
|
76
|
+
res.statusCode = statusCode;
|
|
77
|
+
return res;
|
|
78
|
+
},
|
|
79
|
+
send(response) {
|
|
80
|
+
res.response = response;
|
|
81
|
+
if (response && typeof response === "object") {
|
|
82
|
+
res.headers["Content-Type"] = "application/json";
|
|
83
|
+
}
|
|
84
|
+
return res;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
if (context.mocking?.delay) {
|
|
88
|
+
const delay = context.mocking.delay;
|
|
89
|
+
let delayMs;
|
|
90
|
+
if (typeof delay === "number") {
|
|
91
|
+
delayMs = delay;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const [min, max] = delay;
|
|
95
|
+
if (min > max) {
|
|
96
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
97
|
+
error: new Error("[api-def] Min delay cannot be greater than max delay"),
|
|
98
|
+
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
99
|
+
context,
|
|
100
|
+
});
|
|
74
101
|
}
|
|
75
|
-
|
|
102
|
+
delayMs = (0, Utils_1.randInt)(min, max);
|
|
103
|
+
}
|
|
104
|
+
await (0, Utils_1.delayThenReturn)(await mockingFunc(req, res), delayMs);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
await mockingFunc(req, res);
|
|
108
|
+
}
|
|
109
|
+
if (res.response === undefined) {
|
|
110
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
111
|
+
error: new Error("[api-def] Mocked API did not respond"),
|
|
112
|
+
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
113
|
+
context,
|
|
76
114
|
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
req = {
|
|
96
|
-
body: context.getParsedBody(),
|
|
97
|
-
params: (_d = context.requestConfig.params) !== null && _d !== void 0 ? _d : {},
|
|
98
|
-
query: context.requestConfig.queryObject,
|
|
99
|
-
headers: (_e = context.requestConfig.headers) !== null && _e !== void 0 ? _e : {},
|
|
100
|
-
url: context.requestUrl.toString(),
|
|
101
|
-
state: context.requestConfig.state,
|
|
102
|
-
};
|
|
103
|
-
res = {
|
|
104
|
-
statusCode: -1,
|
|
105
|
-
headers: {},
|
|
106
|
-
response: undefined,
|
|
107
|
-
status: function (statusCode) {
|
|
108
|
-
res.statusCode = statusCode;
|
|
109
|
-
return res;
|
|
110
|
-
},
|
|
111
|
-
send: function (response) {
|
|
112
|
-
res.response = response;
|
|
113
|
-
if (response && typeof response === "object") {
|
|
114
|
-
res.headers["Content-Type"] = "application/json";
|
|
115
|
-
}
|
|
116
|
-
return res;
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
if (!((_f = context.mocking) === null || _f === void 0 ? void 0 : _f.delay)) return [3 /*break*/, 3];
|
|
120
|
-
delay = context.mocking.delay;
|
|
121
|
-
delayMs = void 0;
|
|
122
|
-
if (typeof delay === "number") {
|
|
123
|
-
delayMs = delay;
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
min = delay[0], max = delay[1];
|
|
127
|
-
if (min > max) {
|
|
128
|
-
throw (0, RequestError_1.convertToRequestError)({
|
|
129
|
-
error: new Error("[api-def] Min delay cannot be greater than max delay"),
|
|
130
|
-
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
131
|
-
context: context,
|
|
132
|
-
});
|
|
115
|
+
}
|
|
116
|
+
const parsedHeaders = Object.keys(res.headers).reduce((parsedHeaders, key) => {
|
|
117
|
+
parsedHeaders.set(key, res.headers[key].toString());
|
|
118
|
+
return parsedHeaders;
|
|
119
|
+
}, new Headers());
|
|
120
|
+
const responseType = context.responseType ?? (0, ApiUtils_1.inferResponseType)(res.headers["content-type"]?.toString());
|
|
121
|
+
let data;
|
|
122
|
+
if (responseType === "stream") {
|
|
123
|
+
// For streaming responses, we create a mock async iterator
|
|
124
|
+
data = {
|
|
125
|
+
async *[Symbol.asyncIterator]() {
|
|
126
|
+
if (res.response) {
|
|
127
|
+
// If the response is an array, yield each item
|
|
128
|
+
if (Array.isArray(res.response)) {
|
|
129
|
+
for (const item of res.response) {
|
|
130
|
+
yield new TextEncoder().encode(`${JSON.stringify(item)}\n`);
|
|
133
131
|
}
|
|
134
|
-
delayMs = (0, Utils_1.randInt)(min, max);
|
|
135
|
-
}
|
|
136
|
-
_a = Utils_1.delayThenReturn;
|
|
137
|
-
return [4 /*yield*/, mockingFunc(req, res)];
|
|
138
|
-
case 1: return [4 /*yield*/, _a.apply(void 0, [_j.sent(), delayMs])];
|
|
139
|
-
case 2:
|
|
140
|
-
_j.sent();
|
|
141
|
-
return [3 /*break*/, 5];
|
|
142
|
-
case 3: return [4 /*yield*/, mockingFunc(req, res)];
|
|
143
|
-
case 4:
|
|
144
|
-
_j.sent();
|
|
145
|
-
_j.label = 5;
|
|
146
|
-
case 5:
|
|
147
|
-
if (res.response === undefined) {
|
|
148
|
-
throw (0, RequestError_1.convertToRequestError)({
|
|
149
|
-
error: new Error("[api-def] Mocked API did not respond"),
|
|
150
|
-
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
151
|
-
context: context,
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
parsedHeaders = Object.keys(res.headers).reduce(function (parsedHeaders, key) {
|
|
155
|
-
parsedHeaders.set(key, res.headers[key].toString());
|
|
156
|
-
return parsedHeaders;
|
|
157
|
-
}, new Headers());
|
|
158
|
-
responseType = (_g = context.responseType) !== null && _g !== void 0 ? _g : (0, ApiUtils_1.inferResponseType)((_h = res.headers["content-type"]) === null || _h === void 0 ? void 0 : _h.toString());
|
|
159
|
-
if (responseType === "stream") {
|
|
160
|
-
// For streaming responses, we create a mock async iterator
|
|
161
|
-
data = (_b = {},
|
|
162
|
-
_b[Symbol.asyncIterator] = function () {
|
|
163
|
-
return __asyncGenerator(this, arguments, function _a() {
|
|
164
|
-
var _i, _b, item;
|
|
165
|
-
return __generator(this, function (_c) {
|
|
166
|
-
switch (_c.label) {
|
|
167
|
-
case 0:
|
|
168
|
-
if (!res.response) return [3 /*break*/, 9];
|
|
169
|
-
if (!Array.isArray(res.response)) return [3 /*break*/, 6];
|
|
170
|
-
_i = 0, _b = res.response;
|
|
171
|
-
_c.label = 1;
|
|
172
|
-
case 1:
|
|
173
|
-
if (!(_i < _b.length)) return [3 /*break*/, 5];
|
|
174
|
-
item = _b[_i];
|
|
175
|
-
return [4 /*yield*/, __await(new TextEncoder().encode("".concat(JSON.stringify(item), "\n")))];
|
|
176
|
-
case 2: return [4 /*yield*/, _c.sent()];
|
|
177
|
-
case 3:
|
|
178
|
-
_c.sent();
|
|
179
|
-
_c.label = 4;
|
|
180
|
-
case 4:
|
|
181
|
-
_i++;
|
|
182
|
-
return [3 /*break*/, 1];
|
|
183
|
-
case 5: return [3 /*break*/, 9];
|
|
184
|
-
case 6: return [4 /*yield*/, __await(new TextEncoder().encode("".concat(JSON.stringify(res.response), "\n")))];
|
|
185
|
-
case 7:
|
|
186
|
-
// Otherwise yield the entire response
|
|
187
|
-
return [4 /*yield*/, _c.sent()];
|
|
188
|
-
case 8:
|
|
189
|
-
// Otherwise yield the entire response
|
|
190
|
-
_c.sent();
|
|
191
|
-
_c.label = 9;
|
|
192
|
-
case 9: return [2 /*return*/];
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
},
|
|
197
|
-
_b);
|
|
198
132
|
}
|
|
199
133
|
else {
|
|
200
|
-
|
|
134
|
+
// Otherwise yield the entire response
|
|
135
|
+
yield new TextEncoder().encode(`${JSON.stringify(res.response)}\n`);
|
|
201
136
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
data = res.response;
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
url: context.requestUrl.href,
|
|
146
|
+
method: context.method,
|
|
147
|
+
headers: parsedHeaders,
|
|
148
|
+
data: data,
|
|
149
|
+
status: res.statusCode,
|
|
150
|
+
state: context.requestConfig.state,
|
|
151
|
+
stats: context.stats,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
makeRequest(context) {
|
|
216
155
|
return {
|
|
217
156
|
canceler: Utils.noop,
|
|
218
157
|
promise: this.runRequest(context),
|
|
219
158
|
};
|
|
220
|
-
}
|
|
221
|
-
|
|
159
|
+
}
|
|
160
|
+
getErrorInfo(error, response) {
|
|
222
161
|
return undefined;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
}());
|
|
162
|
+
}
|
|
163
|
+
}
|
|
226
164
|
exports.default = MockRequestBackend;
|
|
@@ -1,88 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
4
|
};
|
|
38
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
6
|
exports.getClientCachedItem = exports.setClientCachedItem = exports.clearClientCache = exports.DEFAULT_CLIENT_CACHE_EXPIRY = exports.setClientCacheBackend = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
7
|
+
const LocalStorageClientCacheBackend_1 = __importDefault(require("./LocalStorageClientCacheBackend"));
|
|
8
|
+
let clientCacheBackend = new LocalStorageClientCacheBackend_1.default();
|
|
9
|
+
const setClientCacheBackend = (backend) => {
|
|
43
10
|
clientCacheBackend = backend;
|
|
44
11
|
};
|
|
45
12
|
exports.setClientCacheBackend = setClientCacheBackend;
|
|
46
13
|
exports.DEFAULT_CLIENT_CACHE_EXPIRY = 60 * 1000 * 15;
|
|
47
|
-
|
|
14
|
+
const clearClientCache = () => {
|
|
48
15
|
return clientCacheBackend.clear();
|
|
49
16
|
};
|
|
50
17
|
exports.clearClientCache = clearClientCache;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
};
|
|
60
|
-
return [4 /*yield*/, clientCacheBackend.setItem(key, entry)];
|
|
61
|
-
case 1:
|
|
62
|
-
_a.sent();
|
|
63
|
-
return [2 /*return*/, value];
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}); };
|
|
18
|
+
const setClientCachedItem = async (key, value, expiry) => {
|
|
19
|
+
const entry = {
|
|
20
|
+
data: value,
|
|
21
|
+
expiry: expiry === undefined || Number.isNaN(expiry) ? null : expiry,
|
|
22
|
+
};
|
|
23
|
+
await clientCacheBackend.setItem(key, entry);
|
|
24
|
+
return value;
|
|
25
|
+
};
|
|
67
26
|
exports.setClientCachedItem = setClientCachedItem;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
if (!(typeof entry.expiry === "number")) return [3 /*break*/, 3];
|
|
79
|
-
if (!(Date.now() >= entry.expiry)) return [3 /*break*/, 3];
|
|
80
|
-
return [4 /*yield*/, clientCacheBackend.removeItem(key)];
|
|
81
|
-
case 2:
|
|
82
|
-
_a.sent();
|
|
83
|
-
return [2 /*return*/, undefined];
|
|
84
|
-
case 3: return [2 /*return*/, entry.data];
|
|
27
|
+
const getClientCachedItem = async (key) => {
|
|
28
|
+
const entry = await clientCacheBackend.getItem(key);
|
|
29
|
+
if (!entry) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
if (typeof entry.expiry === "number") {
|
|
33
|
+
if (Date.now() >= entry.expiry) {
|
|
34
|
+
await clientCacheBackend.removeItem(key);
|
|
35
|
+
return undefined;
|
|
85
36
|
}
|
|
86
|
-
}
|
|
87
|
-
|
|
37
|
+
}
|
|
38
|
+
return entry.data;
|
|
39
|
+
};
|
|
88
40
|
exports.getClientCachedItem = getClientCachedItem;
|
|
@@ -1,90 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
|
|
40
|
-
|
|
3
|
+
class LocalForageClientCacheBackend {
|
|
4
|
+
constructor(localforage) {
|
|
41
5
|
this.store = localforage.createInstance({
|
|
42
6
|
name: "requestClientCache",
|
|
43
7
|
});
|
|
44
8
|
}
|
|
45
|
-
|
|
46
|
-
return
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
_a.sent();
|
|
59
|
-
return [2 /*return*/];
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
LocalForageClientCacheBackend.prototype.removeItem = function (key) {
|
|
65
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
66
|
-
return __generator(this, function (_a) {
|
|
67
|
-
switch (_a.label) {
|
|
68
|
-
case 0: return [4 /*yield*/, this.store.removeItem(key)];
|
|
69
|
-
case 1:
|
|
70
|
-
_a.sent();
|
|
71
|
-
return [2 /*return*/];
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
};
|
|
76
|
-
LocalForageClientCacheBackend.prototype.clear = function () {
|
|
77
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
78
|
-
return __generator(this, function (_a) {
|
|
79
|
-
switch (_a.label) {
|
|
80
|
-
case 0: return [4 /*yield*/, this.store.clear()];
|
|
81
|
-
case 1:
|
|
82
|
-
_a.sent();
|
|
83
|
-
return [2 /*return*/];
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
};
|
|
88
|
-
return LocalForageClientCacheBackend;
|
|
89
|
-
}());
|
|
9
|
+
async getItem(key) {
|
|
10
|
+
return this.store.getItem(key);
|
|
11
|
+
}
|
|
12
|
+
async setItem(key, value) {
|
|
13
|
+
await this.store.setItem(key, value);
|
|
14
|
+
}
|
|
15
|
+
async removeItem(key) {
|
|
16
|
+
await this.store.removeItem(key);
|
|
17
|
+
}
|
|
18
|
+
async clear() {
|
|
19
|
+
await this.store.clear();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
90
22
|
exports.default = LocalForageClientCacheBackend;
|