@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.
Files changed (62) hide show
  1. package/dist/bulkRequests/BulkRequestSender.js +81 -102
  2. package/dist/bulkRequests/BulkRequestWrapper.js +16 -26
  3. package/dist/laravel/pagination/dataDrivers/RequestDriver.js +1 -0
  4. package/dist/pagination/BasePaginator.js +4 -4
  5. package/dist/pagination/PageAwarePaginator.js +4 -1
  6. package/dist/pagination/StatePaginator.js +6 -3
  7. package/dist/pagination/dataDrivers/ArrayDriver.js +1 -0
  8. package/dist/pagination/dtos/PaginationDataDto.js +2 -0
  9. package/dist/pagination/dtos/StatePaginationDataDto.js +1 -0
  10. package/dist/pagination/frontendDrivers/VueBaseViewDriver.js +2 -0
  11. package/dist/pagination/frontendDrivers/VuePaginationDriver.js +6 -0
  12. package/dist/persistenceDrivers/LocalStorageDriver.js +1 -0
  13. package/dist/persistenceDrivers/MemoryPersistenceDriver.js +2 -1
  14. package/dist/persistenceDrivers/SessionStorageDriver.js +1 -0
  15. package/dist/requests/BaseRequest.js +93 -100
  16. package/dist/requests/ErrorHandler.js +21 -31
  17. package/dist/requests/RequestErrorRouter.js +12 -25
  18. package/dist/requests/bodies/BinaryBody.js +2 -0
  19. package/dist/requests/bodies/FormDataBody.js +1 -0
  20. package/dist/requests/bodies/JsonBody.js +1 -0
  21. package/dist/requests/drivers/fetch/FetchDriver.js +26 -26
  22. package/dist/requests/drivers/fetch/FetchResponse.js +7 -21
  23. package/dist/requests/drivers/mock/MockRequestDriver.js +190 -169
  24. package/dist/requests/drivers/mock/MockRequestTestHelpers.js +10 -4
  25. package/dist/requests/drivers/mock/MockResponseHandler.js +12 -23
  26. package/dist/requests/drivers/xhr/XMLHttpRequestDriver.js +68 -74
  27. package/dist/requests/drivers/xhr/XMLHttpRequestResponse.js +9 -21
  28. package/dist/requests/exceptions/InvalidJsonException.js +1 -0
  29. package/dist/requests/exceptions/ResponseBodyException.js +1 -0
  30. package/dist/requests/exceptions/ResponseException.js +1 -0
  31. package/dist/requests/exceptions/StaleResponseException.js +1 -0
  32. package/dist/requests/factories/BinaryBodyFactory.js +1 -0
  33. package/dist/requests/responses/BaseResponse.js +9 -21
  34. package/dist/requests/responses/BlobResponse.js +1 -0
  35. package/dist/support/DeferredPromise.js +5 -4
  36. package/dist/vue/composables/useConfirmDialog.js +7 -18
  37. package/dist/vue/composables/useGlobalCheckbox.js +55 -66
  38. package/dist/vue/forms/BaseForm.d.ts +11 -8
  39. package/dist/vue/forms/BaseForm.js +153 -149
  40. package/dist/vue/forms/PropertyAwareArray.js +0 -1
  41. package/dist/vue/forms/PropertyAwareObject.js +4 -2
  42. package/dist/vue/forms/index.d.ts +2 -2
  43. package/dist/vue/forms/persistence/types.d.ts +2 -0
  44. package/dist/vue/forms/validation/rules/BaseRule.js +3 -16
  45. package/dist/vue/forms/validation/rules/ConfirmedRule.js +2 -0
  46. package/dist/vue/forms/validation/rules/EmailRule.js +1 -0
  47. package/dist/vue/forms/validation/rules/JsonRule.js +2 -1
  48. package/dist/vue/forms/validation/rules/MinRule.js +2 -0
  49. package/dist/vue/forms/validation/rules/PrecognitiveRule.js +21 -31
  50. package/dist/vue/forms/validation/rules/RequiredRule.js +1 -0
  51. package/dist/vue/forms/validation/rules/UrlRule.js +2 -1
  52. package/dist/vue/requests/loaders/VueRequestBatchLoader.js +3 -3
  53. package/dist/vue/requests/loaders/VueRequestLoader.js +1 -0
  54. package/dist/vue/router/routeResourceBinding/RouteResourceBoundView.js +8 -18
  55. package/dist/vue/router/routeResourceBinding/RouteResourceRequestResolver.js +4 -14
  56. package/dist/vue/router/routeResourceBinding/defineRoute.js +8 -7
  57. package/dist/vue/router/routeResourceBinding/installRouteInjection.js +12 -21
  58. package/dist/vue/router/routeResourceBinding/useRouteResource.js +5 -17
  59. package/dist/vue/state/State.d.ts +2 -0
  60. package/dist/vue/state/State.js +24 -11
  61. package/dist/vue/state/index.d.ts +2 -1
  62. 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 = Object.assign(Object.assign({}, this.additionalHeaders), headers);
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, (_a = this.baseUrl()) !== null && _a !== void 0 ? _a : BaseRequest.defaultBaseUrl);
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
- return __awaiter(this, arguments, void 0, function* (options = {}) {
104
- var _a, _b, _c, _d, _e, _f;
105
- const responseSkeleton = this.getResponse();
106
- const acceptHeader = responseSkeleton.getAcceptHeader();
107
- const concurrencyMode = (_b = (_a = this.concurrencyOptions) === null || _a === void 0 ? void 0 : _a.mode) !== null && _b !== void 0 ? _b : RequestConcurrencyMode.ALLOW;
108
- const concurrencyKey = (_d = (_c = this.concurrencyOptions) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : this.requestId;
109
- const useReplace = concurrencyMode === RequestConcurrencyMode.REPLACE || concurrencyMode === RequestConcurrencyMode.REPLACE_LATEST;
110
- const useLatest = concurrencyMode === RequestConcurrencyMode.LATEST || concurrencyMode === RequestConcurrencyMode.REPLACE_LATEST;
111
- const sequence = this.bumpConcurrencySequence(concurrencyKey);
112
- this.incrementConcurrencyInFlight(concurrencyKey);
113
- if (useReplace) {
114
- const previousController = BaseRequest.concurrencyAbortControllerByKey.get(concurrencyKey);
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
- this.dispatch(RequestEvents.LOADING, true);
123
- (_e = this.requestLoader) === null || _e === void 0 ? void 0 : _e.setLoading(true);
124
- const requestBody = this.requestBody === undefined ? undefined : (_f = this.getRequestBodyFactory()) === null || _f === void 0 ? void 0 : _f.make(this.requestBody);
125
- const requestConfig = this.buildRequestConfig(requestBody, concurrencyKey, sequence, useLatest);
126
- const responseHandler = yield this.resolveRequestDriver()
127
- .send(this.buildUrl(), this.method(), Object.assign(Object.assign({ Accept: acceptHeader }, this.requestHeaders()), this.additionalHeaders), requestBody, requestConfig)
128
- .then((driverResponseHandler) => __awaiter(this, void 0, void 0, function* () {
129
- var _a;
130
- if (useLatest && !this.isLatestSequence(concurrencyKey, sequence)) {
131
- throw new StaleResponseException();
132
- }
133
- if (((_a = driverResponseHandler.getStatusCode()) !== null && _a !== void 0 ? _a : 0) >= 400) {
134
- const handler = new ErrorHandler(driverResponseHandler);
135
- yield handler.handle();
136
- }
137
- return driverResponseHandler;
138
- }))
139
- .catch((error) => __awaiter(this, void 0, void 0, function* () {
140
- if (useLatest && !this.isLatestSequence(concurrencyKey, sequence)) {
141
- throw new StaleResponseException('Stale response ignored', error);
142
- }
143
- if (error instanceof StaleResponseException) {
144
- throw error;
145
- }
146
- if (error instanceof ResponseException) {
147
- const handler = new ErrorHandler(error.getResponse());
148
- yield handler.handle();
149
- }
150
- console.error('@blueprint-ts/core: Unknown error received.', error);
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
- yield responseSkeleton.setResponse(responseHandler);
166
- return responseSkeleton;
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
- var _a;
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
- var _a;
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
- var _a;
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
- var _a;
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 Object.assign(Object.assign({}, config), { onUploadProgress: (progress) => {
222
- onUploadProgress === null || onUploadProgress === void 0 ? void 0 : onUploadProgress(progress);
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
- var _a, _b;
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
- return __awaiter(this, void 0, void 0, function* () {
40
- // Check if there is a global error handler set
41
- if (ErrorHandler.handler !== undefined) {
42
- // If handler returns false, we don't process the error further
43
- if (ErrorHandler.handler(this.response) === false) {
44
- console.debug('Skipping further error handling due to global handler returning false.');
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
- try {
49
- this.body = yield this.response.json();
50
- }
51
- catch (error) {
52
- throw new InvalidJsonException(this.response, error);
53
- }
54
- if (this.body === undefined) {
55
- throw new NoResponseReceivedException(this.response);
56
- }
57
- this.handleResponseError(this.response, this.body);
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
- constructor() {
12
- this.handlers = [];
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
- return __awaiter(this, void 0, void 0, function* () {
25
- for (const entry of this.handlers) {
26
- if (error instanceof entry.ctor) {
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
- return false;
36
- });
18
+ }
19
+ if (this.defaultHandler !== undefined) {
20
+ await this.defaultHandler(error);
21
+ return true;
22
+ }
23
+ return false;
37
24
  }
38
25
  }
@@ -1,4 +1,6 @@
1
1
  export class BinaryBody {
2
+ data;
3
+ contentType;
2
4
  constructor(data, contentType) {
3
5
  this.data = data;
4
6
  this.contentType = contentType;
@@ -1,5 +1,6 @@
1
1
  import { isObject } from '../../support/helpers';
2
2
  export class FormDataBody {
3
+ data;
3
4
  constructor(data) {
4
5
  this.data = this.toFormData(data);
5
6
  }
@@ -1,4 +1,5 @@
1
1
  export class JsonBody {
2
+ data;
2
3
  constructor(data) {
3
4
  this.data = data;
4
5
  }
@@ -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
- return __awaiter(this, void 0, void 0, function* () {
24
- var _a;
25
- const mergedConfig = Object.assign(Object.assign({}, this.config), (requestConfig !== null && requestConfig !== void 0 ? requestConfig : {}));
26
- const mergedHeaders = Object.assign(Object.assign(Object.assign({}, (_a = this.config) === null || _a === void 0 ? void 0 : _a.headers), headers), body === null || body === void 0 ? void 0 : body.getHeaders());
27
- const resolvedHeaders = this.resolveHeaders(mergedHeaders);
28
- const fetchConfig = this.buildRequestConfig(mergedConfig, method, resolvedHeaders, body);
29
- const response = yield fetch(url, fetchConfig);
30
- const fetchResponse = new FetchResponse(response);
31
- if (!response.ok) {
32
- throw new ResponseException(fetchResponse);
33
- }
34
- return fetchResponse;
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: (_a = config.abortSignal) !== null && _a !== void 0 ? _a : undefined,
44
- body: [RequestMethodEnum.GET, RequestMethodEnum.HEAD].includes(method) ? undefined : body === null || body === void 0 ? void 0 : body.getContent()
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 __awaiter(this, void 0, void 0, function* () {
25
- return yield this.response.json();
26
- });
15
+ async json() {
16
+ return await this.response.json();
27
17
  }
28
- text() {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- return yield this.response.text();
31
- });
18
+ async text() {
19
+ return await this.response.text();
32
20
  }
33
- blob() {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- return yield this.response.blob();
36
- });
21
+ async blob() {
22
+ return await this.response.blob();
37
23
  }
38
24
  }