@equinor/fusion-framework-module-context 8.0.0 → 8.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +70 -0
  3. package/dist/esm/ContextConfigBuilder.js +17 -6
  4. package/dist/esm/ContextConfigBuilder.js.map +1 -1
  5. package/dist/esm/ContextProvider.js +71 -15
  6. package/dist/esm/ContextProvider.js.map +1 -1
  7. package/dist/esm/client/ContextClient.js +13 -3
  8. package/dist/esm/client/ContextClient.js.map +1 -1
  9. package/dist/esm/configurator.js +10 -11
  10. package/dist/esm/configurator.js.map +1 -1
  11. package/dist/esm/{errors.js → errors/FusionContextSearchError.js} +3 -1
  12. package/dist/esm/errors/FusionContextSearchError.js.map +1 -0
  13. package/dist/esm/errors/index.js +2 -0
  14. package/dist/esm/errors/index.js.map +1 -0
  15. package/dist/esm/get-context-selector.js +14 -0
  16. package/dist/esm/get-context-selector.js.map +1 -0
  17. package/dist/esm/parse-context-item.js +31 -0
  18. package/dist/esm/parse-context-item.js.map +1 -0
  19. package/dist/esm/query-context-selector.js +12 -0
  20. package/dist/esm/query-context-selector.js.map +1 -0
  21. package/dist/esm/related-context-selector.js +12 -0
  22. package/dist/esm/related-context-selector.js.map +1 -0
  23. package/dist/esm/utils/extract-context-id-from-path.js +35 -0
  24. package/dist/esm/utils/extract-context-id-from-path.js.map +1 -0
  25. package/dist/esm/utils/resolve-context-from-path.js +11 -22
  26. package/dist/esm/utils/resolve-context-from-path.js.map +1 -1
  27. package/dist/esm/utils/resolve-initial-context.js +11 -15
  28. package/dist/esm/utils/resolve-initial-context.js.map +1 -1
  29. package/dist/esm/version.js +1 -1
  30. package/dist/tsconfig.tsbuildinfo +1 -1
  31. package/dist/types/ContextConfigBuilder.d.ts +16 -1
  32. package/dist/types/ContextProvider.d.ts +45 -3
  33. package/dist/types/client/ContextClient.d.ts +8 -1
  34. package/dist/types/{errors.d.ts → errors/FusionContextSearchError.d.ts} +2 -0
  35. package/dist/types/errors/index.d.ts +1 -0
  36. package/dist/types/get-context-selector.d.ts +9 -0
  37. package/dist/types/parse-context-item.d.ts +8 -0
  38. package/dist/types/query-context-selector.d.ts +7 -0
  39. package/dist/types/related-context-selector.d.ts +7 -0
  40. package/dist/types/types.d.ts +1 -1
  41. package/dist/types/utils/extract-context-id-from-path.d.ts +22 -0
  42. package/dist/types/utils/resolve-context-from-path.d.ts +9 -16
  43. package/dist/types/utils/resolve-initial-context.d.ts +3 -6
  44. package/dist/types/version.d.ts +1 -1
  45. package/package.json +15 -11
  46. package/src/ContextConfigBuilder.ts +32 -7
  47. package/src/ContextProvider.ts +86 -21
  48. package/src/client/ContextClient.ts +13 -3
  49. package/src/configurator.ts +16 -12
  50. package/src/{errors.ts → errors/FusionContextSearchError.ts} +2 -0
  51. package/src/errors/index.ts +1 -0
  52. package/src/get-context-selector.ts +18 -0
  53. package/src/parse-context-item.ts +39 -0
  54. package/src/query-context-selector.ts +15 -0
  55. package/src/related-context-selector.ts +15 -0
  56. package/src/types.ts +1 -1
  57. package/src/utils/extract-context-id-from-path.ts +39 -0
  58. package/src/utils/resolve-context-from-path.ts +21 -28
  59. package/src/utils/resolve-initial-context.ts +11 -23
  60. package/src/version.ts +1 -1
  61. package/dist/esm/errors.js.map +0 -1
  62. package/dist/esm/selectors.js +0 -58
  63. package/dist/esm/selectors.js.map +0 -1
  64. package/dist/types/selectors.d.ts +0 -19
  65. package/src/selectors.ts +0 -70
@@ -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
  /**
@@ -75,6 +51,10 @@ const validateContextId = (contextId: string): boolean => !!contextId.match(matc
75
51
  *
76
52
  * @param context The context module.
77
53
  * @returns A function that takes a path and returns an Observable of the resolved context item.
54
+ * @deprecated Replaced by the context-navigation plugin which resolves
55
+ * initial context from the URL via adapter `decode()` during module initialization.
56
+ * Portal-level code should use `enableContextNavigation` which handles
57
+ * URL-to-context resolution automatically. Will be removed in a future major version.
78
58
  */
79
59
  export function resolveContextFromPath(
80
60
  context: ModuleType<ContextModule>,
@@ -98,12 +78,23 @@ export function resolveContextFromPath(
98
78
  * @param context The context module.
99
79
  * @param args The arguments for resolving the path.
100
80
  * @returns A function that takes a path and returns an Observable of the resolved context item.
81
+ * @deprecated Replaced by the context-navigation plugin which resolves
82
+ * initial context from the URL via adapter `decode()` during module initialization.
83
+ * Portal-level code should use `enableContextNavigation` which handles
84
+ * URL-to-context resolution automatically. Will be removed in a future major version.
101
85
  */
102
86
  export function resolveContextFromPath(
103
87
  context: ModuleType<ContextModule>,
104
88
  args?: ContextPathResolveArgs,
105
89
  ): (path: string) => Observable<ContextItem>;
106
90
 
91
+ /**
92
+ * Resolves a context item from a path, using the provided extract/validate callbacks or the defaults.
93
+ * @param context The context module.
94
+ * @param args The arguments for resolving the path.
95
+ * @returns A function that takes a path and returns an Observable of the resolved context item.
96
+ * @throws Error if the extracted context id fails validation.
97
+ */
107
98
  export function resolveContextFromPath(
108
99
  context: ModuleType<ContextModule>,
109
100
  args?: ContextPathResolveArgs,
@@ -111,9 +102,11 @@ export function resolveContextFromPath(
111
102
  return (path: string) => {
112
103
  const { extract = extractContextIdFromPath, validate = validateContextId } = args ?? {};
113
104
  const contextId = extract(path);
105
+ // no context id found in the path, nothing to resolve
114
106
  if (!contextId) {
115
107
  return EMPTY;
116
108
  }
109
+ // only resolve the context if the extracted id passes validation
117
110
  if (validate(contextId)) {
118
111
  return context.contextClient.resolveContext(contextId);
119
112
  }
@@ -1,10 +1,7 @@
1
1
  import type { ModulesInstance } from '@equinor/fusion-framework-module';
2
2
  import type { ContextModule } from '../module';
3
3
  import type { ContextModuleConfig } from '../configurator';
4
- import { concat, EMPTY, first, of } from 'rxjs';
5
-
6
- import { type ContextPathResolveArgs, resolveContextFromPath } from './resolve-context-from-path';
7
- import type { NavigationModule } from '@equinor/fusion-framework-module-navigation';
4
+ import { concat, EMPTY, of, take } from 'rxjs';
8
5
 
9
6
  /**
10
7
  * Resolves the initial context from the parent module.
@@ -14,9 +11,9 @@ import type { NavigationModule } from '@equinor/fusion-framework-module-navigati
14
11
  */
15
12
  export const resolveContextFromParent: ContextModuleConfig['resolveInitialContext'] = ({ ref }) => {
16
13
  const parentContext = (ref as ModulesInstance<[ContextModule]>)?.context;
17
- // check if the parent has context module
18
14
  if (!parentContext) {
19
- throw Error(['resolveContextFromNavigation', 'ref does not support context!'].join('\n'));
15
+ // No parent context available either portal level or parent lacks context module.
16
+ return EMPTY;
20
17
  }
21
18
  // return the current context from the parent or empty if the parent does not have a context
22
19
  return parentContext.currentContext ? of(parentContext.currentContext) : EMPTY;
@@ -25,28 +22,19 @@ export const resolveContextFromParent: ContextModuleConfig['resolveInitialContex
25
22
  /**
26
23
  * Resolves the initial context for a Fusion Framework context module.
27
24
  *
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.
25
+ * Attempts to resolve the initial context from the parent context provider.
26
+ * URL-based resolution is handled by the context-navigation plugin.
29
27
  *
30
- * @param options - Optional configuration for resolving the context path.
31
28
  * @returns A function that accepts the module's reference and modules, and returns an Observable of the resolved initial context.
32
29
  */
30
+ // Deliberately co-located with resolveContextFromParent, which it composes with
31
+ // fusion-lint-disable-next-line single-export-per-file
33
32
  export const resolveInitialContext =
34
- (options?: {
35
- path?: ContextPathResolveArgs;
36
- }): Required<ContextModuleConfig>['resolveInitialContext'] =>
33
+ (): Required<ContextModuleConfig>['resolveInitialContext'] =>
37
34
  ({ ref, modules }) => {
38
- const { context, navigation } = modules;
39
- // create a path resolver from the context module
40
- const pathResolver = resolveContextFromPath(context, options?.path);
41
- // use the path from the navigation module, or the path from the parent navigation module
42
- const pathname =
43
- navigation?.path.pathname ??
44
- (ref as Partial<ModulesInstance<[NavigationModule]>>).navigation?.path.pathname;
45
- // try to resolve the context from the path, and if that fails, try to resolve the context from the parent
46
- return concat(
47
- pathname ? pathResolver(pathname) : EMPTY,
48
- resolveContextFromParent({ ref, modules }),
49
- ).pipe(first());
35
+ // Resolve from parent context if available.
36
+ // URL-based resolution is handled by the context-navigation plugin.
37
+ return concat(resolveContextFromParent({ ref, modules })).pipe(take(1));
50
38
  };
51
39
 
52
40
  export default resolveInitialContext;
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '8.0.0';
2
+ export const version = '8.0.2';
@@ -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"}
@@ -1,58 +0,0 @@
1
- /**
2
- * Parses the context type from the response of the GetContext API.
3
- *
4
- * @param type The type property from the GetContext response.
5
- * @returns The parsed context item type.
6
- */
7
- const parseContextType = (type) => ({
8
- id: type.id,
9
- isChildType: type.isChildType,
10
- parentTypeIds: type.parentTypeIds ?? [],
11
- });
12
- /**
13
- * Parses an ApiContextEntity object into a ContextItem object.
14
- * @param item The ApiContextEntity object to parse.
15
- * @returns The parsed ContextItem object.
16
- */
17
- const parseContextItem = (item) => {
18
- return {
19
- id: item.id,
20
- externalId: item.externalId ?? undefined,
21
- isActive: item.isActive,
22
- isDeleted: item.isDeleted,
23
- created: new Date(item.created),
24
- source: item.source ?? undefined,
25
- title: item.title ?? undefined,
26
- type: parseContextType(item.type),
27
- // TODO
28
- value: item.value ?? {},
29
- };
30
- };
31
- /**
32
- * Parse the response from the GetContext API into a context item.
33
- * @param response The response object containing the context item.
34
- * @returns A promise that resolves to the context item.
35
- */
36
- export const getContextSelector = async (response) => {
37
- const result = (await response.json());
38
- return parseContextItem(result);
39
- };
40
- /**
41
- * Parse the response from the QueryContext API into an array of context items.
42
- * @param response The response object.
43
- * @returns A promise that resolves to an array of context items.
44
- */
45
- export const queryContextSelector = async (response) => {
46
- const result = (await response.json());
47
- return result.map(parseContextItem);
48
- };
49
- /**
50
- * Parse the response from the RelatedContext API into an array of context items.
51
- * @param response The response object containing the related context items.
52
- * @returns A promise that resolves to an array of ContextItem objects.
53
- */
54
- export const relatedContextSelector = async (response) => {
55
- const result = (await response.json());
56
- return result.map(parseContextItem);
57
- };
58
- //# sourceMappingURL=selectors.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"selectors.js","sourceRoot":"","sources":["../../src/selectors.ts"],"names":[],"mappings":"AASA;;;;;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,gBAAgB,GAAG,CAAC,IAAqC,EAAe,EAAE;IAC9E,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,OAAO;QACP,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,EAAE;KACxB,CAAC;AACJ,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,QAAkB,EAAwB,EAAE;IACnF,MAAM,MAAM,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAA6B,CAAC;IACnE,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC;AAClC,CAAC,CAAC;AAEF;;;;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,OAAO,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AACtC,CAAC,CAAC;AAEF;;;;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,OAAO,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AACtC,CAAC,CAAC"}
@@ -1,19 +0,0 @@
1
- import type { ContextItem } from './types';
2
- /**
3
- * Parse the response from the GetContext API into a context item.
4
- * @param response The response object containing the context item.
5
- * @returns A promise that resolves to the context item.
6
- */
7
- export declare const getContextSelector: (response: Response) => Promise<ContextItem>;
8
- /**
9
- * Parse the response from the QueryContext API into an array of context items.
10
- * @param response The response object.
11
- * @returns A promise that resolves to an array of context items.
12
- */
13
- export declare const queryContextSelector: (response: Response) => Promise<ContextItem[]>;
14
- /**
15
- * Parse the response from the RelatedContext API into an array of context items.
16
- * @param response The response object containing the related context items.
17
- * @returns A promise that resolves to an array of ContextItem objects.
18
- */
19
- export declare const relatedContextSelector: (response: Response) => Promise<ContextItem[]>;
package/src/selectors.ts DELETED
@@ -1,70 +0,0 @@
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
- import type { QueryContextResponse } from '@equinor/fusion-framework-module-services/context/query';
7
- import type { RelatedContextResponse } from '@equinor/fusion-framework-module-services/context/related';
8
- import type { ContextItem, ContextItemType } from './types';
9
-
10
- /**
11
- * Parses the context type from the response of the GetContext API.
12
- *
13
- * @param type The type property from the GetContext response.
14
- * @returns The parsed context item type.
15
- */
16
- const parseContextType = (type: GetContextResponse<'v1'>['type']): ContextItemType => ({
17
- id: type.id,
18
- isChildType: type.isChildType,
19
- parentTypeIds: type.parentTypeIds ?? [],
20
- });
21
-
22
- /**
23
- * Parses an ApiContextEntity object into a ContextItem object.
24
- * @param item The ApiContextEntity object to parse.
25
- * @returns The parsed ContextItem object.
26
- */
27
- const parseContextItem = (item: ApiContextEntity<ApiVersion.v1>): ContextItem => {
28
- return {
29
- id: item.id,
30
- externalId: item.externalId ?? undefined,
31
- isActive: item.isActive,
32
- isDeleted: item.isDeleted,
33
- created: new Date(item.created),
34
- source: item.source ?? undefined,
35
- title: item.title ?? undefined,
36
- type: parseContextType(item.type),
37
- // TODO
38
- value: item.value ?? {},
39
- };
40
- };
41
-
42
- /**
43
- * Parse the response from the GetContext API into a context item.
44
- * @param response The response object containing the context item.
45
- * @returns A promise that resolves to the context item.
46
- */
47
- export const getContextSelector = async (response: Response): Promise<ContextItem> => {
48
- const result = (await response.json()) as GetContextResponse<'v1'>;
49
- return parseContextItem(result);
50
- };
51
-
52
- /**
53
- * Parse the response from the QueryContext API into an array of context items.
54
- * @param response The response object.
55
- * @returns A promise that resolves to an array of context items.
56
- */
57
- export const queryContextSelector = async (response: Response): Promise<ContextItem[]> => {
58
- const result = (await response.json()) as QueryContextResponse<'v1'>;
59
- return result.map(parseContextItem);
60
- };
61
-
62
- /**
63
- * Parse the response from the RelatedContext API into an array of context items.
64
- * @param response The response object containing the related context items.
65
- * @returns A promise that resolves to an array of ContextItem objects.
66
- */
67
- export const relatedContextSelector = async (response: Response): Promise<ContextItem[]> => {
68
- const result = (await response.json()) as RelatedContextResponse<'v1'>;
69
- return result.map(parseContextItem);
70
- };