UNPKG

1.73 kBTypeScriptView Raw
1import type { Constructable, Entity } from './entity';
2/**
3 * Type to describe possible inputs for `.fromJson`.
4 * This is based on the JSON type of an entity and allows all properties to be optional recursively.
5 * It also allows setting unknown properties, which will be treated as custom fields.
6 * @typeparam JsonT - JSON type of the entity
7 */
8declare type FromJsonType<JsonT> = {
9 [key: string]: any;
10} & {
11 [P in keyof JsonT]?: JsonT[P] extends (infer U)[] ? U extends Record<string, any> ? FromJsonType<U>[] : JsonT[P] : JsonT[P] extends Record<string, any> | null | undefined ? FromJsonType<JsonT[P]> | null | undefined : JsonT[P];
12};
13/**
14 * @hidden
15 */
16export declare class EntityBuilder<EntityT extends Entity, JsonT> {
17 private _entityConstructor;
18 protected entity: EntityT;
19 constructor(_entityConstructor: Constructable<EntityT, JsonT>);
20 /**
21 * Sets the custom fields for the entity.
22 * @param customFields - The custom fields you want to add.
23 * @returns The entity builder itself for method chaining
24 */
25 withCustomFields(customFields: Record<string, any>): this;
26 /**
27 * Builds the entity.
28 * @returns The entity.
29 */
30 build(): EntityT;
31 /**
32 * Builds an entity from JSON representation.
33 * If you have obtained the JSON as a request payload use the [[deserializeEntity]] methods.
34 * Note that fields not mappable to a field in the target entity are silently ignored.
35 * @param json - Representation of the entity in JSON format.
36 * @returns Entity constructed from JSON representation.
37 */
38 fromJson(json: FromJsonType<JsonT>): EntityT;
39 private filterCustomFields;
40}
41export {};
42//# sourceMappingURL=entity-builder.d.ts.map
\No newline at end of file