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.
Files changed (122) hide show
  1. package/README.md +1 -1
  2. package/cjs/Api.d.ts +35 -35
  3. package/cjs/Api.js +140 -144
  4. package/cjs/ApiConstants.d.ts +58 -58
  5. package/cjs/ApiConstants.js +55 -55
  6. package/cjs/ApiTypes.d.ts +103 -103
  7. package/cjs/ApiTypes.js +4 -4
  8. package/cjs/ApiUtils.d.ts +6 -6
  9. package/cjs/ApiUtils.js +51 -53
  10. package/cjs/Endpoint.d.ts +52 -52
  11. package/cjs/Endpoint.js +104 -108
  12. package/cjs/EndpointBuilder.d.ts +14 -14
  13. package/cjs/EndpointBuilder.js +46 -46
  14. package/cjs/MockingTypes.d.ts +30 -30
  15. package/cjs/MockingTypes.js +2 -2
  16. package/cjs/QueryHandling.d.ts +3 -3
  17. package/cjs/QueryHandling.js +24 -24
  18. package/cjs/RequestConfig.d.ts +2 -2
  19. package/cjs/RequestConfig.js +74 -74
  20. package/cjs/RequestContext.d.ts +48 -48
  21. package/cjs/RequestContext.js +235 -238
  22. package/cjs/RequestError.d.ts +30 -30
  23. package/cjs/RequestError.js +47 -47
  24. package/cjs/Requester.d.ts +3 -3
  25. package/cjs/Requester.js +335 -336
  26. package/cjs/TextDecoding.d.ts +1 -1
  27. package/cjs/TextDecoding.js +152 -139
  28. package/cjs/UtilTypes.d.ts +5 -5
  29. package/cjs/UtilTypes.js +2 -2
  30. package/cjs/Utils.d.ts +15 -15
  31. package/cjs/Utils.js +79 -77
  32. package/cjs/Validation.d.ts +8 -8
  33. package/cjs/Validation.js +2 -2
  34. package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
  35. package/cjs/backend/AxiosRequestBackend.js +99 -99
  36. package/cjs/backend/FetchRequestBackend.d.ts +15 -15
  37. package/cjs/backend/FetchRequestBackend.js +203 -202
  38. package/cjs/backend/MockRequestBackend.d.ts +11 -11
  39. package/cjs/backend/MockRequestBackend.js +164 -164
  40. package/cjs/backend/RequestBackend.d.ts +19 -19
  41. package/cjs/backend/RequestBackend.js +2 -2
  42. package/cjs/cache/CacheBackend.d.ts +6 -6
  43. package/cjs/cache/CacheBackend.js +2 -2
  44. package/cjs/cache/Caching.d.ts +10 -10
  45. package/cjs/cache/Caching.js +88 -88
  46. package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
  47. package/cjs/cache/LocalForageCacheBackend.js +24 -24
  48. package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
  49. package/cjs/cache/LocalStorageCacheBackend.js +77 -77
  50. package/cjs/index.d.ts +13 -13
  51. package/cjs/index.js +41 -41
  52. package/cjs/middleware/CacheMiddleware.d.ts +7 -7
  53. package/cjs/middleware/CacheMiddleware.js +107 -107
  54. package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
  55. package/cjs/middleware/LoggingMiddleware.js +82 -88
  56. package/cjs/util/retry/index.d.ts +3 -3
  57. package/cjs/util/retry/index.js +52 -52
  58. package/cjs/util/retry/lib/retry.d.ts +3 -3
  59. package/cjs/util/retry/lib/retry.js +50 -54
  60. package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
  61. package/cjs/util/retry/lib/retryOperation.js +130 -134
  62. package/esm/Api.d.ts +35 -35
  63. package/esm/Api.js +92 -98
  64. package/esm/ApiConstants.d.ts +58 -58
  65. package/esm/ApiConstants.js +52 -52
  66. package/esm/ApiTypes.d.ts +103 -103
  67. package/esm/ApiTypes.js +1 -1
  68. package/esm/ApiUtils.d.ts +6 -6
  69. package/esm/ApiUtils.js +43 -45
  70. package/esm/Endpoint.d.ts +52 -52
  71. package/esm/Endpoint.js +66 -70
  72. package/esm/EndpointBuilder.d.ts +14 -14
  73. package/esm/EndpointBuilder.js +31 -31
  74. package/esm/MockingTypes.d.ts +30 -30
  75. package/esm/MockingTypes.js +1 -1
  76. package/esm/QueryHandling.d.ts +3 -3
  77. package/esm/QueryHandling.js +19 -19
  78. package/esm/RequestConfig.d.ts +2 -2
  79. package/esm/RequestConfig.js +56 -56
  80. package/esm/RequestContext.d.ts +48 -48
  81. package/esm/RequestContext.js +167 -170
  82. package/esm/RequestError.d.ts +30 -30
  83. package/esm/RequestError.js +42 -42
  84. package/esm/Requester.d.ts +3 -3
  85. package/esm/Requester.js +252 -253
  86. package/esm/TextDecoding.d.ts +1 -1
  87. package/esm/TextDecoding.js +148 -135
  88. package/esm/UtilTypes.d.ts +5 -5
  89. package/esm/UtilTypes.js +1 -1
  90. package/esm/Utils.d.ts +15 -15
  91. package/esm/Utils.js +66 -68
  92. package/esm/Validation.d.ts +8 -8
  93. package/esm/Validation.js +1 -1
  94. package/esm/backend/AxiosRequestBackend.d.ts +13 -13
  95. package/esm/backend/AxiosRequestBackend.js +62 -62
  96. package/esm/backend/FetchRequestBackend.d.ts +15 -15
  97. package/esm/backend/FetchRequestBackend.js +136 -136
  98. package/esm/backend/MockRequestBackend.d.ts +11 -11
  99. package/esm/backend/MockRequestBackend.js +116 -116
  100. package/esm/backend/RequestBackend.d.ts +19 -19
  101. package/esm/backend/RequestBackend.js +1 -1
  102. package/esm/cache/CacheBackend.d.ts +6 -6
  103. package/esm/cache/CacheBackend.js +1 -1
  104. package/esm/cache/Caching.d.ts +10 -10
  105. package/esm/cache/Caching.js +39 -39
  106. package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
  107. package/esm/cache/LocalForageCacheBackend.js +20 -20
  108. package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
  109. package/esm/cache/LocalStorageCacheBackend.js +32 -32
  110. package/esm/index.d.ts +13 -13
  111. package/esm/index.js +14 -15
  112. package/esm/middleware/CacheMiddleware.d.ts +7 -7
  113. package/esm/middleware/CacheMiddleware.js +58 -58
  114. package/esm/middleware/LoggingMiddleware.d.ts +6 -6
  115. package/esm/middleware/LoggingMiddleware.js +77 -83
  116. package/esm/util/retry/index.d.ts +3 -3
  117. package/esm/util/retry/index.js +50 -50
  118. package/esm/util/retry/lib/retry.d.ts +3 -3
  119. package/esm/util/retry/lib/retry.js +35 -39
  120. package/esm/util/retry/lib/retryOperation.d.ts +27 -27
  121. package/esm/util/retry/lib/retryOperation.js +126 -132
  122. package/package.json +19 -34
@@ -1,48 +1,48 @@
1
- import { ApiResponse, Body, ComputedRequestConfig, EventResult, Headers, Params, Query, RequestCacheInfo, RequestContextStats, RequestEventHandlers, RequestHost } from "./ApiTypes";
2
- import { Api } from "./Api";
3
- import { RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
4
- import { EndpointMockingConfig } from "./MockingTypes";
5
- import { RequestError } from "./RequestError";
6
- import RequestBackend from "./backend/RequestBackend";
7
- import { Validation } from "./Validation";
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
+ }
@@ -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 += "?" + 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
- return this;
151
- };
152
- RequestContext.prototype.triggerEvent = function (eventType) {
153
- return __awaiter(this, void 0, void 0, function () {
154
- var eventHandlers, i, eventHandler, eventResult;
155
- return __generator(this, function (_a) {
156
- switch (_a.label) {
157
- case 0:
158
- eventHandlers = this.eventHandlers[eventType];
159
- if (!eventHandlers) return [3 /*break*/, 4];
160
- i = 0;
161
- _a.label = 1;
162
- case 1:
163
- if (!(i < eventHandlers.length)) return [3 /*break*/, 4];
164
- eventHandler = eventHandlers[i];
165
- return [4 /*yield*/, eventHandler(this)];
166
- case 2:
167
- eventResult = _a.sent();
168
- if (eventResult) {
169
- return [2 /*return*/, eventResult];
170
- }
171
- _a.label = 3;
172
- case 3:
173
- i++;
174
- return [3 /*break*/, 1];
175
- case 4: return [2 /*return*/, undefined];
176
- }
177
- });
178
- });
179
- };
180
- RequestContext.prototype.addCanceller = function (canceler) {
181
- this.canceler = canceler;
182
- };
183
- RequestContext.prototype.cancel = function () {
184
- this.cancelled = true;
185
- if (this.canceler) {
186
- this.canceler();
187
- }
188
- };
189
- Object.defineProperty(RequestContext.prototype, "requestUrl", {
190
- get: function () {
191
- return this.computedRequestUrl;
192
- },
193
- enumerable: false,
194
- configurable: true
195
- });
196
- Object.defineProperty(RequestContext.prototype, "path", {
197
- get: function () {
198
- return this.computedPath;
199
- },
200
- enumerable: false,
201
- configurable: true
202
- });
203
- RequestContext.prototype.updatePath = function (path) {
204
- this.computedPath = path;
205
- this.computedRequestUrl = this.generateRequestUrl();
206
- };
207
- RequestContext.prototype.updateBaseUrl = function (baseUrl) {
208
- this.computedBaseUrl = baseUrl;
209
- this.computedRequestUrl = this.generateRequestUrl();
210
- };
211
- RequestContext.prototype.updateMethod = function (method) {
212
- this.computedMethod = method;
213
- };
214
- RequestContext.prototype.generateRequestUrl = function () {
215
- var path = !this.baseUrl.endsWith("/")
216
- ? this.baseUrl + "/"
217
- : this.baseUrl;
218
- path += this.computedPath.startsWith("/")
219
- ? this.computedPath.substring(1)
220
- : this.computedPath;
221
- var origin = undefined;
222
- if (typeof window !== "undefined") {
223
- origin = window.origin;
224
- }
225
- if (!origin) {
226
- if (path.indexOf("://") === -1) {
227
- path = "https://".concat(path);
228
- }
229
- }
230
- var url = new URL(path, origin);
231
- if (this.computedConfig.queryString) {
232
- url.search = this.computedConfig.queryString;
233
- }
234
- return url;
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;
@@ -1,30 +1,30 @@
1
- import { ApiResponse } from "./ApiTypes";
2
- import { EnumOf } from "./Utils";
3
- import RequestContext from "./RequestContext";
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 declare 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;
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;