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.
- 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 +202 -197
- 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 -126
- 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 +12 -32
package/esm/RequestContext.js
CHANGED
|
@@ -1,170 +1,167 @@
|
|
|
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 Utils from "./Utils";
|
|
11
|
-
let contextIdCounter = 0;
|
|
12
|
-
export default class RequestContext {
|
|
13
|
-
constructor(backend, host, config, computedPath, mocking) {
|
|
14
|
-
this.canceler = null;
|
|
15
|
-
this.response = undefined;
|
|
16
|
-
this.error = null;
|
|
17
|
-
this.cacheInfo = { cached: false, source: null };
|
|
18
|
-
this.cancelled = false;
|
|
19
|
-
this.backend = backend;
|
|
20
|
-
this.id = contextIdCounter++;
|
|
21
|
-
this.host = host;
|
|
22
|
-
this.computedConfig = config;
|
|
23
|
-
Utils.assign({}, this.computedConfig.headers);
|
|
24
|
-
this.computedPath = computedPath;
|
|
25
|
-
this.computedBaseUrl = host.baseUrl;
|
|
26
|
-
this.computedMethod = host.method;
|
|
27
|
-
this.key = this.generateKey();
|
|
28
|
-
this.stats = {
|
|
29
|
-
attempt: 0,
|
|
30
|
-
cached: false,
|
|
31
|
-
};
|
|
32
|
-
this.eventHandlers = {};
|
|
33
|
-
this.mocking = mocking;
|
|
34
|
-
this.validation = host.validation;
|
|
35
|
-
this.initMiddleware();
|
|
36
|
-
this.parseRequestBody();
|
|
37
|
-
this.computedRequestUrl = this.generateRequestUrl();
|
|
38
|
-
}
|
|
39
|
-
get method() {
|
|
40
|
-
return this.computedMethod;
|
|
41
|
-
}
|
|
42
|
-
get api() {
|
|
43
|
-
return this.host.api;
|
|
44
|
-
}
|
|
45
|
-
get baseUrl() {
|
|
46
|
-
return this.computedBaseUrl;
|
|
47
|
-
}
|
|
48
|
-
get responseType() {
|
|
49
|
-
return this.host.responseType;
|
|
50
|
-
}
|
|
51
|
-
initMiddleware() {
|
|
52
|
-
const { middleware } = this.api;
|
|
53
|
-
for (let i = 0; i < middleware.length; i++) {
|
|
54
|
-
const events = middleware[i];
|
|
55
|
-
const eventTypes = Object.keys(events);
|
|
56
|
-
for (let n = 0; n < eventTypes.length; n++) {
|
|
57
|
-
const eventType = eventTypes[n];
|
|
58
|
-
const eventHandlersForType = this.eventHandlers[eventType] || (this.eventHandlers[eventType] = []);
|
|
59
|
-
const middlewareEventHandlers = events[eventType];
|
|
60
|
-
if (middlewareEventHandlers) {
|
|
61
|
-
eventHandlersForType.push(middlewareEventHandlers);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
generateKey() {
|
|
67
|
-
let key = this.computedPath.trim();
|
|
68
|
-
if (this.computedConfig.queryString) {
|
|
69
|
-
key +=
|
|
70
|
-
}
|
|
71
|
-
return key;
|
|
72
|
-
}
|
|
73
|
-
updateHeaders(newHeaders) {
|
|
74
|
-
this.computedConfig.headers = Utils.assign({}, this.computedConfig.headers, newHeaders);
|
|
75
|
-
this.parseRequestBody();
|
|
76
|
-
return this;
|
|
77
|
-
}
|
|
78
|
-
parseRequestBody() {
|
|
79
|
-
var _a;
|
|
80
|
-
if (this.computedConfig.body && this.computedConfig.headers) {
|
|
81
|
-
const contentTypeKey = Object.keys(this.computedConfig.headers).find(key => key.toLowerCase() === "content-type");
|
|
82
|
-
const contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString().split(";")[0].trim());
|
|
83
|
-
if (contentType === "application/x-www-form-urlencoded") {
|
|
84
|
-
const searchParams = new URLSearchParams();
|
|
85
|
-
for (const key of Object.keys(this.computedConfig.body)) {
|
|
86
|
-
const value = this.computedConfig.body[key];
|
|
87
|
-
searchParams.append(key, value === null || value === void 0 ? void 0 : value.toString());
|
|
88
|
-
}
|
|
89
|
-
this.parsedBody = searchParams;
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
this.parsedBody = this.computedConfig.body;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
this.parsedBody = undefined;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
getParsedBody() {
|
|
100
|
-
return this.parsedBody;
|
|
101
|
-
}
|
|
102
|
-
updateQuery(newQuery) {
|
|
103
|
-
this.computedConfig.queryObject = Utils.assign({}, this.computedConfig.queryObject, newQuery);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
this.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
this.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return url;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
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 Utils from "./Utils";
|
|
11
|
+
let contextIdCounter = 0;
|
|
12
|
+
export default class RequestContext {
|
|
13
|
+
constructor(backend, host, config, computedPath, mocking) {
|
|
14
|
+
this.canceler = null;
|
|
15
|
+
this.response = undefined;
|
|
16
|
+
this.error = null;
|
|
17
|
+
this.cacheInfo = { cached: false, source: null };
|
|
18
|
+
this.cancelled = false;
|
|
19
|
+
this.backend = backend;
|
|
20
|
+
this.id = contextIdCounter++;
|
|
21
|
+
this.host = host;
|
|
22
|
+
this.computedConfig = config;
|
|
23
|
+
Utils.assign({}, this.computedConfig.headers);
|
|
24
|
+
this.computedPath = computedPath;
|
|
25
|
+
this.computedBaseUrl = host.baseUrl;
|
|
26
|
+
this.computedMethod = host.method;
|
|
27
|
+
this.key = this.generateKey();
|
|
28
|
+
this.stats = {
|
|
29
|
+
attempt: 0,
|
|
30
|
+
cached: false,
|
|
31
|
+
};
|
|
32
|
+
this.eventHandlers = {};
|
|
33
|
+
this.mocking = mocking;
|
|
34
|
+
this.validation = host.validation;
|
|
35
|
+
this.initMiddleware();
|
|
36
|
+
this.parseRequestBody();
|
|
37
|
+
this.computedRequestUrl = this.generateRequestUrl();
|
|
38
|
+
}
|
|
39
|
+
get method() {
|
|
40
|
+
return this.computedMethod;
|
|
41
|
+
}
|
|
42
|
+
get api() {
|
|
43
|
+
return this.host.api;
|
|
44
|
+
}
|
|
45
|
+
get baseUrl() {
|
|
46
|
+
return this.computedBaseUrl;
|
|
47
|
+
}
|
|
48
|
+
get responseType() {
|
|
49
|
+
return this.host.responseType;
|
|
50
|
+
}
|
|
51
|
+
initMiddleware() {
|
|
52
|
+
const { middleware } = this.api;
|
|
53
|
+
for (let i = 0; i < middleware.length; i++) {
|
|
54
|
+
const events = middleware[i];
|
|
55
|
+
const eventTypes = Object.keys(events);
|
|
56
|
+
for (let n = 0; n < eventTypes.length; n++) {
|
|
57
|
+
const eventType = eventTypes[n];
|
|
58
|
+
const eventHandlersForType = this.eventHandlers[eventType] || (this.eventHandlers[eventType] = []);
|
|
59
|
+
const middlewareEventHandlers = events[eventType];
|
|
60
|
+
if (middlewareEventHandlers) {
|
|
61
|
+
eventHandlersForType.push(middlewareEventHandlers);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
generateKey() {
|
|
67
|
+
let key = this.computedPath.trim();
|
|
68
|
+
if (this.computedConfig.queryString) {
|
|
69
|
+
key += `?${this.computedConfig.queryString}`;
|
|
70
|
+
}
|
|
71
|
+
return key;
|
|
72
|
+
}
|
|
73
|
+
updateHeaders(newHeaders) {
|
|
74
|
+
this.computedConfig.headers = Utils.assign({}, this.computedConfig.headers, newHeaders);
|
|
75
|
+
this.parseRequestBody();
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
parseRequestBody() {
|
|
79
|
+
var _a;
|
|
80
|
+
if (this.computedConfig.body && this.computedConfig.headers) {
|
|
81
|
+
const contentTypeKey = Object.keys(this.computedConfig.headers).find((key) => key.toLowerCase() === "content-type");
|
|
82
|
+
const contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString().split(";")[0].trim());
|
|
83
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
84
|
+
const searchParams = new URLSearchParams();
|
|
85
|
+
for (const key of Object.keys(this.computedConfig.body)) {
|
|
86
|
+
const value = this.computedConfig.body[key];
|
|
87
|
+
searchParams.append(key, value === null || value === void 0 ? void 0 : value.toString());
|
|
88
|
+
}
|
|
89
|
+
this.parsedBody = searchParams;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
this.parsedBody = this.computedConfig.body;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.parsedBody = undefined;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
getParsedBody() {
|
|
100
|
+
return this.parsedBody;
|
|
101
|
+
}
|
|
102
|
+
updateQuery(newQuery) {
|
|
103
|
+
this.computedConfig.queryObject = Utils.assign({}, this.computedConfig.queryObject, newQuery);
|
|
104
|
+
this.computedRequestUrl = this.generateRequestUrl();
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
triggerEvent(eventType) {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
const eventHandlers = this.eventHandlers[eventType];
|
|
110
|
+
if (eventHandlers) {
|
|
111
|
+
for (let i = 0; i < eventHandlers.length; i++) {
|
|
112
|
+
const eventHandler = eventHandlers[i];
|
|
113
|
+
// eslint-disable-next-line no-await-in-loop
|
|
114
|
+
const eventResult = yield eventHandler(this);
|
|
115
|
+
if (eventResult) {
|
|
116
|
+
return eventResult;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return undefined;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
addCanceller(canceler) {
|
|
124
|
+
this.canceler = canceler;
|
|
125
|
+
}
|
|
126
|
+
cancel() {
|
|
127
|
+
this.cancelled = true;
|
|
128
|
+
if (this.canceler) {
|
|
129
|
+
this.canceler();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
get requestUrl() {
|
|
133
|
+
return this.computedRequestUrl;
|
|
134
|
+
}
|
|
135
|
+
get path() {
|
|
136
|
+
return this.computedPath;
|
|
137
|
+
}
|
|
138
|
+
updatePath(path) {
|
|
139
|
+
this.computedPath = path;
|
|
140
|
+
this.computedRequestUrl = this.generateRequestUrl();
|
|
141
|
+
}
|
|
142
|
+
updateBaseUrl(baseUrl) {
|
|
143
|
+
this.computedBaseUrl = baseUrl;
|
|
144
|
+
this.computedRequestUrl = this.generateRequestUrl();
|
|
145
|
+
}
|
|
146
|
+
updateMethod(method) {
|
|
147
|
+
this.computedMethod = method;
|
|
148
|
+
}
|
|
149
|
+
generateRequestUrl() {
|
|
150
|
+
let path = !this.baseUrl.endsWith("/") ? `${this.baseUrl}/` : this.baseUrl;
|
|
151
|
+
path += this.computedPath.startsWith("/") ? this.computedPath.substring(1) : this.computedPath;
|
|
152
|
+
let origin = undefined;
|
|
153
|
+
if (typeof window !== "undefined") {
|
|
154
|
+
origin = window.origin;
|
|
155
|
+
}
|
|
156
|
+
if (!origin) {
|
|
157
|
+
if (path.indexOf("://") === -1) {
|
|
158
|
+
path = `https://${path}`;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const url = new URL(path, origin);
|
|
162
|
+
if (this.computedConfig.queryString) {
|
|
163
|
+
url.search = this.computedConfig.queryString;
|
|
164
|
+
}
|
|
165
|
+
return url;
|
|
166
|
+
}
|
|
167
|
+
}
|
package/esm/RequestError.d.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { ApiResponse } from "./ApiTypes";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
export declare const RequestErrorCode: {
|
|
5
|
-
readonly MISC_UNKNOWN_ERROR: "misc/unknown-error";
|
|
6
|
-
readonly REQUEST_NETWORK_ERROR: "request/network-error";
|
|
7
|
-
readonly REQUEST_HOST_NAME_NOT_FOUND: "request/host-name-not-found";
|
|
8
|
-
readonly REQUEST_INVALID_STATUS: "request/invalid-status";
|
|
9
|
-
readonly REQUEST_INVALID_CONFIG: "request/invalid-config";
|
|
10
|
-
readonly REQUEST_MISMATCH_RESPONSE_TYPE: "request/mismatch-response-type";
|
|
11
|
-
readonly VALIDATION_QUERY_VALIDATE_ERROR: "validation/query-validate-error";
|
|
12
|
-
readonly VALIDATION_PARAMS_VALIDATE_ERROR: "validation/params-validate-error";
|
|
13
|
-
readonly VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error";
|
|
14
|
-
readonly VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error";
|
|
15
|
-
};
|
|
16
|
-
export
|
|
17
|
-
export interface RequestError extends Error {
|
|
18
|
-
isRequestError: true;
|
|
19
|
-
response: ApiResponse | undefined | null;
|
|
20
|
-
code: string;
|
|
21
|
-
attempts: number;
|
|
22
|
-
}
|
|
23
|
-
export declare const isRequestError: (error: Error) => error is RequestError;
|
|
24
|
-
export interface RequestErrorConfig {
|
|
25
|
-
error: Error;
|
|
26
|
-
code: string;
|
|
27
|
-
context: RequestContext;
|
|
28
|
-
response?: ApiResponse | null;
|
|
29
|
-
}
|
|
30
|
-
export declare const convertToRequestError: (config: RequestErrorConfig) => RequestError;
|
|
1
|
+
import { ApiResponse } from "./ApiTypes";
|
|
2
|
+
import RequestContext from "./RequestContext";
|
|
3
|
+
import { EnumOf } from "./Utils";
|
|
4
|
+
export declare const RequestErrorCode: {
|
|
5
|
+
readonly MISC_UNKNOWN_ERROR: "misc/unknown-error";
|
|
6
|
+
readonly REQUEST_NETWORK_ERROR: "request/network-error";
|
|
7
|
+
readonly REQUEST_HOST_NAME_NOT_FOUND: "request/host-name-not-found";
|
|
8
|
+
readonly REQUEST_INVALID_STATUS: "request/invalid-status";
|
|
9
|
+
readonly REQUEST_INVALID_CONFIG: "request/invalid-config";
|
|
10
|
+
readonly REQUEST_MISMATCH_RESPONSE_TYPE: "request/mismatch-response-type";
|
|
11
|
+
readonly VALIDATION_QUERY_VALIDATE_ERROR: "validation/query-validate-error";
|
|
12
|
+
readonly VALIDATION_PARAMS_VALIDATE_ERROR: "validation/params-validate-error";
|
|
13
|
+
readonly VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error";
|
|
14
|
+
readonly VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error";
|
|
15
|
+
};
|
|
16
|
+
export type RequestErrorCode = EnumOf<typeof RequestErrorCode>;
|
|
17
|
+
export interface RequestError extends Error {
|
|
18
|
+
isRequestError: true;
|
|
19
|
+
response: ApiResponse | undefined | null;
|
|
20
|
+
code: string;
|
|
21
|
+
attempts: number;
|
|
22
|
+
}
|
|
23
|
+
export declare const isRequestError: (error: Error) => error is RequestError;
|
|
24
|
+
export interface RequestErrorConfig {
|
|
25
|
+
error: Error;
|
|
26
|
+
code: string;
|
|
27
|
+
context: RequestContext;
|
|
28
|
+
response?: ApiResponse | null;
|
|
29
|
+
}
|
|
30
|
+
export declare const convertToRequestError: (config: RequestErrorConfig) => RequestError;
|
package/esm/RequestError.js
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
export const RequestErrorCode = {
|
|
2
|
-
MISC_UNKNOWN_ERROR: "misc/unknown-error",
|
|
3
|
-
REQUEST_NETWORK_ERROR: "request/network-error",
|
|
4
|
-
REQUEST_HOST_NAME_NOT_FOUND: "request/host-name-not-found",
|
|
5
|
-
REQUEST_INVALID_STATUS: "request/invalid-status",
|
|
6
|
-
REQUEST_INVALID_CONFIG: "request/invalid-config",
|
|
7
|
-
REQUEST_MISMATCH_RESPONSE_TYPE: "request/mismatch-response-type",
|
|
8
|
-
VALIDATION_QUERY_VALIDATE_ERROR: "validation/query-validate-error",
|
|
9
|
-
VALIDATION_PARAMS_VALIDATE_ERROR: "validation/params-validate-error",
|
|
10
|
-
VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error",
|
|
11
|
-
VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error",
|
|
12
|
-
};
|
|
13
|
-
export const isRequestError = (error) => {
|
|
14
|
-
return "isRequestError" in error;
|
|
15
|
-
};
|
|
16
|
-
export const convertToRequestError = (config) => {
|
|
17
|
-
const { error, context, response, code } = config;
|
|
18
|
-
const body = context.getParsedBody();
|
|
19
|
-
const resultError = Object.assign(error, {
|
|
20
|
-
name: "RequestError",
|
|
21
|
-
response: response,
|
|
22
|
-
code: code,
|
|
23
|
-
isRequestError: true,
|
|
24
|
-
attempts: context.stats.attempt,
|
|
25
|
-
request: {
|
|
26
|
-
url: context.requestUrl.href,
|
|
27
|
-
query: context.computedConfig.queryObject,
|
|
28
|
-
headers: context.computedConfig.headers,
|
|
29
|
-
body: body,
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
try {
|
|
33
|
-
Object.defineProperty(resultError, "message", { value: `Request failed${(response === null || response === void 0 ? void 0 : response.status) ? ` with status code ${response.status}` : ""}
|
|
34
|
-
}
|
|
35
|
-
catch (e) {
|
|
36
|
-
// ignore
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Object.setPrototypeOf(error, Error);
|
|
41
|
-
return resultError;
|
|
42
|
-
};
|
|
1
|
+
export const RequestErrorCode = {
|
|
2
|
+
MISC_UNKNOWN_ERROR: "misc/unknown-error",
|
|
3
|
+
REQUEST_NETWORK_ERROR: "request/network-error",
|
|
4
|
+
REQUEST_HOST_NAME_NOT_FOUND: "request/host-name-not-found",
|
|
5
|
+
REQUEST_INVALID_STATUS: "request/invalid-status",
|
|
6
|
+
REQUEST_INVALID_CONFIG: "request/invalid-config",
|
|
7
|
+
REQUEST_MISMATCH_RESPONSE_TYPE: "request/mismatch-response-type",
|
|
8
|
+
VALIDATION_QUERY_VALIDATE_ERROR: "validation/query-validate-error",
|
|
9
|
+
VALIDATION_PARAMS_VALIDATE_ERROR: "validation/params-validate-error",
|
|
10
|
+
VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error",
|
|
11
|
+
VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error",
|
|
12
|
+
};
|
|
13
|
+
export const isRequestError = (error) => {
|
|
14
|
+
return "isRequestError" in error;
|
|
15
|
+
};
|
|
16
|
+
export const convertToRequestError = (config) => {
|
|
17
|
+
const { error, context, response, code } = config;
|
|
18
|
+
const body = context.getParsedBody();
|
|
19
|
+
const resultError = Object.assign(error, {
|
|
20
|
+
name: "RequestError",
|
|
21
|
+
response: response,
|
|
22
|
+
code: code,
|
|
23
|
+
isRequestError: true,
|
|
24
|
+
attempts: context.stats.attempt,
|
|
25
|
+
request: {
|
|
26
|
+
url: context.requestUrl.href,
|
|
27
|
+
query: context.computedConfig.queryObject,
|
|
28
|
+
headers: context.computedConfig.headers,
|
|
29
|
+
body: body,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
try {
|
|
33
|
+
Object.defineProperty(resultError, "message", { value: `Request failed${(response === null || response === void 0 ? void 0 : response.status) ? ` with status code ${response.status}` : ""} [${code}]: ${resultError.message}` });
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
// ignore
|
|
37
|
+
}
|
|
38
|
+
resultError.config = undefined;
|
|
39
|
+
resultError.toJSON = undefined;
|
|
40
|
+
Object.setPrototypeOf(error, Error);
|
|
41
|
+
return resultError;
|
|
42
|
+
};
|
package/esm/Requester.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
2
|
-
import { EndpointMockingConfig } from "./MockingTypes";
|
|
3
|
-
export declare const submit: <R, P extends string | undefined, Q extends Query, B extends Body | undefined>(host: RequestHost, config: RequestConfig<P, Q, B>, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined) => Promise<ApiResponse<R>>;
|
|
1
|
+
import { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
2
|
+
import { EndpointMockingConfig } from "./MockingTypes";
|
|
3
|
+
export declare const submit: <R, P extends string | undefined, Q extends Query, B extends Body | undefined>(host: RequestHost, config: RequestConfig<P, Q, B>, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined) => Promise<ApiResponse<R>>;
|