import { InterfaceDef, BaseAdapter } from './config'; import { MappingContext } from './mapping-context'; import { EntityQuery } from './entity-query'; import { MetadataStore } from './entity-metadata'; import { JsonResultsAdapter, DataService } from './data-service'; import { Entity } from './entity-aspect'; import { SaveContext, SaveBundle, QueryResult, SaveResult, HttpResponse } from './entity-manager'; export interface InterfaceRegistryConfig { ajax?: InterfaceDef; modelLibrary?: InterfaceDef; dataService?: InterfaceDef; uriBuilder?: InterfaceDef; } /** Registers adapters used by Breeze */ export declare class InterfaceRegistry { ajax: InterfaceDef; modelLibrary: InterfaceDef; dataService: InterfaceDef; uriBuilder: InterfaceDef; } /** @hidden @internal */ declare module "./config" { interface BreezeConfig { /** Initializes a collection of adapter implementations and makes each one the default for its corresponding interface. @method initializeAdapterInstances @param config {Object} @param [config.ajax] {String} - the name of a previously registered "ajax" adapter @param [config.dataService] {String} - the name of a previously registered "dataService" adapter @param [config.modelLibrary] {String} - the name of a previously registered "modelLibrary" adapter @param [config.uriBuilder] {String} - the name of a previously registered "uriBuilder" adapter @return [array of instances] **/ initializeAdapterInstances(irConfig: InterfaceRegistryConfig): void; interfaceRegistry: InterfaceRegistry; } } /** DataServiceAdapter Ajax request configuration */ export interface AjaxConfig { url: string; /** GET, POST, etc. */ type?: string; /** json, etc. */ dataType?: string; contentType?: string; crossDomain?: string | boolean; headers?: {}; data?: any; params?: {}; success: (res: HttpResponse) => void; error: (res: (HttpResponse | Error)) => void; } /** Request sent by AjaxAdapter */ export interface AjaxRequest { /** AjaxAdapter that initiated the request */ adapter: AjaxAdapter; /** Request configuration */ config: RequestInit; /** config from the DataServiceAdapter that called the AjaxAdapter */ dsaConfig: AjaxConfig; /** Function called on response error */ error: (status: number, statusText: string, body: string, response: Response, errorThrown: any) => void; /** Function called on response success */ success: (data: any, statusText: string, response: Response) => void; } /** Handles AJAX requests to server */ export interface AjaxAdapter extends BaseAdapter { /** Function that performs the ajax request and calls the success or error function */ ajax(config: AjaxConfig): void; /** Container for headers to be added to each request */ defaultSettings: { headers?: { [name: string]: string; }; }; /** Function to allow manipulating the request before sending */ requestInterceptor?: (req: AjaxRequest) => void; } /** Adapts breeze change-tracking to the UI framework */ export interface ModelLibraryAdapter extends BaseAdapter { getTrackablePropertyNames: (entity: any) => string[]; initializeEntityPrototype(proto: Object): void; startTracking(entity: any, entityCtor: Function): void; createCtor?: Function; } /** Reshapes data moving between breeze and server */ export interface DataServiceAdapter extends BaseAdapter { fetchMetadata(metadataStore: MetadataStore, dataService: DataService): Promise; executeQuery(mappingContext: MappingContext): Promise; saveChanges(saveContext: SaveContext, saveBundle: SaveBundle): Promise; changeRequestInterceptor: ChangeRequestInterceptorCtor; jsonResultsAdapter: JsonResultsAdapter; } /** Function called by AjaxAdapter before sending request */ export interface AjaxRequestInterceptor { (req: AjaxRequest): void; /** Whether to remove the interceptor after it is called */ oneTime: boolean; } /** Builds URI for performing queries. Serializes the EntityQuery according to the URI syntax. */ export interface UriBuilderAdapter extends BaseAdapter { buildUri(query: EntityQuery, metadataStore: MetadataStore): string; } export interface ChangeRequestInterceptorCtor { new (saveContext: SaveContext, saveBundle: SaveBundle): ChangeRequestInterceptor; } /** Allows manipulating data in DataServiceAdapter before sending to server */ export interface ChangeRequestInterceptor { oneTime?: boolean; /** Prepare and return the save data for an entity change-set. The adapter calls this method for each entity in the change-set, after it has prepared a "change request" for that object. The method can do anything to the request but it must return a valid, non-null request. @example this.getRequest = function (request, entity, index) { // alter the request that the adapter prepared for this entity // based on the entity, saveContext, and saveBundle // e.g., add a custom header or prune the originalValuesMap return request; }; @method getRequest @param request {Object} The object representing the adapter's request to save this entity. @param entity {Entity} The entity-to-be-save as it is in cache @param index {Integer} The zero-based index of this entity in the change-set array @return {Function} The potentially revised request. **/ getRequest(request: any, entity: Entity, index: number): any; /** Last chance to change anything about the 'requests' array after it has been built with requests for all of the entities-to-be-saved. The 'requests' array is the same as 'saveBundle.entities' in many implementations This method can do anything to the array including add and remove requests. It's up to you to ensure that server will accept the requests array data as valid. Returned value is ignored. @example this.done = function (requests) { // alter the array of requests representing the entire change-set // based on the saveContext and saveBundle }; @method done @param requests {Array of Object} The adapter's array of request for this changeset. **/ done(requests: Object[]): void; }