101 lines
3.3 KiB
TypeScript
101 lines
3.3 KiB
TypeScript
import { HttpClient, HttpParams } from '@angular/common/http';
|
|
import { Injector } from '@angular/core';
|
|
import { Observable } from 'rxjs';
|
|
import { CustomHttpUrlEncodingCodec, List, RequiredParam, Validate } from '../utils';
|
|
import { HeadersConfiguration } from '../utils/headers-configuration';
|
|
|
|
export interface HeaderOptions {
|
|
readonly authorization?: boolean;
|
|
readonly accept?: boolean;
|
|
readonly contentType?: boolean;
|
|
}
|
|
|
|
export interface QueryParam {
|
|
readonly key: string;
|
|
readonly value: string | number | boolean;
|
|
}
|
|
|
|
export abstract class GenericService<Entity> {
|
|
protected readonly _http = this._injector.get(HttpClient);
|
|
|
|
protected constructor(protected readonly _injector: Injector, protected readonly _defaultModelPath: string) {}
|
|
|
|
@Validate()
|
|
getAll<R = Entity>(modelPath = this._defaultModelPath): Observable<R[]> {
|
|
return this._http.get<R[]>(`/${encodeURI(modelPath)}`, {
|
|
headers: HeadersConfiguration.getHeaders({ contentType: false }),
|
|
observe: 'body',
|
|
});
|
|
}
|
|
|
|
@Validate()
|
|
delete(
|
|
@RequiredParam() body: unknown,
|
|
modelPath = this._defaultModelPath,
|
|
queryParams?: List<QueryParam>,
|
|
): Observable<unknown> {
|
|
let path = `/${encodeURI(modelPath)}`;
|
|
|
|
if (typeof body === 'string') {
|
|
path += `/${encodeURIComponent(body)}`;
|
|
}
|
|
|
|
return this._http.delete(path, {
|
|
body: body,
|
|
params: this._queryParams(queryParams),
|
|
headers: HeadersConfiguration.getHeaders({ contentType: false }),
|
|
observe: 'body',
|
|
});
|
|
}
|
|
|
|
@Validate()
|
|
protected _post<R = Entity>(
|
|
@RequiredParam() body: unknown,
|
|
modelPath = this._defaultModelPath,
|
|
queryParams?: List<QueryParam>,
|
|
): Observable<R> {
|
|
return this._http.post<R>(`/${encodeURI(modelPath)}`, body, {
|
|
params: this._queryParams(queryParams),
|
|
headers: HeadersConfiguration.getHeaders(),
|
|
observe: 'body',
|
|
});
|
|
}
|
|
|
|
@Validate()
|
|
protected _put<R = Entity>(
|
|
@RequiredParam() body: unknown,
|
|
modelPath = this._defaultModelPath,
|
|
queryParams?: List<QueryParam>,
|
|
): Observable<R> {
|
|
return this._http.put<R>(`/${encodeURI(modelPath)}`, body, {
|
|
params: this._queryParams(queryParams),
|
|
headers: HeadersConfiguration.getHeaders(),
|
|
observe: 'body',
|
|
});
|
|
}
|
|
|
|
@Validate()
|
|
protected _getOne<R = Entity>(
|
|
@RequiredParam() path: List,
|
|
modelPath = this._defaultModelPath,
|
|
queryParams?: List<QueryParam>,
|
|
): Observable<R> {
|
|
const entityPath = path.map(item => encodeURIComponent(item)).join('/');
|
|
|
|
return this._http.get<R>(`/${encodeURI(modelPath)}/${entityPath}`, {
|
|
headers: HeadersConfiguration.getHeaders({ contentType: false }),
|
|
params: this._queryParams(queryParams),
|
|
observe: 'body',
|
|
});
|
|
}
|
|
|
|
protected _queryParams(queryParams?: List<QueryParam>): HttpParams {
|
|
let queryParameters = new HttpParams({ encoder: new CustomHttpUrlEncodingCodec() });
|
|
queryParams?.forEach(param => {
|
|
queryParameters = queryParameters.set(param.key, param.value);
|
|
});
|
|
|
|
return queryParameters;
|
|
}
|
|
}
|