Skip to content

Commit 7e7046a

Browse files
authored
fix: Allow context initialization without data store (#68)
1 parent 1869a9c commit 7e7046a

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed

lib/smart-app.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,17 @@ module.exports = class SmartApp {
498498
// Proactive API calls (not in response to lifecycle events //
499499
/// ///////////////////////////////////////////////////////////
500500

501-
withContext(installedAppId) {
501+
withContext(installedAppIdOrObject) {
502502
const app = this
503+
if (typeof installedAppIdOrObject === 'object') {
504+
return new Promise(resolve => {
505+
resolve(new EndpointContext(app, installedAppIdOrObject, new Mutex()))
506+
})
507+
}
508+
503509
if (this._contextStore) {
504510
return new Promise((resolve, reject) => {
505-
this._contextStore.get(installedAppId).then(data => {
511+
this._contextStore.get(installedAppIdOrObject).then(data => {
506512
resolve(new EndpointContext(app, data, new Mutex()))
507513
}).catch(error => {
508514
reject(error)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@smartthings/smartapp",
3-
"version": "1.1.4",
3+
"version": "1.2.1",
44
"description": "NodeJS SDK for SmartApps",
55
"displayName": "SmartThings SmartApp SDK for NodeJS",
66
"author": "SmartThings",

test/smartapp-context-spec.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* eslint no-undef: "off" */
2+
3+
const assert = require('assert').strict
4+
const SmartApp = require('../lib/smart-app')
5+
6+
class ContextStore {
7+
constructor() {
8+
this.contexts = {}
9+
}
10+
11+
get(installedAppId) {
12+
return new Promise(resolve => {
13+
resolve(this.contexts[installedAppId])
14+
})
15+
}
16+
17+
put(params) {
18+
this.contexts[params.installedAppId] = params
19+
return new Promise(resolve => {
20+
resolve()
21+
})
22+
}
23+
24+
update(installedAppId, params) {
25+
this.contexts[params.installedAppId].authToken = params.authToken
26+
this.contexts[params.installedAppId].refreshToken = params.refreshToken
27+
return new Promise(resolve => {
28+
resolve()
29+
})
30+
}
31+
32+
delete(installedAppId) {
33+
this.contexts[installedAppId] = null
34+
return new Promise(resolve => {
35+
resolve()
36+
})
37+
}
38+
}
39+
40+
describe('smartapp-context-spec', () => {
41+
let app
42+
43+
beforeEach(() => {
44+
app = new SmartApp()
45+
})
46+
47+
it('should handle context store', async () => {
48+
const installData = {
49+
authToken: 'xxx',
50+
refreshToken: 'yyy',
51+
installedApp: {
52+
installedAppId: 'd692699d-e7a6-400d-a0b7-d5be96e7a564',
53+
locationId: 'e675a3d9-2499-406c-86dc-8a492a886494',
54+
config: {}
55+
}
56+
}
57+
58+
const contextStore = new ContextStore()
59+
app.contextStore(contextStore)
60+
61+
await app.handleMockCallback({
62+
lifecycle: 'INSTALL',
63+
executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc',
64+
locale: 'en',
65+
version: '0.1.0',
66+
client: {
67+
os: 'ios',
68+
version: '0.0.0',
69+
language: 'en-US'
70+
},
71+
installData,
72+
settings: {}
73+
})
74+
75+
const ctx = await app.withContext('d692699d-e7a6-400d-a0b7-d5be96e7a564')
76+
77+
assert.equal(installData.installedApp.installedAppId, ctx.installedAppId)
78+
assert.equal(installData.installedApp.locationId, ctx.locationId)
79+
assert.equal(installData.authToken, ctx.api.client.authToken)
80+
assert.equal(installData.refreshToken, ctx.api.client.refreshToken)
81+
})
82+
83+
it('should handle context object', async () => {
84+
const params = {
85+
authToken: 'xxx',
86+
refreshToken: 'yyy',
87+
installedAppId: 'aaa',
88+
locationId: 'bbb',
89+
locale: 'en',
90+
config: {device: 'ccc'}
91+
}
92+
93+
const ctx = await app.withContext(params)
94+
95+
assert.equal(params.installedAppId, ctx.installedAppId)
96+
assert.equal(params.locationId, ctx.locationId)
97+
assert.equal(params.authToken, ctx.api.client.authToken)
98+
assert.equal(params.refreshToken, ctx.api.client.refreshToken)
99+
assert.equal(params.locale, ctx.event.locale)
100+
})
101+
})

0 commit comments

Comments
 (0)