Skip to content

Instantly share code, notes, and snippets.

@bbogdanov
Last active March 12, 2023 14:50
Show Gist options
  • Save bbogdanov/e573c9d1cc8468211b35964a942aa688 to your computer and use it in GitHub Desktop.
Save bbogdanov/e573c9d1cc8468211b35964a942aa688 to your computer and use it in GitHub Desktop.

Revisions

  1. bbogdanov revised this gist Dec 21, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion http-client.ts
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,6 @@ export class ApplicationHttpClient {
    * GET request
    * @param {string} endPoint it doesn't need / in front of the end point
    * @param {IRequestOptions} options options of the request like headers, body, etc.
    * @param {string} api use if there is needed to send request to different back-end than the default one.
    * @returns {Observable<T>}
    */
    public Get<T>(endPoint: string, options?: IRequestOptions): Observable<T> {
  2. bbogdanov renamed this gist Nov 1, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. bbogdanov created this gist Nov 1, 2017.
    72 changes: 72 additions & 0 deletions extended-http-client.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    import {HttpClient, HttpErrorResponse, HttpHeaders, HttpParams} from '@angular/common/http';
    import {Injectable} from '@angular/core';
    import {Observable} from 'rxjs/Observable';

    export interface IRequestOptions {
    headers?: HttpHeaders;
    observe?: 'body';
    params?: HttpParams;
    reportProgress?: boolean;
    responseType?: 'json';
    withCredentials?: boolean;
    body?: any;
    }

    export function applicationHttpClientCreator(http: HttpClient) {
    return new ApplicationHttpClient(http);
    }

    @Injectable()
    export class ApplicationHttpClient {

    private api = 'https://someurl.example';

    // Extending the HttpClient through the Angular DI.
    public constructor(public http: HttpClient) {
    // If you don't want to use the extended versions in some cases you can access the public property and use the original one.
    // for ex. this.httpClient.http.get(...)
    }

    /**
    * GET request
    * @param {string} endPoint it doesn't need / in front of the end point
    * @param {IRequestOptions} options options of the request like headers, body, etc.
    * @param {string} api use if there is needed to send request to different back-end than the default one.
    * @returns {Observable<T>}
    */
    public Get<T>(endPoint: string, options?: IRequestOptions): Observable<T> {
    return this.http.get<T>(this.api + endPoint, options);
    }

    /**
    * POST request
    * @param {string} endPoint end point of the api
    * @param {Object} params body of the request.
    * @param {IRequestOptions} options options of the request like headers, body, etc.
    * @returns {Observable<T>}
    */
    public Post<T>(endPoint: string, params: Object, options?: IRequestOptions): Observable<T> {
    return this.http.post<T>(this.api + endPoint, params, options);
    }

    /**
    * PUT request
    * @param {string} endPoint end point of the api
    * @param {Object} params body of the request.
    * @param {IRequestOptions} options options of the request like headers, body, etc.
    * @returns {Observable<T>}
    */
    public Put<T>(endPoint: string, params: Object, options?: IRequestOptions): Observable<T> {
    return this.http.put<T>(this.api + endPoint, params, options);
    }

    /**
    * DELETE request
    * @param {string} endPoint end point of the api
    * @param {IRequestOptions} options options of the request like headers, body, etc.
    * @returns {Observable<T>}
    */
    public Delete<T>(endPoint: string, options?: IRequestOptions): Observable<T> {
    return this.http.delete<T>(this.api + endPoint, options);
    }
    }