-
Notifications
You must be signed in to change notification settings - Fork 54
/
schema.ts
367 lines (307 loc) · 8.97 KB
/
schema.ts
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { Endpoint } from '../endpoint'
import { EndpointClient, EndpointClientConfig } from '../endpoint-client'
import { SuccessStatusValue, Status } from '../types'
export interface SchemaApp extends SchemaAppRequest {
/**
* Viper endpoint app id for the partner
*/
endpointAppId?: string
/**
* user id for the partner
*/
userId?: string
/**
* Organization that this app belongs to
*/
organizationId?: string
/**
* Client ID assigned by SmartThings for this app
*/
stClientId?: string
/**
* Possible values - '', 'cst', 'wwst'
*/
certificationStatus?: string
}
export type PartnerSTConnection = 'connected' | 'disconnected'
export interface SchemaAppRequest {
/**
* The name of the endpoint app
*/
appName?: string
/**
* The name of the partner/brand
*/
partnerName?: string
/**
* oAuth authorization url of the partner
*/
oAuthAuthorizationUrl?: string
/**
* lambda arn of the partner for US region (default)
*/
lambdaArn?: string
/**
* lambda arn of the partner for EU region
*/
lambdaArnEU?: string
/**
* lambda arn of the partner for AP region
*/
lambdaArnAP?: string
/**
* lambda arn of the partner for CN region
*/
lambdaArnCN?: string
/**
* url of partner icon
*/
icon?: string
/**
* url of partner icon in 2x dimensions
*/
icon2x?: string
/**
* url of partner icon in 3x dimensions
*/
icon3x?: string
/**
* Client id for the partner oAuth
*/
oAuthClientId?: string
/**
* Client secret for the partner oAuth
*/
oAuthClientSecret?: string
/**
* oAuth token refresh url of the partner
*/
oAuthTokenUrl?: string
/**
* oAuth scope for the partner. Example 'remote_control:all' for Lifx
*/
oAuthScope?: string
/**
* Possible values - 'lambda' or 'webhook'
*/
hostingType?: string
/**
* Possible values - 'alexa-schema', 'st-schema', 'google-schema'
*/
schemaType?: string
/**
* webhook url for the partner
*/
webhookUrl?: string
/**
* email for the partner
*/
userEmail: string
/**
* Data to support deep-linking to partner's mobile app
*/
viperAppLinks?: ViperAppLinks
}
export interface ViperAppLinks {
android?: string
ios?: string
isLinkingEnabled?: boolean
}
interface SchemaAppList {
userId?: string
endpointApps: SchemaApp[]
}
export interface DeviceResult {
/**
* deviceId created by DM
*/
deviceId?: string
/**
* initial device name from the partner
*/
name?: string
}
export interface InstalledSchemaApp {
/**
* Possible values - __requiresLogin__ or __loggedIn__. These two values determine what fields are returned in
* this response. If value is "requiresLogin", only "oAuthLink" is returned in the response. If value is
* "loggedIn", only isaId, partnerName, appName, devices and icons are returned.
*/
pageType?: string
/**
* isaId (Installed App Id)
*/
isaId?: string
/**
* partner or brand name eg LIFX Inc.
*/
partnerName?: string
/**
* Connector name. eg Lifx (Connect)
*/
appName?: string
/**
* url of partner icon
*/
icon?: string
/**
* url of partner icon in 2x dimensions
*/
icon2x?: string
/**
* url of partner icon in 3x dimensions
*/
icon3x?: string
/**
* location of the installed smart app
*/
locationId?: string
devices?: DeviceResult[]
/**
* generated oAuth link for the user to login to partner server. This will only be returned when the user is not logged in.
*/
oAuthLink?: string
/**
* connection status between partner and ST platform
*/
partnerSTConnection?: PartnerSTConnection
}
interface InstalledSchemaAppList {
userId?: string
installedSmartApps: InstalledSchemaApp[]
}
export interface SchemaCreateResponse {
endpointAppId?: string
stClientId: string
stClientSecret: string
}
export enum SchemaPageType {
requiresLogin = 'requiresLogin',
loggedIn = 'loggedIn'
}
export interface SchemaPage {
/**
* The type of the page being returned, which is determined by the authentication state of the connector instance,
* i.e. 'requiresLogin' or 'loggedIn'
*/
pageType: SchemaPageType
}
export interface UnauthorizedSchemaPage extends SchemaPage {
/**
* An href to the OAuth page for this connector that allows authentication and connection to the SmartThings
* platform.
*/
oAuthLink?: string
}
export interface AuthorizedSchemaPage extends SchemaPage {
isaId?: string
locationId?: string
devices?: DeviceResult[]
icon?: string
icon2x?: string
icon3x?: string
partnerName?: string
appName?: string
}
export class SchemaEndpoint extends Endpoint {
constructor(config: EndpointClientConfig) {
super(new EndpointClient('schema', config))
}
/**
* Returns a list of all ST Schema C2C connectors belonging to the principal (i.e. the user)
*/
public async list(options?: { includeAllOrganizations?: boolean }): Promise<SchemaApp[]> {
// Querying Schema apps requires a different API for listing by organization in addition to
// the standard header other endpoints use.
const useOrganizationEndpoint = this.client.config.headers?.['X-ST-Organization'] || options?.includeAllOrganizations
const response = await this.client.get<SchemaAppList>('apps' + (useOrganizationEndpoint ? '/organizations/' : ''))
return response.endpointApps
}
/**
* Returns a specific ST Schema connector
*
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
*/
public get(id: string): Promise<SchemaApp> {
return this.client.get<SchemaApp>(`apps/${id}`)
}
/**
* Create an ST Schema connector
*
* @param data definition of the connector
* @param organizationId The organization to associate the connector with. You must be a member
* of the organization. Overrides any organization header included when creating the
* `SmartThingsClient`.
*/
public create(data: SchemaAppRequest, organizationId?: string): Promise<SchemaCreateResponse> {
const options = organizationId ? { headerOverrides: { 'X-ST-Organization': organizationId } } : undefined
return this.client.post<SchemaCreateResponse>('apps', data, undefined, options)
}
/**
* Update an ST Schema connector. The connector cannot be changed if the connector's
* `certificationStatus` is `wwst` or `cst`.
*
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
* @param data new definition of the connector
* @param organizationId The organization to associate the connector with. You must be a member
* of the organization.
*/
public async update(id: string, data: SchemaAppRequest, organizationId?: string): Promise<Status> {
const options = organizationId ? { headerOverrides: { 'X-ST-Organization': organizationId } } : undefined
await this.client.put<SchemaApp>(`apps/${id}`, data, undefined, options)
return SuccessStatusValue
}
/**
* Re-generate the OAuth clientId and clientSecret for an ST Schema connector. The old clientId and clientSecret
* will no longer be valid after this operation.
*
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
*/
public regenerateOauth(id: string): Promise<SchemaCreateResponse> {
return this.client.post<SchemaCreateResponse>('oauth/stclient/credentials', {endpointAppId: id})
}
/**
* Delete an ST Schema connector
*
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
*/
public async delete(id: string): Promise<Status> {
await this.client.delete<SchemaApp>(`apps/${id}`)
return SuccessStatusValue
}
/**
* Get the page definition of an ST Schema installed instance in the specified location.
*
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
* @param locationId UUID of the location in which the connector is or is to be installed.
*/
public getPage(id: string, locationId: string): Promise<AuthorizedSchemaPage | UnauthorizedSchemaPage> {
return this.client.get<SchemaPage>(`install/${id}?locationId=${locationId}&type=oauthLink`)
}
/**
* Returns a list of the installed ST Schema connector instances in the specified location
*
* @param locationId UUID of the location
*/
public async installedApps(locationId?: string): Promise<InstalledSchemaApp[]> {
const response = await this.client.get<InstalledSchemaAppList>(`installedapps/location/${this.locationId(locationId)}`)
return response === undefined ? [] : response.installedSmartApps
}
/**
* Returns a specific installed instance of an ST Schema connector. The returned object includes a list of the
* devices created by the instance.
*
* @param id UUID of the installed app instance
*/
public getInstalledApp(id: string): Promise<InstalledSchemaApp> {
return this.client.get(`installedapps/${id}`)
}
/**
* Deletes a specific installed instance of an ST Schema connector. This operation will also delete all
* devices created by this instance
*/
public async deleteInstalledApp(id: string): Promise<Status> {
await this.client.delete(`installedapps/${id}`)
return SuccessStatusValue
}
}