@equinor/fusion-framework-module-widget 2.0.10 → 3.0.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +82 -70
  2. package/dist/esm/Widget.js +255 -0
  3. package/dist/esm/Widget.js.map +1 -0
  4. package/dist/esm/WidgetModuleConfigurator.js +39 -73
  5. package/dist/esm/WidgetModuleConfigurator.js.map +1 -1
  6. package/dist/esm/WidgetModuleProvider.js +85 -17
  7. package/dist/esm/WidgetModuleProvider.js.map +1 -1
  8. package/dist/esm/enable-widget-module.js +8 -2
  9. package/dist/esm/enable-widget-module.js.map +1 -1
  10. package/dist/esm/errors.js +22 -4
  11. package/dist/esm/errors.js.map +1 -1
  12. package/dist/esm/events.js +2 -0
  13. package/dist/esm/events.js.map +1 -0
  14. package/dist/esm/index.js +1 -1
  15. package/dist/esm/index.js.map +1 -1
  16. package/dist/esm/module.js +5 -2
  17. package/dist/esm/module.js.map +1 -1
  18. package/dist/esm/state/actions.js +31 -0
  19. package/dist/esm/state/actions.js.map +1 -0
  20. package/dist/esm/state/create-reducer.js +32 -0
  21. package/dist/esm/state/create-reducer.js.map +1 -0
  22. package/dist/esm/state/create-state.js +12 -0
  23. package/dist/esm/state/create-state.js.map +1 -0
  24. package/dist/esm/state/flows.js +21 -0
  25. package/dist/esm/state/flows.js.map +1 -0
  26. package/dist/esm/utils.js +43 -0
  27. package/dist/esm/utils.js.map +1 -0
  28. package/dist/esm/version.js +2 -1
  29. package/dist/esm/version.js.map +1 -1
  30. package/dist/tsconfig.tsbuildinfo +1 -1
  31. package/dist/types/Widget.d.ts +78 -0
  32. package/dist/types/WidgetModuleConfigurator.d.ts +27 -27
  33. package/dist/types/WidgetModuleProvider.d.ts +44 -5
  34. package/dist/types/enable-widget-module.d.ts +5 -1
  35. package/dist/types/errors.d.ts +7 -2
  36. package/dist/types/events.d.ts +54 -0
  37. package/dist/types/index.d.ts +1 -1
  38. package/dist/types/module.d.ts +3 -3
  39. package/dist/types/state/actions.d.ts +92 -0
  40. package/dist/types/state/create-reducer.d.ts +3 -0
  41. package/dist/types/state/create-state.d.ts +5 -0
  42. package/dist/types/state/flows.d.ts +7 -0
  43. package/dist/types/types.d.ts +81 -5
  44. package/dist/types/utils.d.ts +5 -0
  45. package/dist/types/version.d.ts +1 -1
  46. package/package.json +4 -1
  47. package/src/Widget.ts +324 -0
  48. package/src/WidgetModuleConfigurator.ts +48 -118
  49. package/src/WidgetModuleProvider.ts +110 -22
  50. package/src/enable-widget-module.ts +2 -2
  51. package/src/errors.ts +38 -4
  52. package/src/events.ts +75 -0
  53. package/src/index.ts +1 -5
  54. package/src/module.ts +8 -5
  55. package/src/state/actions.ts +70 -0
  56. package/src/state/create-reducer.ts +44 -0
  57. package/src/state/create-state.ts +21 -0
  58. package/src/state/flows.ts +75 -0
  59. package/src/types.ts +89 -9
  60. package/src/utils.ts +51 -0
  61. package/src/version.ts +1 -1
  62. package/tsconfig.json +1 -3
  63. package/dist/esm/WidgetModuleConfigBuilder.js +0 -42
  64. package/dist/esm/WidgetModuleConfigBuilder.js.map +0 -1
  65. package/dist/types/WidgetModuleConfigBuilder.d.ts +0 -19
  66. package/src/WidgetModuleConfigBuilder.ts +0 -70
package/src/types.ts CHANGED
@@ -2,22 +2,51 @@ import type { AnyModule, CombinedModules, ModulesInstance } from '@equinor/fusio
2
2
  import type { EventModule } from '@equinor/fusion-framework-module-event';
3
3
  import type { HttpModule } from '@equinor/fusion-framework-module-http';
4
4
  import type { ServiceDiscoveryModule } from '@equinor/fusion-framework-module-service-discovery';
5
+ import { QueryCtorOptions } from '@equinor/fusion-query';
5
6
 
6
7
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
8
  type Fusion = any;
8
9
 
9
- export type WidgetEnv = {
10
- config?: WidgetManifest;
10
+ /**
11
+ * WidgetEnv type represents the environment configuration for a widget.
12
+ */
13
+ export type WidgetEnv<TProps = unknown> = {
14
+ basename?: string;
15
+ manifest?: WidgetManifest;
16
+ props?: TProps;
11
17
  };
12
18
 
13
- // TODO: change to module-services when new app service is created
19
+ /**
20
+ * IClient interface represents a client with properties and methods for widget-related operations.
21
+ */
22
+ export type IClient = {
23
+ apiVersion: string;
24
+ baseImportUrl: string;
25
+ getWidgetManifest: QueryCtorOptions<WidgetManifest, GetWidgetParameters>;
26
+ getWidgetConfig: QueryCtorOptions<WidgetConfig, GetWidgetParameters>;
27
+ };
28
+
29
+ /**
30
+ * ModuleDeps type represents a tuple of dependencies required for a module.
31
+ */
14
32
  export type ModuleDeps = [HttpModule, ServiceDiscoveryModule, EventModule];
15
33
 
34
+ /**
35
+ * GetWidgetParameters type represents parameters for retrieving widget information.
36
+ */
16
37
  export type GetWidgetParameters = {
17
38
  widgetKey: string;
18
39
  args?: { type: 'version' | 'tag'; value: string };
19
40
  };
20
41
 
42
+ /**
43
+ * WidgetEndpointBuilder type represents a function for building widget endpoints based on GetWidgetParameters.
44
+ */
45
+ export type WidgetEndpointBuilder = (args: GetWidgetParameters) => string;
46
+
47
+ /**
48
+ * WidgetManifest type represents the manifest information of a widget.
49
+ */
21
50
  export type WidgetManifest = {
22
51
  id: string;
23
52
  name: string;
@@ -26,26 +55,77 @@ export type WidgetManifest = {
26
55
  maintainers?: string[];
27
56
  entryPoint: string;
28
57
  assetPath: string;
29
- // TODO move to @equinor/fusion-widget
30
- importBundle: () => Promise<WidgetScriptModule>;
31
58
  };
32
59
 
60
+ /**
61
+ * Endpoint type represents an endpoint with properties like name, uri, and optional scopes.
62
+ */
63
+ export type Endpoint = { name: string; uri: string; scopes?: string[] };
64
+
65
+ /**
66
+ * WidgetConfig type represents the configuration for a widget, including environment and endpoints.
67
+ */
68
+ export type WidgetConfig<TEnvironment = unknown> = {
69
+ environment: TEnvironment;
70
+ endpoints: Record<string, string | Endpoint>;
71
+ };
72
+
73
+ /**
74
+ * WidgetModules type represents a combination of modules related to events and service discovery.
75
+ */
33
76
  export type WidgetModules<TModules extends Array<AnyModule> | unknown = unknown> = CombinedModules<
34
77
  TModules,
35
78
  [EventModule, ServiceDiscoveryModule]
36
79
  >;
37
80
 
38
- export type WidgetRenderArgs<TFusion extends Fusion = Fusion, TEnv = WidgetEnv> = {
81
+ /**
82
+ * WidgetProps type represents widget properties as a record of PropertyKey to unknown.
83
+ */
84
+ export type WidgetProps = Record<PropertyKey, unknown>;
85
+
86
+ /**
87
+ * WidgetRenderArgs type represents arguments for rendering a widget, including fusion, environment, and optional properties.
88
+ */
89
+
90
+ export type WidgetRenderArgs<
91
+ TFusion extends Fusion = Fusion,
92
+ TEnv = WidgetEnv,
93
+ TProps extends WidgetProps = WidgetProps,
94
+ > = {
39
95
  fusion: TFusion;
40
96
  env: TEnv;
97
+ props?: TProps;
41
98
  };
42
99
 
43
- export type WidgetScriptModule<
44
- TProps extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>,
45
- > = {
100
+ /**
101
+ * WidgetScriptModule type represents a script module for a widget with functions for rendering, rendering icons, etc.
102
+ */
103
+ export type WidgetScriptModule<TProps extends WidgetProps = WidgetProps> = {
46
104
  default: (el: HTMLElement, args: WidgetRenderArgs, props?: TProps) => VoidFunction;
47
105
  renderWidget: (el: HTMLElement, args: WidgetRenderArgs, props?: TProps) => VoidFunction;
106
+ renderIcon: (el: HTMLElement, args: WidgetRenderArgs, props?: TProps) => VoidFunction;
107
+ render: (el: HTMLElement, args: WidgetRenderArgs, props?: TProps) => VoidFunction;
48
108
  };
49
109
 
110
+ /**
111
+ * WidgetModulesInstance type represents an instance of widget modules.
112
+ */
50
113
  export type WidgetModulesInstance<TModules extends Array<AnyModule> | unknown = unknown> =
51
114
  ModulesInstance<WidgetModules<TModules>>;
115
+
116
+ /**
117
+ * WidgetState type represents the state of a widget, including properties like name, status, config, manifest, modules, and instance.
118
+ */
119
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
+ export type WidgetState<TModules = any> = {
121
+ name: string;
122
+ status: Set<string>;
123
+ config?: WidgetConfig;
124
+ manifest?: WidgetManifest;
125
+ modules?: WidgetScriptModule;
126
+ instance?: WidgetModulesInstance<TModules>;
127
+ };
128
+ /**
129
+ * WidgetStateInitial type represents an initial state of a widget, omitting the status property.
130
+ */
131
+ export type WidgetStateInitial = Omit<WidgetState, 'status'>;
package/src/utils.ts ADDED
@@ -0,0 +1,51 @@
1
+ import { GetWidgetParameters, IClient, WidgetEndpointBuilder } from './types';
2
+ import { IHttpClient } from '@equinor/fusion-framework-module-http';
3
+
4
+ export const defaultManifestEndpointBuilder =
5
+ (apiVersion: string): WidgetEndpointBuilder =>
6
+ (params: GetWidgetParameters) => {
7
+ const { widgetKey, args } = params;
8
+ const { type, value } = args ?? {};
9
+ switch (type) {
10
+ case 'tag':
11
+ case 'version':
12
+ return `/widgets/${widgetKey}/versions/${value}?api-version=${apiVersion}`;
13
+ default:
14
+ return `/widgets/${widgetKey}?api-version=${apiVersion}`;
15
+ }
16
+ };
17
+
18
+ export const defaultConfigEndpointBuilder =
19
+ (apiVersion: string): WidgetEndpointBuilder =>
20
+ (params: GetWidgetParameters) => {
21
+ const { widgetKey, args } = params;
22
+ const { type, value } = args ?? {};
23
+ // Todo Align endpoints with backend when its done!
24
+ switch (type) {
25
+ case 'tag':
26
+ case 'version':
27
+ return `/widgets/${widgetKey}/versions/${value}/config?api-version=${apiVersion}`;
28
+ default:
29
+ return `/widgets/${widgetKey}/config?api-version=${apiVersion}`;
30
+ }
31
+ };
32
+
33
+ export const createDefaultClient = (httpClient: IHttpClient): IClient => {
34
+ const apiVersion = '1.0-preview';
35
+ return {
36
+ apiVersion,
37
+ baseImportUrl: httpClient.uri,
38
+ getWidgetManifest: {
39
+ client: {
40
+ fn: (args) => httpClient.json$(defaultManifestEndpointBuilder(apiVersion)(args)),
41
+ },
42
+ key: (args) => JSON.stringify(args),
43
+ },
44
+ getWidgetConfig: {
45
+ client: {
46
+ fn: (args) => httpClient.json$(defaultConfigEndpointBuilder(apiVersion)(args)),
47
+ },
48
+ key: (args) => JSON.stringify(args),
49
+ },
50
+ };
51
+ };
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '2.0.10';
2
+ export const version = '3.0.0';
package/tsconfig.json CHANGED
@@ -4,6 +4,7 @@
4
4
  "outDir": "dist/esm",
5
5
  "rootDir": "src",
6
6
  "declarationDir": "./dist/types",
7
+ "removeComments": false
7
8
 
8
9
  },
9
10
  "references": [
@@ -13,9 +14,6 @@
13
14
  {
14
15
  "path": "../module"
15
16
  },
16
- {
17
- "path": "../http"
18
- },
19
17
  {
20
18
  "path": "../event"
21
19
  },
@@ -1,42 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _WidgetModuleConfigBuilder_init;
13
- export class WidgetModuleConfigBuilder {
14
- constructor(init, config = {}) {
15
- this.config = config;
16
- _WidgetModuleConfigBuilder_init.set(this, void 0);
17
- __classPrivateFieldSet(this, _WidgetModuleConfigBuilder_init, init, "f");
18
- }
19
- requireInstance(module) {
20
- return __classPrivateFieldGet(this, _WidgetModuleConfigBuilder_init, "f").requireInstance(module);
21
- }
22
- setEndpointBuilder(fn) {
23
- this.config.endpointBuilder = fn;
24
- }
25
- setWidgetClient(client, expire = 1 * 60 * 1000) {
26
- client;
27
- expire;
28
- this.config.client = {
29
- getWidget: typeof client.getWidget === 'function'
30
- ? {
31
- key: (args) => JSON.stringify(args),
32
- client: {
33
- fn: client.getWidget,
34
- },
35
- expire,
36
- }
37
- : client.getWidget,
38
- };
39
- }
40
- }
41
- _WidgetModuleConfigBuilder_init = new WeakMap();
42
- //# sourceMappingURL=WidgetModuleConfigBuilder.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"WidgetModuleConfigBuilder.js","sourceRoot":"","sources":["../../src/WidgetModuleConfigBuilder.ts"],"names":[],"mappings":";;;;;;;;;;;;AAcA,MAAM,OAAO,yBAAyB;IAQlC,YACI,IAAW,EACJ,SAAsC,EAAE;QAAxC,WAAM,GAAN,MAAM,CAAkC;QAHnD,kDAAa;QAKT,uBAAA,IAAI,mCAAS,IAAI,MAAA,CAAC;IACtB,CAAC;IASD,eAAe,CAAC,MAAc;QAC1B,OAAO,uBAAA,IAAI,uCAAM,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;IAC9C,CAAC;IAED,kBAAkB,CAAC,EAAyB;QACxC,IAAI,CAAC,MAAM,CAAC,eAAe,GAAG,EAAE,CAAC;IACrC,CAAC;IAED,eAAe,CACX,MAIC,EACD,MAAM,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI;QAEtB,MAAM,CAAC;QACP,MAAM,CAAC;QAEP,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG;YACjB,SAAS,EACL,OAAO,MAAM,CAAC,SAAS,KAAK,UAAU;gBAClC,CAAC,CAAC;oBAEI,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;oBACnC,MAAM,EAAE;wBACJ,EAAE,EAAE,MAAM,CAAC,SAAS;qBACvB;oBACD,MAAM;iBACT;gBACH,CAAC,CAAC,MAAM,CAAC,SAAS;SAC7B,CAAC;IACN,CAAC;CACJ"}
@@ -1,19 +0,0 @@
1
- import { ModuleInitializerArgs, Modules, ModuleType } from '@equinor/fusion-framework-module';
2
- import { QueryFn, QueryCtorOptions } from '@equinor/fusion-query';
3
- import { WidgetModuleConfig, IWidgetModuleConfigurator } from './WidgetModuleConfigurator';
4
- import type { WidgetManifest, ModuleDeps, GetWidgetParameters } from './types';
5
- export type WidgetModuleConfigBuilderCallback = (builder: WidgetModuleConfigBuilder<ModuleInitializerArgs<IWidgetModuleConfigurator, any>>) => void | Promise<void>;
6
- export type WidgetEndpointBuilder = (args: GetWidgetParameters) => string;
7
- export declare class WidgetModuleConfigBuilder<TInit extends ModuleInitializerArgs<IWidgetModuleConfigurator, any> = ModuleInitializerArgs<IWidgetModuleConfigurator, ModuleDeps>> {
8
- #private;
9
- config: Partial<WidgetModuleConfig>;
10
- constructor(init: TInit, config?: Partial<WidgetModuleConfig>);
11
- requireInstance<TKey extends string = Extract<keyof Modules, string>>(module: TKey): Promise<ModuleType<Modules[TKey]>>;
12
- requireInstance<T>(module: string): Promise<T>;
13
- setEndpointBuilder(fn: WidgetEndpointBuilder): void;
14
- setWidgetClient(client: {
15
- getWidget: QueryFn<WidgetManifest, {
16
- widgetKey: string;
17
- }> | QueryCtorOptions<WidgetManifest, GetWidgetParameters>;
18
- }, expire?: number): void;
19
- }
@@ -1,70 +0,0 @@
1
- import { ModuleInitializerArgs, Modules, ModuleType } from '@equinor/fusion-framework-module';
2
- import { QueryFn, QueryCtorOptions } from '@equinor/fusion-query';
3
-
4
- import { WidgetModuleConfig, IWidgetModuleConfigurator } from './WidgetModuleConfigurator';
5
-
6
- import type { WidgetManifest, ModuleDeps, GetWidgetParameters } from './types';
7
-
8
- export type WidgetModuleConfigBuilderCallback = (
9
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
- builder: WidgetModuleConfigBuilder<ModuleInitializerArgs<IWidgetModuleConfigurator, any>>,
11
- ) => void | Promise<void>;
12
-
13
- export type WidgetEndpointBuilder = (args: GetWidgetParameters) => string;
14
-
15
- export class WidgetModuleConfigBuilder<
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- TInit extends ModuleInitializerArgs<IWidgetModuleConfigurator, any> = ModuleInitializerArgs<
18
- IWidgetModuleConfigurator,
19
- ModuleDeps
20
- >,
21
- > {
22
- #init: TInit;
23
- constructor(
24
- init: TInit,
25
- public config: Partial<WidgetModuleConfig> = {},
26
- ) {
27
- this.#init = init;
28
- }
29
-
30
- requireInstance<TKey extends string = Extract<keyof Modules, string>>(
31
- module: TKey,
32
- ): Promise<ModuleType<Modules[TKey]>>;
33
-
34
- requireInstance<T>(module: string): Promise<T>;
35
-
36
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
- requireInstance(module: string): Promise<any> {
38
- return this.#init.requireInstance(module);
39
- }
40
-
41
- setEndpointBuilder(fn: WidgetEndpointBuilder): void {
42
- this.config.endpointBuilder = fn;
43
- }
44
-
45
- setWidgetClient(
46
- client: {
47
- getWidget:
48
- | QueryFn<WidgetManifest, { widgetKey: string }>
49
- | QueryCtorOptions<WidgetManifest, GetWidgetParameters>;
50
- },
51
- expire = 1 * 60 * 1000,
52
- ) {
53
- client;
54
- expire;
55
-
56
- this.config.client = {
57
- getWidget:
58
- typeof client.getWidget === 'function'
59
- ? {
60
- // TODO - might cast to checksum
61
- key: (args) => JSON.stringify(args),
62
- client: {
63
- fn: client.getWidget,
64
- },
65
- expire,
66
- }
67
- : client.getWidget,
68
- };
69
- }
70
- }