@equinor/fusion-framework-module-context 8.0.2 → 9.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +82 -0
- package/README.md +78 -2
- package/dist/esm/ContextModuleConfig.js +2 -0
- package/dist/esm/ContextModuleConfig.js.map +1 -0
- package/dist/esm/ContextModuleConfigurator.interface.js +2 -0
- package/dist/esm/ContextModuleConfigurator.interface.js.map +1 -0
- package/dist/esm/ContextModuleConfigurator.js +211 -0
- package/dist/esm/ContextModuleConfigurator.js.map +1 -0
- package/dist/esm/ContextProvider.js +1 -1
- package/dist/esm/ContextProvider.js.map +1 -1
- package/dist/esm/__tests__/ContextModuleConfigurator.test.js +282 -0
- package/dist/esm/__tests__/ContextModuleConfigurator.test.js.map +1 -0
- package/dist/esm/__tests__/mock/context-mock.test.js +101 -0
- package/dist/esm/__tests__/mock/context-mock.test.js.map +1 -0
- package/dist/esm/__tests__/mock/create-context-item-factory.test.js +45 -0
- package/dist/esm/__tests__/mock/create-context-item-factory.test.js.map +1 -0
- package/dist/esm/__tests__/mock/create-context-items.test.js +46 -0
- package/dist/esm/__tests__/mock/create-context-items.test.js.map +1 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/mock/ContextMockConfigurator.js +206 -0
- package/dist/esm/mock/ContextMockConfigurator.js.map +1 -0
- package/dist/esm/mock/fixtures/create-context-item-factory.js +50 -0
- package/dist/esm/mock/fixtures/create-context-item-factory.js.map +1 -0
- package/dist/esm/mock/fixtures/create-context-items.js +53 -0
- package/dist/esm/mock/fixtures/create-context-items.js.map +1 -0
- package/dist/esm/mock/fixtures/index.js +22 -0
- package/dist/esm/mock/fixtures/index.js.map +1 -0
- package/dist/esm/mock/fixtures/string-to-seed.js +15 -0
- package/dist/esm/mock/fixtures/string-to-seed.js.map +1 -0
- package/dist/esm/mock/index.js +31 -0
- package/dist/esm/mock/index.js.map +1 -0
- package/dist/esm/mock/module.js +41 -0
- package/dist/esm/mock/module.js.map +1 -0
- package/dist/esm/module.js +35 -6
- package/dist/esm/module.js.map +1 -1
- package/dist/esm/{get-context-selector.js → selectors/get-context-selector.js} +1 -4
- package/dist/esm/selectors/get-context-selector.js.map +1 -0
- package/dist/esm/selectors/index.js +13 -0
- package/dist/esm/selectors/index.js.map +1 -0
- package/dist/esm/{query-context-selector.js → selectors/query-context-selector.js} +1 -1
- package/dist/esm/selectors/query-context-selector.js.map +1 -0
- package/dist/esm/{related-context-selector.js → selectors/related-context-selector.js} +1 -1
- package/dist/esm/selectors/related-context-selector.js.map +1 -0
- package/dist/esm/utils/enable-context.js +1 -1
- package/dist/esm/utils/enable-context.js.map +1 -1
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/parse-context-item.js.map +1 -0
- package/dist/esm/utils/resolve-initial-context.js +16 -7
- package/dist/esm/utils/resolve-initial-context.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/{configurator.d.ts → ContextModuleConfig.d.ts} +4 -64
- package/dist/types/ContextModuleConfigurator.d.ts +97 -0
- package/dist/types/{ContextConfigBuilder.d.ts → ContextModuleConfigurator.interface.d.ts} +27 -47
- package/dist/types/ContextProvider.d.ts +1 -1
- package/dist/types/__tests__/ContextModuleConfigurator.test.d.ts +1 -0
- package/dist/types/__tests__/mock/context-mock.test.d.ts +1 -0
- package/dist/types/__tests__/mock/create-context-item-factory.test.d.ts +1 -0
- package/dist/types/__tests__/mock/create-context-items.test.d.ts +1 -0
- package/dist/types/index.d.ts +3 -1
- package/dist/types/mock/ContextMockConfigurator.d.ts +155 -0
- package/dist/types/mock/fixtures/create-context-item-factory.d.ts +40 -0
- package/dist/types/mock/fixtures/create-context-items.d.ts +59 -0
- package/dist/types/mock/fixtures/index.d.ts +21 -0
- package/dist/types/mock/fixtures/string-to-seed.d.ts +12 -0
- package/dist/types/mock/index.d.ts +30 -0
- package/dist/types/mock/module.d.ts +38 -0
- package/dist/types/module.d.ts +5 -3
- package/dist/types/{get-context-selector.d.ts → selectors/get-context-selector.d.ts} +1 -3
- package/dist/types/selectors/index.d.ts +12 -0
- package/dist/types/{query-context-selector.d.ts → selectors/query-context-selector.d.ts} +1 -1
- package/dist/types/{related-context-selector.d.ts → selectors/related-context-selector.d.ts} +1 -1
- package/dist/types/utils/enable-context.d.ts +4 -5
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/{parse-context-item.d.ts → utils/parse-context-item.d.ts} +1 -1
- package/dist/types/utils/resolve-initial-context.d.ts +7 -4
- package/dist/types/version.d.ts +1 -1
- package/docs/data-model.md +134 -0
- package/docs/lifecycle.md +163 -0
- package/docs/recipes.md +89 -0
- package/package.json +36 -8
- package/src/ContextModuleConfig.ts +147 -0
- package/src/ContextModuleConfigurator.interface.ts +145 -0
- package/src/ContextModuleConfigurator.ts +295 -0
- package/src/ContextProvider.ts +1 -2
- package/src/__tests__/ContextModuleConfigurator.test.ts +412 -0
- package/src/__tests__/mock/context-mock.test.ts +137 -0
- package/src/__tests__/mock/create-context-item-factory.test.ts +60 -0
- package/src/__tests__/mock/create-context-items.test.ts +58 -0
- package/src/index.ts +3 -5
- package/src/mock/ContextMockConfigurator.ts +244 -0
- package/src/mock/fixtures/create-context-item-factory.ts +62 -0
- package/src/mock/fixtures/create-context-items.ts +80 -0
- package/src/mock/fixtures/index.ts +28 -0
- package/src/mock/fixtures/string-to-seed.ts +18 -0
- package/src/mock/index.ts +33 -0
- package/src/mock/module.ts +54 -0
- package/src/module.ts +45 -17
- package/src/{get-context-selector.ts → selectors/get-context-selector.ts} +2 -6
- package/src/selectors/index.ts +12 -0
- package/src/{query-context-selector.ts → selectors/query-context-selector.ts} +2 -2
- package/src/{related-context-selector.ts → selectors/related-context-selector.ts} +2 -2
- package/src/utils/enable-context.ts +6 -10
- package/src/utils/index.ts +2 -0
- package/src/{parse-context-item.ts → utils/parse-context-item.ts} +1 -1
- package/src/utils/resolve-initial-context.ts +26 -8
- package/src/version.ts +1 -1
- package/tsconfig.json +6 -0
- package/vitest.config.ts +11 -0
- package/dist/esm/ContextConfigBuilder.js +0 -175
- package/dist/esm/ContextConfigBuilder.js.map +0 -1
- package/dist/esm/configurator.js +0 -99
- package/dist/esm/configurator.js.map +0 -1
- package/dist/esm/get-context-selector.js.map +0 -1
- package/dist/esm/parse-context-item.js.map +0 -1
- package/dist/esm/query-context-selector.js.map +0 -1
- package/dist/esm/related-context-selector.js.map +0 -1
- package/src/ContextConfigBuilder.ts +0 -258
- package/src/configurator.ts +0 -295
- /package/dist/esm/{parse-context-item.js → utils/parse-context-item.js} +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { QueryContextResponse } from '@equinor/fusion-framework-module-services/context/query';
|
|
2
2
|
|
|
3
|
-
import { parseContextItem } from '
|
|
4
|
-
import type { ContextItem } from '
|
|
3
|
+
import { parseContextItem } from '../utils/parse-context-item';
|
|
4
|
+
import type { ContextItem } from '../types';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Parse the response from the QueryContext API into an array of context items.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { RelatedContextResponse } from '@equinor/fusion-framework-module-services/context/related';
|
|
2
2
|
|
|
3
|
-
import { parseContextItem } from '
|
|
4
|
-
import type { ContextItem } from '
|
|
3
|
+
import { parseContextItem } from '../utils/parse-context-item';
|
|
4
|
+
import type { ContextItem } from '../types';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Parse the response from the RelatedContext API into an array of context items.
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
+
import type { IModulesConfigurator } from '@equinor/fusion-framework-module';
|
|
1
2
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from '@equinor/fusion-framework-module';
|
|
6
|
-
import type { IContextModuleConfigurator } from '../configurator';
|
|
7
|
-
import type { ContextConfigBuilder } from '../ContextConfigBuilder';
|
|
3
|
+
ContextConfigBuilderCallback,
|
|
4
|
+
IContextModuleConfigurator,
|
|
5
|
+
} from '../ContextModuleConfigurator.interface';
|
|
8
6
|
|
|
9
7
|
import { module } from '../module';
|
|
10
8
|
|
|
@@ -12,7 +10,7 @@ import { module } from '../module';
|
|
|
12
10
|
* Enables context configuration for a given modules configurator.
|
|
13
11
|
*
|
|
14
12
|
* @param configurator - The modules configurator instance to which the context module will be added.
|
|
15
|
-
* @param builder - An optional function that receives
|
|
13
|
+
* @param builder - An optional function that receives the {@link IContextModuleConfigurator}. This function can be used to further configure the context module. It can be asynchronous.
|
|
16
14
|
*
|
|
17
15
|
* @remarks
|
|
18
16
|
* This utility function adds the context module to the provided configurator and optionally applies additional configuration using the provided builder function.
|
|
@@ -31,9 +29,7 @@ import { module } from '../module';
|
|
|
31
29
|
export const enableContext = (
|
|
32
30
|
// biome-ignore lint/suspicious/noExplicitAny: must be any to support all module types
|
|
33
31
|
configurator: IModulesConfigurator<any, any>,
|
|
34
|
-
builder?:
|
|
35
|
-
builder: ContextConfigBuilder<TDeps, ModuleInitializerArgs<IContextModuleConfigurator, TDeps>>,
|
|
36
|
-
) => void | Promise<void>,
|
|
32
|
+
builder?: ContextConfigBuilderCallback,
|
|
37
33
|
): void => {
|
|
38
34
|
configurator.addConfig({
|
|
39
35
|
module,
|
package/src/utils/index.ts
CHANGED
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
* - {@link resolveInitialContext} — default initial-context resolver (path → parent fallback).
|
|
6
6
|
* - {@link extractContextIdFromPath} — extract a GUID context ID from a URL path.
|
|
7
7
|
* - {@link resolveContextFromPath} — resolve a context item from a URL path.
|
|
8
|
+
* - {@link parseContextItem} — parse a raw API context entity into a `ContextItem`.
|
|
8
9
|
*
|
|
9
10
|
* @packageDocumentation
|
|
10
11
|
*/
|
|
11
12
|
export { enableContext } from './enable-context';
|
|
12
13
|
export { resolveInitialContext } from './resolve-initial-context';
|
|
13
14
|
export { extractContextIdFromPath, resolveContextFromPath } from './resolve-context-from-path';
|
|
15
|
+
export { parseContextItem } from './parse-context-item';
|
|
@@ -4,7 +4,7 @@ import type {
|
|
|
4
4
|
} from '@equinor/fusion-framework-module-services/context';
|
|
5
5
|
import type { GetContextResponse } from '@equinor/fusion-framework-module-services/context/get';
|
|
6
6
|
|
|
7
|
-
import type { ContextItem, ContextItemType } from '
|
|
7
|
+
import type { ContextItem, ContextItemType } from '../types';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Parses the context type from the response of the GetContext API.
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { ModulesInstance } from '@equinor/fusion-framework-module';
|
|
2
2
|
import type { ContextModule } from '../module';
|
|
3
|
-
import type { ContextModuleConfig } from '../
|
|
4
|
-
import { concat, EMPTY,
|
|
3
|
+
import type { ContextModuleConfig } from '../ContextModuleConfig';
|
|
4
|
+
import { concat, EMPTY, first, of } from 'rxjs';
|
|
5
|
+
import resolveContextFromPath, { type ContextPathResolveArgs } from './resolve-context-from-path';
|
|
6
|
+
import type { NavigationModule } from '@equinor/fusion-framework-module-navigation';
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Resolves the initial context from the parent module.
|
|
@@ -11,6 +13,7 @@ import { concat, EMPTY, of, take } from 'rxjs';
|
|
|
11
13
|
*/
|
|
12
14
|
export const resolveContextFromParent: ContextModuleConfig['resolveInitialContext'] = ({ ref }) => {
|
|
13
15
|
const parentContext = (ref as ModulesInstance<[ContextModule]>)?.context;
|
|
16
|
+
// check if the parent has context module
|
|
14
17
|
if (!parentContext) {
|
|
15
18
|
// No parent context available — either portal level or parent lacks context module.
|
|
16
19
|
return EMPTY;
|
|
@@ -22,19 +25,34 @@ export const resolveContextFromParent: ContextModuleConfig['resolveInitialContex
|
|
|
22
25
|
/**
|
|
23
26
|
* Resolves the initial context for a Fusion Framework context module.
|
|
24
27
|
*
|
|
25
|
-
*
|
|
26
|
-
* URL-based resolution is handled by the context-navigation plugin.
|
|
28
|
+
* will try to resolve the initial context from the path, and if that fails, it will try to resolve the context from the parent.
|
|
27
29
|
*
|
|
30
|
+
* @param options - Optional configuration for resolving the context path.
|
|
28
31
|
* @returns A function that accepts the module's reference and modules, and returns an Observable of the resolved initial context.
|
|
29
32
|
*/
|
|
33
|
+
|
|
30
34
|
// Deliberately co-located with resolveContextFromParent, which it composes with
|
|
31
35
|
// fusion-lint-disable-next-line single-export-per-file
|
|
32
36
|
export const resolveInitialContext =
|
|
33
|
-
(
|
|
37
|
+
(options?: {
|
|
38
|
+
path?: ContextPathResolveArgs;
|
|
39
|
+
}): Required<ContextModuleConfig>['resolveInitialContext'] =>
|
|
34
40
|
({ ref, modules }) => {
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
|
|
41
|
+
const { context, navigation } = modules;
|
|
42
|
+
// create a path resolver from the context module
|
|
43
|
+
const pathResolver = resolveContextFromPath(context, options?.path);
|
|
44
|
+
// use the path from the navigation module, or the path from the parent navigation module
|
|
45
|
+
const pathname =
|
|
46
|
+
navigation?.path.pathname ??
|
|
47
|
+
(ref as Partial<ModulesInstance<[NavigationModule]>>).navigation?.path.pathname;
|
|
48
|
+
// try to resolve the context from the path, and if that fails, try to resolve the context from the parent
|
|
49
|
+
return concat(
|
|
50
|
+
pathname ? pathResolver(pathname) : EMPTY,
|
|
51
|
+
resolveContextFromParent({ ref, modules }),
|
|
52
|
+
).pipe(
|
|
53
|
+
// having no initial context is valid; a default avoids first() throwing EmptyError for it
|
|
54
|
+
first(undefined, undefined),
|
|
55
|
+
);
|
|
38
56
|
};
|
|
39
57
|
|
|
40
58
|
export default resolveInitialContext;
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by genversion.
|
|
2
|
-
export const version = '
|
|
2
|
+
export const version = '9.0.0-next.0';
|
package/tsconfig.json
CHANGED
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineProject } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
import { name, version } from './package.json' with { type: 'json' };
|
|
4
|
+
|
|
5
|
+
export default defineProject({
|
|
6
|
+
test: {
|
|
7
|
+
environment: 'node',
|
|
8
|
+
include: ['src/__tests__/**/*.test.ts'],
|
|
9
|
+
name: `${name}@${version}`,
|
|
10
|
+
},
|
|
11
|
+
});
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A builder class for configuring and customizing context module behavior within the Fusion Framework.
|
|
3
|
-
*
|
|
4
|
-
* `ContextConfigBuilder` provides a fluent API for setting up various aspects of context management,
|
|
5
|
-
* including context type, filtering, parent context connection, parameter resolution, validation,
|
|
6
|
-
* path extraction/generation, and client configuration for fetching context items.
|
|
7
|
-
*
|
|
8
|
-
* @typeParam TModules - An array of modules that extend `AnyModule`. Defaults to an empty array.
|
|
9
|
-
* @typeParam TInit - The initializer arguments for the module, extending `ModuleInitializerArgs`.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* const builder = new ContextConfigBuilder(init);
|
|
14
|
-
* builder.setContextType(['ProjectMaster']);
|
|
15
|
-
* builder.setContextFilter(items => items.filter(ctx => ctx.isActive));
|
|
16
|
-
* builder.setContextClient({ get: fetchContextItem, query: fetchContextItems });
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @remarks
|
|
20
|
-
* - Use the provided setter methods to customize context behavior as needed.
|
|
21
|
-
* - The builder pattern allows chaining configuration methods for clarity and convenience.
|
|
22
|
-
* - The `requireInstance` method enables asynchronous retrieval of module instances by name.
|
|
23
|
-
*
|
|
24
|
-
* @todo(#5120) - this should extend the BaseConfigBuilder
|
|
25
|
-
*
|
|
26
|
-
* @see ContextModuleConfig
|
|
27
|
-
* @see ModuleInitializerArgs
|
|
28
|
-
*/
|
|
29
|
-
export class ContextConfigBuilder {
|
|
30
|
-
config;
|
|
31
|
-
#init;
|
|
32
|
-
/**
|
|
33
|
-
* Creates a new `ContextConfigBuilder`.
|
|
34
|
-
* @param init - Module initializer arguments used to resolve module instances.
|
|
35
|
-
* @param config - The partial context module config to build upon.
|
|
36
|
-
*/
|
|
37
|
-
constructor(init, config = {}) {
|
|
38
|
-
this.config = config;
|
|
39
|
-
this.#init = init;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Requires a module instance by key or name.
|
|
43
|
-
* @param module - The key or name of the module to resolve.
|
|
44
|
-
* @returns A promise that resolves to the requested module instance.
|
|
45
|
-
*/
|
|
46
|
-
requireInstance(module) {
|
|
47
|
-
return this.#init.requireInstance(module);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Sets the context type for the current configuration.
|
|
51
|
-
*
|
|
52
|
-
* @param type - The context type to assign, as defined by `ContextModuleConfig['contextType']`.
|
|
53
|
-
*/
|
|
54
|
-
setContextType(type) {
|
|
55
|
-
this.config.contextType = type;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Sets the context filter function for the configuration.
|
|
59
|
-
*
|
|
60
|
-
* @param filter - A function that determines whether a context should be included, as defined by `ContextModuleConfig['contextFilter']`.
|
|
61
|
-
*/
|
|
62
|
-
setContextFilter(filter) {
|
|
63
|
-
this.config.contextFilter = filter;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Sets the function or configuration used to connect to a parent context.
|
|
67
|
-
*
|
|
68
|
-
* @param connect - The function or configuration that defines how to connect to the parent context.
|
|
69
|
-
*/
|
|
70
|
-
connectParentContext(connect) {
|
|
71
|
-
this.config.connectParentContext = connect;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Sets the function used to provide context parameters for the module configuration.
|
|
75
|
-
*
|
|
76
|
-
* @param fn - A function conforming to the `contextParameterFn` type defined in `ContextModuleConfig`.
|
|
77
|
-
*/
|
|
78
|
-
setContextParameterFn(fn) {
|
|
79
|
-
this.config.contextParameterFn = fn;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Sets the function used to validate the context within the configuration.
|
|
83
|
-
*
|
|
84
|
-
* @param fn - A function that implements the `validateContext` signature from `ContextModuleConfig`.
|
|
85
|
-
*/
|
|
86
|
-
setValidateContext(fn) {
|
|
87
|
-
this.config.validateContext = fn;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Sets the function used to resolve the context for the module configuration.
|
|
91
|
-
*
|
|
92
|
-
* @param fn - A function that defines how the context should be resolved, conforming to the `resolveContext` type from `ContextModuleConfig`.
|
|
93
|
-
*/
|
|
94
|
-
setResolveContext(fn) {
|
|
95
|
-
this.config.resolveContext = fn;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Sets the function responsible for extracting the context ID from a given path.
|
|
99
|
-
*
|
|
100
|
-
* @param fn - A function that defines how to extract the context ID from a path.
|
|
101
|
-
* This function should match the type defined in `ContextModuleConfig['extractContextIdFromPath']`.
|
|
102
|
-
*/
|
|
103
|
-
setContextPathExtractor(fn) {
|
|
104
|
-
this.config.extractContextIdFromPath = fn;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Sets the function responsible for generating a path from the context.
|
|
108
|
-
*
|
|
109
|
-
* @param fn - A function that takes a context and generates a corresponding path.
|
|
110
|
-
*/
|
|
111
|
-
setContextPathGenerator(fn) {
|
|
112
|
-
this.config.generatePathFromContext = fn;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Sets the function used to resolve the initial context during module post-initialization.
|
|
116
|
-
*
|
|
117
|
-
* @param fn - A function that returns an observable input emitting the initial context item.
|
|
118
|
-
* The default resolver extracts a context ID from the navigation path, falling
|
|
119
|
-
* back to the parent provider's current context.
|
|
120
|
-
*/
|
|
121
|
-
setResolveInitialContext(fn) {
|
|
122
|
-
this.config.resolveInitialContext = fn;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Sets the context client configuration for fetching context items.
|
|
126
|
-
*
|
|
127
|
-
* This method allows you to provide custom query functions or query constructor options
|
|
128
|
-
* for retrieving single context items (`get`), querying multiple context items (`query`),
|
|
129
|
-
* and optionally fetching related context items (`related`). Each query can be provided
|
|
130
|
-
* as either a function or a configuration object. The expiration time for cached results
|
|
131
|
-
* can also be specified.
|
|
132
|
-
*
|
|
133
|
-
* @param client - An object containing the query functions or options for `get`, `query`, and optionally `related` context items.
|
|
134
|
-
* @param expire - Optional. The expiration time (in milliseconds) for cached query results. Defaults to 1 minute.
|
|
135
|
-
*/
|
|
136
|
-
setContextClient(client, expire = 1 * 60 * 1000) {
|
|
137
|
-
const clientConfig = {
|
|
138
|
-
get: typeof client.get === 'function'
|
|
139
|
-
? {
|
|
140
|
-
key: ({ id }) => id,
|
|
141
|
-
client: {
|
|
142
|
-
fn: client.get,
|
|
143
|
-
},
|
|
144
|
-
expire,
|
|
145
|
-
}
|
|
146
|
-
: client.get,
|
|
147
|
-
query: typeof client.query === 'function'
|
|
148
|
-
? {
|
|
149
|
-
// TODO(#5118) - might cast to checksum
|
|
150
|
-
key: (args) => JSON.stringify(args),
|
|
151
|
-
client: {
|
|
152
|
-
fn: client.query,
|
|
153
|
-
},
|
|
154
|
-
expire,
|
|
155
|
-
}
|
|
156
|
-
: client.query,
|
|
157
|
-
};
|
|
158
|
-
this.config.client = clientConfig;
|
|
159
|
-
// only override the related-context client config if one was provided
|
|
160
|
-
if (client.related) {
|
|
161
|
-
clientConfig.related =
|
|
162
|
-
typeof client.related === 'function'
|
|
163
|
-
? {
|
|
164
|
-
// TODO(#5118) - might cast to checksum
|
|
165
|
-
key: (args) => JSON.stringify(args),
|
|
166
|
-
client: {
|
|
167
|
-
fn: client.related,
|
|
168
|
-
},
|
|
169
|
-
expire,
|
|
170
|
-
}
|
|
171
|
-
: client.related;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
//# sourceMappingURL=ContextConfigBuilder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"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C,MAAM,CAAC,OAAO,CAAC;QACvB,CAAC;IACH,CAAC;CACF"}
|
package/dist/esm/configurator.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { getContextSelector, queryContextSelector, relatedContextSelector, } from './get-context-selector';
|
|
2
|
-
import { ContextConfigBuilder } from './ContextConfigBuilder';
|
|
3
|
-
import resolveInitialContext from './utils/resolve-initial-context';
|
|
4
|
-
/**
|
|
5
|
-
* Default implementation of {@link IContextModuleConfigurator}.
|
|
6
|
-
*
|
|
7
|
-
* Collects {@link ContextConfigBuilderCallback} registrations and, when
|
|
8
|
-
* {@link createConfig} is called, runs them in order against a
|
|
9
|
-
* {@link ContextConfigBuilder} to produce the final {@link ContextModuleConfig}.
|
|
10
|
-
*
|
|
11
|
-
* If no custom client is configured, the configurator falls back to
|
|
12
|
-
* creating one from the {@link ServicesModule} API provider.
|
|
13
|
-
*/
|
|
14
|
-
export class ContextModuleConfigurator {
|
|
15
|
-
/** Default cache TTL (in ms) for context query results. */
|
|
16
|
-
defaultExpireTime = 1 * 60 * 1000;
|
|
17
|
-
#configBuilders = [];
|
|
18
|
-
/** @inheritdoc */
|
|
19
|
-
addConfigBuilder(init) {
|
|
20
|
-
this.#configBuilders.push(init);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Resolves the services API provider, preferring the local module
|
|
24
|
-
* instance and falling back to the parent module.
|
|
25
|
-
*
|
|
26
|
-
* @param init - Module initializer arguments.
|
|
27
|
-
* @returns The resolved API provider.
|
|
28
|
-
* @throws Error if no services module is available.
|
|
29
|
-
*/
|
|
30
|
-
async _getServiceProvider(init) {
|
|
31
|
-
// prefer the local services module instance if available
|
|
32
|
-
if (init.hasModule('services')) {
|
|
33
|
-
return init.requireInstance('services');
|
|
34
|
-
}
|
|
35
|
-
const parentServiceModule = init.ref?.services;
|
|
36
|
-
// fall back to the parent module's services instance
|
|
37
|
-
if (!parentServiceModule) {
|
|
38
|
-
throw Error('no service services provider configures [ServicesModule]');
|
|
39
|
-
}
|
|
40
|
-
return parentServiceModule;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Runs all registered config builders and produces the final
|
|
44
|
-
* {@link ContextModuleConfig}.
|
|
45
|
-
*
|
|
46
|
-
* If no `resolveInitialContext` was set, the default path + parent
|
|
47
|
-
* resolver is used. If no `client` was set, one is created from the
|
|
48
|
-
* {@link ServicesModule} API provider.
|
|
49
|
-
*
|
|
50
|
-
* @param init - Module initializer arguments including dependency instances.
|
|
51
|
-
* @returns The fully resolved context module configuration.
|
|
52
|
-
*/
|
|
53
|
-
async createConfig(init) {
|
|
54
|
-
// run each registered config builder in sequence, merging results into the accumulated config
|
|
55
|
-
const config = await this.#configBuilders.reduce(async (cur, cb) => {
|
|
56
|
-
// biome-ignore lint/suspicious/noExplicitAny: builder generics are erased here — `unknown` breaks assignability of the concrete `ContextConfigBuilder` instance passed to consumer callbacks
|
|
57
|
-
const builder = new ContextConfigBuilder(init, await cur);
|
|
58
|
-
await Promise.resolve(cb(builder));
|
|
59
|
-
// Merge this builder's config into the accumulator for the next iteration.
|
|
60
|
-
return Object.assign(cur, builder.config);
|
|
61
|
-
}, Promise.resolve({}));
|
|
62
|
-
config.resolveInitialContext ??= resolveInitialContext();
|
|
63
|
-
// TODO(#5119) - make less lazy
|
|
64
|
-
config.client ??= await (async () => {
|
|
65
|
-
const apiProvider = await this._getServiceProvider(init);
|
|
66
|
-
const contextClient = await apiProvider.createContextClient('json$');
|
|
67
|
-
return {
|
|
68
|
-
get: {
|
|
69
|
-
client: {
|
|
70
|
-
fn: (args) => contextClient.get('v1', args, { selector: getContextSelector }),
|
|
71
|
-
},
|
|
72
|
-
key: ({ id }) => id,
|
|
73
|
-
expire: this.defaultExpireTime,
|
|
74
|
-
},
|
|
75
|
-
query: {
|
|
76
|
-
client: {
|
|
77
|
-
fn: (query) => contextClient.query('v1', { query }, { selector: queryContextSelector }),
|
|
78
|
-
},
|
|
79
|
-
// TODO(#5118) - might cast to checksum
|
|
80
|
-
key: (args) => JSON.stringify(args),
|
|
81
|
-
expire: this.defaultExpireTime,
|
|
82
|
-
},
|
|
83
|
-
related: {
|
|
84
|
-
client: {
|
|
85
|
-
fn: (args) => {
|
|
86
|
-
return contextClient.related('v1', { id: args.item.id, query: { filter: args.filter } }, { selector: relatedContextSelector });
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
// TODO(#5118) - might cast to checksum
|
|
90
|
-
key: (args) => JSON.stringify(args),
|
|
91
|
-
expire: this.defaultExpireTime,
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
})();
|
|
95
|
-
return config;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
export default ContextModuleConfigurator;
|
|
99
|
-
//# sourceMappingURL=configurator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"configurator.js","sourceRoot":"","sources":["../../src/configurator.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAShC,OAAO,EAAE,oBAAoB,EAAqC,MAAM,wBAAwB,CAAC;AAEjG,OAAO,qBAAqB,MAAM,iCAAiC,CAAC;AA4JpE;;;;;;;;;GASG;AACH,MAAM,OAAO,yBAAyB;IACpC,2DAA2D;IAC3D,iBAAiB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;IAElC,eAAe,GAAwC,EAAE,CAAC;IAE1D,kBAAkB;IAClB,gBAAgB,CAAC,IAAkC;QACjD,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC;IAED;;;;;;;OAOG;IACO,KAAK,CAAC,mBAAmB,CACjC,IAAyE;QAEzE,yDAAyD;QACzD,IAAI,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;YAC/B,OAAO,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;QAC1C,CAAC;QACD,M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E;IACnF,MAAM,MAAM,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAA6B,CAAC;IACnE,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC;AAClC,CAAC,CAAC;AAEF,8FAA8F;AAC9F,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parse-context-item.js","sourceRoot":"","sources":["../../src/parse-context-item.ts"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,MAAM,gBAAgB,GAAG,CAAC,IAAsC,EAAmB,EAAE,CAAC,CAAC;IACrF,EAAE,EAAE,IAAI,CAAC,EAAE;IACX,WAAW,EAAE,IAAI,CAAC,WAAW;IAC7B,aAAa,EAAE,IAAI,CAAC,aAAa,IAAI,EAAE;CACxC,CAAC,CAAC;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,IAAqC,EAAe,EAAE;IACrF,OAAO;QACL,EAAE,EAAE,IAAI,CAAC,EAAE;QACX,UAAU,EAAE,IAAI,CAAC,UAAU,IAAI,SAAS;QACxC,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,OAAO,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;QAC/B,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,SAAS;QAChC,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,SAAS;QAC9B,IAAI,EAAE,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC;QACjC,qFAAqF;QACrF,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,EAAE;KACxB,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"query-context-selector.js","sourceRoot":"","sources":["../../src/query-context-selector.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGxD;;;;GAIG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,EAAE,QAAkB,EAA0B,EAAE;IACvF,MAAM,MAAM,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAA+B,CAAC;IACrE,8CAA8C;IAC9C,OAAO,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AACtC,CAAC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"related-context-selector.js","sourceRoot":"","sources":["../../src/related-context-selector.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGxD;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,KAAK,EAAE,QAAkB,EAA0B,EAAE;IACzF,MAAM,MAAM,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAiC,CAAC;IACvE,8CAA8C;IAC9C,OAAO,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AACtC,CAAC,CAAC"}
|