Beetle.js icon indicating copy to clipboard operation
Beetle.js copied to clipboard

AngularCommonAjaxProvider using HttpClient from "@angular/common/http"

Open buchatsky opened this issue 5 years ago • 1 comments

It's a feature request. As angular team says it is recommended to move to the new HttpClient api. I've written my custom AngularCommonAjaxProvider class in typescript which you may use as a template

import { HttpClient, /*HttpRequest,*/ HttpHeaders, HttpParams, HttpResponse } from "@angular/common/http";

import {helper, interfaces, baseTypes, impls, AjaxError } from 'beetle.js';

declare type HttpObserve = 'body' | 'events' | 'response';
declare type HttpResponseType = 'arraybuffer' | 'blob' | 'json' | 'text';

interface RequestOptions {
    body?: any;
    headers?: HttpHeaders | {
        [header: string]: string | string[];
    };
    observe: HttpObserve;//string;
    params?: HttpParams | {
        [param: string]: string | string[];
    };
    reportProgress?: boolean;
    responseType: HttpResponseType;//string;
    withCredentials?: boolean;
}

export class AngularCommonAjaxProvider extends baseTypes.AjaxProviderBase {

    protected http: HttpClient;
    protected HeadersConstructor: new (headers?: string | {
        [name: string]: string | string[];
    }) => HttpHeaders;

    constructor(http, HeadersConstructor) {
        super('Angular Common Ajax Provider');
        this.http = http;
        this.HeadersConstructor = HeadersConstructor;
    }

    doAjax(uri: string, method: string, dataType: string, contentType: string, data: any, async: boolean, timeout: number,
        extra: interfaces.Dictionary<any>, headers: interfaces.Dictionary<string>,
        successCallback: (data: any, headerGetter: (name: string) => string, xhr?: XMLHttpRequest) => void,
        errorCallback: (e: AjaxError) => void) {

        var hs = new this.HeadersConstructor({ "Content-Type": contentType });

        if (headers != null) {
            for (var p in headers) {
                hs = hs.append(p, headers[p]);
            }
        }

        var requestOptions: RequestOptions = {
            body: data,
            headers: hs,
            observe: 'response',
            responseType: 'text'
        };
        helper.extend(requestOptions, extra);

        return this.http.request(method, uri, requestOptions)
            .subscribe(
                function (resp: HttpResponse<string>) {
                    return successCallback(resp.body, function (name) {
                        return resp.headers[name];
                    });
                },
                function (error) {
                    var obj = { status: error.status, detail: error._body, error: error };
                    var e = helper.createError(error.statusText, null, obj);
                    errorCallback(<AjaxError>e);
                    return e;
                });
    };

}

buchatsky avatar Sep 16 '19 11:09 buchatsky

Great work! I think this might stay for a little while, you can inject your provider anyway. I wanted to complete quick fixes and release a new package, this could take a while.

umutozel avatar Oct 31 '19 10:10 umutozel