Skip to content
Snippets Groups Projects
Commit 1c9957c9 authored by michael.minelli's avatar michael.minelli
Browse files

DojoModelsHelper => Add a function to serialize objects with LazyVal

parent 209de769
No related branches found
No related tags found
No related merge requests found
import LazyVal from '../shared/helpers/LazyVal';
class DojoModelsHelper {
/**
* Get a full serializable object from a given object that contains LazyVal instances
*
* @param obj
* @param depth The depth of the search for LazyVal instances
*/
async getFullSerializableObject<T extends NonNullable<unknown>>(obj: T, depth: number = 0): Promise<unknown> {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const result: any = {};
for ( const key in obj ) {
let value: unknown = obj[key];
if ( value instanceof LazyVal ) {
value = await (obj[key] as LazyVal<unknown>).value;
}
if ( typeof value === 'object' && obj[key] !== null && depth > 0 ) {
result[key] = await this.getFullSerializableObject(value as NonNullable<unknown>, depth - 1);
} else {
result[key] = value;
}
}
return result;
}
}
export default new DojoModelsHelper();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment