UNPKG

@creamapi/cream

Version:

Concise REST API Maker - An extension library for express to create REST APIs faster

166 lines (165 loc) 6.14 kB
import { SerializerMetaInfo } from './SerializerMetaInfo'; /** * @internal * Defines the base types that should be serialized */ type BaseSerializable = {} | string | number | boolean; /** * @internal * This type defines the unit of data that * should be serialized */ export type SerialBite = { /** * the data that should be serialized */ data: BaseSerializable; /** * The label that should be used when serializing * aka the output name */ dataLabel: string; /** * Additional meta information that can be * used by serializers when serializing objects */ metaInfo: SerializerMetaInfo | undefined; }; /** * This namespace defines Common information * for serializers */ export declare namespace SerializerCommon { /** * This namespace defines the common attributes to all serializers */ namespace Attributes { /** * This attribute is used to declare an object as an array */ const Array: string; /** * This attribute is used to automatically serialize everything in the object */ const AutoSerialize: string; } } /** * This base class that implements the complex logic for handling * serialization of objects. It gives a framework to easily implement * complex serializers. */ export declare abstract class Serializer { private targetName; private contextStack; constructor(targetName: string, baseContext: Object); abstract handleSerializationStream(serializedObjectName: string, serialStream: SerialBite[], objectMetaInfo: SerializerMetaInfo): Promise<String>; abstract serializeNumber(dataLabel: string, num: number): Promise<string>; abstract serializeString(dataLabel: string, data: string): Promise<string>; abstract serializeBoolean(dataLabel: string, data: boolean): Promise<string>; abstract serializeNull(dataLabel: string): Promise<string>; /** * This method is used to handle undefined data. * By default undefined is returned as an empty string * @param dataLabel (unused) the label of the data. * @returns empty string */ serializeUndefined(_dataLabel: string): Promise<string>; /** * This method is used as the default behaviour for serializing Dates * it uses the Date.toISOString method for serializing it. * @param dataLabel (unused) the label of the data * @param data the date to be serialized * @returns the serialized string in ISO format */ serializeDate(_dataLabel: string, data: Date): Promise<string>; /** * This method is used to serialize a piece of data * @param dataLabel the label of the data * @param data the actual data * @returns a string representing the serialized object */ serialize(dataLabel: string, data: any): Promise<String>; /** * This method is used to serialize anything that is not * a base type * @param dataLabel the data label that should be used when rendering * @param data the data that should be serialized * @returns the string representing the serialized object */ private serializeAnyObject; /** * Gets the current context * @returns the current context */ private getContext; /** * removes the current context from the stack */ private popContext; /** * This is used to push a context to the stack. * This context is used to infer data ownership * @param context the context that should be pushed */ private pushContext; /** * This static method makes any object serializable by iterating through the object * this can break when recursive references are taken in place * @param data the data to be serialized * @returns the decorated data */ static makeSerializable(data: any): any; /** * This method is called before handling a custom object * @param dataLabel the label of the object * @param data the object * @param metaInfo any information useful for serialization * @returns a string that should be appended before the serialization of the object */ preObject(dataLabel: string, data: SerialBite[], metaInfo: SerializerMetaInfo | undefined): Promise<string>; /** * This method is called after handling a custom object * @param dataLabel the label of the object * @param data the object * @param metaInfo any information useful for serialization * @returns a string that should be appended after the serialization of the object */ postObject(dataLabel: string, data: SerialBite[], metaInfo: SerializerMetaInfo | undefined): Promise<string>; /** * This method is used to create a serialization of * the array in data by putting the index as the * field name * @param data the array that should be mapped */ private autoMapArray; /** * This method is used to streamify the data from the object given as input * @param data the data that should be streamified * @returns the stream of SerialBites that will be later handled by the serializer */ private streamify; /** * This method will return meta information for the current context based on the * dataLabel * @param dataLabel the name of the field that the metadata should be retrieved from * @returns the metadata associated with the dataLabel */ private fetchMetaInfoForObject; } /** * @internal * This class is only used to bootstrap serialization * It will also handle base types when no complex object is returned * The serialization of those types is language agnostic */ export declare class BootstrapSerializer extends Serializer { serializeNull(_dataLabel: string): Promise<string>; constructor(); start(data: any): Promise<String>; serializeNumber(_: any, num: number): Promise<string>; serializeString(_: any, data: string): Promise<string>; serializeBoolean(_: any, bool: boolean): Promise<string>; handleSerializationStream(_: any): Promise<String>; } export {};