import { FlarumRequestOptions } from './Application'; import Model, { ModelData, SavedModelData } from './Model'; export interface MetaInformation { [key: string]: any; } export interface ApiQueryParamsSingle { fields?: string[]; include?: string; bySlug?: boolean; meta?: MetaInformation; } export interface ApiQueryParamsPlural { fields?: string[]; include?: string; filter?: { q: string; [key: string]: string; }; page?: { offset?: number; number?: number; limit?: number; size?: number; }; sort?: string; meta?: MetaInformation; } export declare type ApiQueryParams = ApiQueryParamsPlural | ApiQueryParamsSingle; export interface ApiPayloadSingle { data: SavedModelData; included?: SavedModelData[]; meta?: MetaInformation; } export interface ApiPayloadPlural { data: SavedModelData[]; included?: SavedModelData[]; links?: { first: string; next?: string; prev?: string; }; meta?: MetaInformation; } export declare type ApiPayload = ApiPayloadSingle | ApiPayloadPlural; export declare type ApiResponseSingle = M & { payload: ApiPayloadSingle; }; export declare type ApiResponsePlural = M[] & { payload: ApiPayloadPlural; }; export declare type ApiResponse = ApiResponseSingle | ApiResponsePlural; interface ApiQueryRequestOptions extends Omit, 'url'> { } interface StoreData { [type: string]: Partial>; } export declare function payloadIsPlural(payload: ApiPayload): payload is ApiPayloadPlural; /** * The `Store` class defines a local data store, and provides methods to * retrieve data from the API. */ export default class Store { /** * The local data store. A tree of resource types to IDs, such that * accessing data[type][id] will return the model for that type/ID. */ protected data: StoreData; /** * The model registry. A map of resource types to the model class that * should be used to represent resources of that type. */ models: Record; constructor(models: Record); /** * Push resources contained within an API payload into the store. * * @return The model(s) representing the resource(s) contained * within the 'data' key of the payload. */ pushPayload(payload: ApiPayloadSingle): ApiResponseSingle; pushPayload(payload: ApiPayloadPlural): ApiResponsePlural; /** * Create a model to represent a resource object (or update an existing one), * and push it into the store. * * @param data The resource object * @return The model, or null if no model class has been * registered for this resource type. */ pushObject(data: SavedModelData): M | null; pushObject(data: SavedModelData, allowUnregistered: false): M; /** * Make a request to the API to find record(s) of a specific type. */ find(type: string, params: ApiQueryParamsSingle): Promise>; find(type: string, params: ApiQueryParamsPlural): Promise>; find(type: string, id: string, params?: ApiQueryParamsSingle, options?: ApiQueryRequestOptions): Promise>; find(type: string, ids: string[], params?: ApiQueryParamsPlural, options?: ApiQueryRequestOptions): Promise>; /** * Get a record from the store by ID. */ getById(type: string, id: string): M | undefined; /** * Get a record from the store by the value of a model attribute. * * @param type The resource type. * @param key The name of the method on the model. * @param value The value of the model attribute. */ getBy(type: string, key: keyof M, value: T): M | undefined; /** * Get all loaded records of a specific type. */ all(type: string): M[]; /** * Remove the given model from the store. */ remove(model: Model): void; /** * Create a new record of the given type. * * @param type The resource type * @param data Any data to initialize the model with */ createRecord(type: string, data?: ModelData): M; } export {};