Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 2 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const { http, https } = require('follow-redirects');
  2. const querystring = require('querystring');
  3. const zlib = require('zlib');
  4. const pkg = require('../../package.json');
  5. module.exports = {
  6. send: async function(options) {
  7. let url = this.parseRequired(options.url, 'string', 'api.send: url is required.');
  8. let method = this.parseOptional(options.method, 'string', 'GET');
  9. let data = this.parseOptional(options.data, '*', '');
  10. let dataType = this.parseOptional(options.dataType, 'string', 'auto');
  11. let verifySSL = this.parseOptional(options.verifySSL, 'boolean', false);
  12. let params = this.parseOptional(options.params, 'object', null);
  13. let headers = this.parseOptional(options.headers, 'object', {});
  14. let username = this.parseOptional(options.username, 'string', '');
  15. let password = this.parseOptional(options.password, 'string', '');
  16. let oauth = this.parseOptional(options.oauth, 'string', '');
  17. let passErrors = this.parseOptional(options.passErrors, 'boolean', true);
  18. let timeout = this.parseOptional(options.timeout, 'number', 0);
  19. if (params) {
  20. url += '?' + querystring.stringify(params);
  21. }
  22. if (dataType == 'auto' && method == 'POST') {
  23. dataType = 'x-www-form-urlencoded';
  24. }
  25. if (dataType != 'auto' && !headers['Content-Type']) {
  26. headers['Content-Type'] = `application/${dataType}`;
  27. }
  28. if (dataType == 'x-www-form-urlencoded') {
  29. data = querystring.stringify(data);
  30. } else if (typeof data != 'string') {
  31. data = JSON.stringify(data);
  32. }
  33. if (data) {
  34. headers['Content-Length'] = Buffer.byteLength(data);
  35. }
  36. const Url = new URL(url);
  37. const opts = { method, headers, rejectUnauthorized: !!verifySSL, maxBodyLength: 1000000000 };
  38. if (timeout > 0) {
  39. opts.timeout = timeout;
  40. }
  41. if (username || password) {
  42. opts.auth = `${username}:${password}`;
  43. }
  44. if (oauth) {
  45. //const provider = this.oauth[oauth];
  46. const provider = await this.getOAuthProvider(oauth);
  47. if (provider && provider.access_token) {
  48. headers['Authorization'] = 'Bearer ' + provider.access_token;
  49. }
  50. }
  51. if (!headers['User-Agent']) headers['User-Agent'] = `${pkg.name}/${pkg.version}`;
  52. if (!headers['Accept']) headers['Accept'] = 'application/json';
  53. return new Promise((resolve, reject) => {
  54. const req = (Url.protocol == 'https:' ? https : http).request(Url, opts, res => {
  55. let body = '';
  56. let output = res;
  57. if (res.headers['content-encoding'] == 'br') {
  58. output = res.pipe(zlib.createBrotliDecompress());
  59. }
  60. if (res.headers['content-encoding'] == 'gzip') {
  61. output = res.pipe(zlib.createGunzip());
  62. }
  63. if (res.headers['content-encoding'] == 'deflate') {
  64. output = res.pipe(zlib.createInflate());
  65. }
  66. output.setEncoding('utf8');
  67. output.on('data', chunk => body += chunk);
  68. output.on('end', () => {
  69. if (passErrors && res.statusCode >= 400) {
  70. if (this.res.status) {
  71. this.res.status(res.statusCode).send(body);
  72. }
  73. return reject(body);
  74. }
  75. if (body.charCodeAt(0) === 0xFEFF) {
  76. body = body.slice(1);
  77. }
  78. if (res.headers['content-type'] && res.headers['content-type'].includes('json')) {
  79. try {
  80. body = JSON.parse(body);
  81. } catch(e) {
  82. console.error(e);
  83. }
  84. }
  85. resolve({
  86. status: res.statusCode,
  87. headers: res.headers,
  88. data: body
  89. });
  90. });
  91. });
  92. req.on('error', reject);
  93. req.write(data);
  94. req.end();
  95. });
  96. },
  97. };