@blueprint-ts/core 4.1.0-beta.6 → 5.0.0
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/dist/bulkRequests/BulkRequestSender.js +81 -102
- package/dist/bulkRequests/BulkRequestWrapper.js +16 -26
- package/dist/laravel/pagination/dataDrivers/RequestDriver.js +1 -0
- package/dist/pagination/BasePaginator.js +4 -4
- package/dist/pagination/PageAwarePaginator.js +4 -1
- package/dist/pagination/StatePaginator.js +6 -3
- package/dist/pagination/dataDrivers/ArrayDriver.js +1 -0
- package/dist/pagination/dtos/PaginationDataDto.js +2 -0
- package/dist/pagination/dtos/StatePaginationDataDto.js +1 -0
- package/dist/pagination/frontendDrivers/VueBaseViewDriver.js +2 -0
- package/dist/pagination/frontendDrivers/VuePaginationDriver.js +6 -0
- package/dist/persistenceDrivers/LocalStorageDriver.js +1 -0
- package/dist/persistenceDrivers/MemoryPersistenceDriver.js +2 -1
- package/dist/persistenceDrivers/SessionStorageDriver.js +1 -0
- package/dist/requests/BaseRequest.js +93 -100
- package/dist/requests/ErrorHandler.js +21 -31
- package/dist/requests/RequestErrorRouter.js +12 -25
- package/dist/requests/bodies/BinaryBody.js +2 -0
- package/dist/requests/bodies/FormDataBody.js +1 -0
- package/dist/requests/bodies/JsonBody.js +1 -0
- package/dist/requests/drivers/fetch/FetchDriver.js +26 -26
- package/dist/requests/drivers/fetch/FetchResponse.js +7 -21
- package/dist/requests/drivers/mock/MockRequestDriver.js +190 -169
- package/dist/requests/drivers/mock/MockRequestTestHelpers.js +10 -4
- package/dist/requests/drivers/mock/MockResponseHandler.js +12 -23
- package/dist/requests/drivers/xhr/XMLHttpRequestDriver.js +68 -74
- package/dist/requests/drivers/xhr/XMLHttpRequestResponse.js +9 -21
- package/dist/requests/exceptions/InvalidJsonException.js +1 -0
- package/dist/requests/exceptions/ResponseBodyException.js +1 -0
- package/dist/requests/exceptions/ResponseException.js +1 -0
- package/dist/requests/exceptions/StaleResponseException.js +1 -0
- package/dist/requests/factories/BinaryBodyFactory.js +1 -0
- package/dist/requests/responses/BaseResponse.js +9 -21
- package/dist/requests/responses/BlobResponse.js +1 -0
- package/dist/support/DeferredPromise.js +5 -4
- package/dist/vue/composables/useConfirmDialog.js +7 -18
- package/dist/vue/composables/useGlobalCheckbox.js +55 -66
- package/dist/vue/forms/BaseForm.d.ts +11 -8
- package/dist/vue/forms/BaseForm.js +153 -149
- package/dist/vue/forms/PropertyAwareArray.js +0 -1
- package/dist/vue/forms/PropertyAwareObject.js +4 -2
- package/dist/vue/forms/index.d.ts +2 -2
- package/dist/vue/forms/persistence/types.d.ts +2 -0
- package/dist/vue/forms/validation/rules/BaseRule.js +3 -16
- package/dist/vue/forms/validation/rules/ConfirmedRule.js +2 -0
- package/dist/vue/forms/validation/rules/EmailRule.js +1 -0
- package/dist/vue/forms/validation/rules/JsonRule.js +2 -1
- package/dist/vue/forms/validation/rules/MinRule.js +2 -0
- package/dist/vue/forms/validation/rules/PrecognitiveRule.js +21 -31
- package/dist/vue/forms/validation/rules/RequiredRule.js +1 -0
- package/dist/vue/forms/validation/rules/UrlRule.js +2 -1
- package/dist/vue/requests/loaders/VueRequestBatchLoader.js +3 -3
- package/dist/vue/requests/loaders/VueRequestLoader.js +1 -0
- package/dist/vue/router/routeResourceBinding/RouteResourceBoundView.js +8 -18
- package/dist/vue/router/routeResourceBinding/RouteResourceRequestResolver.js +4 -14
- package/dist/vue/router/routeResourceBinding/defineRoute.js +8 -7
- package/dist/vue/router/routeResourceBinding/installRouteInjection.js +12 -21
- package/dist/vue/router/routeResourceBinding/useRouteResource.js +5 -17
- package/dist/vue/state/State.d.ts +2 -0
- package/dist/vue/state/State.js +24 -11
- package/dist/vue/state/index.d.ts +2 -1
- package/package.json +27 -27
|
@@ -1,12 +1,3 @@
|
|
|
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
1
|
import qs from 'qs';
|
|
11
2
|
import { ErrorHandler } from './ErrorHandler';
|
|
12
3
|
import { RequestEvents } from './RequestEvents.enum';
|
|
@@ -16,17 +7,23 @@ import { RequestConcurrencyMode } from './RequestConcurrencyMode.enum';
|
|
|
16
7
|
import { mergeDeep } from '../support/helpers';
|
|
17
8
|
import { v4 as uuidv4 } from 'uuid';
|
|
18
9
|
export class BaseRequest {
|
|
10
|
+
requestId = uuidv4();
|
|
11
|
+
params = undefined;
|
|
12
|
+
requestBody = undefined;
|
|
13
|
+
requestLoader = undefined;
|
|
14
|
+
abortSignal = undefined;
|
|
15
|
+
concurrencyOptions = undefined;
|
|
16
|
+
additionalHeaders = {};
|
|
17
|
+
instanceRequestDriver = undefined;
|
|
18
|
+
/* @ts-expect-error Ignore generics */
|
|
19
|
+
events = {};
|
|
20
|
+
static defaultBaseUrl;
|
|
21
|
+
static requestDriver;
|
|
22
|
+
static requestLoaderFactory;
|
|
23
|
+
static concurrencySequenceByKey = new Map();
|
|
24
|
+
static concurrencyAbortControllerByKey = new Map();
|
|
25
|
+
static concurrencyInFlightByKey = new Map();
|
|
19
26
|
constructor() {
|
|
20
|
-
this.requestId = uuidv4();
|
|
21
|
-
this.params = undefined;
|
|
22
|
-
this.requestBody = undefined;
|
|
23
|
-
this.requestLoader = undefined;
|
|
24
|
-
this.abortSignal = undefined;
|
|
25
|
-
this.concurrencyOptions = undefined;
|
|
26
|
-
this.additionalHeaders = {};
|
|
27
|
-
this.instanceRequestDriver = undefined;
|
|
28
|
-
/* @ts-expect-error Ignore generics */
|
|
29
|
-
this.events = {};
|
|
30
27
|
if (BaseRequest.requestLoaderFactory !== undefined) {
|
|
31
28
|
this.requestLoader = BaseRequest.requestLoaderFactory.make();
|
|
32
29
|
}
|
|
@@ -71,7 +68,10 @@ export class BaseRequest {
|
|
|
71
68
|
return this;
|
|
72
69
|
}
|
|
73
70
|
setHeaders(headers) {
|
|
74
|
-
this.additionalHeaders =
|
|
71
|
+
this.additionalHeaders = {
|
|
72
|
+
...this.additionalHeaders,
|
|
73
|
+
...headers
|
|
74
|
+
};
|
|
75
75
|
return this;
|
|
76
76
|
}
|
|
77
77
|
getBody() {
|
|
@@ -81,10 +81,9 @@ export class BaseRequest {
|
|
|
81
81
|
return {};
|
|
82
82
|
}
|
|
83
83
|
buildUrl() {
|
|
84
|
-
var _a;
|
|
85
84
|
const hasParams = this.params !== undefined && Object.keys(this.params).length > 0;
|
|
86
85
|
const url = hasParams ? this.url() + '?' + qs.stringify(this.params) : this.url();
|
|
87
|
-
return new URL(url,
|
|
86
|
+
return new URL(url, this.baseUrl() ?? BaseRequest.defaultBaseUrl);
|
|
88
87
|
}
|
|
89
88
|
on(event, handler) {
|
|
90
89
|
if (!this.events[event]) {
|
|
@@ -99,72 +98,71 @@ export class BaseRequest {
|
|
|
99
98
|
}
|
|
100
99
|
this.events[event].forEach((handler) => handler(value));
|
|
101
100
|
}
|
|
102
|
-
send() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
if (previousController) {
|
|
116
|
-
previousController.abort();
|
|
117
|
-
}
|
|
118
|
-
const controller = new AbortController();
|
|
119
|
-
BaseRequest.concurrencyAbortControllerByKey.set(concurrencyKey, controller);
|
|
120
|
-
this.setAbortSignal(controller.signal);
|
|
101
|
+
async send(options = {}) {
|
|
102
|
+
const responseSkeleton = this.getResponse();
|
|
103
|
+
const acceptHeader = responseSkeleton.getAcceptHeader();
|
|
104
|
+
const concurrencyMode = this.concurrencyOptions?.mode ?? RequestConcurrencyMode.ALLOW;
|
|
105
|
+
const concurrencyKey = this.concurrencyOptions?.key ?? this.requestId;
|
|
106
|
+
const useReplace = concurrencyMode === RequestConcurrencyMode.REPLACE || concurrencyMode === RequestConcurrencyMode.REPLACE_LATEST;
|
|
107
|
+
const useLatest = concurrencyMode === RequestConcurrencyMode.LATEST || concurrencyMode === RequestConcurrencyMode.REPLACE_LATEST;
|
|
108
|
+
const sequence = this.bumpConcurrencySequence(concurrencyKey);
|
|
109
|
+
this.incrementConcurrencyInFlight(concurrencyKey);
|
|
110
|
+
if (useReplace) {
|
|
111
|
+
const previousController = BaseRequest.concurrencyAbortControllerByKey.get(concurrencyKey);
|
|
112
|
+
if (previousController) {
|
|
113
|
+
previousController.abort();
|
|
121
114
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
115
|
+
const controller = new AbortController();
|
|
116
|
+
BaseRequest.concurrencyAbortControllerByKey.set(concurrencyKey, controller);
|
|
117
|
+
this.setAbortSignal(controller.signal);
|
|
118
|
+
}
|
|
119
|
+
this.dispatch(RequestEvents.LOADING, true);
|
|
120
|
+
this.requestLoader?.setLoading(true);
|
|
121
|
+
const requestBody = this.requestBody === undefined ? undefined : this.getRequestBodyFactory()?.make(this.requestBody);
|
|
122
|
+
const requestConfig = this.buildRequestConfig(requestBody, concurrencyKey, sequence, useLatest);
|
|
123
|
+
const responseHandler = await this.resolveRequestDriver()
|
|
124
|
+
.send(this.buildUrl(), this.method(), {
|
|
125
|
+
Accept: acceptHeader,
|
|
126
|
+
...this.requestHeaders(),
|
|
127
|
+
...this.additionalHeaders
|
|
128
|
+
}, requestBody, requestConfig)
|
|
129
|
+
.then(async (driverResponseHandler) => {
|
|
130
|
+
if (useLatest && !this.isLatestSequence(concurrencyKey, sequence)) {
|
|
131
|
+
throw new StaleResponseException();
|
|
132
|
+
}
|
|
133
|
+
if ((driverResponseHandler.getStatusCode() ?? 0) >= 400) {
|
|
134
|
+
const handler = new ErrorHandler(driverResponseHandler);
|
|
135
|
+
await handler.handle();
|
|
136
|
+
}
|
|
137
|
+
return driverResponseHandler;
|
|
138
|
+
})
|
|
139
|
+
.catch(async (error) => {
|
|
140
|
+
if (useLatest && !this.isLatestSequence(concurrencyKey, sequence)) {
|
|
141
|
+
throw new StaleResponseException('Stale response ignored', error);
|
|
142
|
+
}
|
|
143
|
+
if (error instanceof StaleResponseException) {
|
|
151
144
|
throw error;
|
|
152
|
-
}))
|
|
153
|
-
.finally(() => {
|
|
154
|
-
var _a;
|
|
155
|
-
const isStale = useLatest && !this.isLatestSequence(concurrencyKey, sequence);
|
|
156
|
-
if (!isStale) {
|
|
157
|
-
this.dispatch(RequestEvents.LOADING, false);
|
|
158
|
-
(_a = this.requestLoader) === null || _a === void 0 ? void 0 : _a.setLoading(false);
|
|
159
|
-
}
|
|
160
|
-
this.decrementConcurrencyInFlight(concurrencyKey);
|
|
161
|
-
});
|
|
162
|
-
if (options.resolveBody === false) {
|
|
163
|
-
return responseHandler;
|
|
164
145
|
}
|
|
165
|
-
|
|
166
|
-
|
|
146
|
+
if (error instanceof ResponseException) {
|
|
147
|
+
const handler = new ErrorHandler(error.getResponse());
|
|
148
|
+
await handler.handle();
|
|
149
|
+
}
|
|
150
|
+
console.error('@blueprint-ts/core: Unknown error received.', error);
|
|
151
|
+
throw error;
|
|
152
|
+
})
|
|
153
|
+
.finally(() => {
|
|
154
|
+
const isStale = useLatest && !this.isLatestSequence(concurrencyKey, sequence);
|
|
155
|
+
if (!isStale) {
|
|
156
|
+
this.dispatch(RequestEvents.LOADING, false);
|
|
157
|
+
this.requestLoader?.setLoading(false);
|
|
158
|
+
}
|
|
159
|
+
this.decrementConcurrencyInFlight(concurrencyKey);
|
|
167
160
|
});
|
|
161
|
+
if (options.resolveBody === false) {
|
|
162
|
+
return responseHandler;
|
|
163
|
+
}
|
|
164
|
+
await responseSkeleton.setResponse(responseHandler);
|
|
165
|
+
return responseSkeleton;
|
|
168
166
|
}
|
|
169
167
|
isLoading() {
|
|
170
168
|
if (!this.requestLoader) {
|
|
@@ -180,18 +178,15 @@ export class BaseRequest {
|
|
|
180
178
|
return this;
|
|
181
179
|
}
|
|
182
180
|
bumpConcurrencySequence(key) {
|
|
183
|
-
|
|
184
|
-
const next = ((_a = BaseRequest.concurrencySequenceByKey.get(key)) !== null && _a !== void 0 ? _a : 0) + 1;
|
|
181
|
+
const next = (BaseRequest.concurrencySequenceByKey.get(key) ?? 0) + 1;
|
|
185
182
|
BaseRequest.concurrencySequenceByKey.set(key, next);
|
|
186
183
|
return next;
|
|
187
184
|
}
|
|
188
185
|
isLatestSequence(key, sequence) {
|
|
189
|
-
|
|
190
|
-
return ((_a = BaseRequest.concurrencySequenceByKey.get(key)) !== null && _a !== void 0 ? _a : 0) === sequence;
|
|
186
|
+
return (BaseRequest.concurrencySequenceByKey.get(key) ?? 0) === sequence;
|
|
191
187
|
}
|
|
192
188
|
incrementConcurrencyInFlight(key) {
|
|
193
|
-
|
|
194
|
-
const next = ((_a = BaseRequest.concurrencyInFlightByKey.get(key)) !== null && _a !== void 0 ? _a : 0) + 1;
|
|
189
|
+
const next = (BaseRequest.concurrencyInFlightByKey.get(key) ?? 0) + 1;
|
|
195
190
|
BaseRequest.concurrencyInFlightByKey.set(key, next);
|
|
196
191
|
}
|
|
197
192
|
decrementConcurrencyInFlight(key) {
|
|
@@ -212,19 +207,21 @@ export class BaseRequest {
|
|
|
212
207
|
return undefined;
|
|
213
208
|
}
|
|
214
209
|
buildRequestConfig(requestBody, concurrencyKey, sequence, useLatest) {
|
|
215
|
-
|
|
216
|
-
const config = (_a = this.getConfig()) !== null && _a !== void 0 ? _a : {};
|
|
210
|
+
const config = this.getConfig() ?? {};
|
|
217
211
|
const onUploadProgress = config.onUploadProgress;
|
|
218
212
|
if (requestBody === undefined) {
|
|
219
213
|
return config;
|
|
220
214
|
}
|
|
221
|
-
return
|
|
222
|
-
|
|
215
|
+
return {
|
|
216
|
+
...config,
|
|
217
|
+
onUploadProgress: (progress) => {
|
|
218
|
+
onUploadProgress?.(progress);
|
|
223
219
|
if (useLatest && !this.isLatestSequence(concurrencyKey, sequence)) {
|
|
224
220
|
return;
|
|
225
221
|
}
|
|
226
222
|
this.dispatch(RequestEvents.UPLOAD_PROGRESS, progress);
|
|
227
|
-
}
|
|
223
|
+
}
|
|
224
|
+
};
|
|
228
225
|
}
|
|
229
226
|
getConfig() {
|
|
230
227
|
return {
|
|
@@ -232,13 +229,9 @@ export class BaseRequest {
|
|
|
232
229
|
};
|
|
233
230
|
}
|
|
234
231
|
resolveRequestDriver() {
|
|
235
|
-
|
|
236
|
-
return (_b = (_a = this.instanceRequestDriver) !== null && _a !== void 0 ? _a : this.getRequestDriver()) !== null && _b !== void 0 ? _b : BaseRequest.requestDriver;
|
|
232
|
+
return this.instanceRequestDriver ?? this.getRequestDriver() ?? BaseRequest.requestDriver;
|
|
237
233
|
}
|
|
238
234
|
getRequestDriver() {
|
|
239
235
|
return undefined;
|
|
240
236
|
}
|
|
241
237
|
}
|
|
242
|
-
BaseRequest.concurrencySequenceByKey = new Map();
|
|
243
|
-
BaseRequest.concurrencyAbortControllerByKey = new Map();
|
|
244
|
-
BaseRequest.concurrencyInFlightByKey = new Map();
|
|
@@ -1,12 +1,3 @@
|
|
|
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
1
|
import { PageExpiredException } from './exceptions/PageExpiredException';
|
|
11
2
|
import { NotFoundException } from './exceptions/NotFoundException';
|
|
12
3
|
import { UnauthorizedException } from './exceptions/UnauthorizedException';
|
|
@@ -31,31 +22,31 @@ import { GatewayTimeoutException } from './exceptions/GatewayTimeoutException';
|
|
|
31
22
|
import { BadRequestException } from './exceptions/BadRequestException';
|
|
32
23
|
import { InvalidJsonException } from './exceptions/InvalidJsonException';
|
|
33
24
|
export class ErrorHandler {
|
|
25
|
+
response;
|
|
26
|
+
body = undefined;
|
|
27
|
+
static handler = undefined;
|
|
34
28
|
constructor(response) {
|
|
35
29
|
this.response = response;
|
|
36
|
-
this.body = undefined;
|
|
37
30
|
}
|
|
38
|
-
handle() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
31
|
+
async handle() {
|
|
32
|
+
// Check if there is a global error handler set
|
|
33
|
+
if (ErrorHandler.handler !== undefined) {
|
|
34
|
+
// If handler returns false, we don't process the error further
|
|
35
|
+
if (ErrorHandler.handler(this.response) === false) {
|
|
36
|
+
console.debug('Skipping further error handling due to global handler returning false.');
|
|
37
|
+
return;
|
|
47
38
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
this.body = await this.response.json();
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new InvalidJsonException(this.response, error);
|
|
45
|
+
}
|
|
46
|
+
if (this.body === undefined) {
|
|
47
|
+
throw new NoResponseReceivedException(this.response);
|
|
48
|
+
}
|
|
49
|
+
this.handleResponseError(this.response, this.body);
|
|
59
50
|
}
|
|
60
51
|
static registerHandler(callback) {
|
|
61
52
|
ErrorHandler.handler = callback;
|
|
@@ -124,4 +115,3 @@ export class ErrorHandler {
|
|
|
124
115
|
throw new ResponseException(response);
|
|
125
116
|
}
|
|
126
117
|
}
|
|
127
|
-
ErrorHandler.handler = undefined;
|
|
@@ -1,17 +1,6 @@
|
|
|
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
1
|
export class RequestErrorRouter {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
this.defaultHandler = undefined;
|
|
14
|
-
}
|
|
2
|
+
handlers = [];
|
|
3
|
+
defaultHandler = undefined;
|
|
15
4
|
on(ctor, handler) {
|
|
16
5
|
this.handlers.push({ ctor, handler: handler });
|
|
17
6
|
return this;
|
|
@@ -20,19 +9,17 @@ export class RequestErrorRouter {
|
|
|
20
9
|
this.defaultHandler = handler;
|
|
21
10
|
return this;
|
|
22
11
|
}
|
|
23
|
-
handle(error) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
yield entry.handler(error);
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (this.defaultHandler !== undefined) {
|
|
32
|
-
yield this.defaultHandler(error);
|
|
12
|
+
async handle(error) {
|
|
13
|
+
for (const entry of this.handlers) {
|
|
14
|
+
if (error instanceof entry.ctor) {
|
|
15
|
+
await entry.handler(error);
|
|
33
16
|
return true;
|
|
34
17
|
}
|
|
35
|
-
|
|
36
|
-
|
|
18
|
+
}
|
|
19
|
+
if (this.defaultHandler !== undefined) {
|
|
20
|
+
await this.defaultHandler(error);
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
37
24
|
}
|
|
38
25
|
}
|
|
@@ -1,12 +1,3 @@
|
|
|
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
1
|
import { ResponseException } from '../../exceptions/ResponseException';
|
|
11
2
|
import { FetchResponse } from './FetchResponse';
|
|
12
3
|
import { RequestMethodEnum } from '../../RequestMethod.enum';
|
|
@@ -16,32 +7,41 @@ var FetchDriverCredentialConfigEnum;
|
|
|
16
7
|
FetchDriverCredentialConfigEnum["INCLUDE"] = "include";
|
|
17
8
|
})(FetchDriverCredentialConfigEnum || (FetchDriverCredentialConfigEnum = {}));
|
|
18
9
|
export class FetchDriver {
|
|
10
|
+
config;
|
|
19
11
|
constructor(config) {
|
|
20
12
|
this.config = config;
|
|
21
13
|
}
|
|
22
|
-
send(url, method, headers, body, requestConfig) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
14
|
+
async send(url, method, headers, body, requestConfig) {
|
|
15
|
+
const mergedConfig = {
|
|
16
|
+
// Global config
|
|
17
|
+
...this.config,
|
|
18
|
+
// Request specific overrides
|
|
19
|
+
...(requestConfig ?? {})
|
|
20
|
+
};
|
|
21
|
+
const mergedHeaders = {
|
|
22
|
+
// Set global headers
|
|
23
|
+
...this.config?.headers,
|
|
24
|
+
// Set headers from the request
|
|
25
|
+
...headers,
|
|
26
|
+
// Set Content-Type header
|
|
27
|
+
...body?.getHeaders()
|
|
28
|
+
};
|
|
29
|
+
const resolvedHeaders = this.resolveHeaders(mergedHeaders);
|
|
30
|
+
const fetchConfig = this.buildRequestConfig(mergedConfig, method, resolvedHeaders, body);
|
|
31
|
+
const response = await fetch(url, fetchConfig);
|
|
32
|
+
const fetchResponse = new FetchResponse(response);
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
throw new ResponseException(fetchResponse);
|
|
35
|
+
}
|
|
36
|
+
return fetchResponse;
|
|
36
37
|
}
|
|
37
38
|
buildRequestConfig(config, method, headers, body) {
|
|
38
|
-
var _a;
|
|
39
39
|
return {
|
|
40
40
|
method: method,
|
|
41
41
|
headers: headers,
|
|
42
42
|
credentials: this.getCorsWithCredentials(config.corsWithCredentials),
|
|
43
|
-
signal:
|
|
44
|
-
body: [RequestMethodEnum.GET, RequestMethodEnum.HEAD].includes(method) ? undefined : body
|
|
43
|
+
signal: config.abortSignal ?? undefined,
|
|
44
|
+
body: [RequestMethodEnum.GET, RequestMethodEnum.HEAD].includes(method) ? undefined : body?.getContent()
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
getCorsWithCredentials(corsWithCredentials) {
|
|
@@ -1,13 +1,5 @@
|
|
|
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
1
|
export class FetchResponse {
|
|
2
|
+
response;
|
|
11
3
|
constructor(response) {
|
|
12
4
|
this.response = response;
|
|
13
5
|
}
|
|
@@ -20,19 +12,13 @@ export class FetchResponse {
|
|
|
20
12
|
getRawResponse() {
|
|
21
13
|
return this.response;
|
|
22
14
|
}
|
|
23
|
-
json() {
|
|
24
|
-
return
|
|
25
|
-
return yield this.response.json();
|
|
26
|
-
});
|
|
15
|
+
async json() {
|
|
16
|
+
return await this.response.json();
|
|
27
17
|
}
|
|
28
|
-
text() {
|
|
29
|
-
return
|
|
30
|
-
return yield this.response.text();
|
|
31
|
-
});
|
|
18
|
+
async text() {
|
|
19
|
+
return await this.response.text();
|
|
32
20
|
}
|
|
33
|
-
blob() {
|
|
34
|
-
return
|
|
35
|
-
return yield this.response.blob();
|
|
36
|
-
});
|
|
21
|
+
async blob() {
|
|
22
|
+
return await this.response.blob();
|
|
37
23
|
}
|
|
38
24
|
}
|