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

fix: use native fetch in the browser #75

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ build/
coverage
yarn.lock
.vscode
dist/
12 changes: 7 additions & 5 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import * as extend from 'extend';
import {Agent} from 'https';
import fetch, {Response} from 'node-fetch';
import nodeFetch, {Response as NodeFetchResponse} from 'node-fetch';
import * as qs from 'querystring';
import * as stream from 'stream';
import * as url from 'url';
Expand All @@ -23,6 +23,7 @@ import {isBrowser} from './isbrowser';
import {getRetryConfig} from './retry';

const URL = isBrowser() ? window.URL : url.URL;
const fetch = isBrowser() ? window.fetch : nodeFetch;

// tslint:disable-next-line variable-name no-any
let HttpsProxyAgent: any;
Expand Down Expand Up @@ -89,8 +90,8 @@ export class Gaxios {
}
}

private async getResponseData(opts: GaxiosOptions, res: Response):
Promise<any> {
private async getResponseData(
opts: GaxiosOptions, res: Response|NodeFetchResponse): Promise<any> {
switch (opts.responseType) {
case 'stream':
return res.body;
Expand Down Expand Up @@ -199,8 +200,9 @@ export class Gaxios {
return obj instanceof stream.Readable && typeof obj._read === 'function';
}

private translateResponse<T>(opts: GaxiosOptions, res: Response, data?: T):
GaxiosResponse<T> {
private translateResponse<T>(
opts: GaxiosOptions, res: Response|NodeFetchResponse,
data?: T): GaxiosResponse<T> {
// headers need to be converted from a map to an obj
const headers = {} as Headers;
res.headers.forEach((value, key) => {
Expand Down