shelving
Version:
Toolkit for using data in JavaScript.
32 lines (31 loc) • 1.68 kB
TypeScript
import type { Class } from "./class.js";
import type { ImmutableDictionary } from "./dictionary.js";
/**
* A set of hydrations describes a set of string keys and the class constructor to be dehydrated and rehydrated.
* - We can't use `class.name` because we don't know that the name of the class will survive minification.
*/
export type Hydrations = ImmutableDictionary<Class<unknown>>;
/** A dehydrated object with a `$type` key. */
export type DehydratedObject = {
readonly $type: string;
readonly $value: unknown;
};
/**
* Deeply hydrate a class instance based on a set of `Hydrations`
* - Hydration allows a client to receive class instances from a server.
* - By its nature hydration is an unsafe operation.
* - Deeply iterates into arrays and plain objects to hydrate their items and props too.
* - Note: the recursion in this function does not currently protect against infinite loops.
*/
export declare function hydrate(value: unknown, hydrations: Hydrations): unknown;
/**
* Deeply dehydrate a class instance based on a set of `Hydrations`
* - Dehydration allows you to pass class instances from a server back to a client.
* - By its nature dehydration is an unsafe operation.
* - Deeply iterates into arrays and plain objects to dehydrate their items and props too.
* - Note: the recursion in this function does not currently protect against infinite loops.
*
* @returns The dehydrated version of the specified value.
* @throws `Error` if the value is a class instance that cannot be dehydrated (i.e. is not matched by any constructor in `hydrations`).
*/
export declare function dehydrate(value: unknown, hydrations: Hydrations): unknown;