@equinor/fusion-framework-module-bookmark 1.2.13 → 2.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 (113) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +242 -9
  3. package/dist/esm/BookmarkClient.interface.js +2 -0
  4. package/dist/esm/BookmarkClient.interface.js.map +1 -0
  5. package/dist/esm/BookmarkClient.js +174 -0
  6. package/dist/esm/BookmarkClient.js.map +1 -0
  7. package/dist/esm/BookmarkConfigurator.js +348 -0
  8. package/dist/esm/BookmarkConfigurator.js.map +1 -0
  9. package/dist/esm/BookmarkProvider.actions.js +60 -0
  10. package/dist/esm/BookmarkProvider.actions.js.map +1 -0
  11. package/dist/esm/BookmarkProvider.error.js +14 -0
  12. package/dist/esm/BookmarkProvider.error.js.map +1 -0
  13. package/dist/esm/BookmarkProvider.events.js +2 -0
  14. package/dist/esm/BookmarkProvider.events.js.map +1 -0
  15. package/dist/esm/BookmarkProvider.flows.js +219 -0
  16. package/dist/esm/BookmarkProvider.flows.js.map +1 -0
  17. package/dist/esm/BookmarkProvider.js +937 -0
  18. package/dist/esm/BookmarkProvider.js.map +1 -0
  19. package/dist/esm/BookmarkProvider.reducer.js +125 -0
  20. package/dist/esm/BookmarkProvider.reducer.js.map +1 -0
  21. package/dist/esm/BookmarkProvider.selectors.js +43 -0
  22. package/dist/esm/BookmarkProvider.selectors.js.map +1 -0
  23. package/dist/esm/BookmarkProvider.store.js +19 -0
  24. package/dist/esm/BookmarkProvider.store.js.map +1 -0
  25. package/dist/esm/bookmark-config.schema.js +30 -0
  26. package/dist/esm/bookmark-config.schema.js.map +1 -0
  27. package/dist/esm/bookmark-module.js +53 -0
  28. package/dist/esm/bookmark-module.js.map +1 -0
  29. package/dist/esm/bookmark.schemas.js +38 -0
  30. package/dist/esm/bookmark.schemas.js.map +1 -0
  31. package/dist/esm/enable-bookmark.js +20 -7
  32. package/dist/esm/enable-bookmark.js.map +1 -1
  33. package/dist/esm/index.js +3 -3
  34. package/dist/esm/index.js.map +1 -1
  35. package/dist/esm/version.js +1 -1
  36. package/dist/esm/version.js.map +1 -1
  37. package/dist/tsconfig.tsbuildinfo +1 -1
  38. package/dist/types/BookmarkClient.d.ts +42 -0
  39. package/dist/types/BookmarkClient.interface.d.ts +139 -0
  40. package/dist/types/BookmarkConfigurator.d.ts +100 -0
  41. package/dist/types/BookmarkProvider.actions.d.ts +525 -0
  42. package/dist/types/BookmarkProvider.d.ts +462 -0
  43. package/dist/types/BookmarkProvider.error.d.ts +9 -0
  44. package/dist/types/BookmarkProvider.events.d.ts +78 -0
  45. package/dist/types/BookmarkProvider.flows.d.ts +79 -0
  46. package/dist/types/BookmarkProvider.reducer.d.ts +302 -0
  47. package/dist/types/BookmarkProvider.selectors.d.ts +34 -0
  48. package/dist/types/BookmarkProvider.store.d.ts +30 -0
  49. package/dist/types/bookmark-config.schema.d.ts +118 -0
  50. package/dist/types/{module.d.ts → bookmark-module.d.ts} +3 -3
  51. package/dist/types/bookmark.schemas.d.ts +760 -0
  52. package/dist/types/enable-bookmark.d.ts +7 -6
  53. package/dist/types/index.d.ts +4 -3
  54. package/dist/types/types.d.ts +29 -65
  55. package/dist/types/version.d.ts +1 -1
  56. package/package.json +11 -5
  57. package/src/BookmarkClient.interface.ts +157 -0
  58. package/src/BookmarkClient.ts +226 -0
  59. package/src/BookmarkConfigurator.ts +374 -0
  60. package/src/BookmarkProvider.actions.ts +131 -0
  61. package/src/BookmarkProvider.error.ts +21 -0
  62. package/src/BookmarkProvider.events.ts +89 -0
  63. package/src/BookmarkProvider.flows.ts +426 -0
  64. package/src/BookmarkProvider.reducer.ts +141 -0
  65. package/src/BookmarkProvider.selectors.ts +55 -0
  66. package/src/BookmarkProvider.store.ts +52 -0
  67. package/src/BookmarkProvider.ts +1308 -0
  68. package/src/bookmark-config.schema.ts +41 -0
  69. package/src/bookmark-module.ts +70 -0
  70. package/src/bookmark.schemas.ts +50 -0
  71. package/src/enable-bookmark.ts +13 -20
  72. package/src/index.ts +15 -7
  73. package/src/types.ts +46 -73
  74. package/src/version.ts +1 -1
  75. package/dist/esm/bookmark-config-builder.js +0 -131
  76. package/dist/esm/bookmark-config-builder.js.map +0 -1
  77. package/dist/esm/bookmark-provider.js +0 -155
  78. package/dist/esm/bookmark-provider.js.map +0 -1
  79. package/dist/esm/client/bookmarkActions.js +0 -10
  80. package/dist/esm/client/bookmarkActions.js.map +0 -1
  81. package/dist/esm/client/bookmarkClient.js +0 -259
  82. package/dist/esm/client/bookmarkClient.js.map +0 -1
  83. package/dist/esm/client/bookmarkFlows.js +0 -31
  84. package/dist/esm/client/bookmarkFlows.js.map +0 -1
  85. package/dist/esm/client/bookmarkReducer.js +0 -29
  86. package/dist/esm/client/bookmarkReducer.js.map +0 -1
  87. package/dist/esm/configurator.js +0 -155
  88. package/dist/esm/configurator.js.map +0 -1
  89. package/dist/esm/module.js +0 -27
  90. package/dist/esm/module.js.map +0 -1
  91. package/dist/esm/utils/handle-url.js +0 -11
  92. package/dist/esm/utils/handle-url.js.map +0 -1
  93. package/dist/esm/utils/index.js +0 -2
  94. package/dist/esm/utils/index.js.map +0 -1
  95. package/dist/types/bookmark-config-builder.d.ts +0 -85
  96. package/dist/types/bookmark-provider.d.ts +0 -169
  97. package/dist/types/client/bookmarkActions.d.ts +0 -27
  98. package/dist/types/client/bookmarkClient.d.ts +0 -53
  99. package/dist/types/client/bookmarkFlows.d.ts +0 -22
  100. package/dist/types/client/bookmarkReducer.d.ts +0 -5
  101. package/dist/types/configurator.d.ts +0 -75
  102. package/dist/types/utils/handle-url.d.ts +0 -2
  103. package/dist/types/utils/index.d.ts +0 -1
  104. package/src/bookmark-config-builder.ts +0 -177
  105. package/src/bookmark-provider.ts +0 -344
  106. package/src/client/bookmarkActions.ts +0 -44
  107. package/src/client/bookmarkClient.ts +0 -327
  108. package/src/client/bookmarkFlows.ts +0 -67
  109. package/src/client/bookmarkReducer.ts +0 -35
  110. package/src/configurator.ts +0 -235
  111. package/src/module.ts +0 -40
  112. package/src/utils/handle-url.ts +0 -13
  113. package/src/utils/index.ts +0 -1
@@ -1,327 +0,0 @@
1
- import {
2
- FrameworkEvent,
3
- FrameworkEventInit,
4
- IEventModuleProvider,
5
- } from '@equinor/fusion-framework-module-event';
6
- import { IHttpClient } from '@equinor/fusion-framework-module-http';
7
-
8
- import { BookmarksApiClient } from '@equinor/fusion-framework-module-services/bookmarks';
9
- import { createState, FlowState } from '@equinor/fusion-observable';
10
- import Query, { type QueryCtorOptions } from '@equinor/fusion-query';
11
-
12
- import { BehaviorSubject, lastValueFrom, map, Observable, Subscription } from 'rxjs';
13
-
14
- import {
15
- Bookmark,
16
- CreateBookmark,
17
- GetAllBookmarksParameters,
18
- GetBookmarkParameters,
19
- PatchBookmark,
20
- SourceSystem,
21
- } from '../types';
22
- import { ActionBuilder, actions } from './bookmarkActions';
23
- import { reducer, State } from './bookmarkReducer';
24
- import {
25
- handleBookmarkGetAll,
26
- handleCreateBookmark,
27
- handleDeleteBookmark,
28
- handleUpdateBookmark,
29
- } from './bookmarkFlows';
30
-
31
- export type BookmarkClientConfig = {
32
- client: BookmarksApiClient<'fetch', IHttpClient, Bookmark<unknown>>;
33
- queryClientConfig: {
34
- getAllBookmarks: QueryCtorOptions<Array<Bookmark<unknown>>, GetAllBookmarksParameters>;
35
- getBookmarkById: QueryCtorOptions<Bookmark<unknown>, GetBookmarkParameters>;
36
- };
37
- };
38
-
39
- export class BookmarkClient {
40
- #queryBookmarkById: Query<Bookmark<unknown>, GetBookmarkParameters>;
41
- #queryAllBookmarks: Query<Array<Bookmark<unknown>>, GetAllBookmarksParameters>;
42
- #bookmarkAPiClient: BookmarksApiClient<'fetch', IHttpClient, unknown>;
43
-
44
- #currentBookmark$: BehaviorSubject<Bookmark<unknown> | undefined>;
45
-
46
- #sourceSystem?: SourceSystem;
47
- #event?: IEventModuleProvider;
48
-
49
- #state: FlowState<State, ActionBuilder>;
50
- #subscriptions = new Subscription();
51
-
52
- appRoute?(appKey: string): string;
53
-
54
- constructor(
55
- config: BookmarkClientConfig,
56
- sourceSystem?: SourceSystem,
57
- event?: IEventModuleProvider,
58
- ) {
59
- this.#currentBookmark$ = new BehaviorSubject<Bookmark<unknown> | undefined>(undefined);
60
-
61
- this.#bookmarkAPiClient = config.client;
62
- this.#queryBookmarkById = new Query(config.queryClientConfig.getBookmarkById);
63
- this.#queryAllBookmarks = new Query(config.queryClientConfig.getAllBookmarks);
64
-
65
- this.#sourceSystem = sourceSystem;
66
- this.#event = event;
67
-
68
- this.#state = createState(actions, reducer);
69
-
70
- // Add Bookmark API calls as flows
71
- this.#addSubjectFlows();
72
-
73
- // Add Events to actions success
74
- this.#addStateEffects();
75
-
76
- if (event) {
77
- this.#subscriptions.add(
78
- this.#currentBookmark$.subscribe((bookmark) => {
79
- event.dispatchEvent('onBookmarkChanged', {
80
- detail: bookmark,
81
- canBubble: true,
82
- source: this,
83
- });
84
- }),
85
- );
86
- }
87
- }
88
-
89
- public get currentBookmark$(): Observable<Bookmark | undefined> {
90
- return this.#currentBookmark$.asObservable();
91
- }
92
-
93
- public get bookmarks$(): Observable<Bookmark[]> {
94
- return this.#state.subject.pipe(
95
- map((state) => Object.values(state.bookmarks).map((bookmark) => bookmark)),
96
- );
97
- }
98
-
99
- public async setCurrentBookmark<T>(idOrItem?: string | Bookmark<T>): Promise<void> {
100
- if (!idOrItem) {
101
- return this.#currentBookmark$.next(undefined);
102
- }
103
-
104
- if (typeof idOrItem === 'string') {
105
- // TODO add custom error
106
- const bookmark = await this.resolverBookmarkAsync<T>(idOrItem);
107
- return this.setBookmark(bookmark);
108
- // TODO do a deep fast equal, since propagated bookmarks can be of different instances
109
- } else if (idOrItem !== this.#currentBookmark$.value) {
110
- return this.setBookmark(idOrItem);
111
- }
112
- }
113
-
114
- public async setBookmark<T>(bookmark?: Bookmark<T>): Promise<void> {
115
- if (this.#event) {
116
- /** notify that current bookmark is about to change */
117
- const event = await this.#event.dispatchEvent('onBookmarkChange', {
118
- detail: bookmark,
119
- cancelable: true,
120
- canBubble: true,
121
- source: this,
122
- });
123
-
124
- /** check if any listeners requested canceling */
125
- if (!event.canceled) {
126
- this.#currentBookmark$.next(bookmark);
127
- }
128
- } else {
129
- this.#currentBookmark$.next(bookmark);
130
- }
131
- }
132
-
133
- public resolverBookmark<T>(bookmarkId: string): Observable<Bookmark<T>> {
134
- return this.#queryBookmarkById
135
- .query({
136
- id: bookmarkId,
137
- })
138
- .pipe(
139
- map((bookmark) => {
140
- return bookmark.value;
141
- }),
142
- ) as Observable<Bookmark<T>>;
143
- }
144
-
145
- public resolverBookmarkAsync<T>(bookmarkId: string): Promise<Bookmark<T>> {
146
- return lastValueFrom(this.resolverBookmark<T>(bookmarkId));
147
- }
148
-
149
- public getAllBookmarks(args = { isValid: false }): Observable<Array<Bookmark>> {
150
- const { isValid } = args;
151
- return new Observable((subscriber) => {
152
- this.#state.dispatch.getAll(isValid);
153
- subscriber.add(
154
- this.#state.subject.addEffect('getAll::success', (action) => {
155
- subscriber.next(action.payload);
156
- subscriber.complete();
157
-
158
- this.#event?.dispatchEvent('onBookmarksChanged', {
159
- detail: action.payload,
160
- source: this,
161
- });
162
- }),
163
- );
164
- });
165
- }
166
-
167
- public async getAllBookmarksAsync(args = { isValid: false }): Promise<Array<Bookmark>> {
168
- return lastValueFrom(this.getAllBookmarks(args));
169
- }
170
-
171
- public async getBookmarkById<T>(bookmarkId: string): Promise<Bookmark<T>> {
172
- const result = await this.#bookmarkAPiClient.get('v1', { id: bookmarkId });
173
- const bookmark = (await result.json()) as Bookmark<T>;
174
- return bookmark;
175
- }
176
-
177
- public createBookmark(bookmark: CreateBookmark<unknown>): Observable<Bookmark<unknown>> {
178
- return new Observable((subscriber) => {
179
- this.#state.dispatch.create(bookmark);
180
- subscriber.add(
181
- this.#state.subject.addEffect('create::success', (action) => {
182
- subscriber.next(action.payload as Bookmark<unknown>);
183
- subscriber.complete();
184
- }),
185
- );
186
- subscriber.add(
187
- this.#state.subject.addEffect('create::failure', (action) => {
188
- subscriber.error(action.payload);
189
- }),
190
- );
191
- });
192
- }
193
-
194
- public async createBookmarkAsync(
195
- bookmark: CreateBookmark<unknown>,
196
- ): Promise<Bookmark<unknown>> {
197
- return lastValueFrom(this.createBookmark(bookmark));
198
- }
199
-
200
- public updateBookmark<T>(bookmark: PatchBookmark<T>): Observable<Bookmark<T>> {
201
- return new Observable((subscriber) => {
202
- this.#state.dispatch.update(bookmark);
203
- subscriber.add(
204
- this.#state.subject.addEffect('update::success', (action) => {
205
- subscriber.next(action.payload as Bookmark<T>);
206
- subscriber.complete();
207
- this.#queryBookmarkById.cache.invalidate(action.payload.id);
208
- }),
209
- );
210
- });
211
- }
212
-
213
- public async updateBookmarkAsync<T>(bookmark: PatchBookmark<T>): Promise<Bookmark<T>> {
214
- return lastValueFrom(this.updateBookmark(bookmark));
215
- }
216
-
217
- public deleteBookmarkById(bookmarkId: string): Observable<string> {
218
- return new Observable((subscriber) => {
219
- this.#state.dispatch.delete(bookmarkId);
220
- subscriber.add(
221
- this.#state.subject.addEffect('delete::success', (action) => {
222
- subscriber.next(action.payload);
223
- subscriber.complete();
224
- this.#queryBookmarkById.cache.invalidate(action.payload);
225
- }),
226
- );
227
- });
228
- }
229
-
230
- public async deleteBookmarkByIdAsync(bookmarkId: string): Promise<string> {
231
- return lastValueFrom(this.deleteBookmarkById(bookmarkId));
232
- }
233
-
234
- public async addBookmarkFavoriteAsync(bookmarkId: string): Promise<void> {
235
- const response = await this.#bookmarkAPiClient.addFavorite('v1', { bookmarkId });
236
- if (response.ok && this.#currentBookmark$.value) {
237
- this.#state.dispatch.addFavorite(this.#currentBookmark$.value);
238
- }
239
- }
240
-
241
- public async removeBookmarkFavoriteAsync(bookmarkId: string): Promise<void> {
242
- const response = await this.#bookmarkAPiClient.removeFavorite('v1', { bookmarkId });
243
- if (response.ok) {
244
- this.#state.dispatch.removeFavorite(bookmarkId);
245
- }
246
- }
247
-
248
- public async verifyBookmarkFavoriteAsync(bookmarkId: string): Promise<boolean> {
249
- const response = await this.#bookmarkAPiClient.verifyFavorite('v1', { bookmarkId });
250
- if (response.ok) return true;
251
- return false;
252
- }
253
-
254
- dispose(): void {
255
- this.#subscriptions.unsubscribe();
256
- }
257
-
258
- #addStateEffects() {
259
- this.#state.subject.addEffect('create::success', (action) => {
260
- this.#queryAllBookmarks.cache.invalidate('all-bookmarks');
261
- this.#event?.dispatchEvent('onBookmarkCreated', {
262
- detail: action.payload,
263
- canBubble: true,
264
- source: this,
265
- });
266
- });
267
- this.#state.subject.addEffect('delete::success', (action) => {
268
- console.log('here');
269
- this.#queryAllBookmarks.cache.invalidate('all-bookmarks');
270
- this.#event?.dispatchEvent('onBookmarkDeleted', {
271
- detail: action.payload,
272
- canBubble: true,
273
- source: this,
274
- });
275
- });
276
- this.#state.subject.addEffect('update::success', (action) => {
277
- this.#queryAllBookmarks.cache.invalidate('all-bookmarks');
278
- this.#event?.dispatchEvent('onBookmarkUpdated', {
279
- detail: action.payload,
280
- canBubble: true,
281
- source: this,
282
- });
283
- });
284
- this.#state.subject.addEffect('addFavorite::request', () => {
285
- this.#queryAllBookmarks.cache.invalidate('all-bookmarks');
286
- });
287
-
288
- this.#state.subject.addEffect('removeFavorite::request', () => {
289
- this.#queryAllBookmarks.cache.invalidate('all-bookmarks');
290
- });
291
- }
292
-
293
- #addSubjectFlows() {
294
- this.#subscriptions.add(
295
- this.#state.subject.addFlow(
296
- handleBookmarkGetAll(this.#queryAllBookmarks, this.#sourceSystem?.identifier),
297
- ),
298
- );
299
- this.#subscriptions.add(
300
- this.#state.subject.addFlow(handleCreateBookmark(this.#bookmarkAPiClient)),
301
- );
302
- this.#subscriptions.add(
303
- this.#state.subject.addFlow(handleDeleteBookmark(this.#bookmarkAPiClient)),
304
- );
305
- this.#subscriptions.add(
306
- this.#state.subject.addFlow(handleUpdateBookmark(this.#bookmarkAPiClient)),
307
- );
308
- this.#subscriptions.add(
309
- this.#state.subject.addFlow(handleUpdateBookmark(this.#bookmarkAPiClient)),
310
- );
311
- }
312
- }
313
-
314
- declare module '@equinor/fusion-framework-module-event' {
315
- interface FrameworkEventMap {
316
- /** dispatch before bookmark changes */
317
- onBookmarkChanged: FrameworkEvent<FrameworkEventInit<Bookmark<unknown>, BookmarkClient>>;
318
- /** dispatch before bookmarks changes */
319
- onBookmarksChanged: FrameworkEvent<
320
- FrameworkEventInit<Array<Bookmark<unknown>>, BookmarkClient>
321
- >;
322
- onBookmarkCreated: FrameworkEvent<FrameworkEventInit<Bookmark<unknown>, BookmarkClient>>;
323
- onBookmarkUpdated: FrameworkEvent<FrameworkEventInit<Bookmark<unknown>, BookmarkClient>>;
324
- onBookmarkDeleted: FrameworkEvent<FrameworkEventInit<boolean, BookmarkClient>>;
325
- onBookmarkResolved: FrameworkEvent<FrameworkEventInit<Bookmark<unknown>, BookmarkClient>>;
326
- }
327
- }
@@ -1,67 +0,0 @@
1
- import { IHttpClient } from '@equinor/fusion-framework-module-http';
2
- import { BookmarksApiClient } from '@equinor/fusion-framework-module-services/bookmarks';
3
- import { Observable } from '@equinor/fusion-observable';
4
- import { filterAction } from '@equinor/fusion-observable/operators';
5
- import { Query } from '@equinor/fusion-query';
6
- import { switchMap, map, catchError, EMPTY, last } from 'rxjs';
7
- import { GetAllBookmarksParameters, Bookmark } from '../types';
8
- import { actions, Actions } from './bookmarkActions';
9
-
10
- export const handleBookmarkGetAll =
11
- (
12
- queryAllBookmarks: Query<Array<Bookmark<unknown>>, GetAllBookmarksParameters>,
13
- sourceSystemIdentifier?: string,
14
- ) =>
15
- (action$: Observable<Actions>) =>
16
- action$.pipe(
17
- filterAction(actions.getAll.type),
18
- switchMap((action) => {
19
- return queryAllBookmarks.query({ isValid: action.payload }).pipe(
20
- last(),
21
- map((bookmarks) =>
22
- actions.getAll.success(
23
- bookmarks.value.filter(
24
- (bookmark) =>
25
- bookmark.sourceSystem?.identifier === sourceSystemIdentifier,
26
- ),
27
- ),
28
- ),
29
- catchError(() => EMPTY),
30
- );
31
- }),
32
- );
33
-
34
- export const handleCreateBookmark =
35
- (apiClient: BookmarksApiClient<'fetch', IHttpClient, unknown>) =>
36
- (action$: Observable<Actions>) =>
37
- action$.pipe(
38
- filterAction(actions.create.type),
39
- switchMap(async (action) => {
40
- const response = await apiClient.post('v1', action.payload);
41
- return actions.create.success(await response.json());
42
- }),
43
- );
44
-
45
- export const handleUpdateBookmark =
46
- (apiClient: BookmarksApiClient<'fetch', IHttpClient, unknown>) =>
47
- (action$: Observable<Actions>) =>
48
- action$.pipe(
49
- filterAction(actions.update.type),
50
- switchMap(async (action) => {
51
- // Should payload be partial? then api client wil need to change.
52
- const response = await apiClient.patch('v1', action.payload);
53
- return actions.update.success(await response.json());
54
- }),
55
- );
56
-
57
- export const handleDeleteBookmark =
58
- (apiClient: BookmarksApiClient<'fetch', IHttpClient, unknown>) =>
59
- (action$: Observable<Actions>) =>
60
- action$.pipe(
61
- filterAction(actions.delete.type),
62
- switchMap(async (action) => {
63
- const response = await apiClient.delete('v1', { id: action.payload });
64
- if (response.ok) return actions.delete.success(action.payload);
65
- return actions.delete.success(action.payload);
66
- }),
67
- );
@@ -1,35 +0,0 @@
1
- import { createReducer } from '@equinor/fusion-observable';
2
- import { Bookmark } from '../types';
3
- import { actions } from './bookmarkActions';
4
-
5
- export type State = {
6
- bookmarks: Record<string, Bookmark>;
7
- };
8
-
9
- const initialState: State = {
10
- bookmarks: {},
11
- };
12
-
13
- export const reducer = createReducer<State>(initialState, (builder) => {
14
- builder
15
- .addCase(actions.create.success, (state, { payload }) => {
16
- state.bookmarks[payload.id] = payload;
17
- })
18
- .addCase(actions.getAll.success, (state, { payload }) => {
19
- payload.forEach((bookmark) => {
20
- state.bookmarks[bookmark.id] = bookmark;
21
- });
22
- })
23
- .addCase(actions.delete.success, (state, { payload }) => {
24
- delete state.bookmarks[payload];
25
- })
26
- .addCase(actions.removeFavorite, (state, { payload }) => {
27
- delete state.bookmarks[payload];
28
- })
29
- .addCase(actions.addFavorite, (state, { payload }) => {
30
- state.bookmarks[payload.id] = payload;
31
- })
32
- .addCase(actions.update.success, (state, { payload }) => {
33
- state.bookmarks[payload.id] = payload;
34
- });
35
- });
@@ -1,235 +0,0 @@
1
- import { getBookmarkIdFormURL } from './utils/handle-url';
2
- import { BookmarkConfigBuilder, BookmarkConfigBuilderCallback } from './bookmark-config-builder';
3
-
4
- import type { ModuleInitializerArgs, ModulesInstanceType } from '@equinor/fusion-framework-module';
5
- import type { IApiProvider, ServicesModule } from '@equinor/fusion-framework-module-services';
6
- import type { SourceSystem } from './types';
7
- import type { ContextModule, IContextProvider } from '@equinor/fusion-framework-module-context';
8
- import type { AppModule, AppModuleProvider } from '@equinor/fusion-framework-module-app';
9
- import type { EventModule, IEventModuleProvider } from '@equinor/fusion-framework-module-event';
10
- import type { IBookmarkModuleProvider } from './bookmark-provider';
11
-
12
- import type { BookmarkClientConfig } from './client/bookmarkClient';
13
-
14
- export interface BookmarkModuleConfig {
15
- /**
16
- * SourceSystem used when creating a new bookmark,
17
- * used as the identifier for the current client. Only used in app shell / portal configuration.
18
- */
19
- sourceSystem?: SourceSystem;
20
-
21
- /**
22
- * The clintConfiguration consist of both Api client configuration and query client configuration
23
- */
24
- clientConfiguration: BookmarkClientConfig;
25
-
26
- /**
27
- * It is possible to associate a Fusion context with a bookmark and this function is used to resolve the current context id applied.
28
- * @returns - should return a fusion context id.
29
- */
30
- getContextId: () => string | undefined;
31
-
32
- /**
33
- * A bookmark kan be connected to a application this is used to resolve the current applications appKey.
34
- */
35
- getCurrentAppIdentification?(): string | undefined;
36
-
37
- /**
38
- * The resolves bookmarkId at startup, default configured to resolve id form URL with query parm
39
- * ?bookmarkId=GUID but this can be resolved for any where if needed.
40
- */
41
- resolveBookmarkId?(): string | null;
42
-
43
- /**
44
- * default set to Fusion Events Module.
45
- */
46
- event?: IEventModuleProvider;
47
- }
48
-
49
- export type IBookmarkModuleConfigurator = {
50
- /**
51
- * @param init BookmarkConfigBuilderCallback for configuring the bookmark module.
52
- * Configuration is only needed for application shell / portals.
53
- */
54
- addConfigBuilder(init: BookmarkConfigBuilderCallback): void;
55
- /**
56
- * Bookmark Config Factory Providing default configuration if no configuration is provided.
57
- * @param init ModuleInitializerArgs<IBookmarkModuleConfigurator, [ServicesModule, AppModule, ContextModule]>
58
- * @param parentProvider Parent IBookmarkModuleProvider
59
- */
60
- createConfig(
61
- init: ModuleInitializerArgs<
62
- IBookmarkModuleConfigurator,
63
- [ServicesModule, AppModule, ContextModule]
64
- >,
65
- parentProvider?: IBookmarkModuleProvider,
66
- ): Promise<BookmarkModuleConfig>;
67
- };
68
-
69
- export class BookmarkModuleConfigurator implements IBookmarkModuleConfigurator {
70
- defaultExpireTime = 1 * 60 * 1000;
71
-
72
- #configBuilders: Array<BookmarkConfigBuilderCallback> = [];
73
-
74
- addConfigBuilder(init: BookmarkConfigBuilderCallback): void {
75
- this.#configBuilders.push(init);
76
- }
77
-
78
- async createConfig(
79
- init: ModuleInitializerArgs<
80
- IBookmarkModuleConfigurator,
81
- [ServicesModule, AppModule, ContextModule]
82
- >,
83
- parentProvider?: IBookmarkModuleProvider,
84
- ): Promise<BookmarkModuleConfig> {
85
- const config: Partial<BookmarkModuleConfig> = await this.#configBuilders.reduce(
86
- async (cur, cb) => {
87
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
88
- const builder = new BookmarkConfigBuilder<any, any>(init, await cur);
89
- await Promise.resolve(cb(builder));
90
- return Object.assign(cur, builder.config);
91
- },
92
- Promise.resolve({} as Partial<BookmarkModuleConfig>),
93
- );
94
-
95
- const appProvider = await this._getAppProvider(init);
96
-
97
- this.#applyParentProviderConfiguration(parentProvider, config);
98
-
99
- if (!config.resolveBookmarkId) {
100
- config.resolveBookmarkId = getBookmarkIdFormURL;
101
- }
102
-
103
- if (!config.getCurrentAppIdentification) {
104
- config.getCurrentAppIdentification = () => appProvider.current?.appKey;
105
- }
106
-
107
- if (!config.getContextId) {
108
- const contextProvider = await this._getContextProvider(init);
109
- config.getContextId = () => {
110
- return contextProvider.currentContext?.id;
111
- };
112
- }
113
-
114
- if (!config.event) {
115
- const event = await this._getEventProvider(init);
116
- config.event = event;
117
- }
118
-
119
- config.clientConfiguration = await this._createClientConfiguration(
120
- init,
121
- config.clientConfiguration,
122
- );
123
-
124
- return config as BookmarkModuleConfig;
125
- }
126
-
127
- #applyParentProviderConfiguration(
128
- parentProvider: IBookmarkModuleProvider | undefined,
129
- config: Partial<BookmarkModuleConfig>,
130
- ) {
131
- if (parentProvider) {
132
- config.clientConfiguration = parentProvider.config.clientConfiguration;
133
- config.sourceSystem = parentProvider.config.sourceSystem;
134
- config.getContextId = parentProvider.config.getContextId;
135
- config.getCurrentAppIdentification = parentProvider.config.getCurrentAppIdentification;
136
- }
137
- }
138
-
139
- protected async _createClientConfiguration(
140
- init: ModuleInitializerArgs<
141
- IBookmarkModuleConfigurator,
142
- [ServicesModule, AppModule, ContextModule]
143
- >,
144
- config: Partial<BookmarkClientConfig> = {},
145
- ): Promise<BookmarkClientConfig> {
146
- if (!config.client) {
147
- const apiProvider = await this._getServiceProvider(init);
148
- config.client = await apiProvider.createBookmarksClient('fetch');
149
- }
150
-
151
- if (!config.queryClientConfig) {
152
- const apiProvider = await this._getServiceProvider(init);
153
- const bookmarkClient = await apiProvider.createBookmarksClient('json$');
154
-
155
- config.queryClientConfig = {
156
- getBookmarkById: {
157
- client: {
158
- fn: (args) => bookmarkClient.get('v1', args),
159
- },
160
- key: ({ id }) => id,
161
- expire: this.defaultExpireTime,
162
- },
163
- getAllBookmarks: {
164
- client: {
165
- fn: () => bookmarkClient.getAll('v1'),
166
- },
167
- key: () => 'all-bookmarks',
168
- validate: ({ args }) => args.isValid,
169
- expire: this.defaultExpireTime,
170
- },
171
- ...bookmarkClient,
172
- };
173
- }
174
-
175
- return config as BookmarkClientConfig;
176
- }
177
-
178
- protected async _getServiceProvider(
179
- init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [ServicesModule]>,
180
- ): Promise<IApiProvider> {
181
- if (init.hasModule('services')) {
182
- return init.requireInstance('services');
183
- } else {
184
- const parentServiceModule = (init.ref as ModulesInstanceType<[ServicesModule]>)
185
- ?.services;
186
- if (parentServiceModule) {
187
- return parentServiceModule;
188
- }
189
- throw Error('[BookmarkConfigurator] No service provider configures [ServicesModule] ');
190
- }
191
- }
192
-
193
- protected async _getContextProvider(
194
- init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [ContextModule]>,
195
- ): Promise<IContextProvider> {
196
- if (init.hasModule('context')) {
197
- return init.requireInstance('context');
198
- } else {
199
- const parentContextModule = (init.ref as ModulesInstanceType<[ContextModule]>)?.context;
200
- if (parentContextModule) {
201
- return parentContextModule;
202
- }
203
- throw Error('[BookmarkConfigurator] No context provider configures [ContextModule]');
204
- }
205
- }
206
-
207
- protected async _getAppProvider(
208
- init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [AppModule]>,
209
- ): Promise<AppModuleProvider> {
210
- if (init.hasModule('app')) {
211
- return init.requireInstance('app');
212
- } else {
213
- const parentAppModule = (init.ref as ModulesInstanceType<[AppModule]>)?.app;
214
- if (parentAppModule) {
215
- return parentAppModule;
216
- }
217
- throw Error('[BookmarkConfigurator] No app provider configures [AppModule]');
218
- }
219
- }
220
- protected async _getEventProvider(
221
- init: ModuleInitializerArgs<IBookmarkModuleConfigurator, [EventModule]>,
222
- ): Promise<IEventModuleProvider> {
223
- if (init.hasModule('event')) {
224
- return init.requireInstance('event');
225
- } else {
226
- const parentEventModule = (init.ref as ModulesInstanceType<[EventModule]>)?.event;
227
- if (parentEventModule) {
228
- return parentEventModule;
229
- }
230
- throw Error('[BookmarkConfigurator] No event provider configures [EventModule]');
231
- }
232
- }
233
- }
234
-
235
- export default BookmarkModuleConfigurator;