TS ПОЛУЧЕНИЕ ДАННЫХ С СЕРВЕРА - INTERCEPTOR - 3 файла
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { isArray } from 'util';
@Injectable()
export class HttpInterceptor {
public options;
public offline: boolean;
constructor(private http: HttpClient) {
}
/*
request(url: string | Request, options?: any) {
return this.intercept(this.http.request(url, this.options));
}
*/
get(url: string, options?: any) {
return this.intercept(this.http.get(url, options || this.options));
}
post(url: string, body, options?: any) {
return this.intercept(this.http.post(url, body, this.getRequestOptionArgs(options)));
}
put(url: string, body: string, options?: any) {
return this.intercept(this.http.put(url, body, this.getRequestOptionArgs(options)));
}
delete(url: string, options?: any) {
return this.intercept(this.http.delete(url, this.options));
}
getRequestOptionArgs(options?: any): any {
if (options == null) {
options = {};
}
if (options.headers == null) {
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
options.headers = headers;
}
options.withCredentials = true;
options.observe = 'body';
options.responseType = 'json';
return options;
}
intercept(observable: Observable<any>) {
if (this.offline) {
return of([]);
}
return observable.pipe(catchError((err: HttpErrorResponse , source) => {
const errors: Array<Error> = isArray(err.error) ? err.error : [];
switch (err.status) {
case 403:
const response: Array<any> = err.error;
const errorCode: string = response[0].code.toUpperCase();
if (errorCode === 'FORBIDDEN') {
return throwError(err);
} else if (errorCode === 'PASSWORD_CHANGE_NEEDED') {
return throwError('passwordChangeNeeded');
} else {
return throwError('logout');
}
case 401:
return throwError('logout');
case 400: // Ошибка запроса, что то не прошло валидацию
return throwError(errors);
case 404: // Ошибка запроса, что то не прошло валидацию
return throwError(err);
}
return throwError(errors);
}));
}
}
// ...
import { HttpBankService, HttpInterceptor } from './core/api';
@NgModule({
declarations: [
AppComponent,
],
imports: [
],
providers: [
HttpBankService,
HttpInterceptor,
],
bootstrap: [
AppComponent
]
})
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpBankService } from 'app/core/api';
import { buildQueryParams } from 'app/util/build-query-params';
import { ApiDocumentType, ContactInfo } from 'app/core/interfaces';
import { get as _get } from 'lodash';
@Injectable()
export class CorporateDepositHttpBankService extends HttpBankService {
getFilterPayments() {
return this.httpInterceptor.get(`${this.apiBaseUrl}/customer/filter/get_filter_payments_history`);
}
setFilterPayments(data) {
return this.httpInterceptor.post(`${this.apiBaseUrl}/customer/filter/set_filter_payments_history`, data);
}
getCustomerDocument(dealingId?: number): Observable<any> {
const options = {
withCredentials: true,
params: {}
};
if (dealingId) {
options.params = {'customer_dealing_id': dealingId};
}
return this.httpInterceptor.get(`${this.apiBaseUrl}/customer/customer_document`, options);
}
events(params?: Object) {
let query = Object.keys(params).map((key) => `${key}=${params[key]}`).join('&');
if (query !== '') {
query = '?' + query;
}
return this.httpInterceptor.get(this.apiBaseUrl + '/customer/payments' + query);
}
getClient(num: string, type: string) {
const req = {
type: type,
value: num
};
return this.httpInterceptor.post(this.apiBaseUrl + '/client_details', req);
}
getLimitAmount() {
return this.httpInterceptor.get(this.apiBaseUrl + '/limit_amount');
}
petitionInfos(request) {
return this.httpInterceptor.get(this.apiBaseUrl + `/petitions/petition_info${buildQueryParams(request)}`);
}
/**
* @desc Добавление контактных данных для рассылки
*/
updateContacts(documentId: number, contactInfo: ContactInfo, paymentType?: any) {
let type = 'payment';
if (paymentType) {
type = paymentType;
}
return this.httpInterceptor.post(this.apiBaseUrl + '/document/update_contacts', {
type: type,
documentId: documentId,
sendPhones: _get(contactInfo, 'sendPhones', []),
sendEmails: _get(contactInfo, 'sendEmails', [])
});
}
signDocumentISCC(documentType: ApiDocumentType, documentId: number, ISCCData: any) {
const url = '/document/sign';
const request = {
type: documentType,
signMethod: 'iscc',
documentId: documentId,
ISCC_SIGNATURE: ISCCData['ISCC_SIGNATURE'],
ISCC_CONTEXT: ISCCData['ISCC_CONTEXT'],
ISCC_KEY_ID: ISCCData['ISCC_KEY_ID'],
};
return this.httpInterceptor.post(this.apiBaseUrl + url, request);
}
signDocumentSMS(documentType: ApiDocumentType, documentId: number, sms: string) {
const url = '/document/sign';
const request = {
type: documentType,
documentId: documentId,
signMethod: 'sms_otp',
signData: sms,
};
return this.httpInterceptor.post(this.apiBaseUrl + url, request);
}
signFraudDocument(documentType: ApiDocumentType, documentId: number, sms: string) {
const url = '/fraud/confirm';
const request = {
type: documentType,
bankSystemId: documentId,
confirmCode: sms,
};
return this.httpInterceptor.post(this.apiBaseUrl + url, request);
}
getPetitionDocumentLink(petitionId): string {
return `${this.baseUrl}/export/petitions/pdf/${ petitionId }`;
}
}