@c8y/ngx-components
Version:
Angular modules for Cumulocity IoT applications
109 lines • 5.78 kB
TypeScript
import { Observable } from 'rxjs';
import { DynamicBulkRetrievalError } from './dynamic-bulk-retrieval-error.model';
import { DynamicComponentAlert } from './dynamic-component-alert.model';
import { DynamicResolverService } from './dynamic-resolver.service';
import { TranslateService } from '@ngx-translate/core';
/**
* A DynamicDetailsResolver is responsible to resolve items of a dynamic components configuration.
* Ideally the resolve method is implemented in a way that causes not a request per component instance.
* Instead it should collect the requests it would like to perform within the resolve method,
* and returns a Promise/Observable that takes care that duplicate objects are just retrieved a single time.
* The bulkResolvingTrigger$ of the DynamicResolverService can be utilized to know when bulk resolving should be triggered.
*
* The serialize method is used for storing the configuration within the backend.
* It should be implemented in a way, that will reduce it's output to only the required attributes
* of the entity that are needed for retrieving it afterwards again via the resolve method.
*/
export interface DynamicDetailsResolver<T = any> {
/**
* Used to resolve/refresh a certain attribute of a widgets configuration.
* @param {any} config The dynamic components configuration.
* @param {string} attribute The attribute of the dynamic components configuration to be resolved.
* @returns T
*/
resolve(config: any, attribute: string, bulkRequestId: number): T | Array<DynamicComponentAlert<T> | T> | DynamicComponentAlert<T> | Promise<T | Array<DynamicComponentAlert<T> | T> | DynamicComponentAlert<T>> | Observable<T | Array<DynamicComponentAlert<T> | T> | DynamicComponentAlert<T>>;
/**
* Used to serialize a certain attribute of a dynamic components configuration.
* This is e.g. used when storing the configuration of a widget on a dashboard to the backend.
* Usually you should be able to reduce the stored configuration to only e.g. the id of the entity instead of storing the complete entity.
* @param {any} config The dynamic components configuration.
* @param {string} attribute The attribute of the dynamic components configuration to be serialized.
* @returns any The serialized value behind the attribute.
*/
serialize(config: any, attribute: string): Partial<T> | Array<Partial<T>>;
}
/**
* An abstract class to simplify implementing the DynamicDetailsResolver interface for performing bulk resolving.
*/
export declare abstract class DynamicBulkDetailsResolver<T extends {
[key: string]: any;
}> implements DynamicDetailsResolver<T> {
protected dynamicResolver: DynamicResolverService;
protected translateService: TranslateService;
/**
* Provides an Observable of the results of all bulk requests.
*/
resultsOfBulkLoad: Observable<{
result: T[];
bulkRequestId: number;
errors: DynamicBulkRetrievalError[];
}>;
/**
* Map containing the ids to be retrieved per bulk request.
*/
protected idsGroupedByBulkId: Map<number, string[]>;
constructor(dynamicResolver: DynamicResolverService, translateService: TranslateService);
resolve(config: any, attribute: string, bulkRequestId: number): T | Array<DynamicComponentAlert<T> | T> | DynamicComponentAlert<T> | Promise<T | Array<DynamicComponentAlert<T> | T> | DynamicComponentAlert<T>> | Observable<T | Array<DynamicComponentAlert<T> | T> | DynamicComponentAlert<T>>;
/**
* Provides an Observable of the results of the given bulkRequestId.
*/
getResult$(bulkRequestId: number): Observable<{
result: T[];
errors: DynamicBulkRetrievalError[];
}>;
/**
* Adds a single id or an array of ids to the idsGroupedByBulkId Map for the provided bulkRequestId.
*/
addIdsToBeLoaded(bulkRequestId: number, ...ids: string[]): void;
/**
* Default implementation compatible with serializing an object or an Array of objects.
* Calls serializeSingleObject for an object and for every entry within the array.
*/
serialize(config: any, attribute: string): Partial<T> | Array<Partial<T>>;
/**
* Called in case a specific id wasn't found as part of the returned dataset.
*/
abstract buildRetrievalAlert(entity: Partial<T>, errors?: Array<{
id: string;
status: number;
statusText: string;
}>): DynamicComponentAlert;
/**
* Used to perform the request(s) to retrieve the provided ids.
*/
protected abstract performBulkRequest(uniqIds: string[], bulkRequestId: number): Promise<{
result: T[];
bulkRequestId: number;
errors: DynamicBulkRetrievalError[];
}> | Observable<{
result: T[];
bulkRequestId: number;
errors: DynamicBulkRetrievalError[];
}>;
/**
* Used to extract the ids to be provided to the performBulkRequest method from the configuration.
*/
protected abstract extractIdsToBeRetrieved(valueBehindAttribute: Partial<T> | Array<Partial<T>>): string | string[];
/**
* Responsible for serializing a single object. The returned value will be e.g. stored in the widgets configuration to the backend.
* It should reduce the provided object to it's essentials attributes to find it via API, so e.g. the id.
* It is basically reversing the thing performed as part of the resolve method.
*/
protected abstract serializeSingleObject(obj: T): Partial<T>;
/**
* Checks wether an object is of given id.
* Will by default compare the id attribute with the given id.
*/
protected isEntityOfId(obj: T, id: string): boolean;
}
//# sourceMappingURL=dynamic-details-resolver.d.ts.map