Добавить внутренних провайдеров в app.module.ts
{
provide: HTTP_INTERCEPTORS,
useClass: ApiGatewayInterceptor,
multi: true,
}
Создать общий сервис:
import { Injectable,OnInit,Component } from '@angular/core';
import {Http,Headers,RequestOptions, RequestOptionsArgs,Response} from '@angular/http';
import { HttpEvent, HttpHeaders,HttpHandler, HttpInterceptor, HttpRequest, HttpClient,HttpParams,HttpResponse,HttpErrorResponse } from '@angular/common/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import { environment } from '../../environments/environment';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/observable/throw'
import 'rxjs/add/operator/catch';
@Injectable()
export class ApiGatewayInterceptor implements HttpInterceptor {
private queryParam: string = '';
constructor() { }
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
}
}
Использовать вызов apigateway следующим образом:
formUrlParam(data) {
let queryString: string = '';
let httpParams = new HttpParams();
Object.keys(data).forEach(function (key) {
httpParams = httpParams.append(key, data[key]);
});
return httpParams;
}
post<T>(url, params) {
let httpParams = this.formUrlParam(params);
return new Promise((resolve, reject) => {
let result = this.http.post(url,params).subscribe(
(response) => resolve(response),
(error) => resolve(error)
)
});
}
patch<T>(url, params) {
console.log(params);
let httpParams = this.formUrlParam(params);
return new Promise((resolve, reject) => {
let result = this.http.patch(url,params).subscribe(
(response) => resolve(response),
(error) => resolve(error)
)
});
}
get<T>(url,params) {
let httpParams = this.formUrlParam(params);
return new Promise((resolve, reject) => {
let result = this.http.get(url,{params: httpParams }).subscribe(
(response) => resolve(response),
(error) => resolve(error)
)
});
}