Fix deprecation notice in api-client.service.ts
This commit is contained in:
parent
f4feb2b3b7
commit
57e181fe8c
|
@ -6,24 +6,8 @@ import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
|||
})
|
||||
export class ApiClientService {
|
||||
private apiRoot = 'api/';
|
||||
public requestOptions = {};
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
this.requestOptions = {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public setToken(token: string) {
|
||||
this.requestOptions = {
|
||||
headers: new HttpHeaders({
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
})
|
||||
}
|
||||
}
|
||||
constructor(private http: HttpClient) { }
|
||||
|
||||
public apiEndpoint(endpoint: string): string {
|
||||
if(endpoint.startsWith('https')) {
|
||||
|
@ -45,42 +29,37 @@ export class ApiClientService {
|
|||
public get(endpoint: string, data: any = {}) {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
this.http.get(this.apiEndpoint(endpoint), {
|
||||
...this.requestOptions,
|
||||
params: new HttpParams({ fromObject: data })
|
||||
}).subscribe((data: any) => {
|
||||
resolve(data);
|
||||
}, (err) => {
|
||||
reject(err);
|
||||
}).subscribe({
|
||||
next: (v) => resolve(v),
|
||||
error: (e) => reject(e)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public post(endpoint: string, data: any = {}) {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
this.http.post(this.apiEndpoint(endpoint), this.dataToParams(data), this.requestOptions).subscribe((data: any) => {
|
||||
resolve(data);
|
||||
}, (err) => {
|
||||
reject(err);
|
||||
this.http.post(this.apiEndpoint(endpoint), this.dataToParams(data)).subscribe({
|
||||
next: (v) => resolve(v),
|
||||
error: (e) => reject(e)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public put(endpoint: string, data: any = {}) {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
this.http.put(this.apiEndpoint(endpoint), this.dataToParams(data), this.requestOptions).subscribe((data: any) => {
|
||||
resolve(data);
|
||||
}, (err) => {
|
||||
reject(err);
|
||||
this.http.put(this.apiEndpoint(endpoint), this.dataToParams(data)).subscribe({
|
||||
next: (v) => resolve(v),
|
||||
error: (e) => reject(e)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public delete(endpoint: string) {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
this.http.delete(this.apiEndpoint(endpoint), this.requestOptions).subscribe((data: any) => {
|
||||
resolve(data);
|
||||
}, (err) => {
|
||||
reject(err);
|
||||
this.http.delete(this.apiEndpoint(endpoint)).subscribe({
|
||||
next: (v) => resolve(v),
|
||||
error: (e) => reject(e)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue