|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- const { http, https } = require('follow-redirects');
- const querystring = require('querystring');
- const zlib = require('zlib');
- const pkg = require('../../package.json');
-
- module.exports = {
-
- send: async function(options) {
- let url = this.parseRequired(options.url, 'string', 'api.send: url is required.');
- let method = this.parseOptional(options.method, 'string', 'GET');
- let data = this.parseOptional(options.data, '*', '');
- let dataType = this.parseOptional(options.dataType, 'string', 'auto');
- let verifySSL = this.parseOptional(options.verifySSL, 'boolean', false);
- let params = this.parseOptional(options.params, 'object', null);
- let headers = this.parseOptional(options.headers, 'object', {});
- let username = this.parseOptional(options.username, 'string', '');
- let password = this.parseOptional(options.password, 'string', '');
- let oauth = this.parseOptional(options.oauth, 'string', '');
- let passErrors = this.parseOptional(options.passErrors, 'boolean', true);
- let timeout = this.parseOptional(options.timeout, 'number', 0);
-
- if (params) {
- url += '?' + querystring.stringify(params);
- }
-
- if (dataType == 'auto' && method == 'POST') {
- dataType = 'x-www-form-urlencoded';
- }
-
- if (dataType != 'auto' && !headers['Content-Type']) {
- headers['Content-Type'] = `application/${dataType}`;
- }
-
- if (dataType == 'x-www-form-urlencoded') {
- data = querystring.stringify(data);
- } else if (typeof data != 'string') {
- data = JSON.stringify(data);
- }
-
- if (data) {
- headers['Content-Length'] = Buffer.byteLength(data);
- }
-
- const Url = new URL(url);
- const opts = { method, headers, rejectUnauthorized: !!verifySSL, maxBodyLength: 1000000000 };
-
- if (timeout > 0) {
- opts.timeout = timeout;
- }
-
- if (username || password) {
- opts.auth = `${username}:${password}`;
- }
-
- if (oauth) {
- //const provider = this.oauth[oauth];
- const provider = await this.getOAuthProvider(oauth);
- if (provider && provider.access_token) {
- headers['Authorization'] = 'Bearer ' + provider.access_token;
- }
- }
-
- if (!headers['User-Agent']) headers['User-Agent'] = `${pkg.name}/${pkg.version}`;
- if (!headers['Accept']) headers['Accept'] = 'application/json';
-
- return new Promise((resolve, reject) => {
- const req = (Url.protocol == 'https:' ? https : http).request(Url, opts, res => {
- let body = '';
-
- let output = res;
- if (res.headers['content-encoding'] == 'br') {
- output = res.pipe(zlib.createBrotliDecompress());
- }
- if (res.headers['content-encoding'] == 'gzip') {
- output = res.pipe(zlib.createGunzip());
- }
- if (res.headers['content-encoding'] == 'deflate') {
- output = res.pipe(zlib.createInflate());
- }
-
- output.setEncoding('utf8');
- output.on('data', chunk => body += chunk);
- output.on('end', () => {
- if (passErrors && res.statusCode >= 400) {
- if (this.res.status) {
- this.res.status(res.statusCode).send(body);
- }
- return reject(body);
- }
-
- if (body.charCodeAt(0) === 0xFEFF) {
- body = body.slice(1);
- }
-
- if (res.headers['content-type'] && res.headers['content-type'].includes('json')) {
- try {
- body = JSON.parse(body);
- } catch(e) {
- console.error(e);
- }
- }
-
- resolve({
- status: res.statusCode,
- headers: res.headers,
- data: body
- });
- });
- });
-
- req.on('error', reject);
- req.write(data);
- req.end();
- });
- },
-
- };
|