particle-api-js 9.4.0 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.circleci/config.yml +7 -5
- package/.eslintrc.js +3 -3
- package/CHANGELOG.md +8 -0
- package/RELEASE.md +1 -1
- package/dist/particle.min.js +227 -192
- package/dist/particle.min.js.map +1 -1
- package/docs/api.md +561 -103
- package/fs.js +2 -0
- package/lib/Agent.js +320 -221
- package/lib/Agent.js.map +1 -1
- package/lib/Particle.js +645 -62
- package/lib/Particle.js.map +1 -1
- package/package.json +10 -10
- package/test/Agent.integration.js +3 -1
- package/test/Agent.spec.js +173 -289
- package/test/EventStream.spec.js +1 -1
- package/test/Particle.integration.js +5 -4
- package/test/Particle.spec.js +325 -11
- package/lib/superagent-binary-parser.js +0 -20
- package/lib/superagent-binary-parser.js.map +0 -1
- /package/{test/EventStream-e2e-browser.html → EventStream-e2e-browser.html} +0 -0
- /package/{test/EventStream-e2e-node.js → EventStream-e2e-node.js} +0 -0
package/lib/Agent.js
CHANGED
|
@@ -20,13 +20,17 @@ var _getIterator2 = require('babel-runtime/core-js/get-iterator');
|
|
|
20
20
|
|
|
21
21
|
var _getIterator3 = _interopRequireDefault(_getIterator2);
|
|
22
22
|
|
|
23
|
+
var _stringify = require('babel-runtime/core-js/json/stringify');
|
|
24
|
+
|
|
25
|
+
var _stringify2 = _interopRequireDefault(_stringify);
|
|
26
|
+
|
|
23
27
|
var _assign = require('babel-runtime/core-js/object/assign');
|
|
24
28
|
|
|
25
29
|
var _assign2 = _interopRequireDefault(_assign);
|
|
26
30
|
|
|
27
|
-
var
|
|
31
|
+
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
|
|
28
32
|
|
|
29
|
-
var
|
|
33
|
+
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
|
|
30
34
|
|
|
31
35
|
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
|
32
36
|
|
|
@@ -36,33 +40,49 @@ var _createClass2 = require('babel-runtime/helpers/createClass');
|
|
|
36
40
|
|
|
37
41
|
var _createClass3 = _interopRequireDefault(_createClass2);
|
|
38
42
|
|
|
39
|
-
var
|
|
43
|
+
var _nodeFetch = require('node-fetch');
|
|
40
44
|
|
|
41
|
-
var
|
|
45
|
+
var _nodeFetch2 = _interopRequireDefault(_nodeFetch);
|
|
42
46
|
|
|
43
|
-
var
|
|
47
|
+
var _formData = require('form-data');
|
|
44
48
|
|
|
45
|
-
var
|
|
49
|
+
var _formData2 = _interopRequireDefault(_formData);
|
|
46
50
|
|
|
47
|
-
|
|
51
|
+
var _qs = require('qs');
|
|
52
|
+
|
|
53
|
+
var _qs2 = _interopRequireDefault(_qs);
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
******************************************************************************
|
|
51
|
-
Copyright (c) 2016 Particle Industries, Inc. All rights reserved.
|
|
55
|
+
var _fs = require('../fs');
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
modify it under the terms of the GNU Lesser General Public
|
|
55
|
-
License as published by the Free Software Foundation, either
|
|
56
|
-
version 3 of the License, or (at your option) any later version.
|
|
57
|
+
var _fs2 = _interopRequireDefault(_fs);
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
60
|
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
61
|
-
Lesser General Public License for more details.
|
|
59
|
+
var _package = require('../package.json');
|
|
62
60
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
var _package2 = _interopRequireDefault(_package);
|
|
62
|
+
|
|
63
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The object returned for a basic request
|
|
67
|
+
* @typedef {object} JSONResponse
|
|
68
|
+
* @property {number} statusCode The HTTP response status
|
|
69
|
+
* @property {object} body The endpoint's response parsed as a JSON
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The possible response from an API request
|
|
74
|
+
* @typedef {JSONResponse|Buffer|arrayBuffer} RequestResponse The type is based on
|
|
75
|
+
* the request config and whether is on browser or node
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The error object generated in case of a failed request
|
|
80
|
+
* @typedef {object} RequestError
|
|
81
|
+
* @property {number} statusCode The HTTP response status
|
|
82
|
+
* @property {string} errorDescription Details on what caused the failed request
|
|
83
|
+
* @property {string} shortErrorDescription Summarized version of the fail reason
|
|
84
|
+
* @property {object} body The response object from the request
|
|
85
|
+
* @property {object} error The error object from the request
|
|
66
86
|
*/
|
|
67
87
|
|
|
68
88
|
var Agent = function () {
|
|
@@ -75,8 +95,19 @@ var Agent = function () {
|
|
|
75
95
|
(0, _createClass3.default)(Agent, [{
|
|
76
96
|
key: 'setBaseUrl',
|
|
77
97
|
value: function setBaseUrl(baseUrl) {
|
|
78
|
-
this.
|
|
98
|
+
this.baseUrl = baseUrl;
|
|
79
99
|
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Make a GET request
|
|
103
|
+
* @param {string} uri The URI to request
|
|
104
|
+
* @param {string} [auth] Authorization token to use
|
|
105
|
+
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
|
|
106
|
+
* @param {object} [query] Key/Value pairs of query params
|
|
107
|
+
* @param {object} [context] The invocation context, describing the tool and project
|
|
108
|
+
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
|
|
109
|
+
*/
|
|
110
|
+
|
|
80
111
|
}, {
|
|
81
112
|
key: 'get',
|
|
82
113
|
value: function get(_ref) {
|
|
@@ -88,6 +119,17 @@ var Agent = function () {
|
|
|
88
119
|
|
|
89
120
|
return this.request({ uri: uri, method: 'get', auth: auth, headers: headers, query: query, context: context });
|
|
90
121
|
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Make a HEAD request
|
|
125
|
+
* @param {string} uri The URI to request
|
|
126
|
+
* @param {string} [auth] Authorization token to use
|
|
127
|
+
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
|
|
128
|
+
* @param {object} [query] Key/Value pairs of query params
|
|
129
|
+
* @param {object} [context] The invocation context, describing the tool and project
|
|
130
|
+
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
|
|
131
|
+
*/
|
|
132
|
+
|
|
91
133
|
}, {
|
|
92
134
|
key: 'head',
|
|
93
135
|
value: function head(_ref2) {
|
|
@@ -99,6 +141,17 @@ var Agent = function () {
|
|
|
99
141
|
|
|
100
142
|
return this.request({ uri: uri, method: 'head', auth: auth, headers: headers, query: query, context: context });
|
|
101
143
|
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Make a POST request
|
|
147
|
+
* @param {string} uri The URI to request
|
|
148
|
+
* @param {string} [auth] Authorization token to use
|
|
149
|
+
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
|
|
150
|
+
* @param {object} [data] Payload to send in the request body in JSON format
|
|
151
|
+
* @param {object} [context] The invocation context, describing the tool and project
|
|
152
|
+
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
|
|
153
|
+
*/
|
|
154
|
+
|
|
102
155
|
}, {
|
|
103
156
|
key: 'post',
|
|
104
157
|
value: function post(_ref3) {
|
|
@@ -110,6 +163,17 @@ var Agent = function () {
|
|
|
110
163
|
|
|
111
164
|
return this.request({ uri: uri, method: 'post', auth: auth, headers: headers, data: data, context: context });
|
|
112
165
|
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Make a PUT request
|
|
169
|
+
* @param {string} uri The URI to request
|
|
170
|
+
* @param {string} [auth] Authorization token to use
|
|
171
|
+
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
|
|
172
|
+
* @param {object} [data] Payload to send in the request body in JSON format
|
|
173
|
+
* @param {object} [context] The invocation context, describing the tool and project
|
|
174
|
+
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
|
|
175
|
+
*/
|
|
176
|
+
|
|
113
177
|
}, {
|
|
114
178
|
key: 'put',
|
|
115
179
|
value: function put(_ref4) {
|
|
@@ -121,6 +185,17 @@ var Agent = function () {
|
|
|
121
185
|
|
|
122
186
|
return this.request({ uri: uri, method: 'put', auth: auth, headers: headers, data: data, context: context });
|
|
123
187
|
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Make a DELETE request
|
|
191
|
+
* @param {string} uri The URI to request
|
|
192
|
+
* @param {string} [auth] Authorization token to use
|
|
193
|
+
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
|
|
194
|
+
* @param {object} [data] Payload to send in the request body in JSON format
|
|
195
|
+
* @param {object} [context] The invocation context, describing the tool and project
|
|
196
|
+
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
|
|
197
|
+
*/
|
|
198
|
+
|
|
124
199
|
}, {
|
|
125
200
|
key: 'delete',
|
|
126
201
|
value: function _delete(_ref5) {
|
|
@@ -135,16 +210,18 @@ var Agent = function () {
|
|
|
135
210
|
|
|
136
211
|
/**
|
|
137
212
|
*
|
|
138
|
-
* @param {
|
|
139
|
-
* @param {String}
|
|
140
|
-
* @param {
|
|
141
|
-
* @param {
|
|
142
|
-
* @param {Object}
|
|
143
|
-
* @param {
|
|
144
|
-
* @param {
|
|
145
|
-
* @param {Object}
|
|
146
|
-
* @
|
|
147
|
-
* @
|
|
213
|
+
* @param {Object} config An obj with all the possible request configurations
|
|
214
|
+
* @param {String} config.uri The URI to request
|
|
215
|
+
* @param {String} config.method The method used to request the URI, should be in uppercase.
|
|
216
|
+
* @param {Object} config.headers Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
|
|
217
|
+
* @param {Object} config.data Arbitrary data to send as the body.
|
|
218
|
+
* @param {Object} config.auth Authorization
|
|
219
|
+
* @param {String} config.query Query parameters
|
|
220
|
+
* @param {Object} config.form Form fields
|
|
221
|
+
* @param {Object} config.files array of file names and file content
|
|
222
|
+
* @param {Object} config.context the invocation context, describing the tool and project.
|
|
223
|
+
* @param {boolean} config.isBuffer Indicate if the response should be treated as Buffer instead of JSON
|
|
224
|
+
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
|
|
148
225
|
*/
|
|
149
226
|
|
|
150
227
|
}, {
|
|
@@ -165,30 +242,85 @@ var Agent = function () {
|
|
|
165
242
|
files = _ref6$files === undefined ? undefined : _ref6$files,
|
|
166
243
|
_ref6$context = _ref6.context,
|
|
167
244
|
context = _ref6$context === undefined ? undefined : _ref6$context,
|
|
168
|
-
_ref6$
|
|
169
|
-
|
|
245
|
+
_ref6$isBuffer = _ref6.isBuffer,
|
|
246
|
+
isBuffer = _ref6$isBuffer === undefined ? false : _ref6$isBuffer;
|
|
170
247
|
|
|
171
248
|
var requestFiles = this._sanitizeFiles(files);
|
|
172
|
-
|
|
249
|
+
var requestParams = this._buildRequest({ uri: uri, method: method, headers: headers, data: data, auth: auth, query: query, form: form, context: context, files: requestFiles });
|
|
250
|
+
return this._promiseResponse(requestParams, isBuffer);
|
|
173
251
|
}
|
|
174
252
|
|
|
175
253
|
/**
|
|
176
|
-
*
|
|
177
|
-
* @param {
|
|
178
|
-
* @param {
|
|
179
|
-
* @param {
|
|
180
|
-
* @
|
|
181
|
-
* @
|
|
182
|
-
* @param {String} query Query parameters
|
|
183
|
-
* @param {Object} form Form fields
|
|
184
|
-
* @param {Object} files array of file names and file content
|
|
185
|
-
* @param {Object} context the invocation context
|
|
186
|
-
* @return {Promise} A promise. fulfilled with {body, statusCode}, rejected with { statusCode, errorDescription, error, body }
|
|
254
|
+
* Promises to send the request and retreive the response.
|
|
255
|
+
* @param {[string, object]} requestParams First argument is the URI to request, the second one are the options.
|
|
256
|
+
* @param {boolean} isBuffer Indicate if the response body should be returned as a Buffer (Node) / ArrayBuffer (browser) instead of JSON
|
|
257
|
+
* @param {Fetch} [makerequest] The fetch function to use. Override for testing.
|
|
258
|
+
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
|
|
259
|
+
* @private
|
|
187
260
|
*/
|
|
188
261
|
|
|
189
262
|
}, {
|
|
190
|
-
key: '
|
|
191
|
-
value: function
|
|
263
|
+
key: '_promiseResponse',
|
|
264
|
+
value: function _promiseResponse(requestParams, isBuffer) {
|
|
265
|
+
var _this = this;
|
|
266
|
+
|
|
267
|
+
var makerequest = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _nodeFetch2.default;
|
|
268
|
+
|
|
269
|
+
var status = void 0;
|
|
270
|
+
return makerequest.apply(undefined, (0, _toConsumableArray3.default)(requestParams)).then(function (resp) {
|
|
271
|
+
status = resp.status;
|
|
272
|
+
if (!resp.ok) {
|
|
273
|
+
return resp.text().then(function (err) {
|
|
274
|
+
var objError = JSON.parse(err);
|
|
275
|
+
// particle-commnds/src/cmd/api expects response.text. to be a string
|
|
276
|
+
var response = (0, _assign2.default)(resp, { text: err });
|
|
277
|
+
throw (0, _assign2.default)(objError, { response: response });
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
if (status === 204) {
|
|
281
|
+
// Can't do resp.json() since there is no body to parse
|
|
282
|
+
return '';
|
|
283
|
+
}
|
|
284
|
+
if (isBuffer) {
|
|
285
|
+
return resp.blob();
|
|
286
|
+
}
|
|
287
|
+
return resp.json();
|
|
288
|
+
}).then(function (body) {
|
|
289
|
+
if (isBuffer) {
|
|
290
|
+
return body.arrayBuffer().then(function (arrayBuffer) {
|
|
291
|
+
if (!_this.isForBrowser()) {
|
|
292
|
+
return Buffer.from(arrayBuffer);
|
|
293
|
+
}
|
|
294
|
+
return arrayBuffer;
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
body: body,
|
|
299
|
+
statusCode: status
|
|
300
|
+
};
|
|
301
|
+
}).catch(function (error) {
|
|
302
|
+
var errorType = status ? 'HTTP error ' + status : 'Network error';
|
|
303
|
+
var errorDescription = errorType + ' from ' + requestParams[0];
|
|
304
|
+
var shortErrorDescription = void 0;
|
|
305
|
+
if (error.error_description) {
|
|
306
|
+
// Fetch responded with ok false
|
|
307
|
+
errorDescription = errorDescription + ' - ' + error.error_description;
|
|
308
|
+
shortErrorDescription = error.error_description;
|
|
309
|
+
}
|
|
310
|
+
var reason = new Error(errorDescription);
|
|
311
|
+
(0, _assign2.default)(reason, {
|
|
312
|
+
statusCode: status,
|
|
313
|
+
errorDescription: errorDescription,
|
|
314
|
+
shortErrorDescription: shortErrorDescription,
|
|
315
|
+
error: error,
|
|
316
|
+
body: error
|
|
317
|
+
});
|
|
318
|
+
throw reason;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}, {
|
|
322
|
+
key: '_buildRequest',
|
|
323
|
+
value: function _buildRequest(_ref7) {
|
|
192
324
|
var uri = _ref7.uri,
|
|
193
325
|
method = _ref7.method,
|
|
194
326
|
headers = _ref7.headers,
|
|
@@ -197,189 +329,132 @@ var Agent = function () {
|
|
|
197
329
|
query = _ref7.query,
|
|
198
330
|
form = _ref7.form,
|
|
199
331
|
files = _ref7.files,
|
|
200
|
-
context = _ref7.context
|
|
201
|
-
raw = _ref7.raw;
|
|
202
|
-
|
|
203
|
-
var req = this._buildRequest({ uri: uri, method: method, headers: headers, data: data, auth: auth, query: query, form: form, context: context, files: files });
|
|
332
|
+
context = _ref7.context;
|
|
204
333
|
|
|
205
|
-
|
|
206
|
-
|
|
334
|
+
var actualUri = uri;
|
|
335
|
+
if (this.baseUrl && uri[0] === '/') {
|
|
336
|
+
actualUri = '' + this.baseUrl + uri;
|
|
337
|
+
}
|
|
338
|
+
if (query) {
|
|
339
|
+
var queryParams = _qs2.default.stringify(query);
|
|
340
|
+
var hasParams = actualUri.includes('?');
|
|
341
|
+
actualUri = '' + actualUri + (hasParams ? '&' : '?') + queryParams;
|
|
207
342
|
}
|
|
208
|
-
return this._promiseResponse(req);
|
|
209
|
-
}
|
|
210
343
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
344
|
+
var userAgentHeader = { 'User-Agent': _package2.default.name + '/' + _package2.default.version + ' (' + _package2.default.repository.url + ')' };
|
|
345
|
+
var body = void 0;
|
|
346
|
+
var contentTypeHeader = void 0;
|
|
347
|
+
if (files) {
|
|
348
|
+
contentTypeHeader = {}; // Needed to allow fetch create its own
|
|
349
|
+
body = this._getFromData(files, form);
|
|
350
|
+
} else if (form) {
|
|
351
|
+
contentTypeHeader = { 'Content-Type': 'application/x-www-form-urlencoded' };
|
|
352
|
+
body = _qs2.default.stringify(form);
|
|
353
|
+
} else if (data) {
|
|
354
|
+
contentTypeHeader = { 'Content-Type': 'application/json' };
|
|
355
|
+
body = (0, _stringify2.default)(data);
|
|
356
|
+
}
|
|
357
|
+
var finalHeaders = (0, _assign2.default)({}, userAgentHeader, contentTypeHeader, this._getAuthorizationHeader(auth), this._getContextHeaders(context), headers);
|
|
217
358
|
|
|
359
|
+
return [actualUri, { method: method, body: body, headers: finalHeaders }];
|
|
360
|
+
}
|
|
218
361
|
}, {
|
|
219
|
-
key: '
|
|
220
|
-
value: function
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
return new _promise2.default(function (fulfill, reject) {
|
|
224
|
-
return _this._sendRequest(req, fulfill, reject);
|
|
225
|
-
});
|
|
362
|
+
key: 'isForBrowser',
|
|
363
|
+
value: function isForBrowser() {
|
|
364
|
+
return typeof window !== 'undefined';
|
|
226
365
|
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Sends the given request, calling the fulfill or reject methods for success/failure.
|
|
230
|
-
* @param {object} request The request to send
|
|
231
|
-
* @param {function} fulfill Called on success with the response
|
|
232
|
-
* @param {function} reject Called on failure with the failure reason.
|
|
233
|
-
* @private
|
|
234
|
-
* @returns {undefined} Nothing
|
|
235
|
-
*/
|
|
236
|
-
|
|
237
366
|
}, {
|
|
238
|
-
key: '
|
|
239
|
-
value: function
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
367
|
+
key: '_getFromData',
|
|
368
|
+
value: function _getFromData(files, form) {
|
|
369
|
+
var formData = new _formData2.default();
|
|
370
|
+
var _iteratorNormalCompletion = true;
|
|
371
|
+
var _didIteratorError = false;
|
|
372
|
+
var _iteratorError = undefined;
|
|
373
|
+
|
|
374
|
+
try {
|
|
375
|
+
for (var _iterator = (0, _getIterator3.default)((0, _entries2.default)(files)), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
376
|
+
var _step$value = (0, _slicedToArray3.default)(_step.value, 2),
|
|
377
|
+
name = _step$value[0],
|
|
378
|
+
file = _step$value[1];
|
|
379
|
+
|
|
380
|
+
var path = file.path;
|
|
381
|
+
var fileData = file.data;
|
|
382
|
+
if (!this.isForBrowser()) {
|
|
383
|
+
var nodeFormData = this._getNodeFormData(file);
|
|
384
|
+
path = nodeFormData.path;
|
|
385
|
+
fileData = nodeFormData.file;
|
|
250
386
|
}
|
|
251
|
-
|
|
252
|
-
(0, _assign2.default)(reason, { statusCode: statusCode, errorDescription: errorDescription, shortErrorDescription: shortErrorDescription, error: error, body: body });
|
|
253
|
-
reject(reason);
|
|
254
|
-
} else {
|
|
255
|
-
fulfill({
|
|
256
|
-
body: body,
|
|
257
|
-
statusCode: res.statusCode
|
|
258
|
-
});
|
|
387
|
+
formData.append(name, fileData, path);
|
|
259
388
|
}
|
|
260
|
-
})
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
value: function _buildRequest(_ref8) {
|
|
265
|
-
var uri = _ref8.uri,
|
|
266
|
-
method = _ref8.method,
|
|
267
|
-
headers = _ref8.headers,
|
|
268
|
-
data = _ref8.data,
|
|
269
|
-
auth = _ref8.auth,
|
|
270
|
-
query = _ref8.query,
|
|
271
|
-
form = _ref8.form,
|
|
272
|
-
files = _ref8.files,
|
|
273
|
-
context = _ref8.context,
|
|
274
|
-
_ref8$makerequest = _ref8.makerequest,
|
|
275
|
-
makerequest = _ref8$makerequest === undefined ? _superagent2.default : _ref8$makerequest;
|
|
276
|
-
|
|
277
|
-
var req = makerequest(method, uri);
|
|
278
|
-
if (this.prefix) {
|
|
279
|
-
req.use(this.prefix);
|
|
280
|
-
}
|
|
281
|
-
this._authorizationHeader(req, auth);
|
|
282
|
-
if (context) {
|
|
283
|
-
this._applyContext(req, context);
|
|
284
|
-
}
|
|
285
|
-
if (query) {
|
|
286
|
-
req.query(query);
|
|
287
|
-
}
|
|
288
|
-
if (headers) {
|
|
289
|
-
req.set(headers);
|
|
290
|
-
}
|
|
291
|
-
if (files) {
|
|
292
|
-
var _iteratorNormalCompletion = true;
|
|
293
|
-
var _didIteratorError = false;
|
|
294
|
-
var _iteratorError = undefined;
|
|
295
|
-
|
|
389
|
+
} catch (err) {
|
|
390
|
+
_didIteratorError = true;
|
|
391
|
+
_iteratorError = err;
|
|
392
|
+
} finally {
|
|
296
393
|
try {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
name = _step$value[0],
|
|
300
|
-
file = _step$value[1];
|
|
301
|
-
|
|
302
|
-
// API for Form Data is different in Node and in browser
|
|
303
|
-
var options = {
|
|
304
|
-
filepath: file.path
|
|
305
|
-
};
|
|
306
|
-
if (this.isForBrowser(makerequest)) {
|
|
307
|
-
options = file.path;
|
|
308
|
-
}
|
|
309
|
-
req.attach(name, file.data, options);
|
|
394
|
+
if (!_iteratorNormalCompletion && _iterator.return) {
|
|
395
|
+
_iterator.return();
|
|
310
396
|
}
|
|
311
|
-
} catch (err) {
|
|
312
|
-
_didIteratorError = true;
|
|
313
|
-
_iteratorError = err;
|
|
314
397
|
} finally {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
_iterator.return();
|
|
318
|
-
}
|
|
319
|
-
} finally {
|
|
320
|
-
if (_didIteratorError) {
|
|
321
|
-
throw _iteratorError;
|
|
322
|
-
}
|
|
398
|
+
if (_didIteratorError) {
|
|
399
|
+
throw _iteratorError;
|
|
323
400
|
}
|
|
324
401
|
}
|
|
402
|
+
}
|
|
325
403
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
404
|
+
if (form) {
|
|
405
|
+
var _iteratorNormalCompletion2 = true;
|
|
406
|
+
var _didIteratorError2 = false;
|
|
407
|
+
var _iteratorError2 = undefined;
|
|
330
408
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
409
|
+
try {
|
|
410
|
+
for (var _iterator2 = (0, _getIterator3.default)((0, _entries2.default)(form)), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
|
411
|
+
var _step2$value = (0, _slicedToArray3.default)(_step2.value, 2),
|
|
412
|
+
name = _step2$value[0],
|
|
413
|
+
value = _step2$value[1];
|
|
336
414
|
|
|
337
|
-
|
|
415
|
+
formData.append(name, value);
|
|
416
|
+
}
|
|
417
|
+
} catch (err) {
|
|
418
|
+
_didIteratorError2 = true;
|
|
419
|
+
_iteratorError2 = err;
|
|
420
|
+
} finally {
|
|
421
|
+
try {
|
|
422
|
+
if (!_iteratorNormalCompletion2 && _iterator2.return) {
|
|
423
|
+
_iterator2.return();
|
|
338
424
|
}
|
|
339
|
-
} catch (err) {
|
|
340
|
-
_didIteratorError2 = true;
|
|
341
|
-
_iteratorError2 = err;
|
|
342
425
|
} finally {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
_iterator2.return();
|
|
346
|
-
}
|
|
347
|
-
} finally {
|
|
348
|
-
if (_didIteratorError2) {
|
|
349
|
-
throw _iteratorError2;
|
|
350
|
-
}
|
|
426
|
+
if (_didIteratorError2) {
|
|
427
|
+
throw _iteratorError2;
|
|
351
428
|
}
|
|
352
429
|
}
|
|
353
430
|
}
|
|
354
|
-
} else if (form) {
|
|
355
|
-
req.type('form');
|
|
356
|
-
req.send(form);
|
|
357
|
-
} else if (data) {
|
|
358
|
-
req.send(data);
|
|
359
431
|
}
|
|
360
|
-
return
|
|
432
|
+
return formData;
|
|
361
433
|
}
|
|
362
434
|
}, {
|
|
363
|
-
key: '
|
|
364
|
-
value: function
|
|
365
|
-
var
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
435
|
+
key: '_getNodeFormData',
|
|
436
|
+
value: function _getNodeFormData(file) {
|
|
437
|
+
var fileData = file.data;
|
|
438
|
+
if (typeof file.data === 'string') {
|
|
439
|
+
fileData = _fs2.default.createReadStream(file.data);
|
|
440
|
+
}
|
|
441
|
+
return {
|
|
442
|
+
file: fileData,
|
|
443
|
+
path: { filepath: file.path // Different API for nodejs
|
|
444
|
+
} };
|
|
369
445
|
}
|
|
370
446
|
}, {
|
|
371
|
-
key: '
|
|
372
|
-
value: function
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
376
|
-
if (context.project) {
|
|
377
|
-
this._addProjectContext(req, context.project);
|
|
378
|
-
}
|
|
447
|
+
key: '_getContextHeaders',
|
|
448
|
+
value: function _getContextHeaders() {
|
|
449
|
+
var context = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
450
|
+
|
|
451
|
+
return (0, _assign2.default)({}, this._getToolContext(context.tool), this._getProjectContext(context.project));
|
|
379
452
|
}
|
|
380
453
|
}, {
|
|
381
|
-
key: '
|
|
382
|
-
value: function
|
|
454
|
+
key: '_getToolContext',
|
|
455
|
+
value: function _getToolContext() {
|
|
456
|
+
var tool = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
457
|
+
|
|
383
458
|
var value = '';
|
|
384
459
|
if (tool.name) {
|
|
385
460
|
value += this._toolIdent(tool);
|
|
@@ -411,8 +486,9 @@ var Agent = function () {
|
|
|
411
486
|
}
|
|
412
487
|
}
|
|
413
488
|
if (value) {
|
|
414
|
-
|
|
489
|
+
return { 'X-Particle-Tool': value };
|
|
415
490
|
}
|
|
491
|
+
return {};
|
|
416
492
|
}
|
|
417
493
|
}, {
|
|
418
494
|
key: '_toolIdent',
|
|
@@ -432,12 +508,15 @@ var Agent = function () {
|
|
|
432
508
|
return value;
|
|
433
509
|
}
|
|
434
510
|
}, {
|
|
435
|
-
key: '
|
|
436
|
-
value: function
|
|
511
|
+
key: '_getProjectContext',
|
|
512
|
+
value: function _getProjectContext() {
|
|
513
|
+
var project = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
514
|
+
|
|
437
515
|
var value = this._buildSemicolonSeparatedProperties(project, 'name');
|
|
438
516
|
if (value) {
|
|
439
|
-
|
|
517
|
+
return { 'X-Particle-Project': value };
|
|
440
518
|
}
|
|
519
|
+
return {};
|
|
441
520
|
}
|
|
442
521
|
|
|
443
522
|
/**
|
|
@@ -466,28 +545,31 @@ var Agent = function () {
|
|
|
466
545
|
|
|
467
546
|
/**
|
|
468
547
|
* Adds an authorization header.
|
|
469
|
-
* @param {
|
|
470
|
-
* @
|
|
471
|
-
* or a username/password object.
|
|
472
|
-
* @returns {Request} req The original request.
|
|
548
|
+
* @param {string} auth The authorization bearer token.
|
|
549
|
+
* @returns {object} The original request.
|
|
473
550
|
*/
|
|
474
551
|
|
|
475
552
|
}, {
|
|
476
|
-
key: '
|
|
477
|
-
value: function
|
|
478
|
-
if (auth) {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
553
|
+
key: '_getAuthorizationHeader',
|
|
554
|
+
value: function _getAuthorizationHeader(auth) {
|
|
555
|
+
if (!auth) {
|
|
556
|
+
return {};
|
|
557
|
+
}
|
|
558
|
+
if (typeof auth === 'string') {
|
|
559
|
+
return { Authorization: 'Bearer ' + auth };
|
|
560
|
+
}
|
|
561
|
+
var encoded = void 0;
|
|
562
|
+
if (this.isForBrowser()) {
|
|
563
|
+
encoded = btoa(auth.username + ':' + auth.password);
|
|
564
|
+
} else {
|
|
565
|
+
encoded = Buffer.from(auth.username + ':' + auth.password).toString('base64');
|
|
484
566
|
}
|
|
485
|
-
return
|
|
567
|
+
return { Authorization: 'Basic ' + encoded };
|
|
486
568
|
}
|
|
487
569
|
|
|
488
570
|
/**
|
|
489
571
|
*
|
|
490
|
-
* @param {
|
|
572
|
+
* @param {Object} files converts the file names to file, file1, file2.
|
|
491
573
|
* @returns {object} the renamed files.
|
|
492
574
|
*/
|
|
493
575
|
|
|
@@ -509,7 +591,24 @@ var Agent = function () {
|
|
|
509
591
|
}
|
|
510
592
|
}]);
|
|
511
593
|
return Agent;
|
|
512
|
-
}();
|
|
594
|
+
}(); /*
|
|
595
|
+
******************************************************************************
|
|
596
|
+
Copyright (c) 2016 Particle Industries, Inc. All rights reserved.
|
|
597
|
+
|
|
598
|
+
This program is free software; you can redistribute it and/or
|
|
599
|
+
modify it under the terms of the GNU Lesser General Public
|
|
600
|
+
License as published by the Free Software Foundation, either
|
|
601
|
+
version 3 of the License, or (at your option) any later version.
|
|
602
|
+
|
|
603
|
+
This program is distributed in the hope that it will be useful,
|
|
604
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
605
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
606
|
+
Lesser General Public License for more details.
|
|
607
|
+
|
|
608
|
+
You should have received a copy of the GNU Lesser General Public
|
|
609
|
+
License along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
610
|
+
******************************************************************************
|
|
611
|
+
*/
|
|
513
612
|
|
|
514
613
|
exports.default = Agent;
|
|
515
614
|
module.exports = exports['default'];
|