Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 107x 107x 107x 33x 107x 49x 49x 22x 22x 15x 15x 21x 21x 107x | import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
export type RequestMethod = 'delete' | 'get' | 'post' | 'put';
export interface RequestOptions {
headers?:
| HttpHeaders
| {
[header: string]: string | string[];
};
observe?: string;
params?:
| HttpParams
| {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: string;
withCredentials?: boolean;
}
export interface RestParameters {
limit?: number;
select?: string;
skip?: number;
sort?: string;
where?: any;
}
@Injectable({ providedIn: 'root' })
export class ApiService {
constructor(private http: HttpClient) {}
/**
* Sends a request to the API, returning the data as a basic object.
* @param method The HTTP method to use. Ex: 'get', 'post', 'put', 'delete'.
* @param path The relative path from the object's base endpoint. Ex: '/count', '/recent'.
* @param params The parameters to pass to the endpoint. Ex: { where: { name: 'John Doe' }, limit: 10, sort: 'name' }.
*/
public request(
method: RequestMethod,
url: string,
params?: any,
options: RequestOptions = {},
): Observable<any> | Promise<any> {
options.headers = new HttpHeaders();
if ((method === 'get' || method === 'delete') && params) {
options.params = new HttpParams().set('query', JSON.stringify(params));
}
let observable: Observable<ArrayBuffer>;
switch (method) {
case 'get':
observable = this.http.get(url, options as any);
break;
case 'post':
observable = this.http.post(url, params ? params : undefined, options as any);
break;
case 'put':
observable = this.http.put(url, params ? params : undefined, options as any);
break;
case 'delete':
observable = this.http.delete(url, options as any);
break;
default:
throw new Error('Unsupported HTTP verb.');
}
return options.reportProgress ? observable : observable.toPromise();
}
}
|