@izara_frontend/service-schemas 1.0.4 → 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 +1 -0
- package/package.json +1 -1
- package/src/getObjectSchema.js +102 -0
package/index.js
CHANGED
package/package.json
CHANGED
package/src/getObjectSchema.js
CHANGED
|
@@ -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 {
|