UNPKG

@controladad/ng-base

Version:
48 lines (47 loc) 2.22 kB
import { Observable } from 'rxjs'; import { HttpClient } from '@angular/common/http'; import { DataGetOptions, DataGetRequest, ItemRecord } from '../interfaces'; type ResponseType = 'json' | 'text' | 'blob' | undefined; interface ApiOptions { responseType?: ResponseType; } export type EndpointMethods = 'get' | 'getById' | 'post' | 'put' | 'delete' | 'deleteById'; export type EndpointMapper = { [p in EndpointMethods]?: ((path: string) => string) | string; }; declare class Endpoint { path: string; mapper?: EndpointMapper | undefined; constructor(path: string, mapper?: EndpointMapper | undefined); get(method: EndpointMethods): string; } export declare class _BaseApi<ENTITY, CREATE = any, UPDATE = any> { readonly http: HttpClient; readonly apiBaseUrl: string; protected endpoint: Endpoint; protected prefix: string; /** * * @param endpoint is the base url to request at, slash at the start is optional * @param endpointMapper is used when the endpoint path is not following the default rule and needs to be changed. */ constructor(endpoint: string, endpointMapper?: EndpointMapper); getAdapter(options?: DataGetOptions<ENTITY>): any; getAdapterFiltered(defaultFilters?: DataGetRequest['filters']): any; get(id: string | number, opts?: ApiOptions): Observable<ENTITY>; getAll<T = ENTITY>(opts?: DataGetRequest, overrideUrl?: string): Observable<T[]>; create<T>(model: CREATE, opts?: ApiOptions): Observable<T>; update<T>(id: number, model: UPDATE, opts?: ApiOptions): Observable<T>; delete<T>(id: number | number[] | { id: number; } | { id: number; }[], opts?: ApiOptions): Observable<T>; protected getItemRecords(mapTo: keyof ENTITY | ((item: ENTITY) => string | ItemRecord<number>), filterByStatus?: boolean, filterBy?: ((item: ENTITY) => boolean) | DataGetRequest['filters']): Observable<ItemRecord<number>[]>; protected exportAndDownload<T>(url: string, query?: DataGetRequest, filename?: string): Observable<T>; protected downloadUrl(resultUrl: object | string, filename?: string): void; private getOptions; private dropSlash; private prependSlash; } export {};