Skip to content

Commit 21789dd

Browse files
committed
allow for passing requestTimeout in options bag to Code().execute(); bring back unit tests; allow for passing options to new ClearBlade() to allow for easier request mocking in unit tests
1 parent bf10993 commit 21789dd

21 files changed

Lines changed: 8773 additions & 680 deletions

index.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// TODO: change all the occurences where we use CbCallback<any> to supply the actual type that is returned
1010

1111
interface ClearBlade {
12-
new (): IClearBlade;
12+
new (options?: { request: () => void }): IClearBlade;
1313

1414
MESSAGING_QOS_AT_MOST_ONCE: MessagingQOS.MESSAGING_QOS_AT_MOST_ONCE;
1515
MESSAGING_QOS_AT_LEAST_ONCE: MessagingQOS.MESSAGING_QOS_AT_LEAST_ONCE;
@@ -22,6 +22,10 @@ interface ClearBlade {
2222
): string;
2323
}
2424
export var ClearBlade: ClearBlade;
25+
export var getMessageTopic: (
26+
destinationName: string,
27+
callbackDict: { [key: string]: () => void }
28+
) => string;
2529

2630
declare enum MessagingQOS {
2731
MESSAGING_QOS_AT_MOST_ONCE = 0,
@@ -360,7 +364,10 @@ interface Code {
360364
name: string,
361365
params: object,
362366
callback: CbCallback<ServiceCallbackInfo<T>>,
363-
id?: string
367+
options?: {
368+
id?: string;
369+
requestTimeout?: number;
370+
}
364371
): void;
365372
getCompletedServices(callback: CbCallback<ServiceInfo>): void;
366373
getFailedServices(callback: CbCallback<ServiceInfo>): void;

index.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if (!window.console) {
1313
window.console.log = window.console.log || function () {};
1414
}
1515

16-
function createClearBladeInstance (window, undefined) {
16+
function createClearBladeInstance (window, options) {
1717
// 'use strict';
1818
var ClearBlade;
1919
/**
@@ -754,25 +754,7 @@ function createClearBladeInstance (window, undefined) {
754754
httpRequest.send(body);
755755
};
756756

757-
ClearBlade.getMessageTopic = function (destinationName, callbackDict) {
758-
const destArr = destinationName.split('/');
759-
for (const topic in callbackDict) {
760-
const topicArr = topic.split('/');
761-
for (let i = 0; i < destArr.length; i++) {
762-
if (topicArr[i] === '#') {
763-
return topic;
764-
}
765-
if (destArr[i] !== topicArr[i] && topicArr[i] !== '+') {
766-
break;
767-
}
768-
if (i === destArr.length - 1) {
769-
return topic;
770-
}
771-
}
772-
}
773-
};
774-
775-
ClearBlade.request = function (options, callback) {
757+
ClearBlade.request = options.request || function (options, callback) {
776758
if (!options || typeof options !== 'object') {
777759
throw new Error('Request: options is not an object or is empty');
778760
}
@@ -1637,17 +1619,20 @@ function createClearBladeInstance (window, undefined) {
16371619
* }
16381620
* })
16391621
*/
1640-
code.execute = function (name, params, callback, id) {
1622+
code.execute = function (name, params, callback, options) {
1623+
if (typeof options === 'undefined') {
1624+
options = {};
1625+
}
16411626
var reqOptions = {
16421627
method: 'POST',
16431628
endpoint: 'api/v/1/code/' + this.systemKey + '/' + name,
16441629
body: params,
16451630
user: this.user,
16461631
URI: this.URI,
1647-
timeout: this.callTimeout,
1632+
timeout: options.requestTimeout || this.callTimeout,
16481633
};
1649-
if (id) {
1650-
reqOptions.endpoint = reqOptions.endpoint + '?id=' + id;
1634+
if (options.id) {
1635+
reqOptions.endpoint = reqOptions.endpoint + '?id=' + options.id;
16511636
}
16521637
if (typeof callback === 'function') {
16531638
ClearBlade.request(reqOptions, callback);
@@ -2116,7 +2101,7 @@ function createClearBladeInstance (window, undefined) {
21162101
// messageCallbacks from Subscribe() may contain multiple callbacks per topic
21172102
const callbacks = Object.values(
21182103
messageCallbacks[
2119-
ClearBlade.getMessageTopic(message.destinationName, messageCallbacks)
2104+
getMessageTopic(message.destinationName, messageCallbacks)
21202105
]
21212106
);
21222107
for (var theCallback of callbacks) {
@@ -3732,13 +3717,31 @@ function createClearBladeInstance (window, undefined) {
37323717
};
37333718
ClearBlade.request(reqOptions, callback);
37343719
};
3735-
37363720
return ClearBlade;
37373721
}
37383722

3723+
function getMessageTopic (destinationName, callbackDict) {
3724+
const destArr = destinationName.split('/');
3725+
for (const topic in callbackDict) {
3726+
const topicArr = topic.split('/');
3727+
for (let i = 0; i < destArr.length; i++) {
3728+
if (topicArr[i] === '#') {
3729+
return topic;
3730+
}
3731+
if (destArr[i] !== topicArr[i] && topicArr[i] !== '+') {
3732+
break;
3733+
}
3734+
if (i === destArr.length - 1) {
3735+
return topic;
3736+
}
3737+
}
3738+
}
3739+
}
3740+
37393741
module.exports = {
3740-
ClearBlade: function() {
3741-
const cb = createClearBladeInstance(window);
3742+
ClearBlade: function(options) {
3743+
const cb = createClearBladeInstance(window, options || {});
37423744
return new cb();
3743-
}
3745+
},
3746+
getMessageTopic
37443747
};

0 commit comments

Comments
 (0)