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
package/cjs/RequestContext.d.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { EndpointMockingConfig } from "./MockingTypes";
|
|
5
|
-
import { RequestError } from "./RequestError";
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
export default class RequestContext<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
9
|
-
readonly id: number;
|
|
10
|
-
readonly key: string;
|
|
11
|
-
private computedPath;
|
|
12
|
-
private computedBaseUrl;
|
|
13
|
-
private computedMethod;
|
|
14
|
-
readonly stats: RequestContextStats;
|
|
15
|
-
private readonly host;
|
|
16
|
-
readonly eventHandlers: RequestEventHandlers<R>;
|
|
17
|
-
readonly backend: RequestBackend;
|
|
18
|
-
private canceler;
|
|
19
|
-
response: ApiResponse<R> | null | undefined;
|
|
20
|
-
error: RequestError | null;
|
|
21
|
-
readonly cacheInfo: RequestCacheInfo;
|
|
22
|
-
cancelled: boolean;
|
|
23
|
-
readonly computedConfig: ComputedRequestConfig<P, Q, B>;
|
|
24
|
-
private computedRequestUrl;
|
|
25
|
-
readonly mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined;
|
|
26
|
-
private parsedBody;
|
|
27
|
-
readonly validation: Validation<R, P, Q, B>;
|
|
28
|
-
constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<P, Q, B>, computedPath: string, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined);
|
|
29
|
-
get method(): RequestMethod;
|
|
30
|
-
get api(): Api;
|
|
31
|
-
get baseUrl(): string;
|
|
32
|
-
get responseType(): ResponseType | undefined;
|
|
33
|
-
private initMiddleware;
|
|
34
|
-
private generateKey;
|
|
35
|
-
updateHeaders(newHeaders: Headers): this;
|
|
36
|
-
private parseRequestBody;
|
|
37
|
-
getParsedBody(): any;
|
|
38
|
-
updateQuery(newQuery: Partial<Q>): this;
|
|
39
|
-
triggerEvent(eventType: RequestEvent): Promise<EventResult<R> | undefined>;
|
|
40
|
-
addCanceller(canceler: () => void): void;
|
|
41
|
-
cancel(): void;
|
|
42
|
-
get requestUrl(): URL;
|
|
43
|
-
get path(): string;
|
|
44
|
-
updatePath(path: string): void;
|
|
45
|
-
updateBaseUrl(baseUrl: string): void;
|
|
46
|
-
updateMethod(method: RequestMethod): void;
|
|
47
|
-
private generateRequestUrl;
|
|
48
|
-
}
|
|
1
|
+
import { Api } from "./Api";
|
|
2
|
+
import { RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
+
import { ApiResponse, Body, ComputedRequestConfig, EventResult, Headers, Params, Query, RequestCacheInfo, RequestContextStats, RequestEventHandlers, RequestHost } from "./ApiTypes";
|
|
4
|
+
import { EndpointMockingConfig } from "./MockingTypes";
|
|
5
|
+
import { RequestError } from "./RequestError";
|
|
6
|
+
import { Validation } from "./Validation";
|
|
7
|
+
import RequestBackend from "./backend/RequestBackend";
|
|
8
|
+
export default class RequestContext<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
9
|
+
readonly id: number;
|
|
10
|
+
readonly key: string;
|
|
11
|
+
private computedPath;
|
|
12
|
+
private computedBaseUrl;
|
|
13
|
+
private computedMethod;
|
|
14
|
+
readonly stats: RequestContextStats;
|
|
15
|
+
private readonly host;
|
|
16
|
+
readonly eventHandlers: RequestEventHandlers<R>;
|
|
17
|
+
readonly backend: RequestBackend;
|
|
18
|
+
private canceler;
|
|
19
|
+
response: ApiResponse<R> | null | undefined;
|
|
20
|
+
error: RequestError | null;
|
|
21
|
+
readonly cacheInfo: RequestCacheInfo;
|
|
22
|
+
cancelled: boolean;
|
|
23
|
+
readonly computedConfig: ComputedRequestConfig<P, Q, B>;
|
|
24
|
+
private computedRequestUrl;
|
|
25
|
+
readonly mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined;
|
|
26
|
+
private parsedBody;
|
|
27
|
+
readonly validation: Validation<R, P, Q, B>;
|
|
28
|
+
constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<P, Q, B>, computedPath: string, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined);
|
|
29
|
+
get method(): RequestMethod;
|
|
30
|
+
get api(): Api;
|
|
31
|
+
get baseUrl(): string;
|
|
32
|
+
get responseType(): ResponseType | undefined;
|
|
33
|
+
private initMiddleware;
|
|
34
|
+
private generateKey;
|
|
35
|
+
updateHeaders(newHeaders: Headers): this;
|
|
36
|
+
private parseRequestBody;
|
|
37
|
+
getParsedBody(): any;
|
|
38
|
+
updateQuery(newQuery: Partial<Q>): this;
|
|
39
|
+
triggerEvent(eventType: RequestEvent): Promise<EventResult<R> | undefined>;
|
|
40
|
+
addCanceller(canceler: () => void): void;
|
|
41
|
+
cancel(): void;
|
|
42
|
+
get requestUrl(): URL;
|
|
43
|
+
get path(): string;
|
|
44
|
+
updatePath(path: string): void;
|
|
45
|
+
updateBaseUrl(baseUrl: string): void;
|
|
46
|
+
updateMethod(method: RequestMethod): void;
|
|
47
|
+
private generateRequestUrl;
|
|
48
|
+
}
|
package/cjs/RequestContext.js
CHANGED
|
@@ -1,238 +1,235 @@
|
|
|
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;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "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 (_) 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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
var Utils = require("./Utils");
|
|
40
|
-
var contextIdCounter = 0;
|
|
41
|
-
var RequestContext = /** @class */ (function () {
|
|
42
|
-
function RequestContext(backend, host, config, computedPath, mocking) {
|
|
43
|
-
this.canceler = null;
|
|
44
|
-
this.response = undefined;
|
|
45
|
-
this.error = null;
|
|
46
|
-
this.cacheInfo = { cached: false, source: null };
|
|
47
|
-
this.cancelled = false;
|
|
48
|
-
this.backend = backend;
|
|
49
|
-
this.id = contextIdCounter++;
|
|
50
|
-
this.host = host;
|
|
51
|
-
this.computedConfig = config;
|
|
52
|
-
Utils.assign({}, this.computedConfig.headers);
|
|
53
|
-
this.computedPath = computedPath;
|
|
54
|
-
this.computedBaseUrl = host.baseUrl;
|
|
55
|
-
this.computedMethod = host.method;
|
|
56
|
-
this.key = this.generateKey();
|
|
57
|
-
this.stats = {
|
|
58
|
-
attempt: 0,
|
|
59
|
-
cached: false,
|
|
60
|
-
};
|
|
61
|
-
this.eventHandlers = {};
|
|
62
|
-
this.mocking = mocking;
|
|
63
|
-
this.validation = host.validation;
|
|
64
|
-
this.initMiddleware();
|
|
65
|
-
this.parseRequestBody();
|
|
66
|
-
this.computedRequestUrl = this.generateRequestUrl();
|
|
67
|
-
}
|
|
68
|
-
Object.defineProperty(RequestContext.prototype, "method", {
|
|
69
|
-
get: function () {
|
|
70
|
-
return this.computedMethod;
|
|
71
|
-
},
|
|
72
|
-
enumerable: false,
|
|
73
|
-
configurable: true
|
|
74
|
-
});
|
|
75
|
-
Object.defineProperty(RequestContext.prototype, "api", {
|
|
76
|
-
get: function () {
|
|
77
|
-
return this.host.api;
|
|
78
|
-
},
|
|
79
|
-
enumerable: false,
|
|
80
|
-
configurable: true
|
|
81
|
-
});
|
|
82
|
-
Object.defineProperty(RequestContext.prototype, "baseUrl", {
|
|
83
|
-
get: function () {
|
|
84
|
-
return this.computedBaseUrl;
|
|
85
|
-
},
|
|
86
|
-
enumerable: false,
|
|
87
|
-
configurable: true
|
|
88
|
-
});
|
|
89
|
-
Object.defineProperty(RequestContext.prototype, "responseType", {
|
|
90
|
-
get: function () {
|
|
91
|
-
return this.host.responseType;
|
|
92
|
-
},
|
|
93
|
-
enumerable: false,
|
|
94
|
-
configurable: true
|
|
95
|
-
});
|
|
96
|
-
RequestContext.prototype.initMiddleware = function () {
|
|
97
|
-
var middleware = this.api.middleware;
|
|
98
|
-
for (var i = 0; i < middleware.length; i++) {
|
|
99
|
-
var events = middleware[i];
|
|
100
|
-
var eventTypes = Object.keys(events);
|
|
101
|
-
for (var n = 0; n < eventTypes.length; n++) {
|
|
102
|
-
var eventType = eventTypes[n];
|
|
103
|
-
var eventHandlersForType = this.eventHandlers[eventType] || (this.eventHandlers[eventType] = []);
|
|
104
|
-
var middlewareEventHandlers = events[eventType];
|
|
105
|
-
if (middlewareEventHandlers) {
|
|
106
|
-
eventHandlersForType.push(middlewareEventHandlers);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
RequestContext.prototype.generateKey = function () {
|
|
112
|
-
var key = this.computedPath.trim();
|
|
113
|
-
if (this.computedConfig.queryString) {
|
|
114
|
-
key += "?"
|
|
115
|
-
}
|
|
116
|
-
return key;
|
|
117
|
-
};
|
|
118
|
-
RequestContext.prototype.updateHeaders = function (newHeaders) {
|
|
119
|
-
this.computedConfig.headers = Utils.assign({}, this.computedConfig.headers, newHeaders);
|
|
120
|
-
this.parseRequestBody();
|
|
121
|
-
return this;
|
|
122
|
-
};
|
|
123
|
-
RequestContext.prototype.parseRequestBody = function () {
|
|
124
|
-
var _a;
|
|
125
|
-
if (this.computedConfig.body && this.computedConfig.headers) {
|
|
126
|
-
var contentTypeKey = Object.keys(this.computedConfig.headers).find(function (key) { return key.toLowerCase() === "content-type"; });
|
|
127
|
-
var contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString().split(";")[0].trim());
|
|
128
|
-
if (contentType === "application/x-www-form-urlencoded") {
|
|
129
|
-
var searchParams = new URLSearchParams();
|
|
130
|
-
for (var _i = 0, _b = Object.keys(this.computedConfig.body); _i < _b.length; _i++) {
|
|
131
|
-
var key = _b[_i];
|
|
132
|
-
var value = this.computedConfig.body[key];
|
|
133
|
-
searchParams.append(key, value === null || value === void 0 ? void 0 : value.toString());
|
|
134
|
-
}
|
|
135
|
-
this.parsedBody = searchParams;
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
this.parsedBody = this.computedConfig.body;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
this.parsedBody = undefined;
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
RequestContext.prototype.getParsedBody = function () {
|
|
146
|
-
return this.parsedBody;
|
|
147
|
-
};
|
|
148
|
-
RequestContext.prototype.updateQuery = function (newQuery) {
|
|
149
|
-
this.computedConfig.queryObject = Utils.assign({}, this.computedConfig.queryObject, newQuery);
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
this.
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
this.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
if (
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return RequestContext;
|
|
237
|
-
}());
|
|
238
|
-
exports.default = RequestContext;
|
|
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;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
var Utils = require("./Utils");
|
|
40
|
+
var contextIdCounter = 0;
|
|
41
|
+
var RequestContext = /** @class */ (function () {
|
|
42
|
+
function RequestContext(backend, host, config, computedPath, mocking) {
|
|
43
|
+
this.canceler = null;
|
|
44
|
+
this.response = undefined;
|
|
45
|
+
this.error = null;
|
|
46
|
+
this.cacheInfo = { cached: false, source: null };
|
|
47
|
+
this.cancelled = false;
|
|
48
|
+
this.backend = backend;
|
|
49
|
+
this.id = contextIdCounter++;
|
|
50
|
+
this.host = host;
|
|
51
|
+
this.computedConfig = config;
|
|
52
|
+
Utils.assign({}, this.computedConfig.headers);
|
|
53
|
+
this.computedPath = computedPath;
|
|
54
|
+
this.computedBaseUrl = host.baseUrl;
|
|
55
|
+
this.computedMethod = host.method;
|
|
56
|
+
this.key = this.generateKey();
|
|
57
|
+
this.stats = {
|
|
58
|
+
attempt: 0,
|
|
59
|
+
cached: false,
|
|
60
|
+
};
|
|
61
|
+
this.eventHandlers = {};
|
|
62
|
+
this.mocking = mocking;
|
|
63
|
+
this.validation = host.validation;
|
|
64
|
+
this.initMiddleware();
|
|
65
|
+
this.parseRequestBody();
|
|
66
|
+
this.computedRequestUrl = this.generateRequestUrl();
|
|
67
|
+
}
|
|
68
|
+
Object.defineProperty(RequestContext.prototype, "method", {
|
|
69
|
+
get: function () {
|
|
70
|
+
return this.computedMethod;
|
|
71
|
+
},
|
|
72
|
+
enumerable: false,
|
|
73
|
+
configurable: true
|
|
74
|
+
});
|
|
75
|
+
Object.defineProperty(RequestContext.prototype, "api", {
|
|
76
|
+
get: function () {
|
|
77
|
+
return this.host.api;
|
|
78
|
+
},
|
|
79
|
+
enumerable: false,
|
|
80
|
+
configurable: true
|
|
81
|
+
});
|
|
82
|
+
Object.defineProperty(RequestContext.prototype, "baseUrl", {
|
|
83
|
+
get: function () {
|
|
84
|
+
return this.computedBaseUrl;
|
|
85
|
+
},
|
|
86
|
+
enumerable: false,
|
|
87
|
+
configurable: true
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(RequestContext.prototype, "responseType", {
|
|
90
|
+
get: function () {
|
|
91
|
+
return this.host.responseType;
|
|
92
|
+
},
|
|
93
|
+
enumerable: false,
|
|
94
|
+
configurable: true
|
|
95
|
+
});
|
|
96
|
+
RequestContext.prototype.initMiddleware = function () {
|
|
97
|
+
var middleware = this.api.middleware;
|
|
98
|
+
for (var i = 0; i < middleware.length; i++) {
|
|
99
|
+
var events = middleware[i];
|
|
100
|
+
var eventTypes = Object.keys(events);
|
|
101
|
+
for (var n = 0; n < eventTypes.length; n++) {
|
|
102
|
+
var eventType = eventTypes[n];
|
|
103
|
+
var eventHandlersForType = this.eventHandlers[eventType] || (this.eventHandlers[eventType] = []);
|
|
104
|
+
var middlewareEventHandlers = events[eventType];
|
|
105
|
+
if (middlewareEventHandlers) {
|
|
106
|
+
eventHandlersForType.push(middlewareEventHandlers);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
RequestContext.prototype.generateKey = function () {
|
|
112
|
+
var key = this.computedPath.trim();
|
|
113
|
+
if (this.computedConfig.queryString) {
|
|
114
|
+
key += "?".concat(this.computedConfig.queryString);
|
|
115
|
+
}
|
|
116
|
+
return key;
|
|
117
|
+
};
|
|
118
|
+
RequestContext.prototype.updateHeaders = function (newHeaders) {
|
|
119
|
+
this.computedConfig.headers = Utils.assign({}, this.computedConfig.headers, newHeaders);
|
|
120
|
+
this.parseRequestBody();
|
|
121
|
+
return this;
|
|
122
|
+
};
|
|
123
|
+
RequestContext.prototype.parseRequestBody = function () {
|
|
124
|
+
var _a;
|
|
125
|
+
if (this.computedConfig.body && this.computedConfig.headers) {
|
|
126
|
+
var contentTypeKey = Object.keys(this.computedConfig.headers).find(function (key) { return key.toLowerCase() === "content-type"; });
|
|
127
|
+
var contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString().split(";")[0].trim());
|
|
128
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
129
|
+
var searchParams = new URLSearchParams();
|
|
130
|
+
for (var _i = 0, _b = Object.keys(this.computedConfig.body); _i < _b.length; _i++) {
|
|
131
|
+
var key = _b[_i];
|
|
132
|
+
var value = this.computedConfig.body[key];
|
|
133
|
+
searchParams.append(key, value === null || value === void 0 ? void 0 : value.toString());
|
|
134
|
+
}
|
|
135
|
+
this.parsedBody = searchParams;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
this.parsedBody = this.computedConfig.body;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.parsedBody = undefined;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
RequestContext.prototype.getParsedBody = function () {
|
|
146
|
+
return this.parsedBody;
|
|
147
|
+
};
|
|
148
|
+
RequestContext.prototype.updateQuery = function (newQuery) {
|
|
149
|
+
this.computedConfig.queryObject = Utils.assign({}, this.computedConfig.queryObject, newQuery);
|
|
150
|
+
this.computedRequestUrl = this.generateRequestUrl();
|
|
151
|
+
return this;
|
|
152
|
+
};
|
|
153
|
+
RequestContext.prototype.triggerEvent = function (eventType) {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
155
|
+
var eventHandlers, i, eventHandler, eventResult;
|
|
156
|
+
return __generator(this, function (_a) {
|
|
157
|
+
switch (_a.label) {
|
|
158
|
+
case 0:
|
|
159
|
+
eventHandlers = this.eventHandlers[eventType];
|
|
160
|
+
if (!eventHandlers) return [3 /*break*/, 4];
|
|
161
|
+
i = 0;
|
|
162
|
+
_a.label = 1;
|
|
163
|
+
case 1:
|
|
164
|
+
if (!(i < eventHandlers.length)) return [3 /*break*/, 4];
|
|
165
|
+
eventHandler = eventHandlers[i];
|
|
166
|
+
return [4 /*yield*/, eventHandler(this)];
|
|
167
|
+
case 2:
|
|
168
|
+
eventResult = _a.sent();
|
|
169
|
+
if (eventResult) {
|
|
170
|
+
return [2 /*return*/, eventResult];
|
|
171
|
+
}
|
|
172
|
+
_a.label = 3;
|
|
173
|
+
case 3:
|
|
174
|
+
i++;
|
|
175
|
+
return [3 /*break*/, 1];
|
|
176
|
+
case 4: return [2 /*return*/, undefined];
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
RequestContext.prototype.addCanceller = function (canceler) {
|
|
182
|
+
this.canceler = canceler;
|
|
183
|
+
};
|
|
184
|
+
RequestContext.prototype.cancel = function () {
|
|
185
|
+
this.cancelled = true;
|
|
186
|
+
if (this.canceler) {
|
|
187
|
+
this.canceler();
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
Object.defineProperty(RequestContext.prototype, "requestUrl", {
|
|
191
|
+
get: function () {
|
|
192
|
+
return this.computedRequestUrl;
|
|
193
|
+
},
|
|
194
|
+
enumerable: false,
|
|
195
|
+
configurable: true
|
|
196
|
+
});
|
|
197
|
+
Object.defineProperty(RequestContext.prototype, "path", {
|
|
198
|
+
get: function () {
|
|
199
|
+
return this.computedPath;
|
|
200
|
+
},
|
|
201
|
+
enumerable: false,
|
|
202
|
+
configurable: true
|
|
203
|
+
});
|
|
204
|
+
RequestContext.prototype.updatePath = function (path) {
|
|
205
|
+
this.computedPath = path;
|
|
206
|
+
this.computedRequestUrl = this.generateRequestUrl();
|
|
207
|
+
};
|
|
208
|
+
RequestContext.prototype.updateBaseUrl = function (baseUrl) {
|
|
209
|
+
this.computedBaseUrl = baseUrl;
|
|
210
|
+
this.computedRequestUrl = this.generateRequestUrl();
|
|
211
|
+
};
|
|
212
|
+
RequestContext.prototype.updateMethod = function (method) {
|
|
213
|
+
this.computedMethod = method;
|
|
214
|
+
};
|
|
215
|
+
RequestContext.prototype.generateRequestUrl = function () {
|
|
216
|
+
var path = !this.baseUrl.endsWith("/") ? "".concat(this.baseUrl, "/") : this.baseUrl;
|
|
217
|
+
path += this.computedPath.startsWith("/") ? this.computedPath.substring(1) : this.computedPath;
|
|
218
|
+
var origin = undefined;
|
|
219
|
+
if (typeof window !== "undefined") {
|
|
220
|
+
origin = window.origin;
|
|
221
|
+
}
|
|
222
|
+
if (!origin) {
|
|
223
|
+
if (path.indexOf("://") === -1) {
|
|
224
|
+
path = "https://".concat(path);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
var url = new URL(path, origin);
|
|
228
|
+
if (this.computedConfig.queryString) {
|
|
229
|
+
url.search = this.computedConfig.queryString;
|
|
230
|
+
}
|
|
231
|
+
return url;
|
|
232
|
+
};
|
|
233
|
+
return RequestContext;
|
|
234
|
+
}());
|
|
235
|
+
exports.default = RequestContext;
|
package/cjs/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;
|