-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
147 lines (132 loc) · 3.78 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Injectable } from '@angular/core';
import { Config } from 'ionic-angular';
import { AlertController } from 'ionic-angular';
import { LoadingController } from 'ionic-angular';
import { HttpClient } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { HttpParams } from '@angular/common/http';
/**
* Pigeon Restful Provider
*
* HTTP Service Provider for Ionic
*/
@Injectable()
export class RestfulProvider {
/**
* Request Method HTTP
*
* ``` js
*
* console.log('Run');
*
* restful: Restful = Restful();
* restful.request('get', '/api/1.0/user')
* .then((data) => { console.log(data); })
* .catch((data) => { console.log(data); });
*
* console.log('Or');
*
* let data: Promise = restful.request('get', '/api/1.0/user');
* ```
*
* @param {HttpClient} http provider http
* @param {Config} config provider http
* @param {AlertController} alertCtrl controller alert
* @param {LoadingController} loadingCtrl controller loading
*/
constructor(
private http: HttpClient,
private config: Config,
private alertCtrl: AlertController,
private loadingCtrl: LoadingController) { }
/**
* Request Method HTTP
*
* ``` js
* restful.request('get', '/api/1.0/user')
* .then((data) => { console.log(data); })
* .catch((data) => { console.log(data); });
* ```
*
* @param {string} method http method
* @param {string} url server url
* @param {any} body request body
* @param {HttpParams} params request params
* @param {HttpHeaders} headers request headers
* @return {Promise}
*/
public request(
method: string,
url: string,
body?: any,
params: HttpParams = new HttpParams({}),
headers: HttpHeaders = new HttpHeaders({ 'Content-Type': 'application/json' })) {
return new Promise((success, error) => {
const debug: string = this.config.get('pigeon_debug') || false;
const host: string = this.config.get('pigeon_host') || '';
let loader: any = this.loadingCtrl.create({});
if (this.config.get('pigeon_loader')) loader.present();
this.http.request(method, host + url, {
body: JSON.stringify(body),
headers: headers,
params: params
}).subscribe((data: any) => {
loader.dismiss();
if (debug) console.info(data);
success(data);
}, (data: any) => {
loader.dismiss();
if (debug) console.error(data);
if (this.config.get('pigeon_alert')) this.errorMessage(data);
error(data);
});
});
}
/**
* Alert
*
* ``` js
* restful.request('get', '/api/1.0/user')
* .then((data) => { console.log(data); })
* .catch((data) => { console.log(data); });
* ```
*
* @param {string} title alert title
* @param {string} subTitle alert subtitle
* @return {void}
*/
private alert(
title: string,
subTitle: string): void {
this.alertCtrl.create({
title: title,
subTitle: subTitle,
enableBackdropDismiss: false,
buttons: [{
text: 'OK',
role: 'ok'
}]
}).present();
}
/**
* Default Message Error
*
* ``` js
* restful.request('get', '/api/1.0/user')
* .then((data) => { console.log(data); })
* .catch((data) => { console.log(data); });
* ```
*
* @param {any} error http error return
* @return {void}
*/
private errorMessage(
error: any): void {
const status_http: string = this.config.get('pigeon_status');
if (status_http && status_http[error.status] !== undefined) {
this.alert(status_http[error.status][0], status_http[error.status][1]);
} else {
this.alert(error.status, error.error);
}
}
}