-
Notifications
You must be signed in to change notification settings - Fork 60
/
gaxios.ts
205 lines (183 loc) · 6.13 KB
/
gaxios.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018, Google, LLC.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as extend from 'extend';
import {Agent} from 'https';
import fetch, {Response} from 'node-fetch';
import * as qs from 'querystring';
import * as stream from 'stream';
import {URL} from 'url';
import {GaxiosError, GaxiosOptions, GaxiosPromise, GaxiosResponse, Headers} from './common';
import {getRetryConfig} from './retry';
// tslint:disable-next-line variable-name no-any
let HttpsProxyAgent: any;
// Figure out if we should be using a proxy. Only if it's required, load
// the https-proxy-agent module as it adds startup cost.
function loadProxy() {
const proxy = process.env.HTTPS_PROXY || process.env.https_proxy ||
process.env.HTTP_PROXY || process.env.http_proxy;
if (proxy) {
HttpsProxyAgent = require('https-proxy-agent');
}
return proxy;
}
loadProxy();
export class Gaxios {
private agentCache = new Map<string, Agent>();
/**
* Default HTTP options that will be used for every HTTP request.
*/
defaults: GaxiosOptions;
/**
* The Gaxios class is responsible for making HTTP requests.
* @param defaults The default set of options to be used for this instance.
*/
constructor(defaults?: GaxiosOptions) {
this.defaults = defaults || {};
}
/**
* Perform an HTTP request with the given options.
* @param opts Set of HTTP options that will be used for this HTTP request.
*/
async request<T = any>(opts: GaxiosOptions = {}): GaxiosPromise<T> {
opts = this.validateOpts(opts);
try {
let translatedResponse: GaxiosResponse<T>;
if (opts.adapter) {
translatedResponse = await opts.adapter<T>(opts);
} else {
const res = await fetch(opts.url!, opts);
const data = await this.getResponseData(opts, res);
translatedResponse = this.translateResponse<T>(opts, res, data);
}
if (!opts.validateStatus!(translatedResponse.status)) {
throw new GaxiosError<T>(
`Request failed with status code ${translatedResponse.status}`,
opts, translatedResponse);
}
return translatedResponse;
} catch (e) {
const err = e as GaxiosError;
err.config = opts;
const {shouldRetry, config} = await getRetryConfig(e);
if (shouldRetry && config) {
err.config.retryConfig!.currentRetryAttempt =
config.retryConfig!.currentRetryAttempt;
return this.request<T>(err.config);
}
throw err;
}
}
private async getResponseData(opts: GaxiosOptions, res: Response):
Promise<any> {
switch (opts.responseType) {
case 'stream':
return res.body;
case 'json':
let data = await res.text();
try {
data = JSON.parse(data);
} catch (e) {
}
return data as {};
case 'arraybuffer':
return res.arrayBuffer();
case 'blob':
return res.blob();
default:
return res.text();
}
}
/**
* Validate the options, and massage them to match the fetch format.
* @param opts The original options passed from the client.
*/
private validateOpts(options: GaxiosOptions): GaxiosOptions {
const opts = extend(true, {}, this.defaults, options);
if (!opts.url) {
throw new Error('URL is required.');
}
// baseUrl has been deprecated, remove in 2.0
const baseUrl = opts.baseUrl || opts.baseURL;
if (baseUrl) {
opts.url = baseUrl + opts.url;
}
if (typeof options.maxContentLength === 'number') {
opts.size = options.maxContentLength;
}
opts.headers = opts.headers || {};
if (opts.data) {
if (this.isReadableStream(opts.data)) {
opts.body = opts.data;
} else if (typeof opts.data === 'object') {
opts.body = JSON.stringify(opts.data);
opts.headers['Content-Type'] = 'application/json';
} else {
opts.body = opts.data;
}
}
opts.validateStatus = opts.validateStatus || this.validateStatus;
opts.paramsSerializer = opts.paramsSerializer || this.paramsSerializer;
opts.responseType = opts.responseType || 'json';
if (!opts.headers['Accept'] && opts.responseType === 'json') {
opts.headers['Accept'] = 'application/json';
}
opts.method = opts.method || 'GET';
if (opts.params) {
const parts = new URL(opts.url);
parts.search = opts.paramsSerializer(opts.params);
opts.url = parts.href;
}
const proxy = loadProxy();
if (proxy) {
if (this.agentCache.has(proxy)) {
opts.agent = this.agentCache.get(proxy);
} else {
opts.agent = new HttpsProxyAgent(proxy);
this.agentCache.set(proxy, opts.agent!);
}
}
return opts;
}
/**
* By default, throw for any non-2xx status code
* @param status status code from the HTTP response
*/
private validateStatus(status: number) {
return status >= 200 && status < 300;
}
/**
* Encode a set of key/value pars into a querystring format (?foo=bar&baz=boo)
* @param params key value pars to encode
*/
private paramsSerializer(params: {[index: string]: string|number}) {
return qs.stringify(params);
}
private isReadableStream(obj: any): boolean {
return obj instanceof stream.Readable && typeof obj._read === 'function';
}
private translateResponse<T>(opts: GaxiosOptions, res: Response, data?: T):
GaxiosResponse<T> {
// headers need to be converted from a map to an obj
const headers = {} as Headers;
res.headers.forEach((value, key) => {
headers[key] = value;
});
return {
config: opts,
data: data as T,
headers,
status: res.status,
statusText: res.statusText
};
}
}