-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
316 lines (293 loc) · 8.16 KB
/
extension.js
File metadata and controls
316 lines (293 loc) · 8.16 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import EventBus from './event-bus.js'
import Media from './media.js'
import Parameter from './parameter.js'
import Readme from './readme.js'
import Manifest from './manifest.js'
import eventTypes from './event-types.json' with { type: 'json' }
import categories from './categories.json' with { type: 'json' }
import pkg from './package.json' with { type: 'json' }
/**
* Represents an extension.
* @class
*/
export default class Extension {
#callbacks = {}
#category = null
#demo = false
#enabled = false
#eventBus = null
#features = []
#functions = []
#functionSchemas = []
#icon = null
#installationSteps = []
#instructions = null
#name = null
#parameters = []
#started = false
#website = null
async #setup() {
this.#eventBus = new EventBus(
() => {
return {
category: this.#category,
demo: this.#demo,
icon: this.#icon,
installationSteps: this.#installationSteps,
instructions: this.#instructions,
name: this.#name,
parameters: this.#parameters,
sdk: {
name: 'nodejs',
version: pkg.version,
},
functionSchemas: this.#functionSchemas,
}
},
() => {
return {
callbacks: this.#callbacks,
functions: this.#functions,
parameters: this.#parameters,
}
},
(enabled) => {
this.#enabled = enabled
},
)
new Readme(() => {
return {
features: this.#features,
name: this.#name,
}
})
new Manifest(() => {
return {
category: this.#category,
demo: this.#demo,
features: this.#features,
icon: this.#icon,
name: this.#name,
sdk: {
name: 'nodejs',
version: pkg.version,
},
website: this.#website,
}
})
}
/**
* @returns {Extension} The starting extension instance.
*/
start() {
if (this.#started) return
this.#started = true
setTimeout(() => this.#setup(), 500)
return this
}
/**
* @param {String} eventType - The event type.
* @param {Function} cb - The callback function.
* @returns {Extension} The updated extension instance.
*/
on(eventType, cb) {
if (!eventTypes.includes(eventType)) {
throw new Error(`eventType must be one of the following strings: ${eventTypes.join(', ')}`)
}
if (typeof cb !== 'function') {
throw new Error('cb must be a function.')
}
this.#callbacks[eventType] = cb
return this
}
/**
* @returns {Boolean} If the extension is enabled.
*/
isEnabled() {
return this.#enabled
}
/**
* @param {Boolean} demo - If the extension is a demonstration.
* @returns {Extension} The updated extension instance.
*/
setDemo(demo) {
if (typeof demo !== 'boolean') {
throw new Error('name must be a boolean.')
}
this.#demo = demo
return this
}
/**
* @param {String} name - The name of the extension.
* @returns {Extension} The updated extension instance.
*/
setName(name) {
if (typeof name !== 'string' || name.trim() === '') {
throw new Error('name must be a non-empty string.')
}
this.#name = name
return this
}
/**
* @param {String} icon - The icon url of the extension.
* @returns {Extension} The updated extension instance.
*/
setIcon(icon) {
if (typeof icon !== 'string' || icon.trim() === '') {
throw new Error('icon must be a non-empty string.')
}
try {
new URL(icon)
} catch (_) {
throw new Error('icon must be a valid URL.')
}
this.#icon = icon
return this
}
/**
* @param {String} website - The website url of the extension.
* @returns {Extension} The updated extension instance.
*/
setWebsite(website) {
if (typeof website !== 'string' || website.trim() === '') {
throw new Error('website must be a non-empty string.')
}
try {
new URL(website)
} catch (_) {
throw new Error('website must be a valid URL.')
}
this.#website = website
return this
}
/**
* @param {String} category - The category of the extension.
* @returns {Extension} The updated extension instance.
*/
setCategory(category) {
if (!categories.includes(category)) {
throw new Error(`category must be one of the following strings: ${categories.join(', ')}`)
}
this.#category = category
return this
}
/**
* @param {Array<String>} features - The features of the extension.
* @returns {Extension} The updated extension instance.
*/
setFeatures(features) {
if (
!Array.isArray(features) ||
!features.every((i) => typeof i === 'string' && i.trim() !== '')
) {
throw new TypeError('features must be an array of non-empty strings.')
}
this.#features = features
return this
}
/**
* @param {Array<String>} installationSteps - The installation steps of the extension.
* @returns {Extension} The updated extension instance.
*/
setinstallationSteps(installationSteps) {
if (
!Array.isArray(installationSteps) ||
!installationSteps.every((i) => typeof i === 'string' && i.trim() !== '')
) {
throw new TypeError('installationSteps must be an array of non-empty strings.')
}
this.#installationSteps = installationSteps
return this
}
/**
* @param {Array<Parameter>} parameters - The parameters of the extension.
* @returns {Extension} The updated extension instance.
*/
setParameters(parameters) {
if (!Array.isArray(parameters) || !parameters.every((f) => typeof Parameter)) {
throw new TypeError('parameters must be an array of Parameter instances.')
}
this.#parameters = parameters
return this
}
/**
* @param {String} instructions - The instructions of the extension.
* @returns {Extension} The updated extension instance.
*/
setInstructions(instructions) {
if (typeof instructions !== 'string' || instructions.trim() === '') {
throw new Error('instructions must be a non-empty string.')
}
this.#instructions = instructions
return this
}
/**
* @param {Array<Object>} functionSchemas - The function schemas of the extension.
* @returns {Extension} The updated extension instance.
*/
setFunctionSchemas(functionSchemas) {
if (
!Array.isArray(functionSchemas) ||
!functionSchemas.every((schema) => typeof schema === 'object' && schema !== null)
) {
throw new Error('functionSchemas must be an array of non-null objects.')
}
this.#functionSchemas = functionSchemas
return this
}
/**
* @param {Array<Function>} functions - The functions of the extension.
* @returns {Extension} The updated extension instance.
*/
setFunctions(functions) {
if (
!Array.isArray(functions) ||
!this.#functions.every((fn) => typeof fn === 'function') ||
!this.#functions.every((fn) => fn.name.trim().length > 0)
) {
throw new Error('functions must be an array of named functions.')
}
this.#functions = functions
return this
}
/**
* Enables alarm.
*/
enableAlarm() {
this.#eventBus.publishEvent({ type: 'alarm' })
}
/**
* Send one or more medias.
* @param {Media|Array<Media>} medias - The medias.
*/
sendMedias(medias) {
medias = Array.isArray(medias) ? medias : [medias]
if (!medias.every((item) => item instanceof Media)) {
throw new Error('medias must be an array of Media instances.')
}
this.#eventBus.publishEvent({
type: 'medias',
medias: medias.map((media) => media.toJSON()),
})
}
/**
* Send a message.
* @param {String} text - The text of the message.
*/
sendMessage(text) {
if (typeof text !== 'string' || text.trim() === '') {
throw new TypeError('text must be a non-empty string.')
}
this.#eventBus.publishEvent({ type: 'message', text })
}
/**
* Send a notification.
* @param {String} text - The text of the notification.
*/
sendNotification(text) {
if (typeof text !== 'string' || text.trim() === '') {
throw new TypeError('text must be a non-empty string.')
}
this.#eventBus.publishEvent({ type: 'notification', text })
}
}