1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/model/network/network.service.ts

122 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Message} from '../../../../common/entities/Message';
import {SlimLoadingBarService} from 'ng2-slim-loading-bar';
import 'rxjs/Rx';
import {ErrorCodes, ErrorDTO} from '../../../../common/entities/Error';
import {Config} from '../../../../common/config/public/Config';
import {Utils} from '../../../../common/Utils';
2016-03-13 05:19:24 +08:00
2016-05-05 03:08:05 +08:00
@Injectable()
2016-05-09 23:04:56 +08:00
export class NetworkService {
2016-03-13 05:19:24 +08:00
_apiBaseUrl = Utils.concatUrls(Config.Client.urlBase, '/api');
private globalErrorHandlers: Array<(error: ErrorDTO) => boolean> = [];
2016-03-13 05:19:24 +08:00
2018-03-31 03:30:30 +08:00
constructor(private _http: HttpClient,
private slimLoadingBarService: SlimLoadingBarService) {
}
2016-03-13 05:19:24 +08:00
public static buildUrl(url: string, data?: { [key: string]: any }) {
2018-03-31 03:30:30 +08:00
if (data) {
const keys = Object.getOwnPropertyNames(data);
if (keys.length > 0) {
url += '?';
for (let i = 0; i < keys.length; i++) {
url += keys[i] + '=' + data[keys[i]];
if (i < keys.length - 1) {
url += '&';
}
}
}
}
return url;
}
public postJson<T>(url: string, data: any = {}): Promise<T> {
return this.callJson('post', url, data);
}
public putJson<T>(url: string, data: any = {}): Promise<T> {
return this.callJson('put', url, data);
}
public getJson<T>(url: string, data?: { [key: string]: any }): Promise<T> {
return this.callJson('get', NetworkService.buildUrl(url, data));
2018-03-31 03:30:30 +08:00
}
public deleteJson<T>(url: string): Promise<T> {
return this.callJson('delete', url);
}
private callJson<T>(method: 'get' | 'post' | 'delete' | 'put', url: string, data: any = {}): Promise<T> {
const body = data;
2016-03-13 18:28:29 +08:00
2017-06-22 03:16:04 +08:00
this.slimLoadingBarService.visible = true;
this.slimLoadingBarService.start(() => {
this.slimLoadingBarService.visible = false;
});
2018-03-31 03:30:30 +08:00
const process = (res: Message<T>): T => {
2017-06-22 03:16:04 +08:00
this.slimLoadingBarService.complete();
2017-07-04 01:17:49 +08:00
if (!!res.error) {
if (res.error.code) {
res.error['title'] = ErrorCodes[res.error.code];
}
throw res.error;
}
return res.result;
2017-06-22 03:16:04 +08:00
};
2018-03-31 03:30:30 +08:00
const err = (error) => {
2017-06-22 03:16:04 +08:00
this.slimLoadingBarService.complete();
2018-03-31 03:30:30 +08:00
return this.handleError(error);
2017-06-22 03:16:04 +08:00
};
2018-03-31 03:30:30 +08:00
switch (method) {
case 'get':
return this._http.get<Message<T>>(this._apiBaseUrl + url)
2018-03-31 03:30:30 +08:00
.toPromise()
.then(process)
.catch(err);
case 'delete':
return this._http.delete<Message<T>>(this._apiBaseUrl + url)
2018-03-31 03:30:30 +08:00
.toPromise()
.then(process)
.catch(err);
case 'post':
return this._http.post<Message<T>>(this._apiBaseUrl + url, body)
2018-03-31 03:30:30 +08:00
.toPromise()
.then(process)
.catch(err);
case 'put':
return this._http.put<Message<T>>(this._apiBaseUrl + url, body)
2018-03-31 03:30:30 +08:00
.toPromise()
.then(process)
.catch(err);
default:
throw new Error('Unknown method');
}
2016-05-09 23:04:56 +08:00
}
private handleError(error: any) {
2018-03-31 03:30:30 +08:00
if (typeof error.code !== 'undefined') {
for (let i = 0; i < this.globalErrorHandlers.length; i++) {
2018-03-31 03:30:30 +08:00
if (this.globalErrorHandlers[i](error) === true) {
return;
}
}
2017-07-13 00:31:19 +08:00
return Promise.reject(error);
}
// instead of just logging it to the console
2018-03-31 03:30:30 +08:00
console.error('error:', error);
2017-07-04 01:17:49 +08:00
return Promise.reject(error.message || error || 'Server error');
}
addGlobalErrorHandler(fn: (error: ErrorDTO) => boolean) {
this.globalErrorHandlers.push(fn);
}
2016-03-13 05:19:24 +08:00
}