-
Notifications
You must be signed in to change notification settings - Fork 12
/
api.js
76 lines (62 loc) · 1.63 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
module.exports = {
async getScripts({ homey, query }) {
const scripts = await homey.app.getScripts();
const response = {};
for (const script of Object.values(scripts)) {
response[script.id] = {
...script,
code: undefined,
};
}
return response;
},
async getScript({ homey, params }) {
const { id } = params;
return homey.app.getScript({ id });
},
async runScript({ homey, params, body = {} }) {
const { id } = params;
const { code, args } = body;
try {
const script = await homey.app.getScript({ id });
const result = await homey.app.runScript({
id: script.id,
name: script.name,
code: code ?? script.code,
lastExecuted: script.lastExecuted,
version: script.version,
args,
}).finally(() => {
homey.app.updateScript({ id: script.id, lastExecuted: new Date() }).catch(() => {});
});
return {
success: true,
returns: result,
};
} catch (err) {
return {
success: false,
returns: {
message: err.message,
stack: err.stack,
},
};
}
},
async createScript({ homey, params, body = {} }) {
const { name, code } = body;
return homey.app.createScript({ name, code });
},
async updateScript({
homey, params, query, body = {},
}) {
const { id } = params;
const { name, code, version } = body;
return homey.app.updateScript({ id, name, code, version });
},
async deleteScript({ homey, params }) {
const { id } = params;
return homey.app.deleteScript({ id });
},
};