remove runtime dependency on request (#5076)
This commit is contained in:
@@ -1,9 +1,36 @@
|
||||
import request from 'request';
|
||||
import HTTPResponse from './HTTPResponse';
|
||||
import querystring from 'querystring';
|
||||
import log from '../logger';
|
||||
import { http, https } from 'follow-redirects';
|
||||
import { URL } from 'url';
|
||||
|
||||
var encodeBody = function({ body, headers = {} }) {
|
||||
const clients = {
|
||||
'http:': http,
|
||||
'https:': https,
|
||||
};
|
||||
|
||||
function makeCallback(resolve, reject) {
|
||||
return function(response) {
|
||||
const chunks = [];
|
||||
response.on('data', chunk => {
|
||||
chunks.push(chunk);
|
||||
});
|
||||
response.on('end', () => {
|
||||
const body = Buffer.concat(chunks);
|
||||
const httpResponse = new HTTPResponse(response, body);
|
||||
|
||||
// Consider <200 && >= 400 as errors
|
||||
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
||||
return reject(httpResponse);
|
||||
} else {
|
||||
return resolve(httpResponse);
|
||||
}
|
||||
});
|
||||
response.on('error', reject);
|
||||
};
|
||||
}
|
||||
|
||||
const encodeBody = function({ body, headers = {} }) {
|
||||
if (typeof body !== 'object') {
|
||||
return { body, headers };
|
||||
}
|
||||
@@ -63,48 +90,48 @@ var encodeBody = function({ body, headers = {} }) {
|
||||
* @param {Parse.Cloud.HTTPOptions} options The Parse.Cloud.HTTPOptions object that makes the request.
|
||||
* @return {Promise<Parse.Cloud.HTTPResponse>} A promise that will be resolved with a {@link Parse.Cloud.HTTPResponse} object when the request completes.
|
||||
*/
|
||||
module.exports = function(options) {
|
||||
var callbacks = {
|
||||
success: options.success,
|
||||
error: options.error,
|
||||
};
|
||||
delete options.success;
|
||||
delete options.error;
|
||||
delete options.uri; // not supported
|
||||
module.exports = function httpRequest(options) {
|
||||
let url;
|
||||
try {
|
||||
url = new URL(options.url);
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
options = Object.assign(options, encodeBody(options));
|
||||
// set follow redirects to false by default
|
||||
options.followRedirect = options.followRedirects == true;
|
||||
// support params options
|
||||
if (typeof options.params === 'object') {
|
||||
options.qs = options.params;
|
||||
} else if (typeof options.params === 'string') {
|
||||
options.qs = querystring.parse(options.params);
|
||||
}
|
||||
// force the response as a buffer
|
||||
options.encoding = null;
|
||||
const client = clients[url.protocol];
|
||||
if (!client) {
|
||||
return Promise.reject(`Unsupported protocol ${url.protocol}`);
|
||||
}
|
||||
const requestOptions = {
|
||||
method: options.method,
|
||||
port: Number(url.port),
|
||||
path: url.pathname,
|
||||
hostname: url.hostname,
|
||||
headers: options.headers,
|
||||
encoding: null,
|
||||
followRedirects: options.followRedirects === true,
|
||||
};
|
||||
if (options.qs) {
|
||||
requestOptions.path += `?${querystring.stringify(options.qs)}`;
|
||||
}
|
||||
if (options.agent) {
|
||||
requestOptions.agent = options.agent;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
request(options, (error, response, body) => {
|
||||
if (error) {
|
||||
if (callbacks.error) {
|
||||
callbacks.error(error);
|
||||
}
|
||||
return reject(error);
|
||||
}
|
||||
const httpResponse = new HTTPResponse(response, body);
|
||||
|
||||
// Consider <200 && >= 400 as errors
|
||||
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
||||
if (callbacks.error) {
|
||||
callbacks.error(httpResponse);
|
||||
}
|
||||
return reject(httpResponse);
|
||||
} else {
|
||||
if (callbacks.success) {
|
||||
callbacks.success(httpResponse);
|
||||
}
|
||||
return resolve(httpResponse);
|
||||
}
|
||||
});
|
||||
const req = client.request(
|
||||
requestOptions,
|
||||
makeCallback(resolve, reject, options)
|
||||
);
|
||||
if (options.body) {
|
||||
req.write(options.body);
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user