api-def 0.12.1 → 0.14.0-alpha.1

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.
Files changed (87) hide show
  1. package/README.md +3 -0
  2. package/bin/index.js +312 -313
  3. package/cjs/Api.d.ts +1 -1
  4. package/cjs/Api.js +99 -145
  5. package/cjs/ApiTypes.d.ts +3 -2
  6. package/cjs/ApiUtils.js +30 -32
  7. package/cjs/Endpoint.d.ts +1 -1
  8. package/cjs/Endpoint.js +112 -175
  9. package/cjs/EndpointBuilder.d.ts +17 -0
  10. package/cjs/EndpointBuilder.js +55 -36
  11. package/cjs/QueryHandling.js +12 -13
  12. package/cjs/RequestConfig.js +54 -36
  13. package/cjs/RequestContext.d.ts +2 -1
  14. package/cjs/RequestContext.js +143 -180
  15. package/cjs/RequestError.js +16 -19
  16. package/cjs/Requester.js +231 -312
  17. package/cjs/TextDecoding.js +31 -31
  18. package/cjs/Utils.d.ts +1 -0
  19. package/cjs/Utils.js +74 -25
  20. package/cjs/Validation.d.ts +8 -1
  21. package/cjs/backend/AxiosRequestBackend.d.ts +1 -1
  22. package/cjs/backend/AxiosRequestBackend.js +65 -172
  23. package/cjs/backend/FetchRequestBackend.d.ts +1 -1
  24. package/cjs/backend/FetchRequestBackend.js +108 -154
  25. package/cjs/backend/MockRequestBackend.d.ts +1 -1
  26. package/cjs/backend/MockRequestBackend.js +146 -208
  27. package/cjs/cache/ClientCaching.js +26 -74
  28. package/cjs/cache/LocalForageClientCacheBackend.js +15 -83
  29. package/cjs/cache/LocalStorageClientCacheBackend.js +14 -73
  30. package/cjs/index.d.ts +11 -11
  31. package/cjs/index.js +24 -21
  32. package/cjs/middleware/ClientCacheMiddleware.js +80 -103
  33. package/cjs/middleware/LoggingMiddleware.js +71 -42
  34. package/cjs/util/retry/index.js +42 -9
  35. package/cjs/util/retry/lib/retry.js +25 -27
  36. package/cjs/util/retry/lib/retryOperation.d.ts +1 -26
  37. package/cjs/util/retry/lib/retryOperation.js +21 -23
  38. package/esm/Api.d.ts +6 -6
  39. package/esm/Api.js +12 -25
  40. package/esm/ApiConstants.d.ts +1 -1
  41. package/esm/ApiTypes.d.ts +7 -6
  42. package/esm/ApiUtils.d.ts +2 -2
  43. package/esm/ApiUtils.js +4 -5
  44. package/esm/Endpoint.d.ts +6 -6
  45. package/esm/Endpoint.js +22 -28
  46. package/esm/EndpointBuilder.d.ts +21 -4
  47. package/esm/EndpointBuilder.js +38 -10
  48. package/esm/MockingTypes.d.ts +1 -1
  49. package/esm/QueryHandling.d.ts +1 -1
  50. package/esm/QueryHandling.js +2 -3
  51. package/esm/RequestConfig.d.ts +1 -1
  52. package/esm/RequestConfig.js +6 -7
  53. package/esm/RequestContext.d.ts +8 -7
  54. package/esm/RequestContext.js +33 -27
  55. package/esm/RequestError.d.ts +3 -3
  56. package/esm/RequestError.js +2 -3
  57. package/esm/Requester.d.ts +2 -2
  58. package/esm/Requester.js +45 -50
  59. package/esm/UtilTypes.d.ts +1 -1
  60. package/esm/Utils.d.ts +1 -0
  61. package/esm/Utils.js +54 -2
  62. package/esm/Validation.d.ts +8 -1
  63. package/esm/backend/AxiosRequestBackend.d.ts +4 -4
  64. package/esm/backend/AxiosRequestBackend.js +47 -84
  65. package/esm/backend/FetchRequestBackend.d.ts +5 -5
  66. package/esm/backend/FetchRequestBackend.js +50 -64
  67. package/esm/backend/MockRequestBackend.d.ts +4 -4
  68. package/esm/backend/MockRequestBackend.js +105 -136
  69. package/esm/backend/RequestBackend.d.ts +2 -2
  70. package/esm/cache/ClientCaching.d.ts +1 -1
  71. package/esm/cache/ClientCaching.js +8 -17
  72. package/esm/cache/LocalForageClientCacheBackend.d.ts +1 -1
  73. package/esm/cache/LocalForageClientCacheBackend.js +8 -25
  74. package/esm/cache/LocalStorageClientCacheBackend.d.ts +1 -1
  75. package/esm/cache/LocalStorageClientCacheBackend.js +9 -26
  76. package/esm/index.d.ts +16 -16
  77. package/esm/index.js +15 -15
  78. package/esm/middleware/ClientCacheMiddleware.d.ts +1 -1
  79. package/esm/middleware/ClientCacheMiddleware.js +8 -17
  80. package/esm/middleware/LoggingMiddleware.d.ts +1 -1
  81. package/esm/middleware/LoggingMiddleware.js +5 -6
  82. package/esm/util/retry/index.js +1 -1
  83. package/esm/util/retry/lib/retry.d.ts +1 -1
  84. package/esm/util/retry/lib/retry.js +13 -7
  85. package/esm/util/retry/lib/retryOperation.d.ts +1 -26
  86. package/esm/util/retry/lib/retryOperation.js +1 -1
  87. package/package.json +67 -28
@@ -1,77 +1,18 @@
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
- var LocalStorageClientCacheBackend = /** @class */ (function () {
40
- function LocalStorageClientCacheBackend() {
3
+ class LocalStorageClientCacheBackend {
4
+ async getItem(key) {
5
+ const item = localStorage.getItem(key);
6
+ return item ? JSON.parse(item) : null;
7
+ }
8
+ async setItem(key, value) {
9
+ localStorage.setItem(key, JSON.stringify(value));
10
+ }
11
+ async removeItem(key) {
12
+ localStorage.removeItem(key);
13
+ }
14
+ async clear() {
15
+ localStorage.clear();
41
16
  }
42
- LocalStorageClientCacheBackend.prototype.getItem = function (key) {
43
- return __awaiter(this, void 0, void 0, function () {
44
- var item;
45
- return __generator(this, function (_a) {
46
- item = localStorage.getItem(key);
47
- return [2 /*return*/, item ? JSON.parse(item) : null];
48
- });
49
- });
50
- };
51
- LocalStorageClientCacheBackend.prototype.setItem = function (key, value) {
52
- return __awaiter(this, void 0, void 0, function () {
53
- return __generator(this, function (_a) {
54
- localStorage.setItem(key, JSON.stringify(value));
55
- return [2 /*return*/];
56
- });
57
- });
58
- };
59
- LocalStorageClientCacheBackend.prototype.removeItem = function (key) {
60
- return __awaiter(this, void 0, void 0, function () {
61
- return __generator(this, function (_a) {
62
- localStorage.removeItem(key);
63
- return [2 /*return*/];
64
- });
65
- });
66
- };
67
- LocalStorageClientCacheBackend.prototype.clear = function () {
68
- return __awaiter(this, void 0, void 0, function () {
69
- return __generator(this, function (_a) {
70
- localStorage.clear();
71
- return [2 /*return*/];
72
- });
73
- });
74
- };
75
- return LocalStorageClientCacheBackend;
76
- }());
17
+ }
77
18
  exports.default = LocalStorageClientCacheBackend;
package/cjs/index.d.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  export * from "./Api";
2
- export * from "./ApiTypes";
3
- export * from "./UtilTypes";
4
2
  export * from "./ApiConstants";
5
- export { default as retry } from "./util/retry";
6
- export type { RetryOptions } from "./util/retry/interfaces";
7
- export { isRequestError, getErrorResponse, type RequestError } from "./RequestError";
8
- export { default as Endpoint, type EndpointConfig } from "./Endpoint";
9
- export { default as EndpointBuilder, type EndpointBuildOptions } from "./EndpointBuilder";
10
- export { clearClientCache, setClientCacheBackend } from "./cache/ClientCaching";
11
- export { default as LocalStorageClientCacheBackend } from "./cache/LocalStorageClientCacheBackend";
12
- export { default as LocalForageClientCacheBackend } from "./cache/LocalForageClientCacheBackend";
13
- export { default as RequestBackend } from "./backend/RequestBackend";
3
+ export * from "./ApiTypes";
14
4
  export { default as AxiosRequestBackend } from "./backend/AxiosRequestBackend";
15
5
  export { default as FetchRequestBackend } from "./backend/FetchRequestBackend";
6
+ export { default as RequestBackend } from "./backend/RequestBackend";
7
+ export { clearClientCache, setClientCacheBackend } from "./cache/ClientCaching";
8
+ export { default as LocalForageClientCacheBackend } from "./cache/LocalForageClientCacheBackend";
9
+ export { default as LocalStorageClientCacheBackend } from "./cache/LocalStorageClientCacheBackend";
10
+ export { default as Endpoint, type EndpointConfig } from "./Endpoint";
11
+ export { default as EndpointBuilder, type EndpointBuildOptions } from "./EndpointBuilder";
16
12
  export { default as ClientCacheMiddleware } from "./middleware/ClientCacheMiddleware";
17
13
  export { default as LoggingMiddleware } from "./middleware/LoggingMiddleware";
14
+ export { getErrorResponse, isRequestError, type RequestError } from "./RequestError";
15
+ export * from "./UtilTypes";
16
+ export { default as retry } from "./util/retry";
17
+ export type { RetryOptions } from "./util/retry/interfaces";
package/cjs/index.js CHANGED
@@ -14,33 +14,36 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
14
14
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
16
  };
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
17
20
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.LoggingMiddleware = exports.ClientCacheMiddleware = exports.FetchRequestBackend = exports.AxiosRequestBackend = exports.LocalForageClientCacheBackend = exports.LocalStorageClientCacheBackend = exports.setClientCacheBackend = exports.clearClientCache = exports.EndpointBuilder = exports.Endpoint = exports.getErrorResponse = exports.isRequestError = exports.retry = void 0;
21
+ exports.retry = exports.isRequestError = exports.getErrorResponse = exports.LoggingMiddleware = exports.ClientCacheMiddleware = exports.EndpointBuilder = exports.Endpoint = exports.LocalStorageClientCacheBackend = exports.LocalForageClientCacheBackend = exports.setClientCacheBackend = exports.clearClientCache = exports.FetchRequestBackend = exports.AxiosRequestBackend = void 0;
19
22
  __exportStar(require("./Api"), exports);
20
- __exportStar(require("./ApiTypes"), exports);
21
- __exportStar(require("./UtilTypes"), exports);
22
23
  __exportStar(require("./ApiConstants"), exports);
23
- var retry_1 = require("./util/retry");
24
- Object.defineProperty(exports, "retry", { enumerable: true, get: function () { return retry_1.default; } });
25
- var RequestError_1 = require("./RequestError");
26
- Object.defineProperty(exports, "isRequestError", { enumerable: true, get: function () { return RequestError_1.isRequestError; } });
27
- Object.defineProperty(exports, "getErrorResponse", { enumerable: true, get: function () { return RequestError_1.getErrorResponse; } });
28
- var Endpoint_1 = require("./Endpoint");
29
- Object.defineProperty(exports, "Endpoint", { enumerable: true, get: function () { return Endpoint_1.default; } });
30
- var EndpointBuilder_1 = require("./EndpointBuilder");
31
- Object.defineProperty(exports, "EndpointBuilder", { enumerable: true, get: function () { return EndpointBuilder_1.default; } });
24
+ __exportStar(require("./ApiTypes"), exports);
25
+ var AxiosRequestBackend_1 = require("./backend/AxiosRequestBackend");
26
+ Object.defineProperty(exports, "AxiosRequestBackend", { enumerable: true, get: function () { return __importDefault(AxiosRequestBackend_1).default; } });
27
+ var FetchRequestBackend_1 = require("./backend/FetchRequestBackend");
28
+ Object.defineProperty(exports, "FetchRequestBackend", { enumerable: true, get: function () { return __importDefault(FetchRequestBackend_1).default; } });
32
29
  var ClientCaching_1 = require("./cache/ClientCaching");
33
30
  Object.defineProperty(exports, "clearClientCache", { enumerable: true, get: function () { return ClientCaching_1.clearClientCache; } });
34
31
  Object.defineProperty(exports, "setClientCacheBackend", { enumerable: true, get: function () { return ClientCaching_1.setClientCacheBackend; } });
35
- var LocalStorageClientCacheBackend_1 = require("./cache/LocalStorageClientCacheBackend");
36
- Object.defineProperty(exports, "LocalStorageClientCacheBackend", { enumerable: true, get: function () { return LocalStorageClientCacheBackend_1.default; } });
37
32
  var LocalForageClientCacheBackend_1 = require("./cache/LocalForageClientCacheBackend");
38
- Object.defineProperty(exports, "LocalForageClientCacheBackend", { enumerable: true, get: function () { return LocalForageClientCacheBackend_1.default; } });
39
- var AxiosRequestBackend_1 = require("./backend/AxiosRequestBackend");
40
- Object.defineProperty(exports, "AxiosRequestBackend", { enumerable: true, get: function () { return AxiosRequestBackend_1.default; } });
41
- var FetchRequestBackend_1 = require("./backend/FetchRequestBackend");
42
- Object.defineProperty(exports, "FetchRequestBackend", { enumerable: true, get: function () { return FetchRequestBackend_1.default; } });
33
+ Object.defineProperty(exports, "LocalForageClientCacheBackend", { enumerable: true, get: function () { return __importDefault(LocalForageClientCacheBackend_1).default; } });
34
+ var LocalStorageClientCacheBackend_1 = require("./cache/LocalStorageClientCacheBackend");
35
+ Object.defineProperty(exports, "LocalStorageClientCacheBackend", { enumerable: true, get: function () { return __importDefault(LocalStorageClientCacheBackend_1).default; } });
36
+ var Endpoint_1 = require("./Endpoint");
37
+ Object.defineProperty(exports, "Endpoint", { enumerable: true, get: function () { return __importDefault(Endpoint_1).default; } });
38
+ var EndpointBuilder_1 = require("./EndpointBuilder");
39
+ Object.defineProperty(exports, "EndpointBuilder", { enumerable: true, get: function () { return __importDefault(EndpointBuilder_1).default; } });
43
40
  var ClientCacheMiddleware_1 = require("./middleware/ClientCacheMiddleware");
44
- Object.defineProperty(exports, "ClientCacheMiddleware", { enumerable: true, get: function () { return ClientCacheMiddleware_1.default; } });
41
+ Object.defineProperty(exports, "ClientCacheMiddleware", { enumerable: true, get: function () { return __importDefault(ClientCacheMiddleware_1).default; } });
45
42
  var LoggingMiddleware_1 = require("./middleware/LoggingMiddleware");
46
- Object.defineProperty(exports, "LoggingMiddleware", { enumerable: true, get: function () { return LoggingMiddleware_1.default; } });
43
+ Object.defineProperty(exports, "LoggingMiddleware", { enumerable: true, get: function () { return __importDefault(LoggingMiddleware_1).default; } });
44
+ var RequestError_1 = require("./RequestError");
45
+ Object.defineProperty(exports, "getErrorResponse", { enumerable: true, get: function () { return RequestError_1.getErrorResponse; } });
46
+ Object.defineProperty(exports, "isRequestError", { enumerable: true, get: function () { return RequestError_1.isRequestError; } });
47
+ __exportStar(require("./UtilTypes"), exports);
48
+ var retry_1 = require("./util/retry");
49
+ Object.defineProperty(exports, "retry", { enumerable: true, get: function () { return __importDefault(retry_1).default; } });
@@ -1,109 +1,86 @@
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 };
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
- };
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
+ })();
38
35
  Object.defineProperty(exports, "__esModule", { value: true });
39
- var ApiConstants_1 = require("../ApiConstants");
40
- var ClientCaching = require("../cache/ClientCaching");
41
- var ClientCacheMiddleware = function (options) {
42
- var _a;
43
- if (options === void 0) { options = {}; }
44
- return _a = {},
45
- _a[ApiConstants_1.RequestEvent.SUCCESS] = function (context) { return __awaiter(void 0, void 0, void 0, function () {
46
- var clientCache, shouldCache, expiry;
47
- return __generator(this, function (_a) {
48
- switch (_a.label) {
49
- case 0:
50
- if (context.method !== ApiConstants_1.RequestMethod.GET)
51
- return [2 /*return*/];
52
- clientCache = (context.requestConfig || {}).clientCache;
53
- shouldCache = !options.predicate || options.predicate();
54
- if (!(clientCache && shouldCache)) return [3 /*break*/, 2];
55
- expiry = typeof clientCache === "number"
56
- ? clientCache
57
- : options.defaultExpiry || ClientCaching.DEFAULT_CLIENT_CACHE_EXPIRY;
58
- return [4 /*yield*/, ClientCaching.setClientCachedItem(context.key, context.response, expiry)];
59
- case 1:
60
- _a.sent();
61
- _a.label = 2;
62
- case 2: return [2 /*return*/];
63
- }
64
- });
65
- }); },
66
- _a[ApiConstants_1.RequestEvent.BEFORE_SEND] = function (context) { return __awaiter(void 0, void 0, void 0, function () {
67
- var clientCache, shouldCache, cachedValue;
68
- return __generator(this, function (_a) {
69
- switch (_a.label) {
70
- case 0:
71
- if (context.method !== ApiConstants_1.RequestMethod.GET)
72
- return [2 /*return*/];
73
- clientCache = (context.requestConfig || {}).clientCache;
74
- shouldCache = !options.predicate || options.predicate();
75
- if (!(clientCache && shouldCache)) return [3 /*break*/, 2];
76
- if (!clientCache) return [3 /*break*/, 2];
77
- return [4 /*yield*/, ClientCaching.getClientCachedItem(context.key)];
78
- case 1:
79
- cachedValue = _a.sent();
80
- if (cachedValue) {
81
- context.stats.cached = {
82
- is: true,
83
- by: ApiConstants_1.CacheSource.LOCAL,
84
- };
85
- context.cacheInfo.source = ApiConstants_1.CacheSource.LOCAL;
86
- context.cacheInfo.cached = true;
87
- return [2 /*return*/, {
88
- type: ApiConstants_1.EventResultType.RESPOND,
89
- response: cachedValue,
90
- }];
91
- }
92
- _a.label = 2;
93
- case 2:
94
- if (clientCache === false) {
95
- context.updateQuery({
96
- _bust: Math.floor(Math.random() * 9000) + 1000,
97
- });
98
- context.updateHeaders({
99
- Pragma: "no-cache, no-store, must-revalidate",
100
- "Cache-Control": "no-cache, no-store, must-revalidate",
101
- });
102
- }
103
- return [2 /*return*/];
36
+ const ApiConstants_1 = require("../ApiConstants");
37
+ const ClientCaching = __importStar(require("../cache/ClientCaching"));
38
+ const ClientCacheMiddleware = (options = {}) => {
39
+ return {
40
+ [ApiConstants_1.RequestEvent.SUCCESS]: async (context) => {
41
+ if (context.method !== ApiConstants_1.RequestMethod.GET)
42
+ return;
43
+ const { clientCache } = context.requestConfig || {};
44
+ const shouldCache = !options.predicate || options.predicate();
45
+ if (clientCache && shouldCache) {
46
+ const expiry = typeof clientCache === "number"
47
+ ? clientCache
48
+ : options.defaultExpiry || ClientCaching.DEFAULT_CLIENT_CACHE_EXPIRY;
49
+ await ClientCaching.setClientCachedItem(context.key, context.response, expiry);
50
+ }
51
+ },
52
+ [ApiConstants_1.RequestEvent.BEFORE_SEND]: async (context) => {
53
+ if (context.method !== ApiConstants_1.RequestMethod.GET)
54
+ return;
55
+ const { clientCache } = context.requestConfig || {};
56
+ const shouldCache = !options.predicate || options.predicate();
57
+ if (clientCache && shouldCache) {
58
+ if (clientCache) {
59
+ const cachedValue = await ClientCaching.getClientCachedItem(context.key);
60
+ if (cachedValue) {
61
+ context.stats.cached = {
62
+ is: true,
63
+ by: ApiConstants_1.CacheSource.LOCAL,
64
+ };
65
+ context.cacheInfo.source = ApiConstants_1.CacheSource.LOCAL;
66
+ context.cacheInfo.cached = true;
67
+ return {
68
+ type: ApiConstants_1.EventResultType.RESPOND,
69
+ response: cachedValue,
70
+ };
71
+ }
104
72
  }
105
- });
106
- }); },
107
- _a;
73
+ }
74
+ if (clientCache === false) {
75
+ context.updateQuery({
76
+ _bust: Math.floor(Math.random() * 9000) + 1000,
77
+ });
78
+ context.updateHeaders({
79
+ Pragma: "no-cache, no-store, must-revalidate",
80
+ "Cache-Control": "no-cache, no-store, must-revalidate",
81
+ });
82
+ }
83
+ },
84
+ };
108
85
  };
109
86
  exports.default = ClientCacheMiddleware;
@@ -1,9 +1,41 @@
1
1
  "use strict";
2
- var _a;
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]; } };
7
+ }
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
+ })();
3
35
  Object.defineProperty(exports, "__esModule", { value: true });
4
- var ApiConstants_1 = require("../ApiConstants");
5
- var ApiUtils = require("../ApiUtils");
6
- var Utils = require("../Utils");
36
+ const ApiConstants_1 = require("../ApiConstants");
37
+ const ApiUtils = __importStar(require("../ApiUtils"));
38
+ const Utils = __importStar(require("../Utils"));
7
39
  var LogType;
8
40
  (function (LogType) {
9
41
  LogType[LogType["INFO"] = 0] = "INFO";
@@ -11,77 +43,74 @@ var LogType;
11
43
  LogType[LogType["ERROR"] = 2] = "ERROR";
12
44
  LogType[LogType["WARNING"] = 3] = "WARNING";
13
45
  })(LogType || (LogType = {}));
14
- var COLOR_MAP = (_a = {},
15
- _a[LogType.ERROR] = "#c8646c",
16
- _a[LogType.INFO] = "#85a6c7",
17
- _a[LogType.SUCCESS] = "#a9c490",
18
- _a[LogType.WARNING] = "#d19a66",
19
- _a);
20
- var diagnoseError = function (error) {
46
+ const COLOR_MAP = {
47
+ [LogType.ERROR]: "#c8646c",
48
+ [LogType.INFO]: "#85a6c7",
49
+ [LogType.SUCCESS]: "#a9c490",
50
+ [LogType.WARNING]: "#d19a66",
51
+ };
52
+ const diagnoseError = (error) => {
21
53
  if (!error.response) {
22
54
  if (ApiUtils.isNetworkError(error)) {
23
- return { message: "network issue", error: error };
55
+ return { message: "network issue", error };
24
56
  }
25
57
  if (ApiUtils.isCancelledError(error)) {
26
- return { message: "cancelled", error: error };
58
+ return { message: "cancelled", error };
27
59
  }
28
- return { message: "client-side error", error: error };
60
+ return { message: "client-side error", error };
29
61
  }
30
- var _a = error.response, status = _a.status, data = _a.data;
31
- var code = data === null || data === void 0 ? void 0 : data.code;
62
+ const { status, data } = error.response;
63
+ const code = data?.code;
32
64
  return {
33
- message: "responded with ".concat(status).concat(code ? " (".concat(code, ")") : ""),
65
+ message: `responded with ${status}${code ? ` (${code})` : ""}`,
34
66
  response: error.response,
35
67
  };
36
68
  };
37
- var formatTime = function (time) {
38
- return "".concat(Utils.padNumber(time.getHours(), 2), ":").concat(Utils.padNumber(time.getMinutes(), 2), ":").concat(Utils.padNumber(time.getSeconds(), 2), ".").concat(Utils.padNumber(time.getMilliseconds(), 3));
69
+ const formatTime = (time) => {
70
+ return `${Utils.padNumber(time.getHours(), 2)}:${Utils.padNumber(time.getMinutes(), 2)}:${Utils.padNumber(time.getSeconds(), 2)}.${Utils.padNumber(time.getMilliseconds(), 3)}`;
39
71
  };
40
- var log = function (context, type, message, config, objects) {
72
+ const log = (context, type, message, config, objects) => {
41
73
  if (typeof config.predicate === "function" && !config.predicate()) {
42
74
  return;
43
75
  }
44
- var color = COLOR_MAP[type];
45
- var timestamp = formatTime(new Date());
46
- var args = [
47
- "%cnetwork %c[".concat(context.api.name, "] ").concat(context.method.toUpperCase(), " ").concat(context.path, " %c").concat(message, " %c@ ").concat(timestamp),
76
+ const color = COLOR_MAP[type];
77
+ const timestamp = formatTime(new Date());
78
+ const args = [
79
+ `%cnetwork %c[${context.api.name}] ${context.method.toUpperCase()} ${context.path} %c${message} %c@ ${timestamp}`,
48
80
  "color:gray",
49
81
  "color:auto",
50
- "color:".concat(color),
82
+ `color:${color}`,
51
83
  "color:gray",
52
84
  ];
53
- console.groupCollapsed.apply(console, args);
85
+ console.groupCollapsed(...args);
54
86
  console.log(Utils.assign({ context: context }, objects || {}));
55
87
  console.groupEnd();
56
88
  };
57
- var LoggingMiddleware = function (config) {
58
- var _a;
59
- if (config === void 0) { config = {}; }
60
- return _a = {},
61
- _a[ApiConstants_1.RequestEvent.BEFORE_SEND] = function (context) {
89
+ const LoggingMiddleware = (config = {}) => {
90
+ return {
91
+ [ApiConstants_1.RequestEvent.BEFORE_SEND]: (context) => {
62
92
  log(context, LogType.INFO, context.stats.attempt > 1 ? "retrying" : "sending", config);
63
93
  },
64
- _a[ApiConstants_1.RequestEvent.SUCCESS] = function (context) {
65
- var _a;
66
- var cacheSource = context.cacheInfo.source;
67
- log(context, LogType.SUCCESS, "responded with ".concat((_a = context.response) === null || _a === void 0 ? void 0 : _a.status).concat(cacheSource ? " (cached by ".concat(cacheSource, ")") : ""), config);
94
+ [ApiConstants_1.RequestEvent.SUCCESS]: (context) => {
95
+ const cacheSource = context.cacheInfo.source;
96
+ log(context, LogType.SUCCESS, `responded with ${context.response?.status}${cacheSource ? ` (cached by ${cacheSource})` : ""}`, config);
68
97
  },
69
- _a[ApiConstants_1.RequestEvent.ERROR] = function (context) {
98
+ [ApiConstants_1.RequestEvent.ERROR]: (context) => {
70
99
  if (context.error) {
71
- var _a = diagnoseError(context.error), error = _a.error, message = _a.message;
72
- log(context, LogType.WARNING, "error on attempt ".concat(context.stats.attempt, " - ").concat(message), config, {
100
+ const { error, message } = diagnoseError(context.error);
101
+ log(context, LogType.WARNING, `error on attempt ${context.stats.attempt} - ${message}`, config, {
73
102
  error: error,
74
103
  });
75
104
  }
76
105
  },
77
- _a[ApiConstants_1.RequestEvent.UNRECOVERABLE_ERROR] = function (context) {
106
+ [ApiConstants_1.RequestEvent.UNRECOVERABLE_ERROR]: (context) => {
78
107
  if (context.error) {
79
- var _a = diagnoseError(context.error), error = _a.error, message = _a.message;
80
- log(context, LogType.ERROR, "failed - ".concat(message), config, {
108
+ const { error, message } = diagnoseError(context.error);
109
+ log(context, LogType.ERROR, `failed - ${message}`, config, {
81
110
  error: error,
82
111
  });
83
112
  }
84
113
  },
85
- _a;
114
+ };
86
115
  };
87
116
  exports.default = LoggingMiddleware;
@@ -1,25 +1,58 @@
1
1
  "use strict";
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]; } };
7
+ }
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
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var retrier = require("./lib/retry");
36
+ const retrier = __importStar(require("./lib/retry"));
4
37
  // Retry is just transplanted from here:
5
38
  // https://www.npmjs.com/package/retry (lib), and
6
39
  // https://www.npmjs.com/package/async-retry (RetryFunction)
7
40
  // ---
8
- var retry = function (fn, opts) {
9
- return new Promise(function (resolve, reject) {
10
- var options = opts || {};
41
+ const retry = (fn, opts) => {
42
+ return new Promise((resolve, reject) => {
43
+ const options = opts || {};
11
44
  if (!("randomize" in options)) {
12
45
  options.randomize = true;
13
46
  }
14
- var op = retrier.operation(options);
47
+ const op = retrier.operation(options);
15
48
  // We allow the user to abort retrying
16
49
  // this makes sense in the cases where
17
50
  // knowledge is obtained that retrying
18
51
  // would be futile (e.g.: auth errors)
19
- var bail = function (err) {
52
+ const bail = (err) => {
20
53
  reject(err || new Error("Aborted"));
21
54
  };
22
- var onError = function (err, num) {
55
+ const onError = (err, num) => {
23
56
  if (err.bail) {
24
57
  bail(err);
25
58
  return;
@@ -31,8 +64,8 @@ var retry = function (fn, opts) {
31
64
  options.onRetry(err, num);
32
65
  }
33
66
  };
34
- var runAttempt = function (num) {
35
- var val;
67
+ const runAttempt = (num) => {
68
+ let val;
36
69
  try {
37
70
  val = fn(bail, num);
38
71
  }