UNPKG

@nasriya/atomix

Version:

Composable helper functions for building reliable systems

78 lines (77 loc) 2.18 kB
declare class BodyCodec { #private; /** * Encodes the given value into a Buffer, which can be sent over the wire. * * The value is first serialized into a JSON-serializable payload, and then * that payload is stringified into a JSON string. The resulting string is * then encoded into a Buffer using the UTF8 encoding. * * @param value - The value to encode. * @returns The encoded Buffer. * @throws TypeError if the value cannot be serialized. * @since v1.0.2 */ encode<T extends Serializable>(value: T): Buffer<ArrayBuffer>; /** * Decodes the given Buffer into its original value. The buffer is expected to * contain a stringified JSON representation of an encoded payload. * * @param buffer - The buffer containing the encoded data. * @returns The original value, deserialized from the encoded payload. * @throws SyntaxError if the buffer content is not valid JSON. * @since v1.0.2 */ decode(buffer: Buffer): string | number | boolean | any[] | Record<string, any> | Set<any> | Map<any, any> | Date | null | undefined; } export interface Serialization { string: { type: string; label: 'string'; }; number: { type: number; label: 'number'; }; boolean: { type: boolean; label: 'boolean'; }; null: { type: null; label: 'null'; }; undefined: { type: undefined; label: 'undefined'; }; record: { type: Record<string, any>; label: 'record'; }; array: { type: Array<any>; label: 'array'; }; set: { type: Set<any>; label: 'set'; }; map: { type: Map<any, any>; label: 'map'; }; date: { type: Date; label: 'date'; }; } export type Serializable = Serialization[keyof Serialization]['type']; export type EncodedPayload = { [K in keyof Serialization]: { type: Serialization[K]['label']; value: Serialization[K]['type']; }; }[keyof Serialization]; declare const bodyCodec: BodyCodec; export default bodyCodec;