UNPKG

6.2 kBJavaScriptView Raw
1'use strict';
2var __assign = (this && this.__assign) || Object.assign || function(t) {
3 for (var s, i = 1, n = arguments.length; i < n; i++) {
4 s = arguments[i];
5 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6 t[p] = s[p];
7 }
8 return t;
9};
10exports.__esModule = true;
11var GenericResponse = require("http-response-object");
12var Promise = require("promise");
13var concat = require("concat-stream");
14var ResponsePromise_1 = require("./ResponsePromise");
15exports.ResponsePromise = ResponsePromise_1.ResponsePromise;
16var handle_qs_1 = require("./handle-qs");
17var http_basic_1 = require("http-basic");
18var FormData = require("form-data");
19exports.FormData = FormData;
20var caseless = require('caseless');
21var basicRequest = http_basic_1["default"];
22var BufferBody = /** @class */ (function () {
23 function BufferBody(body, extraHeaders) {
24 this._body = body;
25 this._headers = extraHeaders;
26 }
27 BufferBody.prototype.getHeaders = function () {
28 return Promise.resolve(__assign({ 'content-length': '' + this._body.length }, this._headers));
29 };
30 BufferBody.prototype.pipe = function (stream) {
31 stream.end(this._body);
32 };
33 return BufferBody;
34}());
35var FormBody = /** @class */ (function () {
36 function FormBody(body) {
37 this._body = body;
38 }
39 FormBody.prototype.getHeaders = function () {
40 var _this = this;
41 var headers = this._body.getHeaders();
42 return new Promise(function (resolve, reject) {
43 var gotLength = false;
44 _this._body.getLength(function (err, length) {
45 if (gotLength)
46 return;
47 gotLength = true;
48 if (err) {
49 return reject(typeof err == 'string'
50 ? new Error(err)
51 : err);
52 }
53 headers['content-length'] = '' + length;
54 resolve(headers);
55 });
56 });
57 };
58 FormBody.prototype.pipe = function (stream) {
59 this._body.pipe(stream);
60 };
61 return FormBody;
62}());
63var StreamBody = /** @class */ (function () {
64 function StreamBody(body) {
65 this._body = body;
66 }
67 StreamBody.prototype.getHeaders = function () {
68 return Promise.resolve({});
69 };
70 StreamBody.prototype.pipe = function (stream) {
71 this._body.pipe(stream);
72 };
73 return StreamBody;
74}());
75function handleBody(options) {
76 if (options.form) {
77 return new FormBody(options.form);
78 }
79 var extraHeaders = {};
80 var body = options.body;
81 if (options.json) {
82 extraHeaders['content-type'] = 'application/json';
83 body = JSON.stringify(options.json);
84 }
85 if (typeof body === 'string') {
86 body = Buffer.from(body);
87 }
88 if (!body) {
89 body = Buffer.alloc(0);
90 }
91 if (!Buffer.isBuffer(body)) {
92 if (typeof body.pipe === 'function') {
93 return new StreamBody(body);
94 }
95 throw new TypeError('body should be a Buffer or a String');
96 }
97 return new BufferBody(body, extraHeaders);
98}
99function request(method, url, options) {
100 if (options === void 0) { options = {}; }
101 return ResponsePromise_1["default"](new Promise(function (resolve, reject) {
102 // check types of arguments
103 if (typeof method !== 'string') {
104 throw new TypeError('The method must be a string.');
105 }
106 if (typeof url !== 'string') {
107 throw new TypeError('The URL/path must be a string.');
108 }
109 if (options == null) {
110 options = {};
111 }
112 if (typeof options !== 'object') {
113 throw new TypeError('Options must be an object (or null).');
114 }
115 method = method.toUpperCase();
116 options.headers = options.headers || {};
117 var headers = caseless(options.headers);
118 // handle query string
119 if (options.qs) {
120 url = handle_qs_1["default"](url, options.qs);
121 }
122 var duplex = !(method === 'GET' || method === 'DELETE' || method === 'HEAD');
123 if (duplex) {
124 var body_1 = handleBody(options);
125 body_1.getHeaders().then(function (bodyHeaders) {
126 Object.keys(bodyHeaders).forEach(function (key) {
127 if (!headers.has(key)) {
128 headers.set(key, bodyHeaders[key]);
129 }
130 });
131 ready(body_1);
132 })["catch"](reject);
133 }
134 else if (options.body) {
135 throw new Error('You cannot pass a body to a ' + method + ' request.');
136 }
137 else {
138 ready();
139 }
140 function ready(body) {
141 var req = basicRequest(method, url, {
142 allowRedirectHeaders: options.allowRedirectHeaders,
143 headers: options.headers,
144 followRedirects: options.followRedirects !== false,
145 maxRedirects: options.maxRedirects,
146 gzip: options.gzip !== false,
147 cache: options.cache,
148 agent: options.agent,
149 timeout: options.timeout,
150 socketTimeout: options.socketTimeout,
151 retry: options.retry,
152 retryDelay: options.retryDelay,
153 maxRetries: options.maxRetries,
154 isMatch: options.isMatch,
155 isExpired: options.isExpired,
156 canCache: options.canCache
157 }, function (err, res) {
158 if (err)
159 return reject(err);
160 if (!res)
161 return reject(new Error('No request was received'));
162 res.body.on('error', reject);
163 res.body.pipe(concat(function (body) {
164 resolve(new GenericResponse(res.statusCode, res.headers, Array.isArray(body) ? Buffer.alloc(0) : body, res.url));
165 }));
166 });
167 if (req && body) {
168 body.pipe(req);
169 }
170 }
171 }));
172}
173exports["default"] = request;
174module.exports = request;
175module.exports["default"] = request;
176module.exports.FormData = FormData;