from '@maneno/core/types';
import {
Observable,
Subject
} from 'rxjs';
import {LoadingService} from '../loading.service';
import {getOptions} from './util/get-options';
// This service is a base service that is responsible for communication with api
export class CommonHttpService implements OnDestroy {
private destroyed$ = new Subject<void>();
private track: <T>(observable: Observable<T>) => Observable<T>;
constructor(
loadingService: LoadingService,
public apiUrl: string,
private httpClient: HttpClient
) {
this.track = loadingService.track(this.destroyed$);
}
put<TData, TResponse>(
{url, params, additionalOptions, data}: { params?: StrMap<string>, url: string, additionalOptions?: any, data: TData}
): Observable<TResponse> {
return this.track(
this.httpClient
.put<TResponse>(
this.getUrl(url),
data,
getOptions(params, additionalOptions)
) as any as Observable<TResponse>
);
}
get<TResponse>(
{url, params, additionalOptions}: { params?: StrMap<string>, url: string, additionalOptions?: any }
): Observable<TResponse> {
return this.track(
this.httpClient.get<TResponse>(
this.getUrl(url),
getOptions(params, additionalOptions)
) as any as Observable<TResponse>
);
}
post<TResponse, TData = any>(
{url, params, data, additionalOptions}: { url: string, data?: TData, additionalOptions?: any, params?: StrMap<string> }
): Observable<TResponse> {
return this.track(
this.httpClient
.post<TResponse>(
this.getUrl(url),
data,
getOptions(params, additionalOptions)
) as any as Observable<TResponse>
);
}
delete(url: string) {
this.track(
this.httpClient
.delete(this.getUrl(url))
);
}
ngOnDestroy(): void {
this.destroyed$.next();
}
private getUrl(url: string) {
return `${this.apiUrl}/${url}`;
}
}
ага, ну вроде понятно все)
Обсуждают сегодня