Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
buchatsky opened this issue Sep 16, 2019 · 1 comment

Comments

@buchatsky
Copy link

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;
                });
    };

}
@umutozel
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants