Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit bdea409

Browse files
authored
Disable logging API support (#44)
1 parent 661e9fc commit bdea409

File tree

5 files changed

+145
-27
lines changed

5 files changed

+145
-27
lines changed

API.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
- `configuration` **[object][1]**
1212
- `configuration.apiKey` **[string][2]**
1313
- `configuration.hookURL` **[string][2]**
14-
- `onMessage` **[function][3]** callback for incoming messages
14+
- `configuration.conversationLogging` **[boolean][3]**
15+
- `onMessage` **[function][4]** callback for incoming messages
1516

16-
Returns **[Promise][4]<[object][1]>** where object = {botName: string, history: Array[{content: string, made_by: string}]}
17+
Returns **[Promise][5]<[object][1]>** where object = {botName: string, history: Array[{content: string, made_by: string}]}
1718

1819
## send
1920

@@ -35,7 +36,7 @@ Sets callback for connection change
3536

3637
### Parameters
3738

38-
- `callback` **[function][3]** (isConnected: bool)
39+
- `callback` **[function][4]** (isConnected: bool)
3940

4041
## attachToPayload
4142

@@ -65,22 +66,36 @@ Removes a property from the payload object
6566

6667
- `key` **[string][2]**
6768

69+
## setConversationLogging
70+
71+
Turn conversation logging on or off
72+
73+
### Parameters
74+
75+
- `value` **[boolean][3]** true|false
76+
77+
## isConversationLogging
78+
79+
Check if conversation logging is on or off
80+
81+
Returns **[boolean][3]**
82+
6883
## getUserId
6984

7085
Returns a promise that resolves if and when user_id_cookie is available
7186

72-
Returns **[Promise][4]<[string][2]>**
87+
Returns **[Promise][5]<[string][2]>**
7388

7489
## getBotName
7590

7691
Returns a Promise that resolves with bot name
7792

7893
### Parameters
7994

80-
- `hookURL` **[string][2]** (optional, default `store.hookURL`)
81-
- `apiKey` **[string][2]** (optional, default `store.apiKey`)
95+
- `hookURL` **[string][2]** (optional, default `store.configuration.hookURL`)
96+
- `apiKey` **[string][2]** (optional, default `store.configuration.apiKey`)
8297

83-
Returns **[Promise][4]<([Response][5] | never)>**
98+
Returns **[Promise][5]<([Response][6] | never)>**
8499

85100
## endSession
86101

@@ -94,6 +109,7 @@ Returns **[Promise][4]<([Response][5] | never)>**
94109

95110
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
96111
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
97-
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
98-
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
99-
[5]: https://developer.mozilla.org/docs/Web/Guide/HTML/HTML5
112+
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
113+
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
114+
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
115+
[6]: https://developer.mozilla.org/docs/Web/Guide/HTML/HTML5

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "@twyla-ai/widget-core",
3-
"version": "0.10.0",
3+
"version": "0.11.0",
44
"description": "Twyla Widget Core",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "npm run clean && npx webpack --mode production",
88
"clean": "npx rimraf dist",
99
"documentation": "node document.js",
1010
"pre-publish": "npm run build",
11+
"publish": "npm publish --access public",
1112
"test": "npx jest --no-cache ./src"
1213
},
1314
"author": "Twyla GmbH",

src/index.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ const getDefaultPayload = () => ({
1515
});
1616

1717
const store = {
18-
apiKey: undefined,
19-
hookURL: undefined,
18+
configuration: {
19+
apiKey: undefined,
20+
hookURL: undefined,
21+
},
2022
notificationsChannelURL: undefined,
2123
userId: Cookies.get(COOKIE_NAME) || null,
2224
payload: getDefaultPayload(),
@@ -165,7 +167,7 @@ const setUpSocket = () => {
165167
store.socket.send(
166168
JSON.stringify({
167169
user_id_cookie: store.userId,
168-
api_key: store.apiKey,
170+
api_key: store.configuration.apiKey,
169171
})
170172
);
171173
});
@@ -191,6 +193,7 @@ const setUpSocket = () => {
191193
* @param {object} configuration
192194
* @param {string} configuration.apiKey
193195
* @param {string} configuration.hookURL
196+
* @param {boolean} configuration.conversationLogging
194197
* @param {function} onMessage callback for incoming messages
195198
* @returns {Promise<object>}
196199
* where object = {botName: string, history: Array[{content: string, made_by: string}]}
@@ -203,8 +206,14 @@ API.init = (configuration, onMessage) => {
203206
// store promise to resolve on establish session success
204207
store.promises.init = { resolve, reject };
205208

206-
store.apiKey = configuration.apiKey;
207-
store.hookURL = configuration.hookURL;
209+
store.configuration = {
210+
apiKey: configuration.apiKey,
211+
hookURL: configuration.hookURL,
212+
};
213+
214+
if (configuration.conversationLogging === false) {
215+
store.payload._logging_disabled = true;
216+
}
208217

209218
try {
210219
const { notificationsChannelURL, botName } = notificationsChannelURLFromHookURL(
@@ -229,8 +238,8 @@ API.init = (configuration, onMessage) => {
229238
API.send = message => {
230239
if (store.connected) {
231240
postMessage({
232-
url: store.hookURL,
233-
apiKey: store.apiKey,
241+
url: store.configuration.hookURL,
242+
apiKey: store.configuration.apiKey,
234243
input: message,
235244
userId: store.userId,
236245
payload: store.payload,
@@ -314,6 +323,26 @@ API.detachFromPayload = key => {
314323
delete store.payload[key];
315324
};
316325

326+
/**
327+
* Turn conversation logging on or off
328+
* @param {boolean} value true|false
329+
*/
330+
API.setConversationLogging = value => {
331+
if (value === true) {
332+
delete store.payload._logging_disabled;
333+
} else if (value === false) {
334+
store.payload._logging_disabled = true;
335+
}
336+
};
337+
338+
/**
339+
* Check if conversation logging is on or off
340+
* @returns {boolean}
341+
*/
342+
API.isConversationLogging = () => {
343+
return !!!store.payload._logging_disabled;
344+
};
345+
317346
/**
318347
* Returns a promise that resolves if and when user_id_cookie is available
319348
* @returns {Promise<string>}
@@ -334,7 +363,7 @@ API.getUserId = () => {
334363
* @param {string} apiKey
335364
* @returns {Promise<Response | never>}
336365
*/
337-
API.getBotName = (hookURL = store.hookURL, apiKey = store.apiKey) => {
366+
API.getBotName = (hookURL = store.configuration.hookURL, apiKey = store.configuration.apiKey) => {
338367
return fetch(`${hookURL}?key=${apiKey}`, {
339368
method: 'GET',
340369
}).then(response => {
@@ -368,8 +397,7 @@ API.endSession = () => {
368397
API.clearSession = () => {
369398
API.endSession();
370399
store.userId = null;
371-
store.apiKey = null;
372-
store.hookURL = null;
400+
store.configuration = {};
373401
store.notificationsChannelURL = null;
374402
store.payload = getDefaultPayload();
375403
Cookies.remove(COOKIE_NAME);
@@ -385,6 +413,8 @@ export const {
385413
attachToPayload,
386414
detachFromPayload,
387415
setMetadata,
416+
isConversationLogging,
417+
setConversationLogging,
388418
getUserId,
389419
getBotName,
390420
clearSession,

src/tests/api.test.js

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
onConnectionChange,
1010
send,
1111
endSession,
12+
isConversationLogging,
13+
setConversationLogging,
1214
} from '../index';
1315
import { WebSocket, Server } from 'mock-socket';
1416
import { CONVERSATION_STARTER } from '../constants';
@@ -70,9 +72,7 @@ describe('API test', () => {
7072

7173
const mockFetch = url => {
7274
let json = f => f;
73-
const fakeUrl = `https://api.demo.twyla.io/widget-hook/massive-dynamics/templates?key=${
74-
configuration.apiKey
75-
}`;
75+
const fakeUrl = `https://api.demo.twyla.io/widget-hook/massive-dynamics/templates?key=${configuration.apiKey}`;
7676

7777
if (url === fakeUrl) {
7878
json = () => ({ name: 'Templates' });
@@ -229,14 +229,85 @@ describe('API test', () => {
229229

230230
expect(global.fetch.mock.calls.length).toEqual(2);
231231
expect(global.fetch.mock.calls[1][0]).toEqual(configuration.hookURL);
232-
expect(global.fetch.mock.calls[1][1]).toEqual(postMsg('blue', { fruit: 'apple' }));
232+
233+
let requestPayload = global.fetch.mock.calls[1][1];
234+
expect(requestPayload.method).toEqual('POST');
235+
expect(JSON.parse(requestPayload.body)).toEqual({
236+
api_key: 'fakeApiKey',
237+
data: {
238+
_meta: {
239+
origin: 'https://jest-test.com',
240+
pathname: '/widget.html',
241+
},
242+
fruit: 'apple',
243+
},
244+
input: 'blue',
245+
user_id_cookie: 'fakeUserIdCookie',
246+
});
233247

234248
detachFromPayload('fruit');
235249
send('green');
236250

237251
expect(global.fetch.mock.calls.length).toEqual(3);
238252
expect(global.fetch.mock.calls[2][0]).toEqual(configuration.hookURL);
239-
expect(global.fetch.mock.calls[2][1]).toEqual(postMsg('green'));
253+
254+
requestPayload = global.fetch.mock.calls[2][1];
255+
expect(requestPayload.method).toEqual('POST');
256+
expect(JSON.parse(requestPayload.body)).toEqual({
257+
api_key: 'fakeApiKey',
258+
data: {
259+
_meta: {
260+
origin: 'https://jest-test.com',
261+
pathname: '/widget.html',
262+
},
263+
},
264+
input: 'green',
265+
user_id_cookie: 'fakeUserIdCookie',
266+
});
267+
268+
done();
269+
});
270+
});
271+
272+
test('toggle logging', done => {
273+
init({ ...configuration, conversationLogging: false }, onMessage).then(() => {
274+
expect(isConversationLogging()).toEqual(false);
275+
276+
send('blue');
277+
278+
let requestPayload = global.fetch.mock.calls[1][1];
279+
expect(requestPayload.method).toEqual('POST');
280+
expect(JSON.parse(requestPayload.body)).toEqual({
281+
api_key: 'fakeApiKey',
282+
data: {
283+
_meta: {
284+
origin: 'https://jest-test.com',
285+
pathname: '/widget.html',
286+
},
287+
_logging_disabled: true,
288+
},
289+
input: 'blue',
290+
user_id_cookie: 'fakeUserIdCookie',
291+
});
292+
293+
setConversationLogging(true);
294+
295+
expect(isConversationLogging()).toEqual(true);
296+
297+
send('green');
298+
299+
requestPayload = global.fetch.mock.calls[2][1];
300+
expect(JSON.parse(requestPayload.body)).toEqual({
301+
api_key: 'fakeApiKey',
302+
data: {
303+
_meta: {
304+
origin: 'https://jest-test.com',
305+
pathname: '/widget.html',
306+
},
307+
},
308+
input: 'green',
309+
user_id_cookie: 'fakeUserIdCookie',
310+
});
240311

241312
done();
242313
});

0 commit comments

Comments
 (0)