api-def 0.9.1 → 0.9.3
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 +1 -1
- package/cjs/Api.d.ts +35 -35
- package/cjs/Api.js +140 -144
- package/cjs/ApiConstants.d.ts +58 -58
- package/cjs/ApiConstants.js +55 -55
- package/cjs/ApiTypes.d.ts +103 -103
- package/cjs/ApiTypes.js +4 -4
- package/cjs/ApiUtils.d.ts +6 -6
- package/cjs/ApiUtils.js +51 -53
- package/cjs/Endpoint.d.ts +52 -52
- package/cjs/Endpoint.js +104 -108
- package/cjs/EndpointBuilder.d.ts +14 -14
- package/cjs/EndpointBuilder.js +46 -46
- package/cjs/MockingTypes.d.ts +30 -30
- package/cjs/MockingTypes.js +2 -2
- package/cjs/QueryHandling.d.ts +3 -3
- package/cjs/QueryHandling.js +24 -24
- package/cjs/RequestConfig.d.ts +2 -2
- package/cjs/RequestConfig.js +74 -74
- package/cjs/RequestContext.d.ts +48 -48
- package/cjs/RequestContext.js +235 -238
- package/cjs/RequestError.d.ts +30 -30
- package/cjs/RequestError.js +47 -47
- package/cjs/Requester.d.ts +3 -3
- package/cjs/Requester.js +335 -336
- package/cjs/TextDecoding.d.ts +1 -1
- package/cjs/TextDecoding.js +152 -139
- package/cjs/UtilTypes.d.ts +5 -5
- package/cjs/UtilTypes.js +2 -2
- package/cjs/Utils.d.ts +15 -15
- package/cjs/Utils.js +79 -77
- package/cjs/Validation.d.ts +8 -8
- package/cjs/Validation.js +2 -2
- package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
- package/cjs/backend/AxiosRequestBackend.js +99 -99
- package/cjs/backend/FetchRequestBackend.d.ts +15 -15
- package/cjs/backend/FetchRequestBackend.js +203 -202
- package/cjs/backend/MockRequestBackend.d.ts +11 -11
- package/cjs/backend/MockRequestBackend.js +164 -164
- package/cjs/backend/RequestBackend.d.ts +19 -19
- package/cjs/backend/RequestBackend.js +2 -2
- package/cjs/cache/CacheBackend.d.ts +6 -6
- package/cjs/cache/CacheBackend.js +2 -2
- package/cjs/cache/Caching.d.ts +10 -10
- package/cjs/cache/Caching.js +88 -88
- package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
- package/cjs/cache/LocalForageCacheBackend.js +24 -24
- package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
- package/cjs/cache/LocalStorageCacheBackend.js +77 -77
- package/cjs/index.d.ts +13 -13
- package/cjs/index.js +41 -41
- package/cjs/middleware/CacheMiddleware.d.ts +7 -7
- package/cjs/middleware/CacheMiddleware.js +107 -107
- package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
- package/cjs/middleware/LoggingMiddleware.js +82 -88
- package/cjs/util/retry/index.d.ts +3 -3
- package/cjs/util/retry/index.js +52 -52
- package/cjs/util/retry/lib/retry.d.ts +3 -3
- package/cjs/util/retry/lib/retry.js +50 -54
- package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
- package/cjs/util/retry/lib/retryOperation.js +130 -134
- package/esm/Api.d.ts +35 -35
- package/esm/Api.js +92 -98
- package/esm/ApiConstants.d.ts +58 -58
- package/esm/ApiConstants.js +52 -52
- package/esm/ApiTypes.d.ts +103 -103
- package/esm/ApiTypes.js +1 -1
- package/esm/ApiUtils.d.ts +6 -6
- package/esm/ApiUtils.js +43 -45
- package/esm/Endpoint.d.ts +52 -52
- package/esm/Endpoint.js +66 -70
- package/esm/EndpointBuilder.d.ts +14 -14
- package/esm/EndpointBuilder.js +31 -31
- package/esm/MockingTypes.d.ts +30 -30
- package/esm/MockingTypes.js +1 -1
- package/esm/QueryHandling.d.ts +3 -3
- package/esm/QueryHandling.js +19 -19
- package/esm/RequestConfig.d.ts +2 -2
- package/esm/RequestConfig.js +56 -56
- package/esm/RequestContext.d.ts +48 -48
- package/esm/RequestContext.js +167 -170
- package/esm/RequestError.d.ts +30 -30
- package/esm/RequestError.js +42 -42
- package/esm/Requester.d.ts +3 -3
- package/esm/Requester.js +252 -253
- package/esm/TextDecoding.d.ts +1 -1
- package/esm/TextDecoding.js +148 -135
- package/esm/UtilTypes.d.ts +5 -5
- package/esm/UtilTypes.js +1 -1
- package/esm/Utils.d.ts +15 -15
- package/esm/Utils.js +66 -68
- package/esm/Validation.d.ts +8 -8
- package/esm/Validation.js +1 -1
- package/esm/backend/AxiosRequestBackend.d.ts +13 -13
- package/esm/backend/AxiosRequestBackend.js +62 -62
- package/esm/backend/FetchRequestBackend.d.ts +15 -15
- package/esm/backend/FetchRequestBackend.js +136 -136
- package/esm/backend/MockRequestBackend.d.ts +11 -11
- package/esm/backend/MockRequestBackend.js +116 -116
- package/esm/backend/RequestBackend.d.ts +19 -19
- package/esm/backend/RequestBackend.js +1 -1
- package/esm/cache/CacheBackend.d.ts +6 -6
- package/esm/cache/CacheBackend.js +1 -1
- package/esm/cache/Caching.d.ts +10 -10
- package/esm/cache/Caching.js +39 -39
- package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
- package/esm/cache/LocalForageCacheBackend.js +20 -20
- package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
- package/esm/cache/LocalStorageCacheBackend.js +32 -32
- package/esm/index.d.ts +13 -13
- package/esm/index.js +14 -15
- package/esm/middleware/CacheMiddleware.d.ts +7 -7
- package/esm/middleware/CacheMiddleware.js +58 -58
- package/esm/middleware/LoggingMiddleware.d.ts +6 -6
- package/esm/middleware/LoggingMiddleware.js +77 -83
- package/esm/util/retry/index.d.ts +3 -3
- package/esm/util/retry/index.js +50 -50
- package/esm/util/retry/lib/retry.d.ts +3 -3
- package/esm/util/retry/lib/retry.js +35 -39
- package/esm/util/retry/lib/retryOperation.d.ts +27 -27
- package/esm/util/retry/lib/retryOperation.js +126 -132
- package/package.json +19 -34
|
@@ -1,134 +1,130 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
function RetryOperation(timeouts, options) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
RetryOperation.prototype.
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (timeoutOps.
|
|
85
|
-
this.
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.
|
|
99
|
-
};
|
|
100
|
-
RetryOperation.prototype.
|
|
101
|
-
// console.log("Using RetryOperation.
|
|
102
|
-
this.attempt(fn);
|
|
103
|
-
};
|
|
104
|
-
RetryOperation.prototype.start =
|
|
105
|
-
|
|
106
|
-
this.
|
|
107
|
-
};
|
|
108
|
-
RetryOperation.prototype.
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
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
|
|
11
|
-
import EndpointBuilder from "./EndpointBuilder";
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.
|
|
59
|
-
this.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
this.
|
|
63
|
-
this.
|
|
64
|
-
this.
|
|
65
|
-
this.
|
|
66
|
-
this.
|
|
67
|
-
this.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
+
}
|
package/esm/ApiConstants.d.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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>;
|