diff options
author | bobzel <zzzman@gmail.com> | 2024-08-08 12:27:40 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2024-08-08 12:27:40 -0400 |
commit | 4574b7f03ccc85c4bebdbfd9475788456086704f (patch) | |
tree | d23d30343541b9af029ef418492d629d3cc710d7 /src/client/util/SerializationHelper.ts | |
parent | e1db06d59d580aa640212a0d3a6aeecb9122bdf0 (diff) |
many changes to add typing in place of 'any's etc
Diffstat (limited to 'src/client/util/SerializationHelper.ts')
-rw-r--r-- | src/client/util/SerializationHelper.ts | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/client/util/SerializationHelper.ts b/src/client/util/SerializationHelper.ts index d9d22437c..0386b2455 100644 --- a/src/client/util/SerializationHelper.ts +++ b/src/client/util/SerializationHelper.ts @@ -1,22 +1,24 @@ import { PropSchema, serialize, deserialize, custom, setDefaultModelSchema, getDefaultModelSchema } from 'serializr'; +import Context from 'serializr/lib/core/Context'; // import { Field } from '../../fields/Doc'; let serializing = 0; -export function afterDocDeserialize(cb: (err: any, val: any) => void, err: any, newValue: any) { +export function afterDocDeserialize(cb: (err: unknown, val: unknown) => void, err: unknown, newValue: unknown) { serializing++; cb(err, newValue); serializing--; } -const serializationTypes: { [name: string]: { ctor: { new (): any }; afterDeserialize?: (obj: any) => void | Promise<any> } } = {}; +const serializationTypes: { [name: string]: { ctor: { new (): unknown }; afterDeserialize?: (obj: unknown) => void | Promise<unknown> } } = {}; const reverseMap: { [ctor: string]: string } = {}; +// eslint-disable-next-line @typescript-eslint/no-namespace export namespace SerializationHelper { export function IsSerializing() { return serializing > 0; } - export function Serialize(obj: any /* Field */): any { + export function Serialize(obj: unknown /* Field */): unknown { if (obj === undefined || obj === null) { return null; } @@ -37,7 +39,7 @@ export namespace SerializationHelper { return json; } - export async function Deserialize(obj: any): Promise<any> { + export async function Deserialize(obj: unknown): Promise<unknown> { if (obj === undefined || obj === null) { return undefined; } @@ -46,16 +48,17 @@ export namespace SerializationHelper { return obj; } - if (!obj.__type) { - console.warn("No property 'type' found in JSON."); + const objtype = '__type' in obj ? (obj.__type as string) : undefined; + if (!objtype) { + console.warn(`No property ${objtype} found in JSON.`); return undefined; } - if (!(obj.__type in serializationTypes)) { - throw Error(`type '${obj.__type}' not registered. Make sure you register it using a @Deserializable decorator`); + if (!(objtype in serializationTypes)) { + throw Error(`type '${objtype}' not registered. Make sure you register it using a @Deserializable decorator`); } - const type = serializationTypes[obj.__type]; + const type = serializationTypes[objtype]; const value = await new Promise(res => { deserialize(type.ctor, obj, (err, result) => res(result)); }); @@ -65,11 +68,12 @@ export namespace SerializationHelper { } } -export function Deserializable(classNameForSerializer: string, afterDeserialize?: (obj: any) => void | Promise<any>, constructorArgs?: [string]): (constructor: { new (...args: any[]): any }) => void { - function addToMap(className: string, Ctor: { new (...args: any[]): any }) { - const schema = getDefaultModelSchema(Ctor) as any; - if (schema.targetClass !== Ctor || constructorArgs) { - setDefaultModelSchema(Ctor, { ...schema, factory: (context: any) => new Ctor(...(constructorArgs ?? []).map(arg => context.json[arg])) }); +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function Deserializable(classNameForSerializer: string, afterDeserialize?: (obj: unknown) => void | Promise<unknown>, constructorArgs?: [string]): (constructor: { new (...args: any[]): any }) => void { + function addToMap(className: string, Ctor: { new (...args: unknown[]): unknown }) { + const schema = getDefaultModelSchema(Ctor); + if (schema && (schema.targetClass !== Ctor || constructorArgs)) { + setDefaultModelSchema(Ctor, { ...schema, factory: (context: Context) => new Ctor(...(constructorArgs ?? []).map(arg => context.json[arg])) }); } if (!(className in serializationTypes)) { serializationTypes[className] = { ctor: Ctor, afterDeserialize }; @@ -78,12 +82,12 @@ export function Deserializable(classNameForSerializer: string, afterDeserialize? throw new Error(`Name ${className} has already been registered as deserializable`); } } - return (ctor: { new (...args: any[]): any }) => addToMap(classNameForSerializer, ctor); + return (ctor: { new (...args: unknown[]): unknown }) => addToMap(classNameForSerializer, ctor); } export function autoObject(): PropSchema { return custom( s => SerializationHelper.Serialize(s), - (json: any, context: any, oldValue: any, cb: (err: any, result: any) => void) => SerializationHelper.Deserialize(json).then(res => cb(null, res)) + (json: object, context: Context, oldValue: unknown, cb: (err: unknown, result: unknown) => void) => SerializationHelper.Deserialize(json).then(res => cb(null, res)) ); } |