api-def 0.9.0 → 0.9.2

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 (121) hide show
  1. package/cjs/Api.d.ts +35 -35
  2. package/cjs/Api.js +140 -144
  3. package/cjs/ApiConstants.d.ts +58 -58
  4. package/cjs/ApiConstants.js +55 -55
  5. package/cjs/ApiTypes.d.ts +103 -103
  6. package/cjs/ApiTypes.js +4 -4
  7. package/cjs/ApiUtils.d.ts +6 -6
  8. package/cjs/ApiUtils.js +51 -53
  9. package/cjs/Endpoint.d.ts +52 -52
  10. package/cjs/Endpoint.js +104 -108
  11. package/cjs/EndpointBuilder.d.ts +14 -14
  12. package/cjs/EndpointBuilder.js +46 -46
  13. package/cjs/MockingTypes.d.ts +30 -30
  14. package/cjs/MockingTypes.js +2 -2
  15. package/cjs/QueryHandling.d.ts +3 -3
  16. package/cjs/QueryHandling.js +24 -24
  17. package/cjs/RequestConfig.d.ts +2 -2
  18. package/cjs/RequestConfig.js +74 -74
  19. package/cjs/RequestContext.d.ts +48 -48
  20. package/cjs/RequestContext.js +235 -238
  21. package/cjs/RequestError.d.ts +30 -30
  22. package/cjs/RequestError.js +47 -47
  23. package/cjs/Requester.d.ts +3 -3
  24. package/cjs/Requester.js +335 -336
  25. package/cjs/TextDecoding.d.ts +1 -1
  26. package/cjs/TextDecoding.js +152 -139
  27. package/cjs/UtilTypes.d.ts +5 -5
  28. package/cjs/UtilTypes.js +2 -2
  29. package/cjs/Utils.d.ts +15 -15
  30. package/cjs/Utils.js +79 -77
  31. package/cjs/Validation.d.ts +8 -8
  32. package/cjs/Validation.js +2 -2
  33. package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
  34. package/cjs/backend/AxiosRequestBackend.js +99 -99
  35. package/cjs/backend/FetchRequestBackend.d.ts +15 -15
  36. package/cjs/backend/FetchRequestBackend.js +202 -197
  37. package/cjs/backend/MockRequestBackend.d.ts +11 -11
  38. package/cjs/backend/MockRequestBackend.js +164 -164
  39. package/cjs/backend/RequestBackend.d.ts +19 -19
  40. package/cjs/backend/RequestBackend.js +2 -2
  41. package/cjs/cache/CacheBackend.d.ts +6 -6
  42. package/cjs/cache/CacheBackend.js +2 -2
  43. package/cjs/cache/Caching.d.ts +10 -10
  44. package/cjs/cache/Caching.js +88 -88
  45. package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
  46. package/cjs/cache/LocalForageCacheBackend.js +24 -24
  47. package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
  48. package/cjs/cache/LocalStorageCacheBackend.js +77 -77
  49. package/cjs/index.d.ts +13 -13
  50. package/cjs/index.js +41 -41
  51. package/cjs/middleware/CacheMiddleware.d.ts +7 -7
  52. package/cjs/middleware/CacheMiddleware.js +107 -107
  53. package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
  54. package/cjs/middleware/LoggingMiddleware.js +82 -88
  55. package/cjs/util/retry/index.d.ts +3 -3
  56. package/cjs/util/retry/index.js +52 -52
  57. package/cjs/util/retry/lib/retry.d.ts +3 -3
  58. package/cjs/util/retry/lib/retry.js +50 -54
  59. package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
  60. package/cjs/util/retry/lib/retryOperation.js +130 -134
  61. package/esm/Api.d.ts +35 -35
  62. package/esm/Api.js +92 -98
  63. package/esm/ApiConstants.d.ts +58 -58
  64. package/esm/ApiConstants.js +52 -52
  65. package/esm/ApiTypes.d.ts +103 -103
  66. package/esm/ApiTypes.js +1 -1
  67. package/esm/ApiUtils.d.ts +6 -6
  68. package/esm/ApiUtils.js +43 -45
  69. package/esm/Endpoint.d.ts +52 -52
  70. package/esm/Endpoint.js +66 -70
  71. package/esm/EndpointBuilder.d.ts +14 -14
  72. package/esm/EndpointBuilder.js +31 -31
  73. package/esm/MockingTypes.d.ts +30 -30
  74. package/esm/MockingTypes.js +1 -1
  75. package/esm/QueryHandling.d.ts +3 -3
  76. package/esm/QueryHandling.js +19 -19
  77. package/esm/RequestConfig.d.ts +2 -2
  78. package/esm/RequestConfig.js +56 -56
  79. package/esm/RequestContext.d.ts +48 -48
  80. package/esm/RequestContext.js +167 -170
  81. package/esm/RequestError.d.ts +30 -30
  82. package/esm/RequestError.js +42 -42
  83. package/esm/Requester.d.ts +3 -3
  84. package/esm/Requester.js +252 -253
  85. package/esm/TextDecoding.d.ts +1 -1
  86. package/esm/TextDecoding.js +148 -135
  87. package/esm/UtilTypes.d.ts +5 -5
  88. package/esm/UtilTypes.js +1 -1
  89. package/esm/Utils.d.ts +15 -15
  90. package/esm/Utils.js +66 -68
  91. package/esm/Validation.d.ts +8 -8
  92. package/esm/Validation.js +1 -1
  93. package/esm/backend/AxiosRequestBackend.d.ts +13 -13
  94. package/esm/backend/AxiosRequestBackend.js +62 -62
  95. package/esm/backend/FetchRequestBackend.d.ts +15 -15
  96. package/esm/backend/FetchRequestBackend.js +136 -126
  97. package/esm/backend/MockRequestBackend.d.ts +11 -11
  98. package/esm/backend/MockRequestBackend.js +116 -116
  99. package/esm/backend/RequestBackend.d.ts +19 -19
  100. package/esm/backend/RequestBackend.js +1 -1
  101. package/esm/cache/CacheBackend.d.ts +6 -6
  102. package/esm/cache/CacheBackend.js +1 -1
  103. package/esm/cache/Caching.d.ts +10 -10
  104. package/esm/cache/Caching.js +39 -39
  105. package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
  106. package/esm/cache/LocalForageCacheBackend.js +20 -20
  107. package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
  108. package/esm/cache/LocalStorageCacheBackend.js +32 -32
  109. package/esm/index.d.ts +13 -13
  110. package/esm/index.js +14 -15
  111. package/esm/middleware/CacheMiddleware.d.ts +7 -7
  112. package/esm/middleware/CacheMiddleware.js +58 -58
  113. package/esm/middleware/LoggingMiddleware.d.ts +6 -6
  114. package/esm/middleware/LoggingMiddleware.js +77 -83
  115. package/esm/util/retry/index.d.ts +3 -3
  116. package/esm/util/retry/index.js +50 -50
  117. package/esm/util/retry/lib/retry.d.ts +3 -3
  118. package/esm/util/retry/lib/retry.js +35 -39
  119. package/esm/util/retry/lib/retryOperation.d.ts +27 -27
  120. package/esm/util/retry/lib/retryOperation.js +126 -132
  121. package/package.json +12 -32
@@ -1,134 +1,130 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- function RetryOperation(timeouts, options) {
4
- // Compatibility for the old (timeouts, retryForever) signature
5
- if (typeof options === "boolean") {
6
- options = { forever: options };
7
- }
8
- this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
9
- this._timeouts = timeouts;
10
- this._options = options || {};
11
- this._maxRetryTime = options && options.maxRetryTime || Infinity;
12
- this._fn = null;
13
- this._errors = [];
14
- this._attempts = 1;
15
- this._operationTimeout = null;
16
- this._operationTimeoutCb = null;
17
- this._timeout = null;
18
- this._operationStart = null;
19
- this._timer = null;
20
- if (this._options.forever) {
21
- this._cachedTimeouts = this._timeouts.slice(0);
22
- }
23
- }
24
- RetryOperation.prototype.reset = function () {
25
- this._attempts = 1;
26
- this._timeouts = this._originalTimeouts.slice(0);
27
- };
28
- RetryOperation.prototype.stop = function () {
29
- if (this._timeout) {
30
- clearTimeout(this._timeout);
31
- }
32
- if (this._timer) {
33
- clearTimeout(this._timer);
34
- }
35
- this._timeouts = [];
36
- this._cachedTimeouts = null;
37
- };
38
- RetryOperation.prototype.retry = function (err) {
39
- if (this._timeout) {
40
- clearTimeout(this._timeout);
41
- }
42
- if (!err) {
43
- return false;
44
- }
45
- var currentTime = new Date().getTime();
46
- if (err && currentTime - this._operationStart >= this._maxRetryTime) {
47
- this._errors.push(err);
48
- this._errors.unshift(new Error("RetryOperation timeout occurred"));
49
- return false;
50
- }
51
- this._errors.push(err);
52
- var timeout = this._timeouts.shift();
53
- if (timeout === undefined) {
54
- if (this._cachedTimeouts) {
55
- // retry forever, only keep last error
56
- this._errors.splice(0, this._errors.length - 1);
57
- timeout = this._cachedTimeouts.slice(-1);
58
- }
59
- else {
60
- return false;
61
- }
62
- }
63
- var self = this;
64
- this._timer = setTimeout(function () {
65
- self._attempts++;
66
- if (self._operationTimeoutCb) {
67
- self._timeout = setTimeout(function () {
68
- self._operationTimeoutCb(self._attempts);
69
- }, self._operationTimeout);
70
- if (self._options.unref) {
71
- self._timeout.unref();
72
- }
73
- }
74
- self._fn(self._attempts);
75
- }, timeout);
76
- if (this._options.unref) {
77
- this._timer.unref();
78
- }
79
- return true;
80
- };
81
- RetryOperation.prototype.attempt = function (fn, timeoutOps) {
82
- this._fn = fn;
83
- if (timeoutOps) {
84
- if (timeoutOps.timeout) {
85
- this._operationTimeout = timeoutOps.timeout;
86
- }
87
- if (timeoutOps.cb) {
88
- this._operationTimeoutCb = timeoutOps.cb;
89
- }
90
- }
91
- var self = this;
92
- if (this._operationTimeoutCb) {
93
- this._timeout = setTimeout(function () {
94
- self._operationTimeoutCb();
95
- }, self._operationTimeout);
96
- }
97
- this._operationStart = new Date().getTime();
98
- this._fn(this._attempts);
99
- };
100
- RetryOperation.prototype.try = function (fn) {
101
- // console.log("Using RetryOperation.try() is deprecated");
102
- this.attempt(fn);
103
- };
104
- RetryOperation.prototype.start = function (fn) {
105
- // console.log("Using RetryOperation.start() is deprecated");
106
- this.attempt(fn);
107
- };
108
- RetryOperation.prototype.start = RetryOperation.prototype.try;
109
- RetryOperation.prototype.errors = function () {
110
- return this._errors;
111
- };
112
- RetryOperation.prototype.attempts = function () {
113
- return this._attempts;
114
- };
115
- RetryOperation.prototype.mainError = function () {
116
- if (this._errors.length === 0) {
117
- return null;
118
- }
119
- var counts = {};
120
- var mainError = null;
121
- var mainErrorCount = 0;
122
- for (var i = 0; i < this._errors.length; i++) {
123
- var error = this._errors[i];
124
- var message = error.message;
125
- var count = (counts[message] || 0) + 1;
126
- counts[message] = count;
127
- if (count >= mainErrorCount) {
128
- mainError = error;
129
- mainErrorCount = count;
130
- }
131
- }
132
- return mainError;
133
- };
134
- exports.default = RetryOperation;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function RetryOperation(timeouts, options) {
4
+ this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
5
+ this._timeouts = timeouts;
6
+ this._options = options || {};
7
+ this._maxRetryTime = (options === null || options === void 0 ? void 0 : options.maxRetryTime) || Infinity;
8
+ this._fn = null;
9
+ this._errors = [];
10
+ this._attempts = 1;
11
+ this._operationTimeout = null;
12
+ this._operationTimeoutCb = null;
13
+ this._timeout = null;
14
+ this._operationStart = null;
15
+ this._timer = null;
16
+ if (this._options.forever) {
17
+ this._cachedTimeouts = this._timeouts.slice(0);
18
+ }
19
+ }
20
+ RetryOperation.prototype.reset = function () {
21
+ this._attempts = 1;
22
+ this._timeouts = this._originalTimeouts.slice(0);
23
+ };
24
+ RetryOperation.prototype.stop = function () {
25
+ if (this._timeout) {
26
+ clearTimeout(this._timeout);
27
+ }
28
+ if (this._timer) {
29
+ clearTimeout(this._timer);
30
+ }
31
+ this._timeouts = [];
32
+ this._cachedTimeouts = null;
33
+ };
34
+ RetryOperation.prototype.retry = function (err) {
35
+ var _this = this;
36
+ if (this._timeout) {
37
+ clearTimeout(this._timeout);
38
+ }
39
+ if (!err) {
40
+ return false;
41
+ }
42
+ var currentTime = new Date().getTime();
43
+ if (err && currentTime - this._operationStart >= this._maxRetryTime) {
44
+ this._errors.push(err);
45
+ this._errors.unshift(new Error("RetryOperation timeout occurred"));
46
+ return false;
47
+ }
48
+ this._errors.push(err);
49
+ var timeout = this._timeouts.shift();
50
+ if (timeout === undefined) {
51
+ if (this._cachedTimeouts) {
52
+ // retry forever, only keep last error
53
+ this._errors.splice(0, this._errors.length - 1);
54
+ timeout = this._cachedTimeouts.slice(-1);
55
+ }
56
+ else {
57
+ return false;
58
+ }
59
+ }
60
+ this._timer = setTimeout(function () {
61
+ _this._attempts++;
62
+ if (_this._operationTimeoutCb) {
63
+ _this._timeout = setTimeout(function () {
64
+ _this._operationTimeoutCb(_this._attempts);
65
+ }, _this._operationTimeout);
66
+ if (_this._options.unref) {
67
+ _this._timeout.unref();
68
+ }
69
+ }
70
+ _this._fn(_this._attempts);
71
+ }, timeout);
72
+ if (this._options.unref) {
73
+ this._timer.unref();
74
+ }
75
+ return true;
76
+ };
77
+ RetryOperation.prototype.attempt = function (fn, timeoutOps) {
78
+ var _this = this;
79
+ this._fn = fn;
80
+ if (timeoutOps) {
81
+ if (timeoutOps.timeout) {
82
+ this._operationTimeout = timeoutOps.timeout;
83
+ }
84
+ if (timeoutOps.cb) {
85
+ this._operationTimeoutCb = timeoutOps.cb;
86
+ }
87
+ }
88
+ if (this._operationTimeoutCb) {
89
+ this._timeout = setTimeout(function () {
90
+ _this._operationTimeoutCb();
91
+ }, this._operationTimeout);
92
+ }
93
+ this._operationStart = new Date().getTime();
94
+ this._fn(this._attempts);
95
+ };
96
+ RetryOperation.prototype.try = function (fn) {
97
+ // console.log("Using RetryOperation.try() is deprecated");
98
+ this.attempt(fn);
99
+ };
100
+ RetryOperation.prototype.start = function (fn) {
101
+ // console.log("Using RetryOperation.start() is deprecated");
102
+ this.attempt(fn);
103
+ };
104
+ RetryOperation.prototype.start = RetryOperation.prototype.try;
105
+ RetryOperation.prototype.errors = function () {
106
+ return this._errors;
107
+ };
108
+ RetryOperation.prototype.attempts = function () {
109
+ return this._attempts;
110
+ };
111
+ RetryOperation.prototype.mainError = function () {
112
+ if (this._errors.length === 0) {
113
+ return null;
114
+ }
115
+ var counts = {};
116
+ var mainError = null;
117
+ var mainErrorCount = 0;
118
+ for (var i = 0; i < this._errors.length; i++) {
119
+ var error = this._errors[i];
120
+ var message = error.message;
121
+ var count = (counts[message] || 0) + 1;
122
+ counts[message] = count;
123
+ if (count >= mainErrorCount) {
124
+ mainError = error;
125
+ mainErrorCount = count;
126
+ }
127
+ }
128
+ return mainError;
129
+ };
130
+ exports.default = RetryOperation;
package/esm/Api.d.ts CHANGED
@@ -1,35 +1,35 @@
1
- import Endpoint from "./Endpoint";
2
- import { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
3
- import RequestBackend from "./backend/RequestBackend";
4
- import EndpointBuilder from "./EndpointBuilder";
5
- import { ApiMockingConfig } from "./MockingTypes";
6
- export declare const getRequestBackend: () => RequestBackend | null;
7
- export declare const isRequestBackendDefault: () => boolean;
8
- export declare const setRequestBackend: (backend: RequestBackend) => void;
9
- export interface ApiInfo {
10
- readonly name: string;
11
- readonly baseUrl: string;
12
- readonly middleware?: RequestMiddleware[];
13
- readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
14
- readonly mocking?: ApiMockingConfig;
15
- readonly requestBackend?: RequestBackend;
16
- }
17
- export declare class Api implements ApiInfo {
18
- readonly baseUrl: string;
19
- readonly name: string;
20
- readonly middleware: RequestMiddleware[];
21
- readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
22
- readonly mocking?: ApiMockingConfig;
23
- readonly requestBackend: RequestBackend;
24
- protected readonly endpoints: Record<string, Endpoint>;
25
- constructor(info: ApiInfo);
26
- endpoint(): EndpointBuilder;
27
- getRequestBackend(): RequestBackend;
28
- getConfig(): BaseRequestConfig;
29
- private hotRequest;
30
- get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
31
- post: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
32
- put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
33
- delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
34
- patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
35
- }
1
+ import { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
2
+ import Endpoint from "./Endpoint";
3
+ import EndpointBuilder from "./EndpointBuilder";
4
+ import { ApiMockingConfig } from "./MockingTypes";
5
+ import RequestBackend from "./backend/RequestBackend";
6
+ export declare const getRequestBackend: () => RequestBackend | null;
7
+ export declare const isRequestBackendDefault: () => boolean;
8
+ export declare const setRequestBackend: (backend: RequestBackend) => void;
9
+ export interface ApiInfo {
10
+ readonly name: string;
11
+ readonly baseUrl: string;
12
+ readonly middleware?: RequestMiddleware[];
13
+ readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
14
+ readonly mocking?: ApiMockingConfig;
15
+ readonly requestBackend?: RequestBackend;
16
+ }
17
+ export declare class Api implements ApiInfo {
18
+ readonly baseUrl: string;
19
+ readonly name: string;
20
+ readonly middleware: RequestMiddleware[];
21
+ readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
22
+ readonly mocking?: ApiMockingConfig;
23
+ readonly requestBackend: RequestBackend;
24
+ protected readonly endpoints: Record<string, Endpoint>;
25
+ constructor(info: ApiInfo);
26
+ endpoint(): EndpointBuilder;
27
+ getRequestBackend(): RequestBackend;
28
+ getConfig(): BaseRequestConfig;
29
+ private hotRequest;
30
+ get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
31
+ post: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
32
+ put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
33
+ delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
34
+ patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
35
+ }
package/esm/Api.js CHANGED
@@ -1,98 +1,92 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import * as Requester from "./Requester";
11
- import EndpointBuilder from "./EndpointBuilder";
12
- import * as Utils from "./Utils";
13
- import FetchRequestBackend from "./backend/FetchRequestBackend";
14
- import { RequestMethod, } from "./ApiConstants";
15
- import { computeRequestConfig } from "./RequestConfig";
16
- // use fetch as default if it is present
17
- let requestBackend = Utils.getGlobalFetch() ? new FetchRequestBackend() : null;
18
- let requestBackendIsDefault = true;
19
- export const getRequestBackend = () => {
20
- return requestBackend;
21
- };
22
- export const isRequestBackendDefault = () => {
23
- return requestBackendIsDefault;
24
- };
25
- export const setRequestBackend = (backend) => {
26
- requestBackendIsDefault = false;
27
- requestBackend = backend;
28
- };
29
- class HotRequestHost {
30
- constructor(api, path, method) {
31
- this.responseType = undefined;
32
- this.validation = {};
33
- this.api = api;
34
- this.method = method;
35
- this.path = path;
36
- }
37
- get baseUrl() {
38
- return this.api.baseUrl;
39
- }
40
- computeConfig(config) {
41
- const apiDefaults = this.api.getConfig();
42
- return computeRequestConfig([
43
- apiDefaults,
44
- config,
45
- ]);
46
- }
47
- computePath(path, config) {
48
- return path.startsWith("/") ? path : `/${path}`;
49
- }
50
- getRequestBackend() {
51
- return this.api.getRequestBackend();
52
- }
53
- }
54
- let defaultBackendMessageShown = false;
55
- export class Api {
56
- constructor(info) {
57
- var _a, _b;
58
- this.endpoints = {};
59
- this.hotRequest = (requestMethod) => (path, config) => __awaiter(this, void 0, void 0, function* () {
60
- return (yield Requester.submit(new HotRequestHost(this, path, requestMethod), config, null));
61
- });
62
- this.get = this.hotRequest(RequestMethod.GET);
63
- this.post = this.hotRequest(RequestMethod.POST);
64
- this.put = this.hotRequest(RequestMethod.PUT);
65
- this.delete = this.hotRequest(RequestMethod.DELETE);
66
- this.patch = this.hotRequest(RequestMethod.PATCH);
67
- this.name = info.name;
68
- this.baseUrl = info.baseUrl;
69
- this.middleware = info.middleware || [];
70
- this.endpoints = {};
71
- this.config = info.config;
72
- this.mocking = (_a = info.mocking) !== null && _a !== void 0 ? _a : undefined;
73
- const requestBackend = (_b = info.requestBackend) !== null && _b !== void 0 ? _b : getRequestBackend();
74
- if (!requestBackend) {
75
- throw new Error("[api-def] No request backend provided in either Api options or globally, use `setRequestBackend()` to set one or pass one via `requestBackend`");
76
- }
77
- this.requestBackend = requestBackend;
78
- if (!info.requestBackend) {
79
- if (process.env.NODE_ENV === "development") {
80
- if (isRequestBackendDefault() && !defaultBackendMessageShown) {
81
- defaultBackendMessageShown = true;
82
- // eslint-disable-next-line
83
- console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
84
- }
85
- }
86
- }
87
- }
88
- endpoint() {
89
- return new EndpointBuilder(this);
90
- }
91
- getRequestBackend() {
92
- return this.requestBackend;
93
- }
94
- getConfig() {
95
- return ((typeof this.config === "function" ? this.config() : this.config) ||
96
- {});
97
- }
98
- }
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { RequestMethod } from "./ApiConstants";
11
+ import EndpointBuilder from "./EndpointBuilder";
12
+ import { computeRequestConfig } from "./RequestConfig";
13
+ import * as Requester from "./Requester";
14
+ import * as Utils from "./Utils";
15
+ import FetchRequestBackend from "./backend/FetchRequestBackend";
16
+ // use fetch as default if it is present
17
+ let requestBackend = Utils.getGlobalFetch() ? new FetchRequestBackend() : null;
18
+ let requestBackendIsDefault = true;
19
+ export const getRequestBackend = () => {
20
+ return requestBackend;
21
+ };
22
+ export const isRequestBackendDefault = () => {
23
+ return requestBackendIsDefault;
24
+ };
25
+ export const setRequestBackend = (backend) => {
26
+ requestBackendIsDefault = false;
27
+ requestBackend = backend;
28
+ };
29
+ class HotRequestHost {
30
+ constructor(api, path, method) {
31
+ this.responseType = undefined;
32
+ this.validation = {};
33
+ this.api = api;
34
+ this.method = method;
35
+ this.path = path;
36
+ }
37
+ get baseUrl() {
38
+ return this.api.baseUrl;
39
+ }
40
+ computeConfig(config) {
41
+ const apiDefaults = this.api.getConfig();
42
+ return computeRequestConfig([apiDefaults, config]);
43
+ }
44
+ computePath(path, config) {
45
+ return path.startsWith("/") ? path : `/${path}`;
46
+ }
47
+ getRequestBackend() {
48
+ return this.api.getRequestBackend();
49
+ }
50
+ }
51
+ let defaultBackendMessageShown = false;
52
+ export class Api {
53
+ constructor(info) {
54
+ var _a, _b;
55
+ this.endpoints = {};
56
+ this.hotRequest = (requestMethod) => (path, config) => __awaiter(this, void 0, void 0, function* () { return yield Requester.submit(new HotRequestHost(this, path, requestMethod), config, null); });
57
+ this.get = this.hotRequest(RequestMethod.GET);
58
+ this.post = this.hotRequest(RequestMethod.POST);
59
+ this.put = this.hotRequest(RequestMethod.PUT);
60
+ this.delete = this.hotRequest(RequestMethod.DELETE);
61
+ this.patch = this.hotRequest(RequestMethod.PATCH);
62
+ this.name = info.name;
63
+ this.baseUrl = info.baseUrl;
64
+ this.middleware = info.middleware || [];
65
+ this.endpoints = {};
66
+ this.config = info.config;
67
+ this.mocking = (_a = info.mocking) !== null && _a !== void 0 ? _a : undefined;
68
+ const requestBackend = (_b = info.requestBackend) !== null && _b !== void 0 ? _b : getRequestBackend();
69
+ if (!requestBackend) {
70
+ throw new Error("[api-def] No request backend provided in either Api options or globally, use `setRequestBackend()` to set one or pass one via `requestBackend`");
71
+ }
72
+ this.requestBackend = requestBackend;
73
+ if (!info.requestBackend) {
74
+ if (process.env.NODE_ENV === "development") {
75
+ if (isRequestBackendDefault() && !defaultBackendMessageShown) {
76
+ defaultBackendMessageShown = true;
77
+ // eslint-disable-next-line
78
+ console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
79
+ }
80
+ }
81
+ }
82
+ }
83
+ endpoint() {
84
+ return new EndpointBuilder(this);
85
+ }
86
+ getRequestBackend() {
87
+ return this.requestBackend;
88
+ }
89
+ getConfig() {
90
+ return (typeof this.config === "function" ? this.config() : this.config) || {};
91
+ }
92
+ }
@@ -1,58 +1,58 @@
1
- import { EnumOf } from "./Utils";
2
- export declare const RequestMethod: {
3
- /** @deprecated use 'POST' */
4
- readonly Post: "post";
5
- /** @deprecated use 'GET' */
6
- readonly Get: "get";
7
- readonly POST: "post";
8
- readonly GET: "get";
9
- readonly PUT: "put";
10
- readonly DELETE: "delete";
11
- readonly PATCH: "patch";
12
- };
13
- export declare type RequestMethod = EnumOf<typeof RequestMethod>;
14
- export declare const RequestEvent: {
15
- /** @deprecated use 'BEFORE_SEND' */
16
- readonly BeforeSend: "beforeSend";
17
- /** @deprecated use 'SUCCESS' */
18
- readonly Success: "success";
19
- /** @deprecated use 'ERROR' */
20
- readonly Error: "error";
21
- /** @deprecated use 'UNRECOVERABLE_ERROR' */
22
- readonly UnrecoverableError: "unrecoverableError";
23
- readonly BEFORE_SEND: "beforeSend";
24
- readonly SUCCESS: "success";
25
- readonly ERROR: "error";
26
- readonly UNRECOVERABLE_ERROR: "unrecoverableError";
27
- };
28
- export declare type RequestEvent = EnumOf<typeof RequestEvent>;
29
- export declare const EventResultType: {
30
- /** @deprecated use 'RESPOND' */
31
- readonly Respond: "respond";
32
- /** @deprecated use 'RETRY' */
33
- readonly Retry: "retry";
34
- readonly RESPOND: "respond";
35
- readonly RETRY: "retry";
36
- };
37
- export declare type EventResultType = EnumOf<typeof EventResultType>;
38
- export declare const CacheSource: {
39
- /** @deprecated use 'API' */
40
- readonly Api: "api";
41
- /** @deprecated use 'LOCAL' */
42
- readonly Local: "local";
43
- readonly API: "api";
44
- readonly LOCAL: "local";
45
- };
46
- export declare type CacheSource = EnumOf<typeof CacheSource>;
47
- export declare const ResponseType: {
48
- /** @deprecated use 'JSON' */
49
- readonly Json: "json";
50
- /** @deprecated use 'TEXT' */
51
- readonly Text: "text";
52
- /** @deprecated use 'ARRAY_BUFFER' */
53
- readonly ArrayBuffer: "arraybuffer";
54
- readonly JSON: "json";
55
- readonly TEXT: "text";
56
- readonly ARRAY_BUFFER: "arraybuffer";
57
- };
58
- export declare type ResponseType = EnumOf<typeof ResponseType>;
1
+ import { EnumOf } from "./Utils";
2
+ export declare const RequestMethod: {
3
+ /** @deprecated use 'POST' */
4
+ readonly Post: "post";
5
+ /** @deprecated use 'GET' */
6
+ readonly Get: "get";
7
+ readonly POST: "post";
8
+ readonly GET: "get";
9
+ readonly PUT: "put";
10
+ readonly DELETE: "delete";
11
+ readonly PATCH: "patch";
12
+ };
13
+ export type RequestMethod = EnumOf<typeof RequestMethod>;
14
+ export declare const RequestEvent: {
15
+ /** @deprecated use 'BEFORE_SEND' */
16
+ readonly BeforeSend: "beforeSend";
17
+ /** @deprecated use 'SUCCESS' */
18
+ readonly Success: "success";
19
+ /** @deprecated use 'ERROR' */
20
+ readonly Error: "error";
21
+ /** @deprecated use 'UNRECOVERABLE_ERROR' */
22
+ readonly UnrecoverableError: "unrecoverableError";
23
+ readonly BEFORE_SEND: "beforeSend";
24
+ readonly SUCCESS: "success";
25
+ readonly ERROR: "error";
26
+ readonly UNRECOVERABLE_ERROR: "unrecoverableError";
27
+ };
28
+ export type RequestEvent = EnumOf<typeof RequestEvent>;
29
+ export declare const EventResultType: {
30
+ /** @deprecated use 'RESPOND' */
31
+ readonly Respond: "respond";
32
+ /** @deprecated use 'RETRY' */
33
+ readonly Retry: "retry";
34
+ readonly RESPOND: "respond";
35
+ readonly RETRY: "retry";
36
+ };
37
+ export type EventResultType = EnumOf<typeof EventResultType>;
38
+ export declare const CacheSource: {
39
+ /** @deprecated use 'API' */
40
+ readonly Api: "api";
41
+ /** @deprecated use 'LOCAL' */
42
+ readonly Local: "local";
43
+ readonly API: "api";
44
+ readonly LOCAL: "local";
45
+ };
46
+ export type CacheSource = EnumOf<typeof CacheSource>;
47
+ export declare const ResponseType: {
48
+ /** @deprecated use 'JSON' */
49
+ readonly Json: "json";
50
+ /** @deprecated use 'TEXT' */
51
+ readonly Text: "text";
52
+ /** @deprecated use 'ARRAY_BUFFER' */
53
+ readonly ArrayBuffer: "arraybuffer";
54
+ readonly JSON: "json";
55
+ readonly TEXT: "text";
56
+ readonly ARRAY_BUFFER: "arraybuffer";
57
+ };
58
+ export type ResponseType = EnumOf<typeof ResponseType>;