@monorise/react 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actions/app.action.ts +218 -0
- package/actions/auth.action.ts +94 -0
- package/actions/config.action.ts +51 -0
- package/actions/core.action.ts +940 -0
- package/dist/actions/core.action.d.ts.map +1 -1
- package/dist/actions/core.action.js +15 -0
- package/dist/actions/core.action.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/lib/api.ts +150 -0
- package/lib/constant.ts +1 -0
- package/lib/entity.ts +76 -0
- package/lib/utils.ts +12 -0
- package/package.json +1 -1
- package/services/auth.service.ts +65 -0
- package/services/core.service.ts +415 -0
- package/services/filestore.service.ts +136 -0
- package/store/monorise.store.ts +105 -0
- package/types/api.type.ts +57 -0
- package/types/axios.type.ts +14 -0
- package/types/monorise.type.ts +8 -0
- package/types/mutual.type.ts +32 -0
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
import type { CreatedEntity, DraftEntity, Entity } from '@monorise/base';
|
|
2
|
+
import type { AxiosRequestConfig } from 'axios';
|
|
3
|
+
import type { MonoriseStore } from '../store/monorise.store';
|
|
4
|
+
import type { AxiosInterceptor } from '../types/api.type';
|
|
5
|
+
import type { Mutual, MutualData } from '../types/mutual.type';
|
|
6
|
+
|
|
7
|
+
const ENTITY_API_BASE_URL = '/api/core/entity';
|
|
8
|
+
const MUTUAL_API_BASE_URL = '/api/core/mutual';
|
|
9
|
+
const TAG_API_BASE_URL = '/api/core/tag';
|
|
10
|
+
|
|
11
|
+
type ListEntitiesPayload = {
|
|
12
|
+
limit?: number;
|
|
13
|
+
lastKey?: string;
|
|
14
|
+
start?: string;
|
|
15
|
+
end?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type ListEntitiesByTagParams = {
|
|
19
|
+
group?: string;
|
|
20
|
+
query?: string;
|
|
21
|
+
start?: string;
|
|
22
|
+
end?: string;
|
|
23
|
+
limit?: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type ConfigOptions = {
|
|
27
|
+
entityApiBaseUrl?: string;
|
|
28
|
+
mutualApiBaseUrl?: string;
|
|
29
|
+
tagApiBaseUrl?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type CommonOptions = Partial<AxiosRequestConfig> & {
|
|
33
|
+
customUrl?: string;
|
|
34
|
+
isInterruptive?: boolean;
|
|
35
|
+
feedback?: {
|
|
36
|
+
success?: string;
|
|
37
|
+
failure?: string;
|
|
38
|
+
loading?: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const initCoreService = (
|
|
43
|
+
monoriseStore: MonoriseStore,
|
|
44
|
+
axios: AxiosInterceptor,
|
|
45
|
+
opts?: ConfigOptions,
|
|
46
|
+
) => {
|
|
47
|
+
let options: ConfigOptions = opts || {};
|
|
48
|
+
|
|
49
|
+
const listEntities = <T extends Entity>(
|
|
50
|
+
entityType: T,
|
|
51
|
+
payload?: ListEntitiesPayload,
|
|
52
|
+
opts: CommonOptions = {},
|
|
53
|
+
) => {
|
|
54
|
+
const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
|
|
55
|
+
return axios.get<{
|
|
56
|
+
data: CreatedEntity<T>[];
|
|
57
|
+
lastKey?: string;
|
|
58
|
+
totalCount: number;
|
|
59
|
+
}>(opts.customUrl || `${entityApiBaseUrl}/${entityType}`, {
|
|
60
|
+
requestKey: `entity/${entityType}/list`,
|
|
61
|
+
params: payload ?? undefined,
|
|
62
|
+
isInterruptive: opts.isInterruptive,
|
|
63
|
+
feedback: opts.feedback,
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const searchEntities = <T extends Entity>(
|
|
68
|
+
entityType: T,
|
|
69
|
+
query: string,
|
|
70
|
+
opts: CommonOptions = {},
|
|
71
|
+
) => {
|
|
72
|
+
const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
|
|
73
|
+
return axios.get<{ data: CreatedEntity<T>[] }>(
|
|
74
|
+
opts.customUrl || `${entityApiBaseUrl}/${entityType}`,
|
|
75
|
+
{
|
|
76
|
+
requestKey: `entity/${entityType}/search`,
|
|
77
|
+
params: { query },
|
|
78
|
+
isInterruptive: opts.isInterruptive,
|
|
79
|
+
feedback: opts.feedback,
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const listEntitiesByTag = <T extends Entity>(
|
|
85
|
+
entityType: T,
|
|
86
|
+
tagName: string,
|
|
87
|
+
opts: CommonOptions & { params?: ListEntitiesByTagParams } = {},
|
|
88
|
+
) => {
|
|
89
|
+
const { tagApiBaseUrl = TAG_API_BASE_URL } = options;
|
|
90
|
+
return axios.get<{ entities: CreatedEntity<T>[]; lastKey: string }>(
|
|
91
|
+
opts.customUrl || `${tagApiBaseUrl}/${entityType}/${tagName}`,
|
|
92
|
+
{
|
|
93
|
+
requestKey: `tag/${entityType}/${tagName}/${opts.params?.group || ''}/list`,
|
|
94
|
+
params: opts.params,
|
|
95
|
+
isInterruptive: opts.isInterruptive,
|
|
96
|
+
feedback: opts.feedback,
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const getEntity = <T extends Entity>(
|
|
102
|
+
entityType: T,
|
|
103
|
+
id: string,
|
|
104
|
+
opts: CommonOptions = {},
|
|
105
|
+
) => {
|
|
106
|
+
const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
|
|
107
|
+
return axios.get<CreatedEntity<T>>(
|
|
108
|
+
opts.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
|
|
109
|
+
{
|
|
110
|
+
requestKey: `entity/${entityType}/get/${id}`,
|
|
111
|
+
isInterruptive: opts.isInterruptive,
|
|
112
|
+
feedback: {
|
|
113
|
+
loading: `Retrieving ${entityType}`,
|
|
114
|
+
...(opts.feedback || {}),
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const createEntity = <T extends Entity>(
|
|
121
|
+
entityType: T,
|
|
122
|
+
values: DraftEntity<T>,
|
|
123
|
+
opts: CommonOptions = {},
|
|
124
|
+
) => {
|
|
125
|
+
const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
|
|
126
|
+
const entityConfig = monoriseStore.getState().config;
|
|
127
|
+
return axios.post<CreatedEntity<T>>(
|
|
128
|
+
opts.customUrl || `${entityApiBaseUrl}/${entityType}`,
|
|
129
|
+
values,
|
|
130
|
+
{
|
|
131
|
+
requestKey: `entity/${entityType}/create`,
|
|
132
|
+
isInterruptive: opts.isInterruptive ?? true,
|
|
133
|
+
feedback: {
|
|
134
|
+
loading: `Creating ${entityConfig[entityType].displayName}`,
|
|
135
|
+
success: `${entityConfig[entityType].displayName} created`,
|
|
136
|
+
...(opts.feedback || {}),
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const upsertEntity = <T extends Entity>(
|
|
143
|
+
entityType: T,
|
|
144
|
+
id: string,
|
|
145
|
+
values: DraftEntity<T>,
|
|
146
|
+
opts: CommonOptions = {},
|
|
147
|
+
) => {
|
|
148
|
+
const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
|
|
149
|
+
const entityConfig = monoriseStore.getState().config;
|
|
150
|
+
return axios.put<CreatedEntity<T>>(
|
|
151
|
+
opts.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
|
|
152
|
+
values,
|
|
153
|
+
{
|
|
154
|
+
requestKey: `entity/${entityType}/upsert/${id}`,
|
|
155
|
+
isInterruptive: opts.isInterruptive ?? true,
|
|
156
|
+
feedback: {
|
|
157
|
+
loading: `Updating ${entityConfig[entityType].displayName}`,
|
|
158
|
+
success: `${entityConfig[entityType].displayName} updated`,
|
|
159
|
+
...(opts.feedback || {}),
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const editEntity = <T extends Entity>(
|
|
166
|
+
entityType: T,
|
|
167
|
+
id: string,
|
|
168
|
+
values: Partial<DraftEntity<T>>,
|
|
169
|
+
opts: CommonOptions = {},
|
|
170
|
+
) => {
|
|
171
|
+
const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
|
|
172
|
+
const entityConfig = monoriseStore.getState().config;
|
|
173
|
+
return axios.patch<CreatedEntity<T>>(
|
|
174
|
+
opts.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
|
|
175
|
+
values,
|
|
176
|
+
{
|
|
177
|
+
requestKey: `entity/${entityType}/edit/${id}`,
|
|
178
|
+
isInterruptive: opts.isInterruptive ?? true,
|
|
179
|
+
feedback: {
|
|
180
|
+
loading: `Updating ${entityConfig[entityType].displayName}`,
|
|
181
|
+
success: `${entityConfig[entityType].displayName} updated`,
|
|
182
|
+
...(opts.feedback || {}),
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const deleteEntity = <T extends Entity>(
|
|
189
|
+
entityType: T,
|
|
190
|
+
id: string,
|
|
191
|
+
opts: CommonOptions = {},
|
|
192
|
+
) => {
|
|
193
|
+
const { entityApiBaseUrl = ENTITY_API_BASE_URL } = options;
|
|
194
|
+
const entityConfig = monoriseStore.getState().config;
|
|
195
|
+
return axios.delete(
|
|
196
|
+
opts.customUrl || `${entityApiBaseUrl}/${entityType}/${id}`,
|
|
197
|
+
{
|
|
198
|
+
requestKey: `entity/${entityType}/delete/${id}`,
|
|
199
|
+
isInterruptive: opts.isInterruptive ?? true,
|
|
200
|
+
feedback: {
|
|
201
|
+
loading: `Deleting ${entityConfig[entityType].displayName}`,
|
|
202
|
+
success: `${entityConfig[entityType].displayName} deleted`,
|
|
203
|
+
...(opts.feedback || {}),
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const listEntitiesByEntity = <B extends Entity, T extends Entity>(
|
|
210
|
+
byEntityType: B,
|
|
211
|
+
entityType: T,
|
|
212
|
+
byEntityId: string,
|
|
213
|
+
opts: CommonOptions = {},
|
|
214
|
+
chainEntityQuery?: string,
|
|
215
|
+
) => {
|
|
216
|
+
const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
|
|
217
|
+
return axios.get<{ entities: Mutual<B, T>[]; lastKey: string }>(
|
|
218
|
+
opts.customUrl ||
|
|
219
|
+
`${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}${chainEntityQuery ? `?chainEntityQuery=${chainEntityQuery}` : ''}`,
|
|
220
|
+
{
|
|
221
|
+
requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/list${chainEntityQuery ? `?${chainEntityQuery}` : ''}`,
|
|
222
|
+
isInterruptive: opts.isInterruptive,
|
|
223
|
+
feedback: opts.feedback,
|
|
224
|
+
},
|
|
225
|
+
);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const getMutual = <B extends Entity, T extends Entity>(
|
|
229
|
+
byEntityType: B,
|
|
230
|
+
entityType: T,
|
|
231
|
+
byEntityId: string,
|
|
232
|
+
entityId: string,
|
|
233
|
+
opts: CommonOptions = {},
|
|
234
|
+
) => {
|
|
235
|
+
const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
|
|
236
|
+
return axios.get<Mutual<B, T>>(
|
|
237
|
+
opts.customUrl ||
|
|
238
|
+
`${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
239
|
+
{
|
|
240
|
+
requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/${entityId}/get`,
|
|
241
|
+
isInterruptive: opts.isInterruptive ?? false,
|
|
242
|
+
feedback: opts.feedback,
|
|
243
|
+
},
|
|
244
|
+
);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const createMutual = <B extends Entity, T extends Entity>(
|
|
248
|
+
byEntityType: B,
|
|
249
|
+
entityType: T,
|
|
250
|
+
byEntityId: string,
|
|
251
|
+
entityId: string,
|
|
252
|
+
payload: MutualData<B, T> | Record<string, unknown>,
|
|
253
|
+
opts: CommonOptions = {},
|
|
254
|
+
) => {
|
|
255
|
+
const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
|
|
256
|
+
return axios.post<Mutual<B, T>>(
|
|
257
|
+
opts.customUrl ||
|
|
258
|
+
`${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
259
|
+
payload,
|
|
260
|
+
{
|
|
261
|
+
requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/create`,
|
|
262
|
+
isInterruptive: opts.isInterruptive ?? true,
|
|
263
|
+
feedback: {
|
|
264
|
+
loading: 'Creating linkage',
|
|
265
|
+
success: 'Linkage created',
|
|
266
|
+
...(opts.feedback || {}),
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const editMutual = <B extends Entity, T extends Entity>(
|
|
273
|
+
byEntityType: B,
|
|
274
|
+
entityType: T,
|
|
275
|
+
byEntityId: string,
|
|
276
|
+
entityId: string,
|
|
277
|
+
payload: MutualData<B, T> | Record<string, unknown>,
|
|
278
|
+
opts: CommonOptions = {},
|
|
279
|
+
) => {
|
|
280
|
+
const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
|
|
281
|
+
return axios.put<Mutual<B, T>>(
|
|
282
|
+
opts.customUrl ||
|
|
283
|
+
`${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
284
|
+
payload,
|
|
285
|
+
{
|
|
286
|
+
requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/${entityId}/update`,
|
|
287
|
+
isInterruptive: opts.isInterruptive ?? true,
|
|
288
|
+
feedback: {
|
|
289
|
+
loading: 'Updating linkage',
|
|
290
|
+
success: 'Linkage updated',
|
|
291
|
+
...(opts.feedback || {}),
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const deleteMutual = <B extends Entity, T extends Entity>(
|
|
298
|
+
byEntityType: B,
|
|
299
|
+
entityType: T,
|
|
300
|
+
byEntityId: string,
|
|
301
|
+
entityId: string,
|
|
302
|
+
opts: CommonOptions = {},
|
|
303
|
+
) => {
|
|
304
|
+
const { mutualApiBaseUrl = MUTUAL_API_BASE_URL } = options;
|
|
305
|
+
return axios.delete(
|
|
306
|
+
opts.customUrl ||
|
|
307
|
+
`${mutualApiBaseUrl}/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
308
|
+
{
|
|
309
|
+
requestKey: `mutual/${byEntityType}/${byEntityId}/${entityType}/${entityId}/delete`,
|
|
310
|
+
isInterruptive: opts.isInterruptive ?? true,
|
|
311
|
+
feedback: {
|
|
312
|
+
loading: 'Removing linkage',
|
|
313
|
+
success: 'Linkage removed',
|
|
314
|
+
...(opts.feedback || {}),
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const makeEntityService = <T extends Entity>(entityType: T) => ({
|
|
321
|
+
listEntities: (payload: ListEntitiesPayload, opts: CommonOptions = {}) =>
|
|
322
|
+
listEntities(entityType, payload, opts),
|
|
323
|
+
searchEntities: (query: string, opts: CommonOptions = {}) =>
|
|
324
|
+
searchEntities(entityType, query, opts),
|
|
325
|
+
listEntitiesByTag: (tagName: string, opts: CommonOptions = {}) =>
|
|
326
|
+
listEntitiesByTag(entityType, tagName, opts),
|
|
327
|
+
getEntity: (id: string, opts: CommonOptions = {}) =>
|
|
328
|
+
getEntity(entityType, id, opts),
|
|
329
|
+
createEntity: (values: DraftEntity<T>, opts: CommonOptions = {}) =>
|
|
330
|
+
createEntity(entityType, values, opts),
|
|
331
|
+
upsertEntity: (
|
|
332
|
+
id: string,
|
|
333
|
+
values: DraftEntity<T>,
|
|
334
|
+
opts: CommonOptions = {},
|
|
335
|
+
) => upsertEntity(entityType, id, values, opts),
|
|
336
|
+
editEntity: (
|
|
337
|
+
id: string,
|
|
338
|
+
values: Partial<DraftEntity<T>>,
|
|
339
|
+
opts: CommonOptions = {},
|
|
340
|
+
) => editEntity(entityType, id, values, opts),
|
|
341
|
+
updateEntity: (
|
|
342
|
+
id: string,
|
|
343
|
+
values: DraftEntity<T>,
|
|
344
|
+
opts: CommonOptions = {},
|
|
345
|
+
) => editEntity(entityType, id, values, opts),
|
|
346
|
+
deleteEntity: (id: string, opts: CommonOptions = {}) =>
|
|
347
|
+
deleteEntity(entityType, id, opts),
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
const makeMutualService = <B extends Entity, T extends Entity>(
|
|
351
|
+
byEntityType: B,
|
|
352
|
+
entityType: T,
|
|
353
|
+
) => ({
|
|
354
|
+
listEntitiesByEntity: (
|
|
355
|
+
byEntityId: string,
|
|
356
|
+
opts: CommonOptions = {},
|
|
357
|
+
chainEntityQuery?: string,
|
|
358
|
+
) =>
|
|
359
|
+
listEntitiesByEntity(
|
|
360
|
+
byEntityType,
|
|
361
|
+
entityType,
|
|
362
|
+
byEntityId,
|
|
363
|
+
opts,
|
|
364
|
+
chainEntityQuery,
|
|
365
|
+
),
|
|
366
|
+
getMutual: (
|
|
367
|
+
byEntityId: string,
|
|
368
|
+
entityId: string,
|
|
369
|
+
opts: CommonOptions = {},
|
|
370
|
+
) => getMutual(byEntityType, entityType, byEntityId, entityId, opts),
|
|
371
|
+
createMutual: (
|
|
372
|
+
byEntityId: string,
|
|
373
|
+
entityId: string,
|
|
374
|
+
payload: MutualData<B, T> | Record<string, unknown>,
|
|
375
|
+
opts = {},
|
|
376
|
+
) =>
|
|
377
|
+
createMutual(
|
|
378
|
+
byEntityType,
|
|
379
|
+
entityType,
|
|
380
|
+
byEntityId,
|
|
381
|
+
entityId,
|
|
382
|
+
payload,
|
|
383
|
+
opts,
|
|
384
|
+
),
|
|
385
|
+
editMutual: (
|
|
386
|
+
byEntityId: string,
|
|
387
|
+
entityId: string,
|
|
388
|
+
payload: MutualData<B, T> | Record<string, unknown>,
|
|
389
|
+
opts = {},
|
|
390
|
+
) =>
|
|
391
|
+
editMutual(byEntityType, entityType, byEntityId, entityId, payload, opts),
|
|
392
|
+
deleteMutual: (
|
|
393
|
+
byEntityId: string,
|
|
394
|
+
entityId: string,
|
|
395
|
+
opts: CommonOptions = {},
|
|
396
|
+
) => deleteMutual(byEntityType, entityType, byEntityId, entityId, opts),
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
const setOptions = (opts: ConfigOptions) => {
|
|
400
|
+
options = {
|
|
401
|
+
...options,
|
|
402
|
+
...opts,
|
|
403
|
+
};
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
return {
|
|
407
|
+
makeEntityService,
|
|
408
|
+
makeMutualService,
|
|
409
|
+
setOptions,
|
|
410
|
+
};
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
export default initCoreService;
|
|
414
|
+
|
|
415
|
+
export type CoreService = ReturnType<typeof initCoreService>;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { AxiosInterceptor } from '../types/api.type';
|
|
2
|
+
|
|
3
|
+
type UploadFileProps = {
|
|
4
|
+
file: File;
|
|
5
|
+
scope: string;
|
|
6
|
+
directory: string;
|
|
7
|
+
name: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type Options = {
|
|
11
|
+
apiBaseUrl?: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const initFilestoreService = (axios: AxiosInterceptor) => {
|
|
15
|
+
let options: Options = {};
|
|
16
|
+
const { apiBaseUrl = '/api/file' } = options;
|
|
17
|
+
|
|
18
|
+
const uploadFile = async ({
|
|
19
|
+
file,
|
|
20
|
+
scope,
|
|
21
|
+
directory,
|
|
22
|
+
name,
|
|
23
|
+
}: UploadFileProps) => {
|
|
24
|
+
const { data } = await axios.get(`${apiBaseUrl}/upload`, {
|
|
25
|
+
requestKey: `filestore/presign/file/${name}`,
|
|
26
|
+
isInterruptive: true,
|
|
27
|
+
feedback: {
|
|
28
|
+
loading: 'Preparing upload',
|
|
29
|
+
},
|
|
30
|
+
params: {
|
|
31
|
+
scope,
|
|
32
|
+
directory,
|
|
33
|
+
filename: name,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return axios.put(data.url, file, {
|
|
38
|
+
requestKey: `filestore/upload/${name}`,
|
|
39
|
+
isInterruptive: true,
|
|
40
|
+
feedback: {
|
|
41
|
+
loading: 'Uploading file',
|
|
42
|
+
},
|
|
43
|
+
headers: {
|
|
44
|
+
'Content-Type': file.type,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const deleteFiles = async (keys: string[]) => {
|
|
50
|
+
const { data } = await axios.post(
|
|
51
|
+
'/file/delete',
|
|
52
|
+
{
|
|
53
|
+
keys,
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
requestKey: 'filestore/delete',
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
return data;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const uploadVideo = async ({
|
|
64
|
+
file,
|
|
65
|
+
scope,
|
|
66
|
+
directory,
|
|
67
|
+
name,
|
|
68
|
+
}: UploadFileProps) => {
|
|
69
|
+
const { data: presign } = await axios.get(`${apiBaseUrl}/upload`, {
|
|
70
|
+
requestKey: `filestore/presign/video/${name}`,
|
|
71
|
+
isInterruptive: true,
|
|
72
|
+
feedback: {
|
|
73
|
+
loading: 'Preparing upload',
|
|
74
|
+
},
|
|
75
|
+
params: {
|
|
76
|
+
scope,
|
|
77
|
+
directory,
|
|
78
|
+
filename: name,
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
await axios.put(presign.url, file, {
|
|
83
|
+
requestKey: `filestore/upload/video/${name}`,
|
|
84
|
+
isInterruptive: true,
|
|
85
|
+
feedback: {
|
|
86
|
+
loading: 'Uploading video',
|
|
87
|
+
},
|
|
88
|
+
headers: {
|
|
89
|
+
'Content-Type': file.type,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const { data } = await axios.post(
|
|
94
|
+
`${apiBaseUrl}/video/convert`,
|
|
95
|
+
{
|
|
96
|
+
scope,
|
|
97
|
+
directory,
|
|
98
|
+
filename: name,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
requestKey: 'filestore/convert/video',
|
|
102
|
+
isInterruptive: true,
|
|
103
|
+
feedback: {
|
|
104
|
+
loading: 'Triggering video conversion',
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
return data;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const getJobStatus = async (id: string) => {
|
|
113
|
+
const { data } = await axios.get(`${apiBaseUrl}/job/${id}`, {
|
|
114
|
+
requestKey: `filestore/get-job/${id}`,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
return data;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const setOptions = (opts: Options) => {
|
|
121
|
+
options = {
|
|
122
|
+
...options,
|
|
123
|
+
...opts,
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
uploadFile,
|
|
129
|
+
deleteFiles,
|
|
130
|
+
uploadVideo,
|
|
131
|
+
getJobStatus,
|
|
132
|
+
setOptions,
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export default initFilestoreService;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CreatedEntity,
|
|
3
|
+
Entity,
|
|
4
|
+
MonoriseEntityConfig,
|
|
5
|
+
} from '@monorise/base';
|
|
6
|
+
import { enableMapSet } from 'immer';
|
|
7
|
+
import type React from 'react';
|
|
8
|
+
import { create } from 'zustand';
|
|
9
|
+
import { devtools } from 'zustand/middleware';
|
|
10
|
+
import type { ApplicationRequestError, ModalProps } from '../types/api.type';
|
|
11
|
+
import type { CommonStore } from '../types/monorise.type';
|
|
12
|
+
import type { Mutual } from '../types/mutual.type';
|
|
13
|
+
|
|
14
|
+
enableMapSet();
|
|
15
|
+
|
|
16
|
+
const configureDevtools = (storeName: string) => ({
|
|
17
|
+
name: `${storeName} Store`,
|
|
18
|
+
anonymousActionType: `${storeName.toLowerCase()}/action`,
|
|
19
|
+
enabled: !['prod', 'dev'].includes(process.env.NEXT_PUBLIC_ENVIRONMENT ?? ''),
|
|
20
|
+
serialize: {
|
|
21
|
+
options: {
|
|
22
|
+
map: true,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
type Options = {
|
|
28
|
+
modals?: Record<string, React.ComponentType<unknown>>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const initMonoriseStore = () => {
|
|
32
|
+
let options: Options = {};
|
|
33
|
+
const { modals = {} } = options;
|
|
34
|
+
|
|
35
|
+
type AppModalProps = ModalProps<typeof modals>;
|
|
36
|
+
|
|
37
|
+
const monoriseStore = create(
|
|
38
|
+
devtools(
|
|
39
|
+
(): {
|
|
40
|
+
app: {
|
|
41
|
+
ongoingRequests: Map<string, Promise<unknown>>;
|
|
42
|
+
loadStack: Map<string, number>;
|
|
43
|
+
intLoadStack: Map<string, number>;
|
|
44
|
+
loadingMessage: string;
|
|
45
|
+
errorStack: Map<string, ApplicationRequestError>;
|
|
46
|
+
modal: {
|
|
47
|
+
name: keyof AppModalProps | null;
|
|
48
|
+
context?: AppModalProps[keyof AppModalProps];
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
config: Record<Entity, MonoriseEntityConfig>;
|
|
52
|
+
entity: Record<Entity, CommonStore<CreatedEntity<Entity>>>;
|
|
53
|
+
mutual: Record<string, CommonStore<Mutual>>;
|
|
54
|
+
tag: Record<string, CommonStore<CreatedEntity<Entity>>>;
|
|
55
|
+
auth: {
|
|
56
|
+
isUnauthorized: boolean;
|
|
57
|
+
profile: {
|
|
58
|
+
displayName: string;
|
|
59
|
+
email: string;
|
|
60
|
+
accountId: string;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
} => ({
|
|
64
|
+
app: {
|
|
65
|
+
ongoingRequests: new Map(),
|
|
66
|
+
loadStack: new Map(),
|
|
67
|
+
intLoadStack: new Map(),
|
|
68
|
+
loadingMessage: '',
|
|
69
|
+
errorStack: new Map(),
|
|
70
|
+
modal: {
|
|
71
|
+
name: null,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
config: {},
|
|
75
|
+
entity: {},
|
|
76
|
+
mutual: {},
|
|
77
|
+
tag: {},
|
|
78
|
+
auth: {
|
|
79
|
+
isUnauthorized: false,
|
|
80
|
+
profile: {
|
|
81
|
+
displayName: '',
|
|
82
|
+
email: '',
|
|
83
|
+
accountId: '',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
}),
|
|
87
|
+
configureDevtools('monorise'),
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const setOptions = (opts: Options) => {
|
|
92
|
+
options = {
|
|
93
|
+
...options,
|
|
94
|
+
...opts,
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return { setOptions, monoriseStore };
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export { initMonoriseStore };
|
|
102
|
+
|
|
103
|
+
export type MonoriseStore = Awaited<
|
|
104
|
+
ReturnType<typeof initMonoriseStore>
|
|
105
|
+
>['monoriseStore'];
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import request, { type AxiosRequestConfig, type AxiosResponse } from 'axios';
|
|
2
|
+
|
|
3
|
+
export type ApplicationRequestError = {
|
|
4
|
+
code: string;
|
|
5
|
+
message: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type ExtractModalProps<T> = T extends React.ComponentType<infer P> ? P : void;
|
|
9
|
+
|
|
10
|
+
// Dynamically generate ModalProps
|
|
11
|
+
export type ModalProps<M> = {
|
|
12
|
+
[K in keyof M]: ExtractModalProps<M[K]>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// this axios instance is created for the sole purpose of typing
|
|
16
|
+
// why not using typeof initAxiosInterceptor?
|
|
17
|
+
// To break circular dependency
|
|
18
|
+
|
|
19
|
+
const axiosInstance = request.create();
|
|
20
|
+
|
|
21
|
+
function makeRequest<T = any, R = AxiosResponse<T, any>, D = any>(
|
|
22
|
+
url: string,
|
|
23
|
+
config: AxiosRequestConfig<D>,
|
|
24
|
+
data?: D,
|
|
25
|
+
): Promise<R> {
|
|
26
|
+
const promise = axiosInstance.request<T, R, D>({ ...config, url, data });
|
|
27
|
+
return promise;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const axios = {
|
|
31
|
+
...axiosInstance,
|
|
32
|
+
post: <T = any, R = AxiosResponse<T, any>, D = any>(
|
|
33
|
+
url: string,
|
|
34
|
+
data: D,
|
|
35
|
+
config: AxiosRequestConfig<D>,
|
|
36
|
+
): Promise<R> => makeRequest(url, { ...config, method: 'POST' }, data),
|
|
37
|
+
put: <T = any, R = AxiosResponse<T, any>, D = any>(
|
|
38
|
+
url: string,
|
|
39
|
+
data: D,
|
|
40
|
+
config: AxiosRequestConfig<D>,
|
|
41
|
+
): Promise<R> => makeRequest(url, { ...config, method: 'PUT' }, data),
|
|
42
|
+
patch: <T = any, R = AxiosResponse<T, any>, D = any>(
|
|
43
|
+
url: string,
|
|
44
|
+
data: D,
|
|
45
|
+
config: AxiosRequestConfig<D>,
|
|
46
|
+
): Promise<R> => makeRequest(url, { ...config, method: 'PATCH' }, data),
|
|
47
|
+
delete: <T = any, R = AxiosResponse<T, any>, D = any>(
|
|
48
|
+
url: string,
|
|
49
|
+
config: AxiosRequestConfig<D>,
|
|
50
|
+
): Promise<R> => makeRequest(url, { ...config, method: 'DELETE' }),
|
|
51
|
+
get: <T = any, R = AxiosResponse<T, any>, D = any>(
|
|
52
|
+
url: string,
|
|
53
|
+
config: AxiosRequestConfig<D>,
|
|
54
|
+
): Promise<R> => makeRequest(url, { ...config, method: 'GET' }),
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type AxiosInterceptor = typeof axios;
|