Skip to content

Commit

Permalink
fix: use native fetch in the browser (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Apr 22, 2019
1 parent 01d8142 commit fec3744
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
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

0 comments on commit fec3744

Please sign in to comment.