@juice-js/core 1.0.0-alpha.9 → 1.2.0-alpha.1
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/fesm2022/juice-js-core.mjs +360 -38
- package/fesm2022/juice-js-core.mjs.map +1 -1
- package/package.json +5 -6
- package/types/juice-js-core.d.ts +314 -0
- package/esm2022/juice-js-core.mjs +0 -5
- package/esm2022/lib/dialog/async-dialog.service.mjs +0 -14
- package/esm2022/lib/extensions/index.mjs +0 -2
- package/esm2022/lib/extensions/string.extensions.mjs +0 -14
- package/esm2022/lib/menu/menu-item.mjs +0 -53
- package/esm2022/lib/menu/menu-loader.mjs +0 -96
- package/esm2022/lib/menu/menu.module.mjs +0 -69
- package/esm2022/lib/menu/menu.service.mjs +0 -40
- package/esm2022/lib/menu/utils.mjs +0 -15
- package/esm2022/lib/routing/utils.mjs +0 -9
- package/esm2022/lib/tenant/resolvers/config-resolver.service.mjs +0 -29
- package/esm2022/lib/tenant/resolvers/storage-resolver.service.mjs +0 -45
- package/esm2022/lib/tenant/tenant-info.mjs +0 -2
- package/esm2022/lib/tenant/tenant-resolver.service.mjs +0 -17
- package/esm2022/lib/tenant/tenant.module.mjs +0 -34
- package/esm2022/lib/tenant/tenant.service.mjs +0 -63
- package/esm2022/public-api.mjs +0 -13
- package/index.d.ts +0 -5
- package/lib/dialog/async-dialog.service.d.ts +0 -9
- package/lib/extensions/index.d.ts +0 -1
- package/lib/extensions/string.extensions.d.ts +0 -9
- package/lib/menu/menu-item.d.ts +0 -35
- package/lib/menu/menu-loader.d.ts +0 -9
- package/lib/menu/menu.module.d.ts +0 -42
- package/lib/menu/menu.service.d.ts +0 -15
- package/lib/menu/utils.d.ts +0 -2
- package/lib/routing/utils.d.ts +0 -3
- package/lib/tenant/resolvers/config-resolver.service.d.ts +0 -9
- package/lib/tenant/resolvers/storage-resolver.service.d.ts +0 -15
- package/lib/tenant/tenant-info.d.ts +0 -9
- package/lib/tenant/tenant-resolver.service.d.ts +0 -11
- package/lib/tenant/tenant.module.d.ts +0 -9
- package/lib/tenant/tenant.service.d.ts +0 -18
- package/public-api.d.ts +0 -9
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { InjectionToken, Injectable, NgModule, inject, Optional, ɵisPromise, createNgModule, Type, Directive } from '@angular/core';
|
|
2
|
+
import { InjectionToken, Injectable, NgModule, inject, Optional, ɵisPromise as _isPromise, createNgModule, Type, Directive, Inject } from '@angular/core';
|
|
3
3
|
import { firstValueFrom } from 'rxjs/internal/firstValueFrom';
|
|
4
|
-
import { Observable, isObservable, from, of, firstValueFrom as firstValueFrom$1 } from 'rxjs';
|
|
4
|
+
import { Observable, isObservable, from, of, firstValueFrom as firstValueFrom$1, throwError, retry, timer, tap, catchError, finalize, shareReplay } from 'rxjs';
|
|
5
5
|
import { map, mergeMap } from 'rxjs/operators';
|
|
6
6
|
import { ROUTES } from '@angular/router';
|
|
7
7
|
import * as i1 from '@angular/material/dialog';
|
|
8
|
+
import { HttpContextToken, HttpContext, HttpErrorResponse, HttpResponse, HTTP_INTERCEPTORS } from '@angular/common/http';
|
|
8
9
|
|
|
9
10
|
const TENANT_RESOLVER = new InjectionToken('TENANT_RESOLVER');
|
|
10
11
|
class TenantResolverService {
|
|
11
12
|
constructor() {
|
|
12
13
|
this.priority = 0;
|
|
13
14
|
}
|
|
14
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
15
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
15
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantResolverService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
16
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantResolverService, providedIn: 'root' }); }
|
|
16
17
|
}
|
|
17
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
18
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantResolverService, decorators: [{
|
|
18
19
|
type: Injectable,
|
|
19
20
|
args: [{
|
|
20
21
|
providedIn: 'root'
|
|
@@ -67,11 +68,11 @@ class TenantModule {
|
|
|
67
68
|
]
|
|
68
69
|
};
|
|
69
70
|
}
|
|
70
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
71
|
-
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "
|
|
72
|
-
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "
|
|
71
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
|
|
72
|
+
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.0.6", ngImport: i0, type: TenantModule }); }
|
|
73
|
+
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantModule }); }
|
|
73
74
|
}
|
|
74
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
75
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantModule, decorators: [{
|
|
75
76
|
type: NgModule,
|
|
76
77
|
args: [{}]
|
|
77
78
|
}] });
|
|
@@ -106,15 +107,15 @@ class StorageResolver extends TenantResolverService {
|
|
|
106
107
|
var currentTenant = localStorage.getItem(StorageResolver.CURRENT_TENANT);
|
|
107
108
|
return currentTenant ? JSON.parse(currentTenant) : null;
|
|
108
109
|
}
|
|
109
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
110
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
110
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: StorageResolver, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
111
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: StorageResolver, providedIn: TenantModule }); }
|
|
111
112
|
}
|
|
112
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
113
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: StorageResolver, decorators: [{
|
|
113
114
|
type: Injectable,
|
|
114
115
|
args: [{
|
|
115
116
|
providedIn: TenantModule
|
|
116
117
|
}]
|
|
117
|
-
}], ctorParameters:
|
|
118
|
+
}], ctorParameters: () => [] });
|
|
118
119
|
|
|
119
120
|
class TenantService {
|
|
120
121
|
constructor(storageResolver) {
|
|
@@ -161,23 +162,23 @@ class TenantService {
|
|
|
161
162
|
isMatched() {
|
|
162
163
|
return this.currentTenant?.identifier == this.requestTenantIdentifier;
|
|
163
164
|
}
|
|
164
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
165
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
165
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantService, deps: [{ token: StorageResolver, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
166
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantService, providedIn: 'root' }); }
|
|
166
167
|
}
|
|
167
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
168
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: TenantService, decorators: [{
|
|
168
169
|
type: Injectable,
|
|
169
170
|
args: [{
|
|
170
171
|
providedIn: 'root'
|
|
171
172
|
}]
|
|
172
|
-
}], ctorParameters:
|
|
173
|
+
}], ctorParameters: () => [{ type: StorageResolver, decorators: [{
|
|
173
174
|
type: Optional
|
|
174
|
-
}] }]
|
|
175
|
+
}] }] });
|
|
175
176
|
|
|
176
177
|
function wrapIntoObservable(value) {
|
|
177
178
|
if (isObservable(value)) {
|
|
178
179
|
return value;
|
|
179
180
|
}
|
|
180
|
-
if (
|
|
181
|
+
if (_isPromise(value)) {
|
|
181
182
|
// Use `Promise.resolve()` to wrap promise-like instances.
|
|
182
183
|
// Required ie when a Resolver returns a AngularJS `$q` promise to correctly trigger the
|
|
183
184
|
// change detection.
|
|
@@ -192,21 +193,28 @@ async function loadChildren(menu, parentInjector, tenant) {
|
|
|
192
193
|
return;
|
|
193
194
|
}
|
|
194
195
|
// console.debug("loadChildren", menu.rawRouterLink);
|
|
196
|
+
if (!hasChildren(menu)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
195
199
|
await loadChildrenInternal(menu, parentInjector, tenant)
|
|
196
200
|
.then(children => {
|
|
197
|
-
|
|
198
|
-
|
|
201
|
+
if (children != null) {
|
|
202
|
+
console.debug("loadedChildren", menu.rawRouterLink, children.map(c => c.rawRouterLink));
|
|
203
|
+
menu.children = children;
|
|
204
|
+
menu.injectTenant(tenant);
|
|
205
|
+
}
|
|
199
206
|
menu.loaded = true;
|
|
200
|
-
menu.injectTenant(tenant);
|
|
201
207
|
});
|
|
202
208
|
}
|
|
203
209
|
async function loadChildrenInternal(menu, parentInjector, tenant) {
|
|
204
210
|
var result = [];
|
|
205
211
|
var children = await firstValueFrom$1(getChildren(menu, parentInjector, tenant));
|
|
206
212
|
for (let child of children) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
213
|
+
if (hasChildren(child)) {
|
|
214
|
+
var sub = await loadChildrenInternal(child, parentInjector, tenant);
|
|
215
|
+
if (sub.length > 0) {
|
|
216
|
+
result = result.concat(sub);
|
|
217
|
+
}
|
|
210
218
|
}
|
|
211
219
|
else {
|
|
212
220
|
result.push(child);
|
|
@@ -219,6 +227,9 @@ async function loadChildrenInternal(menu, parentInjector, tenant) {
|
|
|
219
227
|
return child;
|
|
220
228
|
});
|
|
221
229
|
}
|
|
230
|
+
function hasChildren(menu) {
|
|
231
|
+
return !((menu.children == null || menu.children.length == 0) && !menu.loadChildren);
|
|
232
|
+
}
|
|
222
233
|
function getChildren(menu, parentInjector, tenant) {
|
|
223
234
|
if (menu.loadChildren) {
|
|
224
235
|
return wrapIntoObservable(menu.loadChildren())
|
|
@@ -349,15 +360,15 @@ class MenuService {
|
|
|
349
360
|
return menuItems;
|
|
350
361
|
}), mergeMap(menus => menus), map(menus => menus.sort((a, b) => (a.order ?? 0) - (b.order ?? 0))));
|
|
351
362
|
}
|
|
352
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
353
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
363
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: MenuService, deps: [{ token: i0.Injector }, { token: TenantService }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
364
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: MenuService, providedIn: 'root' }); }
|
|
354
365
|
}
|
|
355
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
366
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: MenuService, decorators: [{
|
|
356
367
|
type: Injectable,
|
|
357
368
|
args: [{
|
|
358
369
|
providedIn: 'root'
|
|
359
370
|
}]
|
|
360
|
-
}], ctorParameters:
|
|
371
|
+
}], ctorParameters: () => [{ type: i0.Injector }, { type: TenantService }] });
|
|
361
372
|
|
|
362
373
|
class MenuModule {
|
|
363
374
|
/**
|
|
@@ -411,11 +422,11 @@ class MenuModule {
|
|
|
411
422
|
providers: [{ provide: MENUS, multi: true, useValue: menus }],
|
|
412
423
|
};
|
|
413
424
|
}
|
|
414
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
415
|
-
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "
|
|
416
|
-
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "
|
|
425
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: MenuModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
|
|
426
|
+
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.0.6", ngImport: i0, type: MenuModule }); }
|
|
427
|
+
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: MenuModule }); }
|
|
417
428
|
}
|
|
418
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
429
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: MenuModule, decorators: [{
|
|
419
430
|
type: NgModule,
|
|
420
431
|
args: [{
|
|
421
432
|
declarations: [],
|
|
@@ -448,12 +459,323 @@ class AsyncDialog {
|
|
|
448
459
|
constructor(matDialog) {
|
|
449
460
|
this.matDialog = matDialog;
|
|
450
461
|
}
|
|
451
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
452
|
-
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "
|
|
462
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: AsyncDialog, deps: [{ token: i1.MatDialog }], target: i0.ɵɵFactoryTarget.Directive }); }
|
|
463
|
+
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "22.0.6", type: AsyncDialog, isStandalone: true, ngImport: i0 }); }
|
|
453
464
|
}
|
|
454
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
465
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: AsyncDialog, decorators: [{
|
|
455
466
|
type: Directive
|
|
456
|
-
}], ctorParameters:
|
|
467
|
+
}], ctorParameters: () => [{ type: i1.MatDialog }] });
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Operation identifier that groups all HTTP attempts belonging to one logical
|
|
471
|
+
* state-changing operation. Requests carrying the same operation id reuse the
|
|
472
|
+
* same `Idempotency-Key` (across double-clicks and retries); a `null` value
|
|
473
|
+
* means the request is not opted into idempotency.
|
|
474
|
+
*/
|
|
475
|
+
const IDEMPOTENCY_OPERATION = new HttpContextToken(() => null);
|
|
476
|
+
/**
|
|
477
|
+
* Optional client-side retry horizon (in milliseconds) that bounds how long a
|
|
478
|
+
* key may be reused, mirroring the backend retention window. Past this age the
|
|
479
|
+
* request is surfaced as an expired condition rather than silently treated as new.
|
|
480
|
+
*/
|
|
481
|
+
const IDEMPOTENCY_MAX_AGE_MS = new HttpContextToken(() => null);
|
|
482
|
+
/**
|
|
483
|
+
* Build (or extend) an {@link HttpContext} that opts a request into idempotency
|
|
484
|
+
* for the given logical operation.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* this.http.post('/orders', body, { context: withIdempotency('create-order-42') });
|
|
488
|
+
*/
|
|
489
|
+
function withIdempotency(operationId, maxAgeMs, context = new HttpContext()) {
|
|
490
|
+
context.set(IDEMPOTENCY_OPERATION, operationId);
|
|
491
|
+
if (maxAgeMs != null) {
|
|
492
|
+
context.set(IDEMPOTENCY_MAX_AGE_MS, maxAgeMs);
|
|
493
|
+
}
|
|
494
|
+
return context;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Raised when the backend rejects a request because the same `Idempotency-Key`
|
|
499
|
+
* was reused with a materially different payload (HTTP 422).
|
|
500
|
+
*/
|
|
501
|
+
class IdempotencyConflictError extends Error {
|
|
502
|
+
constructor(operationId, message = 'Idempotency key conflict: the same key was reused with a different payload.') {
|
|
503
|
+
super(message);
|
|
504
|
+
this.operationId = operationId;
|
|
505
|
+
this.name = 'IdempotencyConflictError';
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Raised when an endpoint requires an `Idempotency-Key` but none was supplied
|
|
510
|
+
* (HTTP 400). Should not occur in normal client flow because the interceptor
|
|
511
|
+
* always attaches a key to opted-in requests.
|
|
512
|
+
*/
|
|
513
|
+
class IdempotencyKeyRequiredError extends Error {
|
|
514
|
+
constructor(message = 'Idempotency-Key header is required for this request.') {
|
|
515
|
+
super(message);
|
|
516
|
+
this.name = 'IdempotencyKeyRequiredError';
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Raised when a key has exceeded its client-side retry horizon and can no longer
|
|
521
|
+
* be safely reused for the same operation.
|
|
522
|
+
*/
|
|
523
|
+
class IdempotencyExpiredError extends Error {
|
|
524
|
+
constructor(operationId, message = 'Idempotency key expired; the operation can no longer be safely retried with the same key.') {
|
|
525
|
+
super(message);
|
|
526
|
+
this.operationId = operationId;
|
|
527
|
+
this.name = 'IdempotencyExpiredError';
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Classify an {@link HttpErrorResponse} against the backend idempotency contract
|
|
533
|
+
* (see the Juice backend `011-idempotency-key-api` feature):
|
|
534
|
+
* - `409` → operation is still in progress (retry after backoff)
|
|
535
|
+
* - `422` → key reused with a different payload (conflict)
|
|
536
|
+
* - `400` with an idempotency-key ProblemDetails title → key required
|
|
537
|
+
* - anything else → not an idempotency signal
|
|
538
|
+
*/
|
|
539
|
+
function classifyError(error) {
|
|
540
|
+
switch (error.status) {
|
|
541
|
+
case 409:
|
|
542
|
+
return 'in-progress';
|
|
543
|
+
case 422:
|
|
544
|
+
return 'conflict';
|
|
545
|
+
case 400: {
|
|
546
|
+
const title = (error.error && error.error.title) || '';
|
|
547
|
+
return /idempotency[-\s]?key/i.test(title) ? 'required' : 'other';
|
|
548
|
+
}
|
|
549
|
+
default:
|
|
550
|
+
return 'other';
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Optional provider of a user-scoped identifier (e.g. the authenticated subject)
|
|
556
|
+
* folded into the idempotency key scope so keys are never reused across a user
|
|
557
|
+
* switch. Kept as a token so `@juice-js/core` does not depend on `@juice-js/auth`
|
|
558
|
+
* (which would introduce a circular library dependency). Applications with auth
|
|
559
|
+
* provide `{ provide: IDEMPOTENCY_USER_CONTEXT, useValue: () => oauth.getIdentityClaims()?.sub ?? null }`.
|
|
560
|
+
*/
|
|
561
|
+
const IDEMPOTENCY_USER_CONTEXT = new InjectionToken('IDEMPOTENCY_USER_CONTEXT');
|
|
562
|
+
/**
|
|
563
|
+
* Generates, stores, and reuses a collision-resistant `Idempotency-Key` per
|
|
564
|
+
* logical operation. The same operation id yields the same key across retries
|
|
565
|
+
* and double-submits; distinct operations get distinct keys; and keys are scoped
|
|
566
|
+
* to the current tenant/user so they never leak across a context switch.
|
|
567
|
+
*/
|
|
568
|
+
class IdempotencyKeyService {
|
|
569
|
+
constructor(tenant, userContext) {
|
|
570
|
+
this.tenant = tenant;
|
|
571
|
+
this.userContext = userContext;
|
|
572
|
+
this.entries = new Map();
|
|
573
|
+
}
|
|
574
|
+
currentScope() {
|
|
575
|
+
const t = this.tenant?.currentTenant?.identifier ?? this.tenant?.requestTenantIdentifier ?? '';
|
|
576
|
+
const u = this.userContext?.() ?? '';
|
|
577
|
+
return `${t}|${u}`;
|
|
578
|
+
}
|
|
579
|
+
newKey() {
|
|
580
|
+
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
|
581
|
+
return crypto.randomUUID();
|
|
582
|
+
}
|
|
583
|
+
// Fallback RFC4122-style generator for environments without crypto.randomUUID.
|
|
584
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
585
|
+
const r = (Math.random() * 16) | 0;
|
|
586
|
+
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
587
|
+
return v.toString(16);
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Return the existing key for this operation, or generate a new one. If the
|
|
592
|
+
* current tenant/user scope differs from the stored entry, a fresh key is
|
|
593
|
+
* generated (FR-010).
|
|
594
|
+
*/
|
|
595
|
+
getKey(operationId) {
|
|
596
|
+
const scope = this.currentScope();
|
|
597
|
+
const existing = this.entries.get(operationId);
|
|
598
|
+
if (existing && existing.scope === scope) {
|
|
599
|
+
return existing.key;
|
|
600
|
+
}
|
|
601
|
+
const entry = { key: this.newKey(), scope, createdAt: Date.now(), inFlight: false };
|
|
602
|
+
this.entries.set(operationId, entry);
|
|
603
|
+
return entry.key;
|
|
604
|
+
}
|
|
605
|
+
/** Forget the key for a completed (or abandoned) operation. */
|
|
606
|
+
release(operationId) {
|
|
607
|
+
this.entries.delete(operationId);
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Whether the stored key for the operation has exceeded the given retry
|
|
611
|
+
* horizon. Returns `false` when no horizon is set or no key exists yet.
|
|
612
|
+
*/
|
|
613
|
+
isExpired(operationId, maxAgeMs) {
|
|
614
|
+
if (maxAgeMs == null) {
|
|
615
|
+
return false;
|
|
616
|
+
}
|
|
617
|
+
const entry = this.entries.get(operationId);
|
|
618
|
+
if (!entry) {
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
return Date.now() - entry.createdAt > maxAgeMs;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* UI-level double-submit guard (FR-009). Returns `false` if an operation is
|
|
625
|
+
* already in flight, so callers can suppress a duplicate dispatch.
|
|
626
|
+
*/
|
|
627
|
+
tryBeginInFlight(operationId) {
|
|
628
|
+
const scope = this.currentScope();
|
|
629
|
+
const entry = this.entries.get(operationId) ?? { key: this.newKey(), scope, createdAt: Date.now(), inFlight: false };
|
|
630
|
+
if (entry.inFlight) {
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
entry.inFlight = true;
|
|
634
|
+
this.entries.set(operationId, entry);
|
|
635
|
+
return true;
|
|
636
|
+
}
|
|
637
|
+
/** Clear the in-flight flag set by {@link tryBeginInFlight}. */
|
|
638
|
+
endInFlight(operationId) {
|
|
639
|
+
const entry = this.entries.get(operationId);
|
|
640
|
+
if (entry) {
|
|
641
|
+
entry.inFlight = false;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyKeyService, deps: [{ token: TenantService, optional: true }, { token: IDEMPOTENCY_USER_CONTEXT, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
645
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyKeyService, providedIn: 'root' }); }
|
|
646
|
+
}
|
|
647
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyKeyService, decorators: [{
|
|
648
|
+
type: Injectable,
|
|
649
|
+
args: [{ providedIn: 'root' }]
|
|
650
|
+
}], ctorParameters: () => [{ type: TenantService, decorators: [{
|
|
651
|
+
type: Optional
|
|
652
|
+
}] }, { type: undefined, decorators: [{
|
|
653
|
+
type: Optional
|
|
654
|
+
}, {
|
|
655
|
+
type: Inject,
|
|
656
|
+
args: [IDEMPOTENCY_USER_CONTEXT]
|
|
657
|
+
}] }] });
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Attaches an `Idempotency-Key` header to opted-in state-changing requests and
|
|
661
|
+
* interprets the backend idempotency responses:
|
|
662
|
+
* - replayed success is passed through and the key released;
|
|
663
|
+
* - `409` in-progress triggers a bounded backoff retry reusing the same key;
|
|
664
|
+
* - `422` conflict and `400` required surface as typed errors without resubmission;
|
|
665
|
+
* - keys past their retry horizon surface as an expired error.
|
|
666
|
+
*
|
|
667
|
+
* On top of the shared key, it de-duplicates concurrent dispatches of the same
|
|
668
|
+
* operation (FR-009): while one attempt is in flight, a second subscription to
|
|
669
|
+
* the same operation is coalesced onto the first in-flight response instead of
|
|
670
|
+
* issuing a parallel request, so a double-click settles on a single outcome.
|
|
671
|
+
*
|
|
672
|
+
* Mirrors the `@juice-js/auth` `AuthInterceptor` idiom.
|
|
673
|
+
*/
|
|
674
|
+
class IdempotencyInterceptor {
|
|
675
|
+
constructor(keys) {
|
|
676
|
+
this.keys = keys;
|
|
677
|
+
this.maxInProgressRetries = 3;
|
|
678
|
+
this.baseBackoffMs = 500;
|
|
679
|
+
/**
|
|
680
|
+
* Shared, in-flight response streams keyed by operation id. Present only while
|
|
681
|
+
* an operation is being sent; cleared when it settles (success or error).
|
|
682
|
+
*/
|
|
683
|
+
this.inFlight = new Map();
|
|
684
|
+
}
|
|
685
|
+
isUnsafe(method) {
|
|
686
|
+
const m = method.toUpperCase();
|
|
687
|
+
return m === 'POST' || m === 'PUT' || m === 'PATCH' || m === 'DELETE';
|
|
688
|
+
}
|
|
689
|
+
intercept(req, next) {
|
|
690
|
+
const operationId = req.context.get(IDEMPOTENCY_OPERATION);
|
|
691
|
+
// Not opted in, or a safe/read-only request → pass through untouched (FR-004).
|
|
692
|
+
if (!operationId || !this.isUnsafe(req.method)) {
|
|
693
|
+
return next.handle(req);
|
|
694
|
+
}
|
|
695
|
+
const maxAgeMs = req.context.get(IDEMPOTENCY_MAX_AGE_MS);
|
|
696
|
+
if (this.keys.isExpired(operationId, maxAgeMs)) {
|
|
697
|
+
this.keys.release(operationId);
|
|
698
|
+
return throwError(() => new IdempotencyExpiredError(operationId));
|
|
699
|
+
}
|
|
700
|
+
// FR-009: a duplicate dispatch while the operation is in flight rides the
|
|
701
|
+
// existing response instead of firing a parallel request.
|
|
702
|
+
const existing = this.inFlight.get(operationId);
|
|
703
|
+
if (existing) {
|
|
704
|
+
return existing;
|
|
705
|
+
}
|
|
706
|
+
const key = this.keys.getKey(operationId);
|
|
707
|
+
const idempotentReq = req.clone({ headers: req.headers.set('Idempotency-Key', key) });
|
|
708
|
+
const shared = next.handle(idempotentReq).pipe(retry({
|
|
709
|
+
count: this.maxInProgressRetries,
|
|
710
|
+
delay: (error, retryCount) => {
|
|
711
|
+
// Only retry the in-progress signal; every other error propagates immediately.
|
|
712
|
+
if (!(error instanceof HttpErrorResponse) || classifyError(error) !== 'in-progress') {
|
|
713
|
+
throw error;
|
|
714
|
+
}
|
|
715
|
+
return timer(this.baseBackoffMs * Math.pow(2, retryCount - 1));
|
|
716
|
+
},
|
|
717
|
+
}), tap((event) => {
|
|
718
|
+
if (event instanceof HttpResponse) {
|
|
719
|
+
this.keys.release(operationId);
|
|
720
|
+
}
|
|
721
|
+
}), catchError((error) => {
|
|
722
|
+
if (error instanceof HttpErrorResponse) {
|
|
723
|
+
switch (classifyError(error)) {
|
|
724
|
+
case 'conflict':
|
|
725
|
+
this.keys.release(operationId);
|
|
726
|
+
return throwError(() => new IdempotencyConflictError(operationId));
|
|
727
|
+
case 'required':
|
|
728
|
+
this.keys.release(operationId);
|
|
729
|
+
return throwError(() => new IdempotencyKeyRequiredError());
|
|
730
|
+
case 'in-progress':
|
|
731
|
+
// Retries exhausted while still in progress — surface the original error,
|
|
732
|
+
// keeping the key so a later manual retry can reuse it within retention.
|
|
733
|
+
return throwError(() => error);
|
|
734
|
+
default:
|
|
735
|
+
return throwError(() => error);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return throwError(() => error);
|
|
739
|
+
}),
|
|
740
|
+
// Drop the coalescing entry once the operation settles so the next dispatch
|
|
741
|
+
// starts a fresh attempt. With refCount:false below this fires on terminal
|
|
742
|
+
// success or error, not on a subscriber leaving mid-flight.
|
|
743
|
+
finalize(() => this.inFlight.delete(operationId)),
|
|
744
|
+
// Multicast the single attempt to every coalesced subscriber; refCount:false
|
|
745
|
+
// keeps the request running even if the initial subscriber unsubscribes.
|
|
746
|
+
shareReplay({ bufferSize: 1, refCount: false }));
|
|
747
|
+
this.inFlight.set(operationId, shared);
|
|
748
|
+
return shared;
|
|
749
|
+
}
|
|
750
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyInterceptor, deps: [{ token: IdempotencyKeyService }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
751
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyInterceptor }); }
|
|
752
|
+
}
|
|
753
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyInterceptor, decorators: [{
|
|
754
|
+
type: Injectable
|
|
755
|
+
}], ctorParameters: () => [{ type: IdempotencyKeyService }] });
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Registers the {@link IdempotencyInterceptor} so opted-in state-changing
|
|
759
|
+
* requests automatically carry an `Idempotency-Key` header.
|
|
760
|
+
*
|
|
761
|
+
* @example
|
|
762
|
+
* imports: [IdempotencyModule]
|
|
763
|
+
*/
|
|
764
|
+
class IdempotencyModule {
|
|
765
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
|
|
766
|
+
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyModule }); }
|
|
767
|
+
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyModule, providers: [
|
|
768
|
+
{ provide: HTTP_INTERCEPTORS, useClass: IdempotencyInterceptor, multi: true },
|
|
769
|
+
] }); }
|
|
770
|
+
}
|
|
771
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: IdempotencyModule, decorators: [{
|
|
772
|
+
type: NgModule,
|
|
773
|
+
args: [{
|
|
774
|
+
providers: [
|
|
775
|
+
{ provide: HTTP_INTERCEPTORS, useClass: IdempotencyInterceptor, multi: true },
|
|
776
|
+
],
|
|
777
|
+
}]
|
|
778
|
+
}] });
|
|
457
779
|
|
|
458
780
|
/*
|
|
459
781
|
* Public API Surface of core
|
|
@@ -463,5 +785,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImpo
|
|
|
463
785
|
* Generated bundle index. Do not edit.
|
|
464
786
|
*/
|
|
465
787
|
|
|
466
|
-
export { AsyncDialog, MENUS, MenuItem, MenuModule, MenuService, TenantModule, TenantService, loadChildrenRoutes };
|
|
788
|
+
export { AsyncDialog, IDEMPOTENCY_MAX_AGE_MS, IDEMPOTENCY_OPERATION, IDEMPOTENCY_USER_CONTEXT, IdempotencyConflictError, IdempotencyExpiredError, IdempotencyInterceptor, IdempotencyKeyRequiredError, IdempotencyKeyService, IdempotencyModule, MENUS, MenuItem, MenuModule, MenuService, TenantModule, TenantService, classifyError, loadChildrenRoutes, withIdempotency };
|
|
467
789
|
//# sourceMappingURL=juice-js-core.mjs.map
|