@equinor/fusion-framework-module-context 8.0.0 → 8.0.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/CHANGELOG.md +15 -0
- package/dist/esm/ContextConfigBuilder.js +17 -6
- package/dist/esm/ContextConfigBuilder.js.map +1 -1
- package/dist/esm/ContextProvider.js +71 -15
- package/dist/esm/ContextProvider.js.map +1 -1
- package/dist/esm/client/ContextClient.js +13 -3
- package/dist/esm/client/ContextClient.js.map +1 -1
- package/dist/esm/configurator.js +9 -5
- package/dist/esm/configurator.js.map +1 -1
- package/dist/esm/{errors.js → errors/FusionContextSearchError.js} +3 -1
- package/dist/esm/errors/FusionContextSearchError.js.map +1 -0
- package/dist/esm/errors/index.js +2 -0
- package/dist/esm/errors/index.js.map +1 -0
- package/dist/esm/get-context-selector.js +14 -0
- package/dist/esm/get-context-selector.js.map +1 -0
- package/dist/esm/parse-context-item.js +31 -0
- package/dist/esm/parse-context-item.js.map +1 -0
- package/dist/esm/query-context-selector.js +12 -0
- package/dist/esm/query-context-selector.js.map +1 -0
- package/dist/esm/related-context-selector.js +12 -0
- package/dist/esm/related-context-selector.js.map +1 -0
- package/dist/esm/utils/extract-context-id-from-path.js +25 -0
- package/dist/esm/utils/extract-context-id-from-path.js.map +1 -0
- package/dist/esm/utils/resolve-context-from-path.js +11 -22
- package/dist/esm/utils/resolve-context-from-path.js.map +1 -1
- package/dist/esm/utils/resolve-initial-context.js +2 -0
- package/dist/esm/utils/resolve-initial-context.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/ContextConfigBuilder.d.ts +16 -1
- package/dist/types/ContextProvider.d.ts +37 -3
- package/dist/types/client/ContextClient.d.ts +8 -1
- package/dist/types/{errors.d.ts → errors/FusionContextSearchError.d.ts} +2 -0
- package/dist/types/errors/index.d.ts +1 -0
- package/dist/types/get-context-selector.d.ts +9 -0
- package/dist/types/parse-context-item.d.ts +8 -0
- package/dist/types/query-context-selector.d.ts +7 -0
- package/dist/types/related-context-selector.d.ts +7 -0
- package/dist/types/types.d.ts +1 -1
- package/dist/types/utils/extract-context-id-from-path.d.ts +16 -0
- package/dist/types/utils/resolve-context-from-path.d.ts +1 -16
- package/dist/types/utils/resolve-initial-context.d.ts +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +11 -11
- package/src/ContextConfigBuilder.ts +32 -7
- package/src/ContextProvider.ts +78 -21
- package/src/client/ContextClient.ts +13 -3
- package/src/configurator.ts +15 -6
- package/src/{errors.ts → errors/FusionContextSearchError.ts} +2 -0
- package/src/errors/index.ts +1 -0
- package/src/get-context-selector.ts +18 -0
- package/src/parse-context-item.ts +39 -0
- package/src/query-context-selector.ts +15 -0
- package/src/related-context-selector.ts +15 -0
- package/src/types.ts +1 -1
- package/src/utils/extract-context-id-from-path.ts +30 -0
- package/src/utils/resolve-context-from-path.ts +13 -28
- package/src/utils/resolve-initial-context.ts +2 -0
- package/src/version.ts +1 -1
- package/dist/esm/errors.js.map +0 -1
- package/dist/esm/selectors.js +0 -58
- package/dist/esm/selectors.js.map +0 -1
- package/dist/types/selectors.d.ts +0 -19
- package/src/selectors.ts +0 -70
package/src/ContextProvider.ts
CHANGED
|
@@ -346,35 +346,64 @@ export class ContextProvider
|
|
|
346
346
|
|
|
347
347
|
#contextQueue = new Subject<Observable<ContextItem<Record<string, unknown>>>>();
|
|
348
348
|
|
|
349
|
+
/**
|
|
350
|
+
* The underlying context client used to resolve and hold the current context item.
|
|
351
|
+
* @returns The internal {@link ContextClient} instance.
|
|
352
|
+
*/
|
|
349
353
|
public get contextClient() {
|
|
350
354
|
return this.#contextClient;
|
|
351
355
|
}
|
|
352
356
|
|
|
357
|
+
/**
|
|
358
|
+
* The query client used to search for context items.
|
|
359
|
+
* @returns The internal `Query` instance used by {@link queryContext}.
|
|
360
|
+
*/
|
|
353
361
|
public get queryClient() {
|
|
354
362
|
return this.#contextQuery;
|
|
355
363
|
}
|
|
356
364
|
|
|
365
|
+
/**
|
|
366
|
+
* Observable stream emitting the current context item.
|
|
367
|
+
* @returns Observable that emits the current `ContextItem`, `null`, or `undefined`.
|
|
368
|
+
*/
|
|
357
369
|
get currentContext$(): Observable<ContextItem | null | undefined> {
|
|
358
370
|
return this.#contextClient.currentContext$;
|
|
359
371
|
}
|
|
360
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Snapshot of the current context item.
|
|
375
|
+
* @returns The current `ContextItem`, or `null`/`undefined` if not set.
|
|
376
|
+
*/
|
|
361
377
|
get currentContext(): ContextItem | undefined | null {
|
|
362
378
|
return this.#contextClient.currentContext;
|
|
363
379
|
}
|
|
364
380
|
|
|
365
|
-
/**
|
|
381
|
+
/**
|
|
382
|
+
* Sets the current context item.
|
|
383
|
+
* @deprecated do not use, will be removed
|
|
384
|
+
* @param context - The context item to set as current. Must not be `undefined`.
|
|
385
|
+
* @throws Error if `context` is `undefined`.
|
|
386
|
+
*/
|
|
366
387
|
set currentContext(context: ContextItem | null | undefined) {
|
|
367
388
|
console.warn(
|
|
368
389
|
'@deprecated',
|
|
369
390
|
'ContextProvider.currentContext',
|
|
370
391
|
'use setCurrentContextById|setCurrentContext|clearCurrentContext',
|
|
371
392
|
);
|
|
393
|
+
// undefined is reserved to mean "not yet initialized", so it cannot be set explicitly
|
|
372
394
|
if (context === undefined) {
|
|
373
395
|
throw Error('not allowed to set current context as undefined undefined!');
|
|
374
396
|
}
|
|
375
397
|
this.setCurrentContextAsync(context);
|
|
376
398
|
}
|
|
377
399
|
|
|
400
|
+
/**
|
|
401
|
+
* Creates a new instance of `ContextProvider`.
|
|
402
|
+
* @param args - Constructor arguments.
|
|
403
|
+
* @param args.config - The context module configuration.
|
|
404
|
+
* @param args.event - Optional event module instance for dispatching context change events.
|
|
405
|
+
* @param args.parentContext - Optional parent context provider. Deprecated, use {@link connectParentContext}.
|
|
406
|
+
*/
|
|
378
407
|
constructor(args: {
|
|
379
408
|
config: ContextModuleConfig;
|
|
380
409
|
event?: ModuleType<EventModule>;
|
|
@@ -385,6 +414,7 @@ export class ContextProvider
|
|
|
385
414
|
|
|
386
415
|
super({ version, config });
|
|
387
416
|
|
|
417
|
+
// warn about deprecated parentContext constructor arg
|
|
388
418
|
if (args.parentContext) {
|
|
389
419
|
console.warn(
|
|
390
420
|
'@deprecated',
|
|
@@ -398,16 +428,19 @@ export class ContextProvider
|
|
|
398
428
|
if (config.resolveContext) {
|
|
399
429
|
this.resolveContext = config.resolveContext?.bind(this);
|
|
400
430
|
}
|
|
431
|
+
// override validateContext if configured
|
|
401
432
|
if (config.validateContext) {
|
|
402
433
|
this.validateContext = config.validateContext?.bind(this);
|
|
403
434
|
}
|
|
404
435
|
|
|
436
|
+
// override extractContextIdFromPath if configured
|
|
405
437
|
if (config.extractContextIdFromPath) {
|
|
406
|
-
// @ts-
|
|
438
|
+
// @ts-expect-error
|
|
407
439
|
this.extractContextIdFromPath = config.extractContextIdFromPath;
|
|
408
440
|
}
|
|
441
|
+
// override generatePathFromContext if configured
|
|
409
442
|
if (config.generatePathFromContext) {
|
|
410
|
-
// @ts-
|
|
443
|
+
// @ts-expect-error
|
|
411
444
|
this.generatePathFromContext = config.generatePathFromContext;
|
|
412
445
|
}
|
|
413
446
|
|
|
@@ -418,6 +451,7 @@ export class ContextProvider
|
|
|
418
451
|
this.#contextClient = new ContextClient(config.client.get);
|
|
419
452
|
this.#contextQuery = new Query(config.client.query);
|
|
420
453
|
|
|
454
|
+
// only create the related-context query when the config provides one
|
|
421
455
|
if (config.client.related) {
|
|
422
456
|
this.#contextRelated = new Query(config.client.related);
|
|
423
457
|
}
|
|
@@ -436,10 +470,8 @@ export class ContextProvider
|
|
|
436
470
|
this.#subscriptions.add(
|
|
437
471
|
// observe current context changes
|
|
438
472
|
this.currentContext$
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
pairwise(),
|
|
442
|
-
)
|
|
473
|
+
// emit previous and next context together for change comparisons
|
|
474
|
+
.pipe(pairwise())
|
|
443
475
|
.subscribe(([previous, next]) => {
|
|
444
476
|
this.#event?.dispatchEvent('onCurrentContextChanged', {
|
|
445
477
|
source: this,
|
|
@@ -494,12 +526,13 @@ export class ContextProvider
|
|
|
494
526
|
provider: IContextProvider,
|
|
495
527
|
opt?: { skipFirst: boolean },
|
|
496
528
|
): Subscription {
|
|
529
|
+
// build a stream of validated context changes from the parent provider
|
|
497
530
|
const parentContext$ = provider.currentContext$.pipe(
|
|
498
531
|
// do not set context if parent has not initialized
|
|
499
532
|
filter((x): x is ContextItem | null => x !== undefined),
|
|
500
533
|
filter((next, index) => {
|
|
501
534
|
// skip first item if opt.skipFirst is true
|
|
502
|
-
// TODO: this is a bit hacky, should be handled in a better way
|
|
535
|
+
// TODO(#5121): this is a bit hacky, should be handled in a better way
|
|
503
536
|
if (opt?.skipFirst && index <= 1) {
|
|
504
537
|
console.debug('ContextProvider::connectParentContext', 'skipping first item', next);
|
|
505
538
|
return false;
|
|
@@ -508,8 +541,8 @@ export class ContextProvider
|
|
|
508
541
|
return this.currentContext?.id !== next?.id;
|
|
509
542
|
}),
|
|
510
543
|
switchMap(async (next) => {
|
|
544
|
+
// if parent context is null, just return
|
|
511
545
|
if (!next) {
|
|
512
|
-
// if parent context is null, just return
|
|
513
546
|
return { next };
|
|
514
547
|
}
|
|
515
548
|
// notify event observers that parent context is about to change and await for cancelation
|
|
@@ -524,15 +557,19 @@ export class ContextProvider
|
|
|
524
557
|
filter((x) => !x.canceled),
|
|
525
558
|
switchMap(({ next }) => {
|
|
526
559
|
// set current context with validation and resolution
|
|
527
|
-
return
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
560
|
+
return (
|
|
561
|
+
this.setCurrentContext(next, {
|
|
562
|
+
validate: true,
|
|
563
|
+
resolve: true,
|
|
564
|
+
})
|
|
565
|
+
// swallow errors so a failed context change doesn't break the parent subscription
|
|
566
|
+
.pipe(
|
|
567
|
+
catchError((err) => {
|
|
568
|
+
console.warn('ContextProvider::onParentContextChanged', 'setCurrentContext', err);
|
|
569
|
+
// do not emit any value if an error occurs
|
|
570
|
+
return EMPTY;
|
|
571
|
+
}),
|
|
572
|
+
)
|
|
536
573
|
);
|
|
537
574
|
}),
|
|
538
575
|
catchError((err) => {
|
|
@@ -568,6 +605,7 @@ export class ContextProvider
|
|
|
568
605
|
this.#contextClient
|
|
569
606
|
// resolve context item by id
|
|
570
607
|
.resolveContext(id)
|
|
608
|
+
// filter out invalid items and set as current context
|
|
571
609
|
.pipe(
|
|
572
610
|
// filter out invalid context items
|
|
573
611
|
filter((item): item is ContextItem => !!item),
|
|
@@ -609,6 +647,11 @@ export class ContextProvider
|
|
|
609
647
|
* If the observable is subscribe, unsubscribing __WILL__ abort the task and remove it from queue
|
|
610
648
|
*
|
|
611
649
|
* @param context context item which would be queue to set as current
|
|
650
|
+
* @param opt Optional settings.
|
|
651
|
+
* @param opt.validate Whether to validate the context item before setting it.
|
|
652
|
+
* @param opt.resolve Whether to attempt to resolve the context item if validation fails.
|
|
653
|
+
* @template T The type of the context item, which extends `ContextItem<Record<string, unknown>>` or can be `null`.
|
|
654
|
+
* @returns An observable that emits the context item once the queued task completes.
|
|
612
655
|
*/
|
|
613
656
|
public setCurrentContext<T extends ContextItem<Record<string, unknown>> | null>(
|
|
614
657
|
context: T,
|
|
@@ -620,6 +663,7 @@ export class ContextProvider
|
|
|
620
663
|
// wrapper for returning an observable to the caller
|
|
621
664
|
const subject$ = new Subject<T>();
|
|
622
665
|
|
|
666
|
+
// run the actual context-setting logic and relay results/errors to the caller's subject
|
|
623
667
|
const task$ = this._setCurrentContext(context, opt).pipe(
|
|
624
668
|
// send context item which was set to the caller
|
|
625
669
|
tap((x) => subject$.next(x)),
|
|
@@ -639,6 +683,7 @@ export class ContextProvider
|
|
|
639
683
|
// add task to internal queue
|
|
640
684
|
this.#contextQueue.next(task$ as Observable<ContextItem<Record<string, unknown>>>);
|
|
641
685
|
|
|
686
|
+
// tear down the queued task if the caller unsubscribes
|
|
642
687
|
return subject$.pipe(
|
|
643
688
|
// if caller subscribes, unsubscribe should abort queue entry
|
|
644
689
|
finalize(() => abort$.next(true)),
|
|
@@ -674,7 +719,7 @@ export class ContextProvider
|
|
|
674
719
|
* - Emit the context and complete the observable.
|
|
675
720
|
*
|
|
676
721
|
* @protected
|
|
677
|
-
* @
|
|
722
|
+
* @template T - The type of the context item, which extends `ContextItem<Record<string, unknown>>` or can be `null`.
|
|
678
723
|
* @param context - The new context to set.
|
|
679
724
|
* @param opt - Optional settings:
|
|
680
725
|
* - `validate`: Whether to validate the context before setting.
|
|
@@ -708,7 +753,10 @@ export class ContextProvider
|
|
|
708
753
|
// emit error and complete
|
|
709
754
|
return subscriber.error(Error('failed to validate provided context'));
|
|
710
755
|
}
|
|
756
|
+
// if resolve is enabled, attempt to resolve the invalid context before setting it
|
|
711
757
|
if (opt.resolve) {
|
|
758
|
+
// the recursive `_setCurrentContext` call below is re-entered with the already
|
|
759
|
+
// validated/resolved context, so the generic `T` cast is safe here.
|
|
712
760
|
return of(context)
|
|
713
761
|
.pipe(
|
|
714
762
|
// notify event observers that context is about to get resolved
|
|
@@ -727,6 +775,7 @@ export class ContextProvider
|
|
|
727
775
|
}),
|
|
728
776
|
// resolve context
|
|
729
777
|
switchMap((context) =>
|
|
778
|
+
// Pair the resolved context alongside the original for downstream consumers.
|
|
730
779
|
this.resolveContext(context).pipe(
|
|
731
780
|
map((resolved) => ({
|
|
732
781
|
context,
|
|
@@ -789,7 +838,7 @@ export class ContextProvider
|
|
|
789
838
|
*
|
|
790
839
|
* @see {@link setCurrentContext} for more details.
|
|
791
840
|
*
|
|
792
|
-
* @
|
|
841
|
+
* @template T - The type of the context item, which extends `ContextItem<Record<string, unknown>>` or can be `null`.
|
|
793
842
|
* @param context - The context item to set as the current context, or `null` to clear it.
|
|
794
843
|
* @param opt - Optional settings for context handling.
|
|
795
844
|
* @param opt.validate - If `true`, validates the context before setting it.
|
|
@@ -813,6 +862,7 @@ export class ContextProvider
|
|
|
813
862
|
*
|
|
814
863
|
* @param search - The search string to filter context items.
|
|
815
864
|
* @returns An Observable that emits an array of `ContextItem` objects matching the search criteria.
|
|
865
|
+
* @throws Re-throws the underlying cause of a `QueryClientError`, otherwise re-throws the original error.
|
|
816
866
|
*/
|
|
817
867
|
public queryContext(search: string): Observable<Array<ContextItem>> {
|
|
818
868
|
const query$ = this.queryClient
|
|
@@ -823,6 +873,7 @@ export class ContextProvider
|
|
|
823
873
|
type: this.#contextType,
|
|
824
874
|
}) as QueryContextParameters,
|
|
825
875
|
)
|
|
876
|
+
// unwrap query-client errors and expose the underlying cause to subscribers
|
|
826
877
|
.pipe(
|
|
827
878
|
catchError((err) => {
|
|
828
879
|
// if query client throws a QueryClientError, extract the cause and throw it
|
|
@@ -857,7 +908,9 @@ export class ContextProvider
|
|
|
857
908
|
* @returns `true` if the context type is not set or if the item's type ID matches one of the allowed types (case-insensitive); otherwise, `false`.
|
|
858
909
|
*/
|
|
859
910
|
public validateContext(item: ContextItem<Record<string, unknown>>): boolean {
|
|
911
|
+
// no context type configured means every context item is considered valid
|
|
860
912
|
if (!this.#contextType) return true;
|
|
913
|
+
// normalize allowed types for a case-insensitive comparison
|
|
861
914
|
return this.#contextType.map((x) => x.toLowerCase()).includes(item.type.id.toLowerCase());
|
|
862
915
|
}
|
|
863
916
|
|
|
@@ -881,7 +934,10 @@ export class ContextProvider
|
|
|
881
934
|
// request related context items for the given context item with the same context type which the provider is configured with
|
|
882
935
|
return this.relatedContexts({ item, filter: { type: this.#contextType } }).pipe(
|
|
883
936
|
// filter out invalid context items
|
|
884
|
-
map((x) =>
|
|
937
|
+
map((x) =>
|
|
938
|
+
// keep only context items that validate against the provider's context type
|
|
939
|
+
x.filter((item) => this.validateContext(item)),
|
|
940
|
+
),
|
|
885
941
|
map((values) => {
|
|
886
942
|
// related context should be resolved to a single context item
|
|
887
943
|
const value = values.shift();
|
|
@@ -941,6 +997,7 @@ export class ContextProvider
|
|
|
941
997
|
return this.#contextRelated.query(args).pipe(
|
|
942
998
|
map(({ value }) => value),
|
|
943
999
|
catchError((err) => {
|
|
1000
|
+
// unwrap the underlying cause so callers see the original error
|
|
944
1001
|
if (err.cause) {
|
|
945
1002
|
throw err.cause;
|
|
946
1003
|
}
|
|
@@ -25,7 +25,7 @@ export type GetContextParameters = { id: string };
|
|
|
25
25
|
* @template ContextItem The type of the context item managed by the client.
|
|
26
26
|
* @extends Observable<ContextItem | null | undefined>
|
|
27
27
|
*
|
|
28
|
-
* @todo - should this have `undefined` as a valid value?
|
|
28
|
+
* @todo(#5116) - should this have `undefined` as a valid value?
|
|
29
29
|
*
|
|
30
30
|
* @example
|
|
31
31
|
* ```typescript
|
|
@@ -70,6 +70,10 @@ export class ContextClient extends Observable<ContextItem | null | undefined> {
|
|
|
70
70
|
return this.#client;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Creates a new `ContextClient`.
|
|
75
|
+
* @param options - Query constructor options used to fetch a `ContextItem` by its ID.
|
|
76
|
+
*/
|
|
73
77
|
constructor(options: QueryCtorOptions<ContextItem, GetContextParameters>) {
|
|
74
78
|
super((observer) => this.#currentContext$.subscribe(observer));
|
|
75
79
|
this.#client = new Query(options);
|
|
@@ -85,10 +89,11 @@ export class ContextClient extends Observable<ContextItem | null | undefined> {
|
|
|
85
89
|
* @param idOrItem - The context identifier (string), a `ContextItem`, or `null`. If omitted, the current context may be cleared.
|
|
86
90
|
*/
|
|
87
91
|
public setCurrentContext(idOrItem?: string | ContextItem | null): void {
|
|
92
|
+
// resolve string identifiers to a context item before setting
|
|
88
93
|
if (typeof idOrItem === 'string') {
|
|
89
|
-
// TODO - compare context
|
|
94
|
+
// TODO(#5117) - compare context
|
|
95
|
+
// TODO(#5117) should this catch error?
|
|
90
96
|
this.resolveContext(idOrItem)
|
|
91
|
-
// TODO should this catch error?
|
|
92
97
|
.pipe(catchError(() => EMPTY))
|
|
93
98
|
.subscribe((value) => this.setCurrentContext(value));
|
|
94
99
|
/** only add context if not match */
|
|
@@ -105,10 +110,12 @@ export class ContextClient extends Observable<ContextItem | null | undefined> {
|
|
|
105
110
|
* @throws Rethrows the underlying error cause if present, otherwise throws the original error.
|
|
106
111
|
*/
|
|
107
112
|
public resolveContext(id: string): Observable<ContextItem> {
|
|
113
|
+
// unwrap the query result into the resolved context item
|
|
108
114
|
return this.#client.query({ id }).pipe(
|
|
109
115
|
map((x) => x.value),
|
|
110
116
|
// unwrap error
|
|
111
117
|
catchError((err) => {
|
|
118
|
+
// unwrap the underlying cause so callers see the original error
|
|
112
119
|
if (err.cause) {
|
|
113
120
|
throw err.cause;
|
|
114
121
|
}
|
|
@@ -131,6 +138,9 @@ export class ContextClient extends Observable<ContextItem | null | undefined> {
|
|
|
131
138
|
return fn(this.resolveContext(id));
|
|
132
139
|
}
|
|
133
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Disposes of the client, completing the internal current-context subject.
|
|
143
|
+
*/
|
|
134
144
|
public dispose(): void {
|
|
135
145
|
this.#currentContext$.complete();
|
|
136
146
|
}
|
package/src/configurator.ts
CHANGED
|
@@ -8,7 +8,11 @@ import type {
|
|
|
8
8
|
} from '@equinor/fusion-framework-module';
|
|
9
9
|
import type { ServicesModule, IApiProvider } from '@equinor/fusion-framework-module-services';
|
|
10
10
|
import type { NavigationModule } from '@equinor/fusion-framework-module-navigation';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
getContextSelector,
|
|
13
|
+
queryContextSelector,
|
|
14
|
+
relatedContextSelector,
|
|
15
|
+
} from './get-context-selector';
|
|
12
16
|
import type { QueryCtorOptions } from '@equinor/fusion-query';
|
|
13
17
|
import type {
|
|
14
18
|
ContextFilterFn,
|
|
@@ -149,9 +153,10 @@ export interface ContextModuleConfig {
|
|
|
149
153
|
* @returns An observable input emitting the initial context item, or void.
|
|
150
154
|
*/
|
|
151
155
|
resolveInitialContext?: (args: {
|
|
152
|
-
//
|
|
156
|
+
// biome-ignore lint/suspicious/noExplicitAny: `AnyModuleInstance | any` intentionally widens to accept any module instance shape for `ref`
|
|
153
157
|
ref?: AnyModuleInstance | any;
|
|
154
158
|
modules: ModuleInstance;
|
|
159
|
+
// biome-ignore lint/suspicious/noConfusingVoidType: `void` here relies on TypeScript's special-cased "void-returning callback accepts any return value" behavior — `undefined` would break assignability of resolver functions that only conditionally emit a `ContextItem`
|
|
155
160
|
}) => ObservableInput<ContextItem | void>;
|
|
156
161
|
}
|
|
157
162
|
|
|
@@ -206,10 +211,12 @@ export class ContextModuleConfigurator implements IContextModuleConfigurator {
|
|
|
206
211
|
protected async _getServiceProvider(
|
|
207
212
|
init: ModuleInitializerArgs<IContextModuleConfigurator, [ServicesModule]>,
|
|
208
213
|
): Promise<IApiProvider> {
|
|
214
|
+
// prefer the local services module instance if available
|
|
209
215
|
if (init.hasModule('services')) {
|
|
210
216
|
return init.requireInstance('services');
|
|
211
217
|
}
|
|
212
218
|
const parentServiceModule = (init.ref as ModulesInstanceType<[ServicesModule]>)?.services;
|
|
219
|
+
// fall back to the parent module's services instance
|
|
213
220
|
if (!parentServiceModule) {
|
|
214
221
|
throw Error('no service services provider configures [ServicesModule]');
|
|
215
222
|
}
|
|
@@ -230,11 +237,13 @@ export class ContextModuleConfigurator implements IContextModuleConfigurator {
|
|
|
230
237
|
public async createConfig(
|
|
231
238
|
init: ModuleInitializerArgs<IContextModuleConfigurator, [ServicesModule, NavigationModule]>,
|
|
232
239
|
): Promise<ContextModuleConfig> {
|
|
240
|
+
// run each registered config builder in sequence, merging results into the accumulated config
|
|
233
241
|
const config = await this.#configBuilders.reduce(
|
|
234
242
|
async (cur, cb) => {
|
|
235
|
-
//
|
|
243
|
+
// biome-ignore lint/suspicious/noExplicitAny: builder generics are erased here — `unknown` breaks assignability of the concrete `ContextConfigBuilder` instance passed to consumer callbacks
|
|
236
244
|
const builder = new ContextConfigBuilder<any, any>(init, await cur);
|
|
237
245
|
await Promise.resolve(cb(builder));
|
|
246
|
+
// Merge this builder's config into the accumulator for the next iteration.
|
|
238
247
|
return Object.assign(cur, builder.config);
|
|
239
248
|
},
|
|
240
249
|
Promise.resolve({} as Partial<ContextModuleConfig>),
|
|
@@ -247,7 +256,7 @@ export class ContextModuleConfigurator implements IContextModuleConfigurator {
|
|
|
247
256
|
},
|
|
248
257
|
});
|
|
249
258
|
|
|
250
|
-
// TODO - make less lazy
|
|
259
|
+
// TODO(#5119) - make less lazy
|
|
251
260
|
config.client ??= await (async (): Promise<ContextModuleConfig['client']> => {
|
|
252
261
|
const apiProvider = await this._getServiceProvider(init);
|
|
253
262
|
const contextClient = await apiProvider.createContextClient('json$');
|
|
@@ -263,7 +272,7 @@ export class ContextModuleConfigurator implements IContextModuleConfigurator {
|
|
|
263
272
|
client: {
|
|
264
273
|
fn: (query) => contextClient.query('v1', { query }, { selector: queryContextSelector }),
|
|
265
274
|
},
|
|
266
|
-
// TODO - might cast to checksum
|
|
275
|
+
// TODO(#5118) - might cast to checksum
|
|
267
276
|
key: (args) => JSON.stringify(args),
|
|
268
277
|
expire: this.defaultExpireTime,
|
|
269
278
|
},
|
|
@@ -277,7 +286,7 @@ export class ContextModuleConfigurator implements IContextModuleConfigurator {
|
|
|
277
286
|
);
|
|
278
287
|
},
|
|
279
288
|
},
|
|
280
|
-
// TODO - might cast to checksum
|
|
289
|
+
// TODO(#5118) - might cast to checksum
|
|
281
290
|
key: (args) => JSON.stringify(args),
|
|
282
291
|
expire: this.defaultExpireTime,
|
|
283
292
|
},
|
|
@@ -17,6 +17,7 @@ export class FusionContextSearchError extends Error {
|
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* The title of the error.
|
|
20
|
+
* @returns The error title.
|
|
20
21
|
*/
|
|
21
22
|
get title(): string {
|
|
22
23
|
return this.#details.title;
|
|
@@ -24,6 +25,7 @@ export class FusionContextSearchError extends Error {
|
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* The description of the error, if available.
|
|
28
|
+
* @returns The error description, or `undefined` if none was provided.
|
|
27
29
|
*/
|
|
28
30
|
get description(): string | undefined {
|
|
29
31
|
return this.#details.description;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './FusionContextSearchError';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { GetContextResponse } from '@equinor/fusion-framework-module-services/context/get';
|
|
2
|
+
|
|
3
|
+
import { parseContextItem } from './parse-context-item';
|
|
4
|
+
import type { ContextItem } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse the response from the GetContext API into a context item.
|
|
8
|
+
* @param response The response object containing the context item.
|
|
9
|
+
* @returns A promise that resolves to the context item.
|
|
10
|
+
*/
|
|
11
|
+
export const getContextSelector = async (response: Response): Promise<ContextItem> => {
|
|
12
|
+
const result = (await response.json()) as GetContextResponse<'v1'>;
|
|
13
|
+
return parseContextItem(result);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Deliberately re-exported from here to preserve the package's existing selectors entry point
|
|
17
|
+
export { queryContextSelector } from './query-context-selector';
|
|
18
|
+
export { relatedContextSelector } from './related-context-selector';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ApiVersion,
|
|
3
|
+
ApiContextEntity,
|
|
4
|
+
} from '@equinor/fusion-framework-module-services/context';
|
|
5
|
+
import type { GetContextResponse } from '@equinor/fusion-framework-module-services/context/get';
|
|
6
|
+
|
|
7
|
+
import type { ContextItem, ContextItemType } from './types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Parses the context type from the response of the GetContext API.
|
|
11
|
+
*
|
|
12
|
+
* @param type The type property from the GetContext response.
|
|
13
|
+
* @returns The parsed context item type.
|
|
14
|
+
*/
|
|
15
|
+
const parseContextType = (type: GetContextResponse<'v1'>['type']): ContextItemType => ({
|
|
16
|
+
id: type.id,
|
|
17
|
+
isChildType: type.isChildType,
|
|
18
|
+
parentTypeIds: type.parentTypeIds ?? [],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Parses an ApiContextEntity object into a ContextItem object.
|
|
23
|
+
* @param item The ApiContextEntity object to parse.
|
|
24
|
+
* @returns The parsed ContextItem object.
|
|
25
|
+
*/
|
|
26
|
+
export const parseContextItem = (item: ApiContextEntity<ApiVersion.v1>): ContextItem => {
|
|
27
|
+
return {
|
|
28
|
+
id: item.id,
|
|
29
|
+
externalId: item.externalId ?? undefined,
|
|
30
|
+
isActive: item.isActive,
|
|
31
|
+
isDeleted: item.isDeleted,
|
|
32
|
+
created: new Date(item.created),
|
|
33
|
+
source: item.source ?? undefined,
|
|
34
|
+
title: item.title ?? undefined,
|
|
35
|
+
type: parseContextType(item.type),
|
|
36
|
+
// TODO(#5115): parse and map the raw `value` payload into a typed context item value
|
|
37
|
+
value: item.value ?? {},
|
|
38
|
+
};
|
|
39
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { QueryContextResponse } from '@equinor/fusion-framework-module-services/context/query';
|
|
2
|
+
|
|
3
|
+
import { parseContextItem } from './parse-context-item';
|
|
4
|
+
import type { ContextItem } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse the response from the QueryContext API into an array of context items.
|
|
8
|
+
* @param response The response object.
|
|
9
|
+
* @returns A promise that resolves to an array of context items.
|
|
10
|
+
*/
|
|
11
|
+
export const queryContextSelector = async (response: Response): Promise<ContextItem[]> => {
|
|
12
|
+
const result = (await response.json()) as QueryContextResponse<'v1'>;
|
|
13
|
+
// parse each raw API entry into a ContextItem
|
|
14
|
+
return result.map(parseContextItem);
|
|
15
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RelatedContextResponse } from '@equinor/fusion-framework-module-services/context/related';
|
|
2
|
+
|
|
3
|
+
import { parseContextItem } from './parse-context-item';
|
|
4
|
+
import type { ContextItem } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse the response from the RelatedContext API into an array of context items.
|
|
8
|
+
* @param response The response object containing the related context items.
|
|
9
|
+
* @returns A promise that resolves to an array of ContextItem objects.
|
|
10
|
+
*/
|
|
11
|
+
export const relatedContextSelector = async (response: Response): Promise<ContextItem[]> => {
|
|
12
|
+
const result = (await response.json()) as RelatedContextResponse<'v1'>;
|
|
13
|
+
// parse each raw API entry into a ContextItem
|
|
14
|
+
return result.map(parseContextItem);
|
|
15
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* @property graphic - Optional graphical representation, either as a string or an object containing type and content.
|
|
17
17
|
* @property meta - Optional meta information, either as a string or an object containing type and content.
|
|
18
18
|
*
|
|
19
|
-
* @todo - convert to Zod schema for validation and type safety.
|
|
19
|
+
* @todo(#5122) - convert to Zod schema for validation and type safety.
|
|
20
20
|
*/
|
|
21
21
|
export type ContextItem<TType extends Record<string, unknown> = Record<string, unknown>> = {
|
|
22
22
|
id: string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// GUID pattern
|
|
2
|
+
const matchGUID =
|
|
3
|
+
/^(?:(?:[0-9a-fA-F]){8}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){12})$/;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Method will try to extract a context id from a path.
|
|
7
|
+
* The default matcher is a GUID pattern.
|
|
8
|
+
* Will iterate over the path and return the first match.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const path = '/apps/context/7fd97952-7fe6-409b-a6dc-292dbf0e50d7?dsadasdas#example';
|
|
13
|
+
* const contextId = extractContextIdFromPath(path); // '7fd97952-7fe6-409b-a6dc-292dbf0e50d7'
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param path string - the path to extract the context id from
|
|
17
|
+
* @param matcher RegExp - the pattern to match against
|
|
18
|
+
* @returns string | undefined - the context id or undefined
|
|
19
|
+
*/
|
|
20
|
+
export const extractContextIdFromPath = (
|
|
21
|
+
path: string,
|
|
22
|
+
matcher: RegExp = matchGUID,
|
|
23
|
+
): string | undefined =>
|
|
24
|
+
path
|
|
25
|
+
// remove leading slashes
|
|
26
|
+
.replace(/^\/+/, '')
|
|
27
|
+
// split path by slashes
|
|
28
|
+
.split('/')
|
|
29
|
+
// find the first path fragment that matches the matcher
|
|
30
|
+
.find((x) => x.match(matcher));
|
|
@@ -4,6 +4,9 @@ import type { ModuleType } from '@equinor/fusion-framework-module';
|
|
|
4
4
|
|
|
5
5
|
import type { ContextModule } from '../module';
|
|
6
6
|
import type { ContextItem } from '../types';
|
|
7
|
+
import { extractContextIdFromPath } from './extract-context-id-from-path';
|
|
8
|
+
|
|
9
|
+
export { extractContextIdFromPath } from './extract-context-id-from-path';
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
12
|
* Arguments for resolving a context from a path.
|
|
@@ -23,37 +26,10 @@ export type ContextPathResolveArgs = {
|
|
|
23
26
|
validate?: (contextId: string) => boolean;
|
|
24
27
|
};
|
|
25
28
|
|
|
26
|
-
// GUID pattern
|
|
29
|
+
// GUID pattern, used as the default context id validator
|
|
27
30
|
const matchGUID =
|
|
28
31
|
/^(?:(?:[0-9a-fA-F]){8}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){12})$/;
|
|
29
32
|
|
|
30
|
-
/**
|
|
31
|
-
* Method will try to extract a context id from a path.
|
|
32
|
-
* The default matcher is a GUID pattern.
|
|
33
|
-
* Will iterate over the path and return the first match.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```ts
|
|
37
|
-
* const path = '/apps/context/7fd97952-7fe6-409b-a6dc-292dbf0e50d7?dsadasdas#example';
|
|
38
|
-
* const contextId = extractContextIdFromPath(path); // '7fd97952-7fe6-409b-a6dc-292dbf0e50d7'
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @param path string - the path to extract the context id from
|
|
42
|
-
* @param matcher RegExp - the pattern to match against
|
|
43
|
-
* @returns string | undefined - the context id or undefined
|
|
44
|
-
*/
|
|
45
|
-
export const extractContextIdFromPath = (
|
|
46
|
-
path: string,
|
|
47
|
-
matcher: RegExp = matchGUID,
|
|
48
|
-
): string | undefined =>
|
|
49
|
-
path
|
|
50
|
-
// remove leading slashes
|
|
51
|
-
.replace(/^\/+/, '')
|
|
52
|
-
// split path by slashes
|
|
53
|
-
.split('/')
|
|
54
|
-
// find the first path fragment that matches the matcher
|
|
55
|
-
.find((x) => x.match(matcher));
|
|
56
|
-
|
|
57
33
|
const validateContextId = (contextId: string): boolean => !!contextId.match(matchGUID);
|
|
58
34
|
|
|
59
35
|
/**
|
|
@@ -104,6 +80,13 @@ export function resolveContextFromPath(
|
|
|
104
80
|
args?: ContextPathResolveArgs,
|
|
105
81
|
): (path: string) => Observable<ContextItem>;
|
|
106
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Resolves a context item from a path, using the provided extract/validate callbacks or the defaults.
|
|
85
|
+
* @param context The context module.
|
|
86
|
+
* @param args The arguments for resolving the path.
|
|
87
|
+
* @returns A function that takes a path and returns an Observable of the resolved context item.
|
|
88
|
+
* @throws Error if the extracted context id fails validation.
|
|
89
|
+
*/
|
|
107
90
|
export function resolveContextFromPath(
|
|
108
91
|
context: ModuleType<ContextModule>,
|
|
109
92
|
args?: ContextPathResolveArgs,
|
|
@@ -111,9 +94,11 @@ export function resolveContextFromPath(
|
|
|
111
94
|
return (path: string) => {
|
|
112
95
|
const { extract = extractContextIdFromPath, validate = validateContextId } = args ?? {};
|
|
113
96
|
const contextId = extract(path);
|
|
97
|
+
// no context id found in the path, nothing to resolve
|
|
114
98
|
if (!contextId) {
|
|
115
99
|
return EMPTY;
|
|
116
100
|
}
|
|
101
|
+
// only resolve the context if the extracted id passes validation
|
|
117
102
|
if (validate(contextId)) {
|
|
118
103
|
return context.contextClient.resolveContext(contextId);
|
|
119
104
|
}
|
|
@@ -30,6 +30,8 @@ export const resolveContextFromParent: ContextModuleConfig['resolveInitialContex
|
|
|
30
30
|
* @param options - Optional configuration for resolving the context path.
|
|
31
31
|
* @returns A function that accepts the module's reference and modules, and returns an Observable of the resolved initial context.
|
|
32
32
|
*/
|
|
33
|
+
// Deliberately co-located with resolveContextFromParent, which it composes with
|
|
34
|
+
// fusion-lint-disable-next-line single-export-per-file
|
|
33
35
|
export const resolveInitialContext =
|
|
34
36
|
(options?: {
|
|
35
37
|
path?: ContextPathResolveArgs;
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by genversion.
|
|
2
|
-
export const version = '8.0.
|
|
2
|
+
export const version = '8.0.1';
|
package/dist/esm/errors.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACjD,QAAQ,CAAC;IAET;;OAEG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,YACE,OASC,EACD,OAAsB;QAEtB,KAAK,CAAC,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACrD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF"}
|