@monorise/react 0.3.2 → 0.3.3

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.
@@ -6,11 +6,17 @@ import { convertToMap, getEntityRequestKey, getMutualRequestKey, getMutualStateK
6
6
  // Should we use store.getState() or store()?
7
7
  // USE store.getState() WHEN using within an action such as getEntity, getProfile
8
8
  // USE store() WHEN using within a react hook so that it gets the benefit of reactivity, such as useProfile, useEntity
9
+ const defaultOnError = (error) => {
10
+ if (!(error instanceof Error) && error.status >= 500) {
11
+ // we only want server error to be captured by error tracking service
12
+ throw error;
13
+ }
14
+ };
9
15
  const initCoreActions = (monoriseStore, appActions, coreService) => {
10
16
  const { makeEntityService, makeMutualService } = coreService;
11
17
  const { checkIsLoading, getError, useLoadStore, useErrorStore } = appActions;
12
18
  const listEntities = async (entityType, params = {}, opts = {}) => {
13
- var _a;
19
+ var _a, _b;
14
20
  const store = monoriseStore.getState();
15
21
  const entityState = (_a = store.entity[entityType]) !== null && _a !== void 0 ? _a : {};
16
22
  const { isFirstFetched } = entityState;
@@ -19,54 +25,94 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
19
25
  const requestKey = getEntityRequestKey('list', entityType);
20
26
  const isLoading = checkIsLoading(requestKey);
21
27
  const error = getError(requestKey);
22
- if ((isFirstFetched && !params.skRange) || isLoading || error) {
28
+ const onError = (_b = opts.onError) !== null && _b !== void 0 ? _b : defaultOnError;
29
+ if (error) {
30
+ onError(error);
23
31
  return;
24
32
  }
25
- const { data: result } = await entityService.listEntities(Object.assign(Object.assign({}, ((params === null || params === void 0 ? void 0 : params.all) ? {} : { limit: 20 })), { start: skRange === null || skRange === void 0 ? void 0 : skRange.start, end: skRange === null || skRange === void 0 ? void 0 : skRange.end }), opts);
26
- const newEntityMap = convertToMap(result.data, 'entityId');
27
- const mergedMap = new Map([
28
- ...newEntityMap,
29
- ...store.entity[entityType].dataMap,
30
- ]);
31
- monoriseStore.setState(produce((state) => {
32
- state.entity[entityType] = {
33
- dataMap: params.skRange ? newEntityMap : mergedMap,
34
- isFirstFetched: true,
35
- lastKey: result.lastKey,
36
- };
37
- }), undefined, `mr/entity/list/${entityType}`);
33
+ if ((isFirstFetched && !params.skRange) || isLoading) {
34
+ return;
35
+ }
36
+ try {
37
+ const { data: result } = await entityService.listEntities(Object.assign(Object.assign({}, ((params === null || params === void 0 ? void 0 : params.all) ? {} : { limit: 20 })), { start: skRange === null || skRange === void 0 ? void 0 : skRange.start, end: skRange === null || skRange === void 0 ? void 0 : skRange.end }), opts);
38
+ const newEntityMap = convertToMap(result.data, 'entityId');
39
+ const mergedMap = new Map([
40
+ ...newEntityMap,
41
+ ...store.entity[entityType].dataMap,
42
+ ]);
43
+ monoriseStore.setState(produce((state) => {
44
+ state.entity[entityType] = {
45
+ dataMap: params.skRange ? newEntityMap : mergedMap,
46
+ isFirstFetched: true,
47
+ lastKey: result.lastKey,
48
+ };
49
+ }), undefined, `mr/entity/list/${entityType}`);
50
+ }
51
+ catch (err) {
52
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
53
+ return { error };
54
+ }
38
55
  };
39
56
  const listMoreEntities = async (entityType, opts = {}) => {
57
+ var _a;
58
+ const requestKey = getEntityRequestKey('list', entityType);
40
59
  const store = monoriseStore.getState();
41
60
  const entityState = store.entity[entityType];
42
61
  const { dataMap, lastKey } = entityState;
43
62
  const entityService = makeEntityService(entityType);
63
+ const error = getError(requestKey);
64
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
44
65
  if (!lastKey) {
45
66
  return;
46
67
  }
47
- const { data: result } = await entityService.listEntities({
48
- limit: 20,
49
- lastKey,
50
- }, opts);
51
- monoriseStore.setState(produce((state) => {
52
- state.entity[entityType].isFirstFetched = true;
53
- state.entity[entityType].lastKey = result.lastKey;
54
- for (const i in result.data) {
55
- state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
56
- }
57
- }), undefined, `mr/entity/list-more/${entityType}`);
68
+ if (error) {
69
+ onError(error);
70
+ return;
71
+ }
72
+ try {
73
+ const { data: result } = await entityService.listEntities({
74
+ limit: 20,
75
+ lastKey,
76
+ }, opts);
77
+ monoriseStore.setState(produce((state) => {
78
+ state.entity[entityType].isFirstFetched = true;
79
+ state.entity[entityType].lastKey = result.lastKey;
80
+ for (const i in result.data) {
81
+ state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
82
+ }
83
+ }), undefined, `mr/entity/list-more/${entityType}`);
84
+ }
85
+ catch (err) {
86
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
87
+ return { error };
88
+ }
58
89
  };
59
90
  const searchEntities = async (entityType, query, opts = {}) => {
91
+ var _a;
92
+ const requestKey = getEntityRequestKey('search', entityType);
60
93
  const entityService = makeEntityService(entityType);
61
- const { data: result } = await entityService.searchEntities(query, opts);
62
- monoriseStore.setState(produce((state) => {
63
- state.entity[entityType].searchResults = result.data;
64
- for (const i in result.data) {
65
- state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
66
- }
67
- }));
94
+ const error = getError(requestKey);
95
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
96
+ if (error) {
97
+ onError(error);
98
+ return;
99
+ }
100
+ try {
101
+ const { data: result } = await entityService.searchEntities(query, opts);
102
+ monoriseStore.setState(produce((state) => {
103
+ state.entity[entityType].searchResults = result.data;
104
+ for (const i in result.data) {
105
+ state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
106
+ }
107
+ }));
108
+ }
109
+ catch (err) {
110
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
111
+ return { error };
112
+ }
68
113
  };
69
114
  const listEntitiesByTag = async (entityType, tagName, opts = {}) => {
115
+ var _a;
70
116
  const tagKey = getTagStateKey(entityType, tagName, opts.params);
71
117
  const state = monoriseStore.getState();
72
118
  const tagState = state.tag[tagKey] || {};
@@ -76,26 +122,43 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
76
122
  const requestKey = getTagRequestKey('list', entityType, tagName, opts.params);
77
123
  const isLoading = checkIsLoading(requestKey);
78
124
  const error = getError(requestKey);
79
- if (!forceFetch && (isFirstFetched || isLoading || error)) {
80
- return;
81
- }
82
- const { data } = await entityService.listEntitiesByTag(tagName, Object.assign(Object.assign({}, opts), { requestKey }));
83
- const { entities, lastKey } = data;
84
- monoriseStore.setState(produce((state) => {
85
- for (const entity of entities) {
86
- state.entity[entityType].dataMap.set(entity.entityId, entity);
125
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
126
+ if (!forceFetch) {
127
+ if (error) {
128
+ onError(error);
129
+ return { error, data: null };
87
130
  }
88
- }));
89
- monoriseStore.setState(produce((state) => {
90
- state.tag[tagKey] = {
91
- dataMap: convertToMap(entities, 'entityId'),
92
- isFirstFetched: true,
93
- lastKey,
94
- };
95
- }));
96
- return data;
131
+ if (isFirstFetched || isLoading) {
132
+ return {
133
+ data: { entities: Array.from(dataMap.values()), lastKey: null },
134
+ error: null,
135
+ };
136
+ }
137
+ }
138
+ try {
139
+ const { data } = await entityService.listEntitiesByTag(tagName, Object.assign(Object.assign({}, opts), { requestKey }));
140
+ const { entities, lastKey } = data;
141
+ monoriseStore.setState(produce((state) => {
142
+ for (const entity of entities) {
143
+ state.entity[entityType].dataMap.set(entity.entityId, entity);
144
+ }
145
+ }));
146
+ monoriseStore.setState(produce((state) => {
147
+ state.tag[tagKey] = {
148
+ dataMap: convertToMap(entities, 'entityId'),
149
+ isFirstFetched: true,
150
+ lastKey,
151
+ };
152
+ }));
153
+ return { data };
154
+ }
155
+ catch (err) {
156
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
157
+ return { error, data: null };
158
+ }
97
159
  };
98
160
  const getEntity = async (entityType, id, opts = {}) => {
161
+ var _a;
99
162
  const store = monoriseStore.getState();
100
163
  const entityState = store.entity[entityType];
101
164
  const { dataMap } = entityState;
@@ -105,16 +168,36 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
105
168
  const isLoading = checkIsLoading(requestKey);
106
169
  const error = getError(requestKey);
107
170
  const { forceFetch } = opts;
108
- if (!forceFetch && (entity || isLoading || error)) {
109
- return entity;
171
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
172
+ if (!forceFetch) {
173
+ if (error) {
174
+ // If there's an existing error, handle it and return the error.
175
+ onError === null || onError === void 0 ? void 0 : onError(error);
176
+ return { data: undefined, error };
177
+ }
178
+ if (entity) {
179
+ // If entity already exists and no force fetch, return it.
180
+ return { data: entity, error: undefined };
181
+ }
182
+ if (isLoading) {
183
+ // If already loading, return undefined data and error to indicate ongoing operation.
184
+ return { data: undefined, error: undefined };
185
+ }
186
+ }
187
+ try {
188
+ ({ data: entity } = await entityService.getEntity(id, opts));
189
+ monoriseStore.setState(produce((state) => {
190
+ state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
191
+ }), undefined, `mr/entity/get/${entityType}/${id}`);
192
+ return { data: entity, error: null };
193
+ }
194
+ catch (err) {
195
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
196
+ return { data: undefined, error };
110
197
  }
111
- ({ data: entity } = await entityService.getEntity(id, opts));
112
- monoriseStore.setState(produce((state) => {
113
- state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
114
- }), undefined, `mr/entity/get/${entityType}/${id}`);
115
- return entity;
116
198
  };
117
199
  const getEntityByUniqueField = async (entityType, fieldName, value, opts = {}) => {
200
+ var _a;
118
201
  const store = monoriseStore.getState();
119
202
  const entityState = store.entity[entityType];
120
203
  const { dataMap } = entityState;
@@ -125,64 +208,120 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
125
208
  const isLoading = checkIsLoading(requestKey);
126
209
  const error = getError(requestKey);
127
210
  const { forceFetch } = opts;
128
- if (!forceFetch && (entity || isLoading || error)) {
129
- return entity;
211
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
212
+ if (!forceFetch) {
213
+ if (error) {
214
+ onError === null || onError === void 0 ? void 0 : onError(error);
215
+ return { data: undefined, error };
216
+ }
217
+ if (entity) {
218
+ return { data: entity, error: undefined };
219
+ }
220
+ if (isLoading) {
221
+ return { data: undefined, error: undefined };
222
+ }
223
+ }
224
+ try {
225
+ ({ data: entity } = await entityService.getEntityByUniqueField(fieldName, value, opts));
226
+ monoriseStore.setState(produce((state) => {
227
+ state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
228
+ state.entity[entityType].dataMap.set(`${stateKey}`, entity);
229
+ }), undefined, `mr/entity/unique/${entityType}/${stateKey}`);
230
+ return { data: entity, error: null };
231
+ }
232
+ catch (err) {
233
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
234
+ return { data: undefined, error };
130
235
  }
131
- ({ data: entity } = await entityService.getEntityByUniqueField(fieldName, value, opts));
132
- monoriseStore.setState(produce((state) => {
133
- state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
134
- state.entity[entityType].dataMap.set(`${stateKey}`, entity);
135
- }), undefined, `mr/entity/unique/${entityType}/${stateKey}`);
136
- return entity;
137
236
  };
138
237
  const createEntity = async (entityType, entity, opts = {}) => {
238
+ var _a;
139
239
  const entityService = makeEntityService(entityType);
140
- const { data } = await entityService.createEntity(entity, opts);
141
- monoriseStore.setState(produce((state) => {
142
- state.entity[entityType].dataMap.set(data.entityId, data);
143
- }), undefined, `mr/entity/create/${entityType}`);
144
- return data;
240
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
241
+ try {
242
+ const { data } = await entityService.createEntity(entity, opts);
243
+ monoriseStore.setState(produce((state) => {
244
+ state.entity[entityType].dataMap.set(data.entityId, data);
245
+ }), undefined, `mr/entity/create/${entityType}`);
246
+ return { data, error: null };
247
+ }
248
+ catch (err) {
249
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
250
+ onError === null || onError === void 0 ? void 0 : onError(error);
251
+ return { data: null, error };
252
+ }
145
253
  };
146
254
  const upsertEntity = async (entityType, id, entity, opts = {}) => {
255
+ var _a;
147
256
  const entityService = makeEntityService(entityType);
148
- const { data } = await entityService.upsertEntity(id, entity, opts);
149
- monoriseStore.setState(produce((state) => {
150
- state.entity[entityType].dataMap.set(data.entityId, data);
151
- }), undefined, `mr/entity/upsert/${entityType}/${id}`);
257
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
258
+ try {
259
+ const { data } = await entityService.upsertEntity(id, entity, opts);
260
+ monoriseStore.setState(produce((state) => {
261
+ state.entity[entityType].dataMap.set(data.entityId, data);
262
+ }), undefined, `mr/entity/upsert/${entityType}/${id}`);
263
+ return { data, error: null };
264
+ }
265
+ catch (err) {
266
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
267
+ onError(error);
268
+ return { data: null, error };
269
+ }
152
270
  };
153
271
  const editEntity = async (entityType, id, entity, opts = {}) => {
272
+ var _a;
154
273
  const entityService = makeEntityService(entityType);
155
- const { data } = await entityService.editEntity(id, entity, opts);
156
- monoriseStore.setState(produce((state) => {
157
- state.entity[entityType].dataMap.set(data.entityId, data);
158
- // update mutual's entity data
159
- for (const key of Object.keys(state.mutual)) {
160
- const [_byEntity, _byId, _entityType] = key.split('/');
161
- if (_entityType === entityType) {
162
- const mutual = state.mutual[key].dataMap.get(id);
163
- state.mutual[key].dataMap = new Map(state.mutual[key].dataMap).set(id, Object.assign(Object.assign({}, mutual), { data: data.data }));
274
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
275
+ try {
276
+ const { data } = await entityService.editEntity(id, entity, opts);
277
+ monoriseStore.setState(produce((state) => {
278
+ state.entity[entityType].dataMap.set(data.entityId, data);
279
+ // update mutual's entity data
280
+ for (const key of Object.keys(state.mutual)) {
281
+ const [_byEntity, _byId, _entityType] = key.split('/');
282
+ if (_entityType === entityType) {
283
+ const mutual = state.mutual[key].dataMap.get(id);
284
+ state.mutual[key].dataMap = new Map(state.mutual[key].dataMap).set(id, Object.assign(Object.assign({}, mutual), { data: data.data }));
285
+ }
164
286
  }
165
- }
166
- }), undefined, `mr/entity/edit/${entityType}/${id}`);
287
+ }), undefined, `mr/entity/edit/${entityType}/${id}`);
288
+ return { data, error: null };
289
+ }
290
+ catch (err) {
291
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
292
+ onError(error);
293
+ return { data: null, error };
294
+ }
167
295
  };
168
296
  const deleteEntity = async (entityType, id, opts = {}) => {
297
+ var _a;
169
298
  const entityService = makeEntityService(entityType);
170
- await entityService.deleteEntity(id, opts);
171
- deleteLocalMutualsByEntity(entityType, id);
172
- monoriseStore.setState(produce((state) => {
173
- state.entity[entityType].dataMap.delete(id);
174
- // delete mutual's entity data
175
- for (const key of Object.keys(state.mutual)) {
176
- const [_byEntity, _byId, _entityType] = key.split('/');
177
- if (_entityType === entityType) {
178
- state.mutual[key].dataMap.delete(id);
299
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
300
+ try {
301
+ await entityService.deleteEntity(id, opts);
302
+ deleteLocalMutualsByEntity(entityType, id);
303
+ monoriseStore.setState(produce((state) => {
304
+ state.entity[entityType].dataMap.delete(id);
305
+ // delete mutual's entity data
306
+ for (const key of Object.keys(state.mutual)) {
307
+ const [_byEntity, _byId, _entityType] = key.split('/');
308
+ if (_entityType === entityType) {
309
+ state.mutual[key].dataMap.delete(id);
310
+ }
179
311
  }
180
- }
181
- }), undefined, `mr/entity/delete/${entityType}/${id}`);
312
+ }), undefined, `mr/entity/delete/${entityType}/${id}`);
313
+ return { data: { entityId: id }, error: null }; // Indicate success with the deleted ID
314
+ }
315
+ catch (err) {
316
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
317
+ onError(error);
318
+ return { data: null, error };
319
+ }
182
320
  };
183
321
  const listEntitiesByEntity = async (byEntityType, entityType, id, opts = {}, chainEntityQuery) => {
184
- var _a;
185
- const selfKey = (_a = opts.stateKey) !== null && _a !== void 0 ? _a : getMutualStateKey(byEntityType, id, entityType, undefined, chainEntityQuery);
322
+ var _a, _b, _c;
323
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
324
+ const selfKey = (_b = opts.stateKey) !== null && _b !== void 0 ? _b : getMutualStateKey(byEntityType, id, entityType, undefined, chainEntityQuery);
186
325
  const mutualService = makeMutualService(byEntityType, entityType);
187
326
  const store = monoriseStore.getState();
188
327
  const mutualState = store.mutual[selfKey] || {};
@@ -191,26 +330,45 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
191
330
  const isLoading = checkIsLoading(requestKey);
192
331
  const error = getError(requestKey);
193
332
  const { forceFetch } = opts;
194
- if (!forceFetch && (isFirstFetched || isLoading || error)) {
195
- return;
196
- }
197
- const { data } = await mutualService.listEntitiesByEntity(id, opts, chainEntityQuery);
198
- const { entities, lastKey } = data;
199
- const newEntityDataMap = new Map();
200
- for (const i in entities) {
201
- newEntityDataMap.set(entities[i].entityId, entities[i]);
333
+ if (!forceFetch) {
334
+ if (error) {
335
+ onError(error);
336
+ return { error, data: null };
337
+ }
338
+ if (isFirstFetched || isLoading) {
339
+ return {
340
+ error: null,
341
+ data: {
342
+ entities: Array.from(((_c = mutualState.dataMap) === null || _c === void 0 ? void 0 : _c.values()) || []),
343
+ lastKey: mutualState.lastKey,
344
+ },
345
+ };
346
+ }
202
347
  }
203
- monoriseStore.setState(produce((state) => {
204
- var _a;
205
- for (const [key, value] of newEntityDataMap) {
206
- (_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap.set(key, value);
348
+ try {
349
+ const { data } = await mutualService.listEntitiesByEntity(id, opts, chainEntityQuery);
350
+ const { entities, lastKey } = data;
351
+ const newEntityDataMap = new Map();
352
+ for (const i in entities) {
353
+ newEntityDataMap.set(entities[i].entityId, entities[i]);
207
354
  }
208
- state.mutual[selfKey] = {
209
- dataMap: convertToMap(entities, 'entityId'),
210
- isFirstFetched: true,
211
- lastKey,
212
- };
213
- }), undefined, `mr/mutual/list/${selfKey}`);
355
+ monoriseStore.setState(produce((state) => {
356
+ var _a;
357
+ for (const [key, value] of newEntityDataMap) {
358
+ (_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap.set(key, value);
359
+ }
360
+ state.mutual[selfKey] = {
361
+ dataMap: convertToMap(entities, 'entityId'),
362
+ isFirstFetched: true,
363
+ lastKey,
364
+ };
365
+ }), undefined, `mr/mutual/list/${selfKey}`);
366
+ return { data, error: null };
367
+ }
368
+ catch (err) {
369
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
370
+ return { error, data: null };
371
+ }
214
372
  };
215
373
  // todo: list more mutuals by entity
216
374
  // const listMoremutualsByEntity = async (
@@ -219,7 +377,7 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
219
377
  // id: string,
220
378
  // ) =>
221
379
  const getMutual = async (byEntityType, entityType, byEntityId, entityId, opts = {}) => {
222
- var _a, _b;
380
+ var _a, _b, _c, _d;
223
381
  const selfKey = getMutualStateKey(byEntityType, byEntityId, entityType);
224
382
  const mutualService = makeMutualService(byEntityType, entityType);
225
383
  const store = monoriseStore.getState();
@@ -227,11 +385,22 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
227
385
  const requestKey = getMutualRequestKey('get', byEntityType, entityType, byEntityId, entityId);
228
386
  const isLoading = checkIsLoading(requestKey);
229
387
  const error = getError(requestKey);
230
- if (!byEntityId || isLoading || error) {
231
- return;
388
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
389
+ if (error) {
390
+ onError === null || onError === void 0 ? void 0 : onError(error);
391
+ return { data: undefined, error };
392
+ }
393
+ if (!byEntityId || isLoading) {
394
+ return {
395
+ data: (_b = mutualState.dataMap) === null || _b === void 0 ? void 0 : _b.get(entityId),
396
+ error: null,
397
+ };
232
398
  }
233
- if ((_a = mutualState.dataMap) === null || _a === void 0 ? void 0 : _a.get(entityId)) {
234
- return mutualState.dataMap.get(entityId);
399
+ if ((_c = mutualState.dataMap) === null || _c === void 0 ? void 0 : _c.get(entityId)) {
400
+ return {
401
+ data: mutualState.dataMap.get(entityId),
402
+ error: null,
403
+ };
235
404
  }
236
405
  let mutual;
237
406
  let hasRequestFailed = false;
@@ -240,7 +409,8 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
240
409
  }
241
410
  catch (err) {
242
411
  if (!opts.defaultMutualData) {
243
- return;
412
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
413
+ return { data: null, error };
244
414
  }
245
415
  hasRequestFailed = true;
246
416
  mutual = {
@@ -255,7 +425,7 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
255
425
  mutualData: opts.defaultMutualData,
256
426
  };
257
427
  }
258
- const newMutualDataMap = new Map((_b = store.mutual[selfKey]) === null || _b === void 0 ? void 0 : _b.dataMap);
428
+ const newMutualDataMap = new Map((_d = store.mutual[selfKey]) === null || _d === void 0 ? void 0 : _d.dataMap);
259
429
  newMutualDataMap.set(mutual.entityId, mutual);
260
430
  if (!hasRequestFailed) {
261
431
  const entityState = store.entity[entityType];
@@ -267,27 +437,38 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
267
437
  state.entity[entityType].dataMap = newEntityDataMap;
268
438
  }), undefined, `mr/mutual/get/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
269
439
  }
440
+ return { data: mutual };
270
441
  };
271
442
  const createMutual = async (byEntityType, entityType, byEntityId, entityId, payload = {}, opts = {}) => {
443
+ var _a;
272
444
  const mutualService = makeMutualService(byEntityType, entityType);
273
- const { data: mutual } = await mutualService.createMutual(byEntityId, entityId, payload, opts);
274
- monoriseStore.setState(produce((state) => {
275
- var _a, _b;
276
- const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
277
- const side = getMutualStateKey(entityType, entityId, byEntityType);
278
- if (!state.mutual[bySide]) {
279
- state.mutual[bySide] = {
280
- dataMap: new Map(),
281
- };
282
- }
283
- state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
284
- if (!state.mutual[side]) {
285
- state.mutual[side] = {
286
- dataMap: new Map(),
287
- };
288
- }
289
- state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
290
- }), undefined, `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
445
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
446
+ try {
447
+ const { data: mutual } = await mutualService.createMutual(byEntityId, entityId, payload, opts);
448
+ monoriseStore.setState(produce((state) => {
449
+ var _a, _b;
450
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
451
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
452
+ if (!state.mutual[bySide]) {
453
+ state.mutual[bySide] = {
454
+ dataMap: new Map(),
455
+ };
456
+ }
457
+ state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
458
+ if (!state.mutual[side]) {
459
+ state.mutual[side] = {
460
+ dataMap: new Map(),
461
+ };
462
+ }
463
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
464
+ }), undefined, `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
465
+ return { data: mutual, error: null };
466
+ }
467
+ catch (err) {
468
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
469
+ onError(error);
470
+ return { data: null, error };
471
+ }
291
472
  };
292
473
  const createLocalMutual = async (byEntityType, entityType, byEntityId, entityId, mutualData, data) => {
293
474
  const mutual = constructMutual(byEntityType, byEntityId, entityType, entityId, mutualData, data);
@@ -336,54 +517,78 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
336
517
  }), undefined, `mr/mutual/local-update/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
337
518
  };
338
519
  const editMutual = async (byEntityType, entityType, byEntityId, entityId, payload = {}, opts = {}) => {
520
+ var _a;
339
521
  const mutualService = makeMutualService(byEntityType, entityType);
340
- const { data: mutual } = await mutualService.editMutual(byEntityId, entityId, payload, opts);
341
- monoriseStore.setState(produce((state) => {
342
- var _a, _b;
343
- const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
344
- const side = getMutualStateKey(entityType, entityId, byEntityType);
345
- if (!state.mutual[bySide]) {
346
- state.mutual[bySide] = {
347
- dataMap: new Map(),
348
- };
349
- }
350
- state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
351
- if (!state.mutual[side]) {
352
- state.mutual[side] = {
353
- dataMap: new Map(),
354
- };
355
- }
356
- state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
357
- }), undefined, `mr/mutual/edit/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
522
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
523
+ try {
524
+ const { data: mutual } = await mutualService.editMutual(byEntityId, entityId, payload, opts);
525
+ monoriseStore.setState(produce((state) => {
526
+ var _a, _b;
527
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
528
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
529
+ if (!state.mutual[bySide]) {
530
+ state.mutual[bySide] = {
531
+ dataMap: new Map(),
532
+ };
533
+ }
534
+ state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
535
+ if (!state.mutual[side]) {
536
+ state.mutual[side] = {
537
+ dataMap: new Map(),
538
+ };
539
+ }
540
+ state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
541
+ }), undefined, `mr/mutual/edit/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
542
+ return { data: mutual, error: null };
543
+ }
544
+ catch (err) {
545
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
546
+ onError(error);
547
+ return { data: null, error };
548
+ }
358
549
  };
359
550
  const deleteMutual = async (byEntityType, entityType, byEntityId, entityId = '', opts = {}) => {
551
+ var _a;
360
552
  const mutualService = makeMutualService(byEntityType, entityType);
361
- const { data } = await mutualService.deleteMutual(byEntityId, entityId, opts);
362
- monoriseStore.setState(produce((state) => {
363
- const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
364
- const side = getMutualStateKey(entityType, entityId, byEntityType);
365
- state.mutual[bySide].dataMap.delete(data.entityId);
366
- if (state.mutual[side]) {
367
- state.mutual[side].dataMap.delete(data.byEntityId);
368
- }
369
- }), undefined, `mr/mutual/delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
553
+ const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
554
+ try {
555
+ const { data } = await mutualService.deleteMutual(byEntityId, entityId, opts);
556
+ monoriseStore.setState(produce((state) => {
557
+ const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
558
+ const side = getMutualStateKey(entityType, entityId, byEntityType);
559
+ state.mutual[bySide].dataMap.delete(data.entityId);
560
+ if (state.mutual[side]) {
561
+ state.mutual[side].dataMap.delete(data.byEntityId);
562
+ }
563
+ }), undefined, `mr/mutual/delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
564
+ return { data, error: null };
565
+ }
566
+ catch (err) {
567
+ const error = err instanceof Error ? err : new Error('Unknown error occurred');
568
+ onError(error);
569
+ return { data: null, error };
570
+ }
370
571
  };
371
572
  const deleteLocalMutual = (byEntityType, entityType, byEntityId, entityId) => {
372
573
  monoriseStore.setState(produce((state) => {
373
574
  var _a, _b;
374
575
  const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
375
576
  const side = getMutualStateKey(entityType, entityId, byEntityType);
376
- const bySideDataMap = new Map((_a = state[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap);
377
- const sideDataMap = new Map((_b = state[side]) === null || _b === void 0 ? void 0 : _b.dataMap);
577
+ const bySideDataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap);
578
+ const sideDataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap);
378
579
  bySideDataMap.delete(entityId);
379
580
  sideDataMap.delete(byEntityId);
380
- state.mutual[bySide].dataMap.delete(entityId);
381
- state.mutual[side].dataMap.delete(byEntityId);
581
+ if (state.mutual[bySide]) {
582
+ state.mutual[bySide].dataMap.delete(entityId);
583
+ }
584
+ if (state.mutual[side]) {
585
+ state.mutual[side].dataMap.delete(byEntityId);
586
+ }
382
587
  }), undefined, `mr/mutual/local-delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
383
588
  };
384
589
  const deleteLocalMutualsByEntity = (entityType, id) => {
385
590
  const store = monoriseStore.getState();
386
- const entityConfig = store.config;
591
+ const entityConfig = store.config; // Assuming store.config holds mutual related configurations
387
592
  let mutuals = [];
388
593
  for (const i of Object.keys(entityConfig)) {
389
594
  const mutualState = store.mutual[`${entityType}/${id}/${i}`];
@@ -393,14 +598,20 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
393
598
  ];
394
599
  }
395
600
  const updatedState = mutuals.reduce((acc, mutual) => {
601
+ var _a;
396
602
  const side = getMutualStateKey(mutual.entityType, mutual.entityId, mutual.byEntityType);
397
- const dataMap = new Map(store.mutual[side].dataMap);
603
+ const dataMap = new Map((_a = store.mutual[side]) === null || _a === void 0 ? void 0 : _a.dataMap); // Use optional chaining
398
604
  dataMap.delete(id);
399
605
  acc[side] = Object.assign(Object.assign({}, store.mutual[side]), { dataMap });
400
606
  return acc;
401
607
  }, {});
402
608
  monoriseStore.setState(produce((state) => {
403
- state.mutual = updatedState;
609
+ // This is a full replacement, might not be desired for partial updates.
610
+ // If state.mutual can contain other keys, this might clear them.
611
+ // Consider a more granular update if needed.
612
+ for (const key in updatedState) {
613
+ state.mutual[key] = updatedState[key];
614
+ }
404
615
  }), undefined, `mr/mutual/local-delete-by-entity/${entityType}/${id}`);
405
616
  };
406
617
  const useEntity = (entityType, id, opts = {}) => {
@@ -422,7 +633,11 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
422
633
  isFirstFetched,
423
634
  refetch: async () => {
424
635
  if (id) {
425
- return await getEntity(entityType, id, Object.assign(Object.assign({}, opts), { forceFetch: true }));
636
+ const { data, error: fetchError } = await getEntity(entityType, id, Object.assign(Object.assign({}, opts), { forceFetch: true }));
637
+ if (fetchError) {
638
+ // Handle refetch error if necessary, e.g., throw or set local state
639
+ }
640
+ return data;
426
641
  }
427
642
  },
428
643
  };
@@ -447,7 +662,11 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
447
662
  isFirstFetched,
448
663
  refetch: async () => {
449
664
  if (value) {
450
- return await getEntityByUniqueField(entityType, fieldName, value, Object.assign(Object.assign({}, opts), { forceFetch: true }));
665
+ const { data, error: fetchError } = await getEntityByUniqueField(entityType, fieldName, value, Object.assign(Object.assign({}, opts), { forceFetch: true }));
666
+ if (fetchError) {
667
+ // Handle refetch error if necessary
668
+ }
669
+ return data;
451
670
  }
452
671
  },
453
672
  };