@izara_frontend/service-schemas 1.0.3 → 1.0.5

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/index.js CHANGED
@@ -6,6 +6,7 @@ export {
6
6
  getRequiredOnCreateLinks,
7
7
  getRelationshipSchema,
8
8
  getObjSchemaWithOutHierarchy,
9
+ collectObjectSchemaWithHierarchy,
9
10
  getObjSchemaWithHierarchy,
10
11
  getObjSchemaCombineFieldNames,
11
12
  getLinkConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@izara_frontend/service-schemas",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -198,6 +198,108 @@ export function getObjectSchemaWithAllHierarchy() {
198
198
  return [result, run, { errorsFound }];
199
199
  }
200
200
 
201
+ export function collectObjectSchemaWithHierarchy() {
202
+ const [, getObjectSchemaWithAllHierarchyFn] = getObjectSchemaWithAllHierarchy();
203
+
204
+ const [result, setResult] = useState(null);
205
+ const [errorsFound, setErrorsFound] = useState([]);
206
+
207
+
208
+
209
+ // ref to always read latest result inside async functions (avoids stale closures)
210
+ const resultRef = useRef(result);
211
+ useEffect(() => { resultRef.current = result; }, [result]);
212
+
213
+ // core executor that does the actual work and optionally updates hook state
214
+ const execute = useCallback(async (objType, { updateState = true } = {}) => {
215
+
216
+ async function collectObjectSchemaWithHierarchyMain(objType) {
217
+
218
+ // validate objType (some validators throw, some return errors)
219
+ try {
220
+ validateObjType(objType);
221
+ } catch (err) {
222
+ return [null, [err.message]];
223
+ }
224
+
225
+ // compute mainObjTypeConcat and validate
226
+ const [mainObjTypeConcat, mainObjTypeErrors] = createObjTypeConcat(objType);
227
+ if (mainObjTypeErrors && mainObjTypeErrors.length) {
228
+ return [null, mainObjTypeErrors];
229
+ }
230
+
231
+ // short-circuit: if updateState and we already have this objType in result, return cached value
232
+ if (updateState && resultRef.current && resultRef.current.hasOwnProperty(mainObjTypeConcat)) {
233
+ return [{ [mainObjTypeConcat]: resultRef.current[mainObjTypeConcat] }, []];
234
+ }
235
+
236
+
237
+
238
+ const [objSchema, getObjSchemaErrors] = await getObjectSchemaWithAllHierarchyFn().initiate(objType);
239
+
240
+
241
+ return [objSchema, getObjSchemaErrors];
242
+ }
243
+
244
+ try {
245
+
246
+
247
+ const [schema, errorsFound] = await collectObjectSchemaWithHierarchyMain(objType);
248
+
249
+ if (updateState) {
250
+ if (errorsFound && errorsFound.length) {
251
+ setErrorsFound(errorsFound);
252
+ setResult(null);
253
+ } else {
254
+ const [currentObjTypeConcat] = createObjTypeConcat(objType);
255
+
256
+ setResult(prev => {
257
+ // nothing to add
258
+ if (!schema || !Object.keys(schema).length) return prev;
259
+
260
+ // first-time fill
261
+ if (!prev) return schema;
262
+
263
+ // already have this objType => no update
264
+ if (prev.hasOwnProperty(currentObjTypeConcat)) return prev;
265
+
266
+ // merge but return a NEW object to trigger React update (do not mutate prev)
267
+ return { ...prev, ...schema };
268
+ });
269
+
270
+ setErrorsFound([]);
271
+ }
272
+ }
273
+
274
+ return [schema, errorsFound];
275
+ } catch (err) {
276
+ const msg = err?.message || String(err);
277
+ if (updateState) {
278
+ setErrorsFound([msg]);
279
+ setResult(null);
280
+ }
281
+ return [null, [msg]];
282
+ }
283
+ }, [getObjectSchemaWithAllHierarchyFn]);
284
+
285
+
286
+ // run can be used two ways:
287
+ // 1) run(objType) -> updates hook state and returns Promise<[collected, errors]>
288
+ // 2) run().initiate(objType) -> does NOT touch hook state, returns Promise<[collected, errors]>
289
+ const run = useCallback((objType) => {
290
+ if (typeof objType === "undefined") {
291
+ return {
292
+ initiate: (objType) => execute(objType, { updateState: false }),
293
+ clear: () => { setResult(null); setErrorsFound([]); }
294
+ };
295
+ }
296
+ // direct call: update state
297
+ return execute(objType, { updateState: true });
298
+ }, [execute]);
299
+
300
+ return [result, run, { errorsFound }];
301
+ }
302
+
201
303
 
202
304
  export function getObjectLinks() {
203
305
  const {
@@ -231,7 +333,7 @@ export function getObjectLinks() {
231
333
 
232
334
  let mainErrorsFound = [];
233
335
 
234
- const mainObjTypeConcat = createObjTypeConcat(objType);
336
+ const [mainObjTypeConcat] = createObjTypeConcat(objType);
235
337
 
236
338
  const [objectSchemaHierarChy, getSchemaErrors] = await getObjectSchemaWithAllHierarchyFn().initiate(objType);
237
339
 
@@ -1096,7 +1198,7 @@ export function getObjectLinksWithRequestProperties() {
1096
1198
  let returnLinks = [];
1097
1199
  let mainErrorsFound = [];
1098
1200
 
1099
- const mainObjTypeConcat = createObjTypeConcat(objType);
1201
+ const [mainObjTypeConcat] = createObjTypeConcat(objType);
1100
1202
 
1101
1203
  const [objectLinks, getSchemaErrors] = await getObjectLinksFn().initiate(objType);
1102
1204