UNPKG

ts-japi

Version:

A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification

83 lines 3.03 kB
import type { DataDocument } from "../interfaces/json-api.interface"; import type { SerializerOptions } from "../interfaces/serializer.interface"; import Resource from "../models/resource.model"; import ResourceIdentifier from "../models/resource-identifier.model"; import type { Dictionary, nullish, SingleOrArray } from "../types/global.types"; import { Helpers } from "../utils/serializer.utils"; import Cache from "./cache"; import type Relator from "./relator"; /** * The {@link Serializer} class is the main class used to serializer data * (you can use the {@link ErrorSerializer} class to serialize errors). * * Example: * ```typescript * [[include:serializer.example.ts]] * ``` */ export default class Serializer<PrimaryType extends Dictionary<any> = any> { /** * Default options. Can be edited to change default options globally. */ static defaultOptions: { idKey: string; version: string; onlyIdentifier: boolean; nullData: boolean; asIncluded: boolean; onlyRelationship: boolean; cache: boolean; depth: number; include: number; projection: null; linkers: {}; metaizers: {}; }; /** * The name to use for the type. */ collectionName: string; /** * The set of options for the serializer. */ private options; /** * The set of helper functions for the serializer */ helpers: Helpers<PrimaryType>; /** * Caching */ cache: Cache<PrimaryType>; /** * Creates a {@link Serializer}. * * @param collectionName - The name of the collection of objects. * @param options - Options for the serializer. */ constructor(collectionName: string, options?: Partial<SerializerOptions<PrimaryType>>); /** * Gets the idKey (ie. name of the id field for the given model) */ getIdKeyFieldName(): keyof PrimaryType; /** * Gets the {@link Relator}s associated with this serializer */ getRelators(): Record<string, Relator<PrimaryType, any>> | undefined; /** * Sets the {@link Relator}s associated with this serializer */ setRelators(relators: SerializerOptions<PrimaryType>["relators"]): void; /** @internal Generates a `ResourceIdentifier`. */ createIdentifier(data: PrimaryType, options?: SerializerOptions<PrimaryType>): ResourceIdentifier; /** @internal Generates a `Resource`. */ createResource(data: PrimaryType, options?: Partial<SerializerOptions<PrimaryType>>, helpers?: Helpers<PrimaryType>, relatorDataCache?: Map<Relator<any>, Dictionary<any>[]>): Promise<Resource<PrimaryType>>; /** * The actual serialization function. * * @param data - Data to serialize. * @param options - Options to use at runtime. */ serialize(data: SingleOrArray<PrimaryType> | nullish, options?: Partial<SerializerOptions<PrimaryType>>): Promise<Partial<DataDocument<PrimaryType>>>; } //# sourceMappingURL=serializer.d.ts.map