type DescriptorHolder = { $MODEL_TYPE: 'descriptor'; descriptor: PropertyDescriptor; __type__: T; }; declare class IdentifiersMap { private readonly valuesByTypes; constructor(); get(type: Type, id: Id): NonNullable | null; set(type: Type, id: Id, value: T): void; delete(type: Type, id: Id): void; clear(type: Type): void; } type Dictionary = { [K: string]: T; }; type Constructor = new (...args: any[]) => T; type Awaitable = T | Promise; type Optional = T | null | undefined; type HookCallback = (event: E) => Awaitable; type HooksDefinition = Dictionary>; type HooksRegistrar = { [K in keyof D]?: D[K][]; }; type Hookable = { $hooks: HooksRegistrar | null; }; type Normalizer = (value: T) => U; type FunctionTransform = (value: S) => Awaitable; type ObjectTransform = { serialize(value: T): Awaitable; deserialize(value: S): Awaitable; }; type Transform = FunctionTransform | ObjectTransform; /** * Configuration of a model class. */ type ModelConfig = { /** * Path which is used to query the model. * Defaults to the model's type. */ path?: Optional; /** * Normalize a model/relation path when injecting it inside an action context. */ normalizePath?: Optional>; /** * Normalize a model path when injecting it inside an action context. * Prioritized over `normalizePath`. */ normalizeModelPath?: Optional>; /** * Normalize a relation path when injecting it inside an action context. * Prioritized over `normalizePath`. */ normalizeRelationPath?: Optional>; /** * Normalize an attribute/relation key when (de)serializing for data source. */ normalizeKey?: Optional>; /** * Normalize an attribute key when (de)serializing for data source. * Prioritized over `normalizeKey`. */ normalizeAttributeKey?: Optional>; /** * Normalize a relation key when (de)serializing for data source. * Prioritized over `normalizeKey`. */ normalizeRelationKey?: Optional>; /** * Guess the type for a given relation. */ guessRelationType?: Optional; /** * Compare two values when checking model instance changed values. * * @param nextValue * @param prevValue * * @see {@link changed} */ compareValue?: Optional<((nextValue: unknown, prevValue: unknown) => boolean)>; /** * Clone two values when sync model instances values state. * * @param value * * @see {@link restore} * @see {@link markSynced} */ cloneValue?: Optional<((value: T) => T)>; /** * Dedicated base URL. Will overwrite the default base URL. */ baseURL?: Optional; }; /** * Guess a model's relation related type. */ type ModelRelationTypeGuesser = (model: ModelClass, def: ModelRelation) => Optional; /** * Unique identifier for a model instance. */ type ModelId = string | number; /** * Sync precise configuration for a property (will only do defined action). */ type ModelPropSync = 'retrieve' | 'write'; /** * Normalized part of a property's definition (attribute or relation). */ type ModelPropNormalized = { key: K; }; /** * Raw definition for a model's property (attribute or relation). */ type ModelPropRaw = { /** * Default value for the property. */ default?: T | (() => T) | undefined; /** * Alias of the property (might be used when (de)serializing). */ alias?: string | undefined; /** * Makes the property read-only. */ readOnly?: boolean; /** * Tells if the property should be synced with the data store. */ sync?: boolean | ModelPropSync; }; /** * Raw definition for a model's attribute. */ type ModelAttributeRaw = ModelPropRaw & { /** * Internal type identifier for Foscia's type guards. */ $MODEL_TYPE: 'attribute'; transformer?: Transform | undefined; }; /** * Definition for a model's attribute. */ type ModelAttribute = ModelPropNormalized & ModelAttributeRaw; /** * Available model relation types. */ type ModelRelationType = 'hasOne' | 'hasMany'; /** * Raw definition for a model's relation. */ type ModelRelationRaw = ModelPropRaw & { /** * Internal type identifier for Foscia's type guards. */ $MODEL_TYPE: 'relation'; $RELATION_TYPE: ModelRelationType; type?: string; path?: string; }; /** * Definition for a model's attribute. */ type ModelRelation = ModelPropNormalized & ModelRelationRaw; /** * Extract model's attributes and relations from the whole definition. */ type ModelSchema = { [K in keyof D]: D[K] extends ModelAttribute ? D[K] : D[K] extends ModelRelation ? D[K] : never; }; /** * Model hook callback function. */ type ModelHookCallback = HookCallback; /** * Model's hooks definition. */ type ModelHooksDefinition = { retrieved: ModelHookCallback; creating: ModelHookCallback; created: ModelHookCallback; updating: ModelHookCallback; updated: ModelHookCallback; saving: ModelHookCallback; saved: ModelHookCallback; destroying: ModelHookCallback; destroyed: ModelHookCallback; }; /** * Extendable model class holding the configuration and schema. */ type ModelClass = Hookable & { /** * Internal type identifier for Foscia's type guards. */ readonly $MODEL_TYPE: 'model'; readonly $type: string; readonly $config: ModelConfig; readonly $schema: ModelSchema; configure(rawConfig?: ModelConfig, override?: boolean): void; extends(rawDefinition?: ND & ThisType>): Model>; }; /** * Model class of a dedicated instance. * This type is used to keep instance generic typing across actions enhancements. */ type Model = any> = ModelClass & Constructor; /** * Model instance for a dedicated model class. * This type is used to keep instance generic typing across actions enhancements. */ type ModelClassInstance = { readonly $model: ModelClass; }; /** * Model instance holding state and values. */ type ModelInstance = { /** * Internal type identifier for Foscia's type guards. */ readonly $MODEL_TYPE: 'instance'; readonly $model: ModelClass; id: ModelId; lid: Optional; exists: boolean; $loaded: Dictionary; $values: Partial>>; $raw: any; $original: ModelSnapshot>; } & { [K in keyof D]: D[K] extends ModelAttribute ? T : D[K] extends ModelRelation ? T : D[K] extends DescriptorHolder ? T : D[K]; }; /** * Model class or instance snapshot. */ type ModelSnapshot = { id: Optional; lid: Optional; exists: boolean; $raw: any; $loaded: Dictionary; $values: Partial>; }; type ModelClassOrInstance = ModelClass | ModelClassInstance; /** * Infer the definition from a model class or model instance. */ type ModelInferDefinition = M extends ModelClassOrInstance ? D : M extends {} ? M : never; /** * Infer the schema from a model class or model instance. */ type ModelInferSchema = ModelSchema>; /** * Model class or instance values map (only attributes/relations). */ type ModelValues = { [K in keyof ModelInferSchema]: ModelInferSchema[K] extends ModelAttribute ? T : ModelInferSchema[K] extends ModelRelation ? T : never; }; type SerializerI = { serialize(instance: ModelInstance, context: {}): Awaitable; }; type DeserializedData = { instances: I[]; }; type DeserializerI = { deserialize(data: AdapterData, context: {}): Awaitable; }; type ObjectOptionalIdentifier = { type?: string; id?: ModelId; lid?: ModelId; }; type ObjectNormalizedIdentifier = { type: string; id?: ModelId; lid?: ModelId; }; type ObjectExtractedData = { resources: Optional; }; type ObjectDeserializerConfig = {}; type ObjectSerializerConfig = {}; declare abstract class ObjectDeserializer = ObjectExtractedData, Data extends DeserializedData = DeserializedData> implements DeserializerI { protected static NON_IDENTIFIED_LOCAL_ID: string; constructor(config?: ObjectDeserializerConfig); configure(config?: ObjectDeserializerConfig, override?: boolean): void; deserialize(data: AdapterData, context: {}): Promise; protected abstract makeDeserializedData(instances: ModelInstance[], extractedData: Extract): Promise; protected abstract extractData(data: AdapterData, context: {}): Promise; protected abstract extractOptionalIdentifier(resource: Resource, context: {}, parent?: ModelInstance, relation?: ModelRelation): Promise; protected initInstancesMap(): Promise>>>; protected prepareInstancesMap(extractedData: Extract, instancesMap: IdentifiersMap>, context: {}): Promise; protected deserializeResource(extractedData: Extract, instancesMap: IdentifiersMap>, resource: Resource, context: {}, parent?: ModelInstance, relation?: ModelRelation): Promise>; protected deserializeResourceOnInstance(extractedData: Extract, instancesMap: IdentifiersMap>, resource: Resource, identifier: ObjectNormalizedIdentifier, instance: ModelInstance, context: {}): Promise>; protected extractIdentifier(resource: Resource, context: {}, parent?: ModelInstance, relation?: ModelRelation): Promise; protected extractLocalId(_resource: Resource, identifier: ObjectNormalizedIdentifier, _context: {}): Promise; protected findOrMakeInstance(resource: Resource, identifier: ObjectNormalizedIdentifier, context: {}): Promise>; protected findInstance(_resource: Resource, identifier: ObjectNormalizedIdentifier, context: {}): Promise; protected makeInstance(_resource: Resource, identifier: ObjectNormalizedIdentifier, context: {}): Promise; protected releaseInstance(resource: Resource, instance: ModelInstance, context: {}): Promise; protected extractPropValue(_resource: Resource, _serializedKey: string, _context: {}): Promise; protected extractAttributeValue(_extractedData: Extract, resource: Resource, serializedKey: string, context: {}): Promise; protected extractRelationValue(_extractedData: Extract, resource: Resource, serializedKey: string, context: {}): Promise>; protected hydratePropInInstance(instance: ModelInstance, def: ModelAttribute | ModelRelation, value: unknown): void; protected hydrateAttributeInInstance(instance: ModelInstance, def: ModelAttribute, value: unknown): Promise; protected hydrateRelationInInstance(instance: ModelInstance, def: ModelRelation, value: unknown): Promise; protected serializeAttributeKey(instance: ModelInstance, def: ModelAttribute, _context: {}): Promise; protected serializeRelationKey(instance: ModelInstance, def: ModelRelation, _context: {}): Promise; protected shouldDeserializeAttribute(instance: ModelInstance, def: ModelAttribute, rawValue: unknown, context: {}): Promise; protected shouldDeserializeRelation(instance: ModelInstance, def: ModelRelation, rawValue: unknown, context: {}): Promise; protected shouldDeserializeProp(_instance: ModelInstance, def: ModelAttribute | ModelRelation, rawValue: unknown, _context: {}): Promise; protected deserializeAttributeValue(_instance: ModelInstance, def: ModelAttribute, rawValue: unknown, _context: {}): Promise; protected deserializeRelationValue(extractedData: Extract, instancesMap: IdentifiersMap>, instance: ModelInstance, def: ModelRelation, rawValue: Optional, context: {}): Promise | Resource | ({ readonly $MODEL_TYPE: "instance"; readonly $model: ModelClass; id: ModelId; lid: Optional; exists: boolean; $loaded: Dictionary; $values: Partial>>; $raw: any; $original: ModelSnapshot>; } & { [x: string]: any; })[] | null | undefined>; } declare abstract class ObjectSerializer implements SerializerI { constructor(config?: ObjectSerializerConfig); configure(config?: ObjectSerializerConfig, override?: boolean): void; serialize(instance: ModelInstance, context: {}): Promise; protected abstract makeResource(instance: ModelInstance, context: {}): Promise; protected abstract serializeRelatedInstance(instance: ModelInstance, def: ModelRelation, related: ModelInstance, context: {}): Promise; protected hydratePropInResource(_resource: Data, _serializedKey: string, _serializedValue: unknown): Promise; protected hydrateAttributeInResource(resource: Data, serializedKey: string, serializedValue: unknown): Promise; protected hydrateRelationInResource(resource: Data, serializedKey: string, serializedValue: unknown): Promise; protected serializeAttributeKey(instance: ModelInstance, def: ModelAttribute, _context: {}): Promise; protected serializeRelationKey(instance: ModelInstance, def: ModelRelation, _context: {}): Promise; protected shouldSerializeAttribute(instance: ModelInstance, def: ModelAttribute, rawValue: unknown, context: {}): Promise; protected shouldSerializeRelation(instance: ModelInstance, def: ModelRelation, rawValue: unknown, context: {}): Promise; protected shouldSerializeProp(instance: ModelInstance, def: ModelAttribute | ModelRelation, rawValue: unknown, _context: {}): Promise; protected serializeAttributeValue(_instance: ModelInstance, def: ModelAttribute, rawValue: unknown, _context: {}): Promise; protected serializeRelationValue(instance: ModelInstance, def: ModelRelation, rawValue: unknown, context: {}): Promise; } type RestResourceId = ModelId; type RestAbstractResource = Dictionary & { type?: string; }; type RestResource = RestAbstractResource & { id: RestResourceId; }; type RestNewResource = RestAbstractResource & { id?: RestResourceId; }; type RestDeserializerConfig = ObjectDeserializerConfig & { dataReader?: DataReader; dataExtractor?: DataExtractor | null; }; type RestSerializerConfig = ObjectSerializerConfig & { dataWrapper?: DataWrapper | null; }; type DataReader = (response: Response) => Awaitable; type DataExtractor = (document: any) => Awaitable; type DataWrapper = (resource: Dictionary) => Awaitable; declare class RestDeserializer extends ObjectDeserializer { private dataReader; private dataExtractor; constructor(config?: RestDeserializerConfig); configure(config?: RestDeserializerConfig, override?: boolean): void; /** * @inheritDoc */ protected makeDeserializedData(instances: ModelInstance[]): Promise<{ instances: ModelInstance[]; }>; /** * @inheritDoc */ protected extractData(response: Response): Promise>; /** * @inheritDoc */ protected extractOptionalIdentifier(resource: RestNewResource): Promise; /** * @inheritDoc */ protected extractPropValue(resource: RestNewResource, serializedKey: string): Promise; } declare class RestSerializer extends ObjectSerializer { private dataWrapper; constructor(config?: RestSerializerConfig); configure(config?: RestSerializerConfig, override?: boolean): void; serialize(instance: ModelInstance, context: {}): Promise>; /** * @inheritDoc */ protected makeResource(instance: ModelInstance): Promise<{ id: ModelId; lid: Optional; }>; /** * @inheritDoc */ protected hydratePropInResource(resource: RestNewResource, serializedKey: string, serializedValue: unknown): Promise; /** * @inheritDoc */ protected serializeRelatedInstance(_instance: ModelInstance, _def: ModelRelation, related: ModelInstance): Promise; } export { DataExtractor, DataReader, DataWrapper, RestAbstractResource, RestDeserializer, RestDeserializerConfig, RestNewResource, RestResource, RestResourceId, RestSerializer, RestSerializerConfig };