@sevtech/kit 21.1.1 → 21.2.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.
@@ -8,7 +8,7 @@ import { NgTemplateOutlet } from '@angular/common';
8
8
  import { SlotOutletDirective, HighlightPipe } from '@sevtech/kit/ui';
9
9
  import { cssDimensionAttribute, htmlBooleanAttribute, defineKitFeature, resolveConfigProviders, collectFeatureProviders } from '@sevtech/kit/ng-misc';
10
10
  import { IS_MOBILE_BREAKPOINT } from '@sevtech/kit/browser';
11
- import { HttpClient } from '@angular/common/http';
11
+ import { HttpErrorResponse, HttpClient } from '@angular/common/http';
12
12
  import * as i1 from '@angular/cdk/overlay';
13
13
  import { ScrollStrategyOptions, OverlayContainer, OverlayModule } from '@angular/cdk/overlay';
14
14
 
@@ -116,6 +116,44 @@ var RequestType;
116
116
  RequestType[RequestType["Create"] = 2] = "Create";
117
117
  })(RequestType || (RequestType = {}));
118
118
 
119
+ class YodaError extends Error {
120
+ constructor(query, causeOrType) {
121
+ const reason = computeReason(causeOrType);
122
+ super(computeMessage(reason, query, causeOrType), {
123
+ cause: reason === 'request' ? causeOrType : undefined,
124
+ });
125
+ this.name = 'YodaError';
126
+ this.query = query;
127
+ this.reason = reason;
128
+ }
129
+ getPayload() {
130
+ return {
131
+ query: this.query,
132
+ reason: this.reason,
133
+ };
134
+ }
135
+ }
136
+ function computeReason(cause) {
137
+ if (RequestType.Create === cause)
138
+ return 'create';
139
+ if (RequestType.Get === cause)
140
+ return 'unresolve';
141
+ return cause instanceof Error || cause instanceof HttpErrorResponse ? 'request' : 'unknown';
142
+ }
143
+ function computeMessage(reason, query, cause) {
144
+ switch (reason) {
145
+ case 'create':
146
+ return `[YODA]: creating "${query}" returned no option`;
147
+ case 'unresolve':
148
+ return `[YODA]: form value "${query}" did not resolve`;
149
+ default: {
150
+ return cause instanceof Error || cause instanceof HttpErrorResponse
151
+ ? `[YODA]: request "${query}" failed — ${cause.message}`
152
+ : '[YODA]: unknown error';
153
+ }
154
+ }
155
+ }
156
+
119
157
  class ApiService {
120
158
  constructor() {
121
159
  this.config = inject(CONFIG);
@@ -298,7 +336,11 @@ class StateService {
298
336
  this.stateSource.set({ status: 'loading' });
299
337
  return this.api.request(request);
300
338
  }).pipe(tap((options) => this.apply(request, options, fromSearch)), catchError((error) => {
301
- this.stateSource.set({ status: 'error', request, error });
339
+ this.stateSource.set({
340
+ request,
341
+ status: 'error',
342
+ error: error instanceof YodaError ? error : new YodaError(request.query, error),
343
+ });
302
344
  return EMPTY;
303
345
  }));
304
346
  }
@@ -308,8 +350,9 @@ class StateService {
308
350
  this.optionsSource.set(options);
309
351
  }
310
352
  applySelection(request, [option]) {
311
- if (isNil(option))
312
- return;
353
+ if (isNil(option)) {
354
+ throw new YodaError(request.query, request.type);
355
+ }
313
356
  if (request.type === RequestType.Create) {
314
357
  this.optionsSource.update((current) => [option, ...(current ?? [])]);
315
358
  this.createdSource.set(option);
@@ -1078,7 +1121,7 @@ class Yoda extends BaseDirective {
1078
1121
  effect(() => this.emitOptionCreated(this.state.created$$()));
1079
1122
  effect(() => this.syncSelection(this.state.selected$$()));
1080
1123
  effect(() => this.autoSelectSingle(this.state.options$$()));
1081
- effect(() => this.syncRequestError(this.hasRequestError$$()));
1124
+ effect(() => this.syncRequestError());
1082
1125
  effect(() => this.emitRequestError());
1083
1126
  }
1084
1127
  writeValue(value) {
@@ -1087,6 +1130,8 @@ class Yoda extends BaseDirective {
1087
1130
  this.typed$$.set('');
1088
1131
  return this.state.clear();
1089
1132
  }
1133
+ /* Стор обязан совпасть с формой до ответа и эффект примет пустоту за очистку */
1134
+ this.state.select({ kind: 'option', value, label: '' });
1090
1135
  this.state.resolve(value, this.requestOptions$$());
1091
1136
  }
1092
1137
  registerOnChange(onChange) {
@@ -1207,13 +1252,14 @@ class Yoda extends BaseDirective {
1207
1252
  this.loadedParent = parent;
1208
1253
  this.load(isDefined(parent) ? String(parent) : this.preloadQuery(), this.preloadType());
1209
1254
  }
1210
- syncRequestError(hasError) {
1255
+ syncRequestError() {
1211
1256
  const control = this.control$$();
1212
1257
  if (isNil(control))
1213
1258
  return;
1259
+ const state = this.state.requestState$$();
1214
1260
  const errors = { ...(control.errors ?? {}) };
1215
- if (hasError) {
1216
- errors[REQUEST_ERROR] = true;
1261
+ if (state.status === 'error') {
1262
+ errors[REQUEST_ERROR] = state.error.getPayload();
1217
1263
  }
1218
1264
  else {
1219
1265
  delete errors[REQUEST_ERROR];
@@ -1335,5 +1381,5 @@ function mergeMessages(messages) {
1335
1381
  * Generated bundle index. Do not edit.
1336
1382
  */
1337
1383
 
1338
- export { Yoda, YodaChain, RequestType as YodaRequestType, provideYoda, withIcons, withLabelFormatters, withMessages, withZIndex };
1384
+ export { Yoda, YodaChain, YodaError, RequestType as YodaRequestType, provideYoda, withIcons, withLabelFormatters, withMessages, withZIndex };
1339
1385
  //# sourceMappingURL=ui-yoda.mjs.map