1
1
import { Injectable } from '@angular/core' ;
2
2
import { Storage } from '@ionic/storage-angular' ;
3
+ import { CapacitorHttp } from '@capacitor/core' ;
3
4
4
5
@Injectable ( {
5
6
providedIn : 'root'
@@ -37,14 +38,15 @@ export class ApiService {
37
38
console . log ( 'signIn' ) ;
38
39
let url = new URL ( this . endpoint + '/api/auth/signin' ) ;
39
40
40
- return fetch ( url , {
41
+ return CapacitorHttp . request ( {
42
+ url : url . toString ( ) ,
41
43
method : 'POST' ,
42
44
headers : this . headers ,
43
- body : JSON . stringify ( {
45
+ data : JSON . stringify ( {
44
46
email : email ,
45
47
password : password
46
48
} )
47
- } ) . then ( res => res . json ( ) ) . then ( res => {
49
+ } ) . then ( res => res . data ) . then ( res => {
48
50
if ( res . accessToken ) {
49
51
this . setToken ( res . accessToken ) ;
50
52
}
@@ -56,15 +58,16 @@ export class ApiService {
56
58
console . log ( 'register' ) ;
57
59
let url = new URL ( this . endpoint + '/api/auth/signup' ) ;
58
60
59
- return fetch ( url , {
61
+ return CapacitorHttp . request ( {
62
+ url : url . toString ( ) ,
60
63
method : 'POST' ,
61
64
headers : this . headers ,
62
- body : JSON . stringify ( {
65
+ data : JSON . stringify ( {
63
66
email : email ,
64
67
username : username ,
65
68
password : password
66
69
} )
67
- } ) . then ( res => res . json ( ) ) ;
70
+ } ) . then ( res => res . data ) ;
68
71
}
69
72
70
73
public isLoggedIn ( ) {
@@ -79,10 +82,11 @@ export class ApiService {
79
82
if ( this . accessToken )
80
83
url . searchParams . append ( 'accessToken' , this . accessToken ) ;
81
84
82
- return fetch ( url , {
85
+ return CapacitorHttp . request ( {
86
+ url : url . toString ( ) ,
83
87
method : 'GET' ,
84
88
headers : this . headers ,
85
- } ) . then ( res => res . json ( ) ) . then ( res => {
89
+ } ) . then ( res => res . data ) . then ( res => {
86
90
if ( res . id ) {
87
91
this . id = res . id ;
88
92
this . username = res . username ;
@@ -117,20 +121,22 @@ export class ApiService {
117
121
this . username = "" ;
118
122
this . email = "" ;
119
123
120
- return fetch ( url , {
124
+ return CapacitorHttp . request ( {
125
+ url : url . toString ( ) ,
121
126
method : 'POST' ,
122
127
headers : this . headers
123
- } ) . then ( res => res . json ( ) ) ;
128
+ } ) . then ( res => res . data ) ;
124
129
}
125
130
126
131
public searchSets ( search : string , count : number = 10 ) {
127
132
console . log ( 'searchSets' ) ;
128
133
let url = new URL ( this . endpoint + '/api/set/search/' + search + "/" + count ) ;
129
134
130
- return fetch ( url , {
135
+ return CapacitorHttp . request ( {
136
+ url :url . toString ( ) ,
131
137
method : 'GET' ,
132
138
headers : this . headers ,
133
- } ) . then ( res => res . json ( ) ) ;
139
+ } ) . then ( res => res . data ) ;
134
140
}
135
141
136
142
public getUserSets ( ) {
@@ -140,10 +146,11 @@ export class ApiService {
140
146
if ( this . accessToken )
141
147
url . searchParams . append ( 'accessToken' , this . accessToken ) ;
142
148
143
- return fetch ( url , {
149
+ return CapacitorHttp . request ( {
150
+ url : url . toString ( ) ,
144
151
method : 'GET' ,
145
152
headers : this . headers ,
146
- } ) . then ( res => res . json ( ) ) ;
153
+ } ) . then ( res => res . data ) ;
147
154
}
148
155
149
156
public createSet ( set : any ) {
@@ -153,11 +160,12 @@ export class ApiService {
153
160
if ( this . accessToken )
154
161
url . searchParams . append ( 'accessToken' , this . accessToken ) ;
155
162
156
- return fetch ( url , {
163
+ return CapacitorHttp . request ( {
164
+ url : url . toString ( ) ,
157
165
method : 'POST' ,
158
166
headers : this . headers ,
159
- body : JSON . stringify ( set )
160
- } ) . then ( res => res . json ( ) ) ;
167
+ data : JSON . stringify ( set )
168
+ } ) . then ( res => res . data ) ;
161
169
}
162
170
163
171
public updateSet ( id : number , set : any ) {
@@ -167,11 +175,12 @@ export class ApiService {
167
175
if ( this . accessToken )
168
176
url . searchParams . append ( 'accessToken' , this . accessToken ) ;
169
177
170
- return fetch ( url , {
178
+ return CapacitorHttp . request ( {
179
+ url : url . toString ( ) ,
171
180
method : 'PUT' ,
172
181
headers : this . headers ,
173
- body : JSON . stringify ( set )
174
- } ) . then ( res => res . json ( ) ) ;
182
+ data : JSON . stringify ( set )
183
+ } ) . then ( res => res . data ) ;
175
184
}
176
185
177
186
public deleteSet ( id : string ) {
@@ -181,20 +190,22 @@ export class ApiService {
181
190
if ( this . accessToken )
182
191
url . searchParams . append ( 'accessToken' , this . accessToken ) ;
183
192
184
- return fetch ( url , {
193
+ return CapacitorHttp . request ( {
194
+ url : url . toString ( ) ,
185
195
method : 'DELETE' ,
186
196
headers : this . headers ,
187
- } ) . then ( res => res . json ( ) ) ;
197
+ } ) . then ( res => res . data ) ;
188
198
}
189
199
190
200
public getSet ( id : string ) {
191
201
console . log ( 'getSet' ) ;
192
202
let url = new URL ( this . endpoint + '/api/set/' + id ) ;
193
203
194
- return fetch ( url , {
204
+ return CapacitorHttp . request ( {
205
+ url : url . toString ( ) ,
195
206
method : 'GET' ,
196
207
headers : this . headers ,
197
- } ) . then ( res => res . json ( ) ) ;
208
+ } ) . then ( res => res . data ) ;
198
209
}
199
210
200
211
public getUserStats ( id : string ) {
@@ -204,10 +215,11 @@ export class ApiService {
204
215
if ( this . accessToken )
205
216
url . searchParams . append ( 'accessToken' , this . accessToken ) ;
206
217
207
- return fetch ( url , {
218
+ return CapacitorHttp . request ( {
219
+ url : url . toString ( ) ,
208
220
method : 'GET' ,
209
221
headers : this . headers ,
210
- } ) . then ( res => res . json ( ) ) ;
222
+ } ) . then ( res => res . data ) ;
211
223
}
212
224
213
225
public updateUserStats ( id : string , card : string , type : string ) {
@@ -217,13 +229,14 @@ export class ApiService {
217
229
if ( this . accessToken )
218
230
url . searchParams . append ( 'accessToken' , this . accessToken ) ;
219
231
220
- return fetch ( url , {
232
+ return CapacitorHttp . request ( {
233
+ url : url . toString ( ) ,
221
234
method : 'PUT' ,
222
235
headers : this . headers ,
223
- body : JSON . stringify ( {
236
+ data : JSON . stringify ( {
224
237
type : type
225
238
} )
226
- } ) . then ( res => res . json ( ) ) ;
239
+ } ) . then ( res => res . data ) ;
227
240
}
228
241
229
242
public updateUserStared ( id : string , card : string , stared : boolean ) {
@@ -233,13 +246,14 @@ export class ApiService {
233
246
if ( this . accessToken )
234
247
url . searchParams . append ( 'accessToken' , this . accessToken ) ;
235
248
236
- return fetch ( url , {
249
+ return CapacitorHttp . request ( {
250
+ url : url . toString ( ) ,
237
251
method : 'PUT' ,
238
252
headers : this . headers ,
239
- body : JSON . stringify ( {
253
+ data : JSON . stringify ( {
240
254
stared : stared
241
255
} )
242
- } ) . then ( res => res . json ( ) ) ;
256
+ } ) . then ( res => res . data ) ;
243
257
}
244
258
245
259
private setToken ( token : string ) {
0 commit comments