telnyx 1.23.0 → 1.25.1
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/CHANGELOG.md +2 -0
- package/README.md +53 -8
- package/VERSION +1 -1
- package/lib/TelnyxResource.js +134 -96
- package/lib/makeRequest.js +46 -14
- package/lib/resources/AccessIpAddress.js +6 -0
- package/lib/resources/AccessIpRanges.js +6 -0
- package/lib/resources/ActivateDeactivateBulkCredentials.js +20 -0
- package/lib/resources/Addresses.js +11 -16
- package/lib/resources/BillingGroups.js +2 -51
- package/lib/resources/BulkCreation.js +52 -0
- package/lib/resources/BulkTelephonyCredentials.js +46 -0
- package/lib/resources/Campaign.js +84 -0
- package/lib/resources/CampaignBuilder.js +39 -0
- package/lib/resources/ClientStateUpdate.js +6 -0
- package/lib/resources/CredentialConnections.js +2 -51
- package/lib/resources/DetailRecords.js +2 -2
- package/lib/resources/DocumentLinks.js +6 -0
- package/lib/resources/Documents.js +50 -0
- package/lib/resources/ManagedAccounts.js +57 -0
- package/lib/resources/MessagingHostedNumberOrders.js +1 -1
- package/lib/resources/MessagingHostedNumbers.js +2 -2
- package/lib/resources/MessagingProfiles.js +9 -7
- package/lib/resources/Notifications.js +0 -0
- package/lib/resources/NumberPortouts.js +71 -0
- package/lib/resources/PhoneNumberAssignmentByProfile.js +55 -0
- package/lib/resources/PortoutRequests.js +26 -2
- package/lib/resources/ProgrammableFaxCommands.js +59 -0
- package/lib/resources/RegisterCall.js +6 -0
- package/lib/resources/ReportsMdrs.js +6 -0
- package/lib/resources/RoomCompositions.js +8 -0
- package/lib/resources/RoomParticipants.js +8 -0
- package/lib/resources/RoomSessions.js +43 -0
- package/lib/resources/Rooms.js +6 -0
- package/lib/resources/RoomsClientToken.js +18 -0
- package/lib/resources/SimCardActions.js +6 -0
- package/lib/resources/SimCardOrders.js +6 -0
- package/lib/resources/SimCards.js +105 -110
- package/lib/resources/TeXMLApplication.js +6 -0
- package/lib/resources/UpdateClientState.js +22 -0
- package/lib/resources/VerifiedCallsDisplayProfiles.js +13 -0
- package/lib/resources/VerifiedNumbers.js +6 -0
- package/lib/resources/Webhooks.js +6 -0
- package/lib/resources/WhatsAppBusinessAccount.js +37 -0
- package/lib/resources/WhatsAppContacts.js +6 -0
- package/lib/resources/WhatsAppMessages.js +6 -0
- package/lib/resources/WhatsappMedia.js +34 -0
- package/lib/telnyx.js +70 -35
- package/lib/utils.js +109 -59
- package/package.json +66 -64
- package/npm-shrinkwrap.json +0 -5422
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -180,6 +180,29 @@ const event = telnyx.webhooks.constructEvent(
|
|
|
180
180
|
);
|
|
181
181
|
```
|
|
182
182
|
|
|
183
|
+
#### TeXML Signature
|
|
184
|
+
|
|
185
|
+
TeXML sends webhooks as form-encoded payloads instead of JSON. To validate the signature, use the `telnyx.webhooks.signature.verifySignature` method.
|
|
186
|
+
|
|
187
|
+
You can find an example of how to use this with [Express](https://expressjs.com/) in the [`examples/webhook-signing`](examples/webhook-signing) folder.
|
|
188
|
+
|
|
189
|
+
```js
|
|
190
|
+
const timeToleranceInSeconds = 300; // Will validate signatures of webhooks up to 5 minutes after Telnyx sent the request
|
|
191
|
+
try {
|
|
192
|
+
telnyx.webhooks.signature.verifySignature(
|
|
193
|
+
webhookRawBody,
|
|
194
|
+
webhookTelnyxSignatureHeader,
|
|
195
|
+
webhookTelnyxTimestampHeader,
|
|
196
|
+
publicKey,
|
|
197
|
+
timeToleranceInSeconds
|
|
198
|
+
);
|
|
199
|
+
} catch (e) {
|
|
200
|
+
console.log("Failed to validate the signature")
|
|
201
|
+
console.log(e);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
|
|
183
206
|
### Writing a Plugin
|
|
184
207
|
|
|
185
208
|
If you're writing a plugin that uses the library, we'd appreciate it if you identified using `telnyx.setAppInfo()`:
|
|
@@ -285,16 +308,38 @@ const allMessagingProfiles = await telnyx.messagingProfiles.list()
|
|
|
285
308
|
|
|
286
309
|
## Development
|
|
287
310
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
311
|
+
### Setup
|
|
312
|
+
The test suite depends on the [Prism Mock Server](https://github.com/stoplightio/prism).
|
|
313
|
+
|
|
314
|
+
```bash
|
|
315
|
+
npm install -g @stoplight/prism-cli
|
|
316
|
+
|
|
317
|
+
# OR
|
|
293
318
|
|
|
294
|
-
|
|
295
|
-
|
|
319
|
+
yarn global add @stoplight/prism-cli
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
Once installed, start the prism mock service with the following command:
|
|
323
|
+
|
|
324
|
+
```bash
|
|
325
|
+
prism mock https://raw.githubusercontent.com/team-telnyx/openapi/master/openapi/spec3.json
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
--------
|
|
329
|
+
|
|
330
|
+
One final step -- because the Node SDK originally expected to reach the legacy `telnyx-mock` service at port 12111 (in addition to providing a `/v2/` base path), we need to setup the [Telnyx mock proxy server](https://github.com/team-telnyx/telnyx-mock-server-proxy) to modify the request path and forward along to the prism mock server.
|
|
331
|
+
|
|
332
|
+
```bash
|
|
333
|
+
# In new terminal window
|
|
334
|
+
|
|
335
|
+
git clone git@github.com:team-telnyx/telnyx-prism-mock.git
|
|
336
|
+
cd telnyx-prism-mock/proxy
|
|
337
|
+
|
|
338
|
+
yarn install
|
|
339
|
+
node index.js
|
|
340
|
+
```
|
|
296
341
|
|
|
297
|
-
|
|
342
|
+
### Running Tests
|
|
298
343
|
|
|
299
344
|
```bash
|
|
300
345
|
$ npm install
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.
|
|
1
|
+
1.24.0
|
package/lib/TelnyxResource.js
CHANGED
|
@@ -25,19 +25,23 @@ function TelnyxResource(telnyx, urlData) {
|
|
|
25
25
|
this._telnyx = telnyx;
|
|
26
26
|
this._urlData = urlData || {};
|
|
27
27
|
|
|
28
|
-
this.basePath = utils.makeURLInterpolator(
|
|
28
|
+
this.basePath = utils.makeURLInterpolator(
|
|
29
|
+
this.basePath || telnyx.getApiField('basePath')
|
|
30
|
+
);
|
|
29
31
|
this.resourcePath = this.path;
|
|
30
32
|
this.path = utils.makeURLInterpolator(this.path);
|
|
31
33
|
|
|
32
34
|
if (this.includeBasic) {
|
|
33
|
-
this.includeBasic.forEach(function(methodName) {
|
|
35
|
+
this.includeBasic.forEach(function (methodName) {
|
|
34
36
|
this[methodName] = TelnyxResource.BASIC_METHODS[methodName];
|
|
35
37
|
}, this);
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
if (this.nestedResources) {
|
|
39
41
|
for (var resource in this.nestedResources) {
|
|
40
|
-
this[utils.pascalToCamelCase(resource)] = new this.nestedResources[
|
|
42
|
+
this[utils.pascalToCamelCase(resource)] = new this.nestedResources[
|
|
43
|
+
resource
|
|
44
|
+
](telnyx);
|
|
41
45
|
}
|
|
42
46
|
}
|
|
43
47
|
|
|
@@ -49,13 +53,12 @@ function TelnyxResource(telnyx, urlData) {
|
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
TelnyxResource.prototype = {
|
|
52
|
-
|
|
53
56
|
path: '',
|
|
54
57
|
|
|
55
58
|
// Methods that don't use the API's default '/v1' path can override it with this setting.
|
|
56
59
|
basePath: null,
|
|
57
60
|
|
|
58
|
-
initialize: function() {},
|
|
61
|
+
initialize: function () {},
|
|
59
62
|
|
|
60
63
|
// Function to override the default data processor. This allows full control
|
|
61
64
|
// over how a TelnyxResource's request data will get converted into an HTTP
|
|
@@ -67,26 +70,27 @@ TelnyxResource.prototype = {
|
|
|
67
70
|
// be thrown, and they will be passed to the callback/promise.
|
|
68
71
|
validateRequest: null,
|
|
69
72
|
|
|
70
|
-
createFullPath: function(commandPath, urlData) {
|
|
71
|
-
return path
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
commandPath(urlData) : commandPath
|
|
76
|
-
|
|
73
|
+
createFullPath: function (commandPath, urlData) {
|
|
74
|
+
return path
|
|
75
|
+
.join(
|
|
76
|
+
this.basePath(urlData),
|
|
77
|
+
this.path(urlData),
|
|
78
|
+
typeof commandPath == 'function' ? commandPath(urlData) : commandPath
|
|
79
|
+
)
|
|
80
|
+
.replace(/\\/g, '/'); // ugly workaround for Windows
|
|
77
81
|
},
|
|
78
82
|
|
|
79
83
|
// Creates a relative resource path with symbols left in (unlike
|
|
80
84
|
// createFullPath which takes some data to replace them with). For example it
|
|
81
85
|
// might produce: /invoices/{id}
|
|
82
|
-
createResourcePathWithSymbols: function(pathWithSymbols) {
|
|
83
|
-
return
|
|
84
|
-
|
|
85
|
-
pathWithSymbols || ''
|
|
86
|
-
)
|
|
86
|
+
createResourcePathWithSymbols: function (pathWithSymbols) {
|
|
87
|
+
return (
|
|
88
|
+
'/' +
|
|
89
|
+
path.join(this.resourcePath, pathWithSymbols || '').replace(/\\/g, '/')
|
|
90
|
+
); // ugly workaround for Windows
|
|
87
91
|
},
|
|
88
92
|
|
|
89
|
-
createUrlData: function() {
|
|
93
|
+
createUrlData: function () {
|
|
90
94
|
var urlData = {};
|
|
91
95
|
// Merge in baseData
|
|
92
96
|
for (var i in this._urlData) {
|
|
@@ -97,9 +101,9 @@ TelnyxResource.prototype = {
|
|
|
97
101
|
return urlData;
|
|
98
102
|
},
|
|
99
103
|
|
|
100
|
-
_timeoutHandler: function(timeout, req, callback) {
|
|
104
|
+
_timeoutHandler: function (timeout, req, callback) {
|
|
101
105
|
var self = this;
|
|
102
|
-
return function() {
|
|
106
|
+
return function () {
|
|
103
107
|
var timeoutErr = new Error('ETIMEDOUT');
|
|
104
108
|
timeoutErr.code = 'ETIMEDOUT';
|
|
105
109
|
|
|
@@ -109,24 +113,25 @@ TelnyxResource.prototype = {
|
|
|
109
113
|
callback.call(
|
|
110
114
|
self,
|
|
111
115
|
new Error.TelnyxConnectionError({
|
|
112
|
-
message:
|
|
116
|
+
message:
|
|
117
|
+
'Request aborted due to timeout being reached (' + timeout + 'ms)',
|
|
113
118
|
detail: timeoutErr,
|
|
114
119
|
}),
|
|
115
120
|
null
|
|
116
121
|
);
|
|
117
|
-
}
|
|
122
|
+
};
|
|
118
123
|
},
|
|
119
124
|
|
|
120
|
-
_responseHandler: function(req, callback) {
|
|
125
|
+
_responseHandler: function (req, callback) {
|
|
121
126
|
var self = this;
|
|
122
|
-
return function(res) {
|
|
127
|
+
return function (res) {
|
|
123
128
|
var response = '';
|
|
124
129
|
|
|
125
130
|
res.setEncoding('utf8');
|
|
126
|
-
res.on('data', function(chunk) {
|
|
131
|
+
res.on('data', function (chunk) {
|
|
127
132
|
response += chunk;
|
|
128
133
|
});
|
|
129
|
-
res.on('end', function() {
|
|
134
|
+
res.on('end', function () {
|
|
130
135
|
var headers = res.headers || {};
|
|
131
136
|
// NOTE: Telnyx responds with lowercase header names/keys.
|
|
132
137
|
|
|
@@ -152,7 +157,7 @@ TelnyxResource.prototype = {
|
|
|
152
157
|
if (response.errors) {
|
|
153
158
|
var error = {};
|
|
154
159
|
|
|
155
|
-
error.errors = response.errors
|
|
160
|
+
error.errors = response.errors;
|
|
156
161
|
|
|
157
162
|
error.headers = headers;
|
|
158
163
|
error.statusCode = res.statusCode;
|
|
@@ -186,56 +191,61 @@ TelnyxResource.prototype = {
|
|
|
186
191
|
};
|
|
187
192
|
},
|
|
188
193
|
|
|
189
|
-
_buildError: function(error, statusCode) {
|
|
194
|
+
_buildError: function (error, statusCode) {
|
|
190
195
|
var err;
|
|
191
196
|
switch (statusCode) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
197
|
+
case 400:
|
|
198
|
+
err = new Error.TelnyxInvalidRequestError(error);
|
|
199
|
+
break;
|
|
200
|
+
case 401:
|
|
201
|
+
err = new Error.TelnyxAuthenticationError(error);
|
|
202
|
+
break;
|
|
203
|
+
case 403:
|
|
204
|
+
err = new Error.TelnyxPermissionError(error);
|
|
205
|
+
break;
|
|
206
|
+
case 404:
|
|
207
|
+
err = new Error.TelnyxResourceNotFoundError(error);
|
|
208
|
+
break;
|
|
209
|
+
case 405:
|
|
210
|
+
err = new Error.TelnyxMethodNotSupportedError(error);
|
|
211
|
+
break;
|
|
212
|
+
case 408:
|
|
213
|
+
err = new Error.TelnyxTimeoutError(error);
|
|
214
|
+
break;
|
|
215
|
+
case 415:
|
|
216
|
+
err = new Error.TelnyxUnsupportedMediaTypeError(error);
|
|
217
|
+
break;
|
|
218
|
+
case 422:
|
|
219
|
+
err = new Error.TelnyxInvalidParametersError(error);
|
|
220
|
+
break;
|
|
221
|
+
case 429:
|
|
222
|
+
err = new Error.TelnyxRateLimitError(error);
|
|
223
|
+
break;
|
|
224
|
+
case 500:
|
|
225
|
+
err = new Error.TelnyxAPIError(error);
|
|
226
|
+
break;
|
|
227
|
+
case 503:
|
|
228
|
+
err = new Error.TelnyxServiceUnavailableError(error);
|
|
229
|
+
break;
|
|
230
|
+
default:
|
|
231
|
+
err = new Error.TelnyxAPIError(error);
|
|
227
232
|
}
|
|
228
233
|
|
|
229
234
|
return err;
|
|
230
235
|
},
|
|
231
236
|
|
|
232
|
-
_generateConnectionErrorMessage: function(requestRetries) {
|
|
233
|
-
return
|
|
237
|
+
_generateConnectionErrorMessage: function (requestRetries) {
|
|
238
|
+
return (
|
|
239
|
+
'An error occurred with our connection to Telnyx.' +
|
|
240
|
+
(requestRetries > 0
|
|
241
|
+
? ' Request was retried ' + requestRetries + ' times.'
|
|
242
|
+
: '')
|
|
243
|
+
);
|
|
234
244
|
},
|
|
235
245
|
|
|
236
|
-
_errorHandler: function(req, requestRetries, callback) {
|
|
246
|
+
_errorHandler: function (req, requestRetries, callback) {
|
|
237
247
|
var self = this;
|
|
238
|
-
return function(error) {
|
|
248
|
+
return function (error) {
|
|
239
249
|
if (req._isAborted) {
|
|
240
250
|
// already handled
|
|
241
251
|
return;
|
|
@@ -248,10 +258,10 @@ TelnyxResource.prototype = {
|
|
|
248
258
|
}),
|
|
249
259
|
null
|
|
250
260
|
);
|
|
251
|
-
}
|
|
261
|
+
};
|
|
252
262
|
},
|
|
253
263
|
|
|
254
|
-
_shouldRetry: function(res, numRetries) {
|
|
264
|
+
_shouldRetry: function (res, numRetries) {
|
|
255
265
|
// Do not retry if we are out of retries.
|
|
256
266
|
if (numRetries >= this._telnyx.getMaxNetworkRetries()) {
|
|
257
267
|
return false;
|
|
@@ -275,7 +285,7 @@ TelnyxResource.prototype = {
|
|
|
275
285
|
return false;
|
|
276
286
|
},
|
|
277
287
|
|
|
278
|
-
_getSleepTimeInMS: function(numRetries) {
|
|
288
|
+
_getSleepTimeInMS: function (numRetries) {
|
|
279
289
|
var initialNetworkRetryDelay = this._telnyx.getInitialNetworkRetryDelay();
|
|
280
290
|
var maxNetworkRetryDelay = this._telnyx.getMaxNetworkRetryDelay();
|
|
281
291
|
|
|
@@ -297,8 +307,9 @@ TelnyxResource.prototype = {
|
|
|
297
307
|
return sleepSeconds * 1000;
|
|
298
308
|
},
|
|
299
309
|
|
|
300
|
-
_defaultHeaders: function(auth, requestData) {
|
|
301
|
-
var userAgentString =
|
|
310
|
+
_defaultHeaders: function (auth, requestData) {
|
|
311
|
+
var userAgentString =
|
|
312
|
+
'Telnyx/v1 NodeBindings/' + this._telnyx.getConstant('PACKAGE_VERSION');
|
|
302
313
|
|
|
303
314
|
if (this._telnyx._appInfo) {
|
|
304
315
|
userAgentString += ' ' + this._telnyx.getAppInfoAsString();
|
|
@@ -306,10 +317,8 @@ TelnyxResource.prototype = {
|
|
|
306
317
|
|
|
307
318
|
var headers = {
|
|
308
319
|
// Use specified auth token or use default from this telnyx instance:
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
this._telnyx.getApiField('auth'),
|
|
312
|
-
'Accept': 'application/json',
|
|
320
|
+
Authorization: auth ? 'Bearer ' + auth : this._telnyx.getApiField('auth'),
|
|
321
|
+
Accept: '*/*',
|
|
313
322
|
'Content-Type': 'application/json',
|
|
314
323
|
'Content-Length': Buffer.byteLength(requestData),
|
|
315
324
|
'User-Agent': userAgentString,
|
|
@@ -318,7 +327,7 @@ TelnyxResource.prototype = {
|
|
|
318
327
|
return headers;
|
|
319
328
|
},
|
|
320
329
|
|
|
321
|
-
_request: function(method, host, path, data, auth, options, callback) {
|
|
330
|
+
_request: function (method, host, path, data, auth, options, callback) {
|
|
322
331
|
var self = this;
|
|
323
332
|
var requestData;
|
|
324
333
|
|
|
@@ -336,12 +345,17 @@ TelnyxResource.prototype = {
|
|
|
336
345
|
} else {
|
|
337
346
|
requestData = '';
|
|
338
347
|
}
|
|
339
|
-
} else {
|
|
348
|
+
} else if (utils.isJsonString(data)) {
|
|
340
349
|
requestData = data;
|
|
350
|
+
} else if (data != '' && data != null) {
|
|
351
|
+
path = path + '?' + encodeURI(data);
|
|
352
|
+
requestData = '';
|
|
353
|
+
} else {
|
|
354
|
+
requestData = '';
|
|
341
355
|
}
|
|
342
356
|
|
|
343
357
|
headers = self._defaultHeaders(auth, requestData);
|
|
344
|
-
self._telnyx.getClientUserAgent(function(cua) {
|
|
358
|
+
self._telnyx.getClientUserAgent(function (cua) {
|
|
345
359
|
headers['X-Telnyx-Client-User-Agent'] = cua;
|
|
346
360
|
|
|
347
361
|
if (options.headers) {
|
|
@@ -353,13 +367,29 @@ TelnyxResource.prototype = {
|
|
|
353
367
|
}
|
|
354
368
|
|
|
355
369
|
if (self.requestDataProcessor) {
|
|
356
|
-
self.requestDataProcessor(
|
|
370
|
+
self.requestDataProcessor(
|
|
371
|
+
method,
|
|
372
|
+
data,
|
|
373
|
+
options.headers,
|
|
374
|
+
makeRequestWithData
|
|
375
|
+
);
|
|
357
376
|
} else if (method == 'GET') {
|
|
358
377
|
makeRequestWithData(null, data ? utils.stringifyRequestData(data) : '');
|
|
359
|
-
} else if (method == 'DELETE') {
|
|
360
|
-
makeRequestWithData(null, '');
|
|
361
378
|
} else {
|
|
362
|
-
|
|
379
|
+
let parameters;
|
|
380
|
+
if (data) {
|
|
381
|
+
if (data.filter && Object.keys(data.filter).length) {
|
|
382
|
+
parameters = utils.stringifyRequestData(data);
|
|
383
|
+
} else {
|
|
384
|
+
parameters = JSON.stringify(data);
|
|
385
|
+
if (parameters === '{}') {
|
|
386
|
+
parameters = '';
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
390
|
+
parameters = '';
|
|
391
|
+
}
|
|
392
|
+
makeRequestWithData(null, parameters);
|
|
363
393
|
}
|
|
364
394
|
|
|
365
395
|
function retryRequest(requestFn, headers, requestRetries) {
|
|
@@ -376,12 +406,17 @@ TelnyxResource.prototype = {
|
|
|
376
406
|
function makeRequest(headers, numRetries) {
|
|
377
407
|
var timeout = self._telnyx.getApiField('timeout');
|
|
378
408
|
var isInsecureConnection = self._telnyx.getApiField('protocol') == 'http';
|
|
379
|
-
var agent = isInsecureConnection
|
|
409
|
+
var agent = isInsecureConnection
|
|
410
|
+
? self._telnyx.getApiField('http_agent')
|
|
411
|
+
: self._telnyx.getApiField('https_agent');
|
|
412
|
+
|
|
413
|
+
if (headers['Content-Length'] === 0) {
|
|
414
|
+
delete headers['Content-Length'];
|
|
415
|
+
delete headers['Content-Type'];
|
|
416
|
+
}
|
|
380
417
|
|
|
381
|
-
var req = (
|
|
382
|
-
|
|
383
|
-
).request({
|
|
384
|
-
host: host || self._telnyx.getApiField('host'),
|
|
418
|
+
var req = (isInsecureConnection ? http : https).request({
|
|
419
|
+
host: self._telnyx.getApiField('host') || '127.0.0.1',
|
|
385
420
|
port: self._telnyx.getApiField('port'),
|
|
386
421
|
path: path,
|
|
387
422
|
method: method,
|
|
@@ -405,7 +440,7 @@ TelnyxResource.prototype = {
|
|
|
405
440
|
|
|
406
441
|
req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback));
|
|
407
442
|
|
|
408
|
-
req.on('response', function(res) {
|
|
443
|
+
req.on('response', function (res) {
|
|
409
444
|
if (self._shouldRetry(res, requestRetries)) {
|
|
410
445
|
return retryRequest(makeRequest, headers, requestRetries);
|
|
411
446
|
} else {
|
|
@@ -413,7 +448,7 @@ TelnyxResource.prototype = {
|
|
|
413
448
|
}
|
|
414
449
|
});
|
|
415
450
|
|
|
416
|
-
req.on('error', function(error) {
|
|
451
|
+
req.on('error', function (error) {
|
|
417
452
|
if (self._shouldRetry(null, requestRetries)) {
|
|
418
453
|
return retryRequest(makeRequest, headers, requestRetries);
|
|
419
454
|
} else {
|
|
@@ -421,13 +456,16 @@ TelnyxResource.prototype = {
|
|
|
421
456
|
}
|
|
422
457
|
});
|
|
423
458
|
|
|
424
|
-
req.on('socket', function(socket) {
|
|
459
|
+
req.on('socket', function (socket) {
|
|
425
460
|
if (socket.connecting) {
|
|
426
|
-
socket.on(
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
461
|
+
socket.on(
|
|
462
|
+
isInsecureConnection ? 'connect' : 'secureConnect',
|
|
463
|
+
function () {
|
|
464
|
+
// Send payload; we're safe:
|
|
465
|
+
req.write(requestData);
|
|
466
|
+
req.end();
|
|
467
|
+
}
|
|
468
|
+
);
|
|
431
469
|
} else {
|
|
432
470
|
// we're already connected
|
|
433
471
|
req.write(requestData);
|
package/lib/makeRequest.js
CHANGED
|
@@ -5,11 +5,17 @@ var OPTIONAL_REGEX = /^optional!/;
|
|
|
5
5
|
|
|
6
6
|
function getRequestOpts(self, requestArgs, spec, overrideData) {
|
|
7
7
|
// Extract spec values with defaults.
|
|
8
|
-
var commandPath =
|
|
9
|
-
|
|
8
|
+
var commandPath =
|
|
9
|
+
typeof spec.path == 'function'
|
|
10
|
+
? spec.path
|
|
11
|
+
: utils.makeURLInterpolator(spec.path || '');
|
|
10
12
|
var requestMethod = (spec.method || 'GET').toUpperCase();
|
|
11
13
|
var urlParams = spec.urlParams || [];
|
|
12
|
-
var encode =
|
|
14
|
+
var encode =
|
|
15
|
+
spec.encode ||
|
|
16
|
+
function (data) {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
13
19
|
var host = spec.host;
|
|
14
20
|
|
|
15
21
|
// Don't mutate args externally.
|
|
@@ -32,8 +38,13 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
|
|
|
32
38
|
if (param == 'id' && typeof arg !== 'string') {
|
|
33
39
|
path = self.createResourcePathWithSymbols(spec.path);
|
|
34
40
|
throw new Error(
|
|
35
|
-
'Telnyx: "id" must be a string, but got: ' +
|
|
36
|
-
|
|
41
|
+
'Telnyx: "id" must be a string, but got: ' +
|
|
42
|
+
typeof arg +
|
|
43
|
+
' (on API request to `' +
|
|
44
|
+
requestMethod +
|
|
45
|
+
' ' +
|
|
46
|
+
path +
|
|
47
|
+
'`)'
|
|
37
48
|
);
|
|
38
49
|
}
|
|
39
50
|
|
|
@@ -45,8 +56,15 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
|
|
|
45
56
|
|
|
46
57
|
path = self.createResourcePathWithSymbols(spec.path);
|
|
47
58
|
throw new Error(
|
|
48
|
-
'Telnyx: Argument "' +
|
|
49
|
-
|
|
59
|
+
'Telnyx: Argument "' +
|
|
60
|
+
urlParams[i] +
|
|
61
|
+
'" required, but got: ' +
|
|
62
|
+
arg +
|
|
63
|
+
' (on API request to `' +
|
|
64
|
+
requestMethod +
|
|
65
|
+
' ' +
|
|
66
|
+
path +
|
|
67
|
+
'`)'
|
|
50
68
|
);
|
|
51
69
|
}
|
|
52
70
|
|
|
@@ -62,8 +80,14 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
|
|
|
62
80
|
if (args.length) {
|
|
63
81
|
path = self.createResourcePathWithSymbols(spec.path);
|
|
64
82
|
throw new Error(
|
|
65
|
-
'Telnyx: Unknown arguments (' +
|
|
66
|
-
|
|
83
|
+
'Telnyx: Unknown arguments (' +
|
|
84
|
+
args +
|
|
85
|
+
'). Did you mean to pass an options ' +
|
|
86
|
+
'object? (on API request to ' +
|
|
87
|
+
requestMethod +
|
|
88
|
+
' `' +
|
|
89
|
+
path +
|
|
90
|
+
'`)'
|
|
67
91
|
);
|
|
68
92
|
}
|
|
69
93
|
|
|
@@ -85,7 +109,7 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
|
|
|
85
109
|
}
|
|
86
110
|
|
|
87
111
|
function makeRequest(self, requestArgs, spec, overrideData) {
|
|
88
|
-
return new Promise(function(resolve, reject) {
|
|
112
|
+
return new Promise(function (resolve, reject) {
|
|
89
113
|
try {
|
|
90
114
|
var opts = getRequestOpts(self, requestArgs, spec, overrideData);
|
|
91
115
|
} catch (err) {
|
|
@@ -98,14 +122,22 @@ function makeRequest(self, requestArgs, spec, overrideData) {
|
|
|
98
122
|
reject(err);
|
|
99
123
|
} else {
|
|
100
124
|
resolve(
|
|
101
|
-
spec.transformResponseData && response.data
|
|
102
|
-
spec.transformResponseData(response, self._telnyx)
|
|
103
|
-
response
|
|
125
|
+
spec.transformResponseData && response.data
|
|
126
|
+
? spec.transformResponseData(response, self._telnyx)
|
|
127
|
+
: response
|
|
104
128
|
);
|
|
105
129
|
}
|
|
106
130
|
}
|
|
107
131
|
|
|
108
|
-
self._request(
|
|
132
|
+
self._request(
|
|
133
|
+
opts.requestMethod,
|
|
134
|
+
opts.host,
|
|
135
|
+
opts.requestPath,
|
|
136
|
+
opts.data,
|
|
137
|
+
opts.auth,
|
|
138
|
+
{headers: opts.headers},
|
|
139
|
+
requestCallback
|
|
140
|
+
);
|
|
109
141
|
});
|
|
110
142
|
}
|
|
111
143
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var TelnyxResource = require('../TelnyxResource');
|
|
4
|
+
var utils = require('../utils');
|
|
5
|
+
var telnyxMethod = TelnyxResource.method;
|
|
6
|
+
|
|
7
|
+
function transformResponseData(response, telnyx) {
|
|
8
|
+
return utils.addResourceToResponseData(response, telnyx, 'actions', {});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = TelnyxResource.extend({
|
|
12
|
+
path: 'actions',
|
|
13
|
+
|
|
14
|
+
create: telnyxMethod({
|
|
15
|
+
method: 'POST',
|
|
16
|
+
path: '/{action}/telephony_credentials',
|
|
17
|
+
urlParams: ['action'],
|
|
18
|
+
transformResponseData: transformResponseData,
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
@@ -5,24 +5,19 @@ var utils = require('../utils');
|
|
|
5
5
|
var telnyxMethod = TelnyxResource.method;
|
|
6
6
|
|
|
7
7
|
function transformResponseData(response, telnyx) {
|
|
8
|
-
return utils.addResourceToResponseData(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
paramsValues: [response.data.id],
|
|
18
|
-
paramsNames: ['id'],
|
|
19
|
-
}),
|
|
20
|
-
}
|
|
21
|
-
);
|
|
8
|
+
return utils.addResourceToResponseData(response, telnyx, 'addresses', {
|
|
9
|
+
del: telnyxMethod({
|
|
10
|
+
method: 'DELETE',
|
|
11
|
+
path: '/{addressId}',
|
|
12
|
+
urlParams: ['addressId'],
|
|
13
|
+
paramsValues: [response.data.id],
|
|
14
|
+
paramsNames: ['id'],
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
22
17
|
}
|
|
23
18
|
|
|
24
19
|
module.exports = TelnyxResource.extend({
|
|
25
|
-
path: 'addresses',
|
|
20
|
+
path: 'account/addresses',
|
|
26
21
|
|
|
27
22
|
list: telnyxMethod({
|
|
28
23
|
method: 'GET',
|
|
@@ -47,6 +42,6 @@ module.exports = TelnyxResource.extend({
|
|
|
47
42
|
|
|
48
43
|
validate: telnyxMethod({
|
|
49
44
|
method: 'POST',
|
|
50
|
-
path: '/actions/validate'
|
|
45
|
+
path: '/actions/validate',
|
|
51
46
|
}),
|
|
52
47
|
});
|