Last active
April 1, 2021 00:09
-
-
Save Kurtz1993/c578673bdbd01707de11769129395f93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
type ValidHeaders = HttpHeaders | { [header: string]: string | string[] }; | |
export abstract class BaseService { | |
protected url: string; | |
/** | |
* Use the base service class to create your services. | |
* @param http Angular HTTP service. | |
* @param apiUrl Base URL of the API without the trailing slash. For example: http://example.com | |
* @param resource Prefix to use with the service. For example: 'auth'; | |
*/ | |
constructor(private readonly http: HttpClient, apiUrl: string, resource: string = null) { | |
// Use this property to populate your API base URL | |
this.url = resource ? `${apiUrl}/${resource}` : `${apiUrl}`; | |
} | |
/** | |
* Triggers a HTTP GET to the server with the specified endpoint and query parameters. | |
* @param [endpoint=''] The endpoint to GET from. Defaults to empty string. | |
* @param [params] Any data to be sent to the server. Keys must match query parameter name. | |
* @param [headers=null] Additional headers to pass to the request. | |
* @returns An observable in the format of ApiResponse<T>. | |
*/ | |
protected get<T>( | |
endpoint: string = '', | |
params?: HttpParams, | |
headers: ValidHeaders = null | |
): Observable<T> { | |
const requestUrl = `${this.url}/${endpoint}`; | |
return this.http.get<T>(requestUrl, { params, headers }); | |
} | |
/** | |
* Triggers a HTTP GET to the specified URL and query parameters. | |
* @param url URL of the service to call. | |
* @param [params] Any data to be sent to the server. Keys must match query parameter name. | |
* @param [headers=null] Additional headers to pass to the request. | |
* @returns An observable in the format of ApiResponse<T>. | |
*/ | |
protected getExternal<T>( | |
url: string, | |
params?: HttpParams, | |
headers: ValidHeaders = null | |
): Observable<T> { | |
return this.http.get<T>(url, { params, headers }); | |
} | |
/** | |
* Triggers a HTTP POST to the server with the specified endpoint and data. | |
* @param [endpoint=''] The endpoint to POST to. Defaults to empty string. | |
* @param [data=null] Any data to be sent to the server. | |
* @returns An observable in the format of ApiResponse<T>. | |
*/ | |
protected post<T>( | |
endpoint: string = '', | |
data: any = null, | |
headers: ValidHeaders = null | |
): Observable<T> { | |
return this.http.post<T>(`${this.url}/${endpoint}`, data, { headers }); | |
} | |
/** | |
* Triggers a HTTP PUT to the server with the specified endpoint and query parameters. | |
* @param [endpoint=''] The endpoint to PUT at. Defaults to empty string. | |
* @param [data=null] Any data to be sent to the server. | |
* @returns An observable in the format of ApiResponse<T>. | |
*/ | |
protected put<T>( | |
endpoint: string = '', | |
data: any = null, | |
headers: ValidHeaders = null | |
): Observable<T> { | |
return this.http.put<T>(`${this.url}/${endpoint}`, data, { headers }); | |
} | |
/** | |
* Triggers a HTTP PATCH to the server with the specified endpoint and query parameters. | |
* @param [endpoint=''] The endpoint to PATCH at. Defaults to empty string. | |
* @param [data=null] Any data to be sent to the server. | |
* @returns An observable in the format of ApiResponse<T>. | |
*/ | |
protected patch<T>( | |
endpoint: string = '', | |
data: any = null, | |
headers: ValidHeaders = null | |
): Observable<T> { | |
return this.http.patch<T>(`${this.url}/${endpoint}`, data, { headers }); | |
} | |
/** | |
* Triggers a HTTP DELETE to the server with the specified endpoint and query parameters. | |
* @param [endpoint=''] The endpoint to DELETE from. Defaults to empty string. | |
* @param [params] Any data to be sent to the server. Keys must match query parameter name. | |
* @param [headers=null] Additional headers to pass to the request. | |
* @returns An observable in the format of ApiResponse<T>. | |
*/ | |
protected delete<T>( | |
endpoint: string = '', | |
params?: HttpParams, | |
headers: ValidHeaders = null | |
): Observable<T> { | |
return this.http.delete<T>(`${this.url}/${endpoint}`, { | |
params, | |
headers, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment