All Downloads are FREE. Search and download functionalities are using the official Maven repository.

typescript.http.isomorphic-fetch.mustache Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
import {HttpLibrary, RequestContext, ResponseContext} from './http{{importFileExtension}}';
import { from, Observable } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};
{{#platforms}}
{{#node}}
import fetch from "node-fetch";
{{/node}}
{{#browser}}
import "whatwg-fetch";
{{/browser}}
{{/platforms}}

export class IsomorphicFetchHttpLibrary implements HttpLibrary {

    public send(request: RequestContext): Observable {
        let method = request.getHttpMethod().toString();
        let body = request.getBody();

        const resultPromise = fetch(request.getUrl(), {
            method: method,
            body: body as any,
            headers: request.getHeaders(),
            {{#platforms}}
            {{#node}}
            agent: request.getAgent(),
            {{/node}}
            {{#browser}}
            credentials: "same-origin"
            {{/browser}}
            {{/platforms}}
        }).then((resp: any) => {
            const headers: { [name: string]: string } = {};
            resp.headers.forEach((value: string, name: string) => {
              headers[name] = value;
            });

            {{#platforms}}
            {{#node}}
            const body = {
              text: () => resp.text(),
              binary: () => resp.buffer()
            };
            {{/node}}
            {{^node}}
            const body = {
              text: () => resp.text(),
              binary: () => resp.blob()
            };
            {{/node}}
            {{/platforms}}
            return new ResponseContext(resp.status, headers, body);
        });

        return from>(resultPromise);

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy