Skip to content

Commit aadb5d3

Browse files
committed
feat: add ability to include organization with schema creation and update
1 parent 87734c1 commit aadb5d3

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

.changeset/pink-bananas-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smartthings/core-sdk": minor
3+
---
4+
5+
add ability to include organization with schema creation and update

src/endpoint/schema.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ export interface DeviceResult {
157157

158158
export interface InstalledSchemaApp {
159159
/**
160-
* 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.
160+
* Possible values - __requiresLogin__ or __loggedIn__. These two values determine what fields are returned in
161+
* this response. If value is "requiresLogin", only "oAuthLink" is returned in the response. If value is
162+
* "loggedIn", only isaId, partnerName, appName, devices and icons are returned.
161163
*/
162164
pageType?: string
163165

@@ -235,7 +237,7 @@ export interface SchemaPage {
235237
export interface UnauthorizedSchemaPage extends SchemaPage {
236238
/**
237239
* An href to the OAuth page for this connector that allows authentication and connection to the SmartThings
238-
* patform.
240+
* platform.
239241
*/
240242
oAuthLink?: string
241243
}
@@ -269,6 +271,7 @@ export class SchemaEndpoint extends Endpoint {
269271

270272
/**
271273
* Returns a specific ST Schema connector
274+
*
272275
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
273276
*/
274277
public get(id: string): Promise<SchemaApp> {
@@ -277,25 +280,35 @@ export class SchemaEndpoint extends Endpoint {
277280

278281
/**
279282
* Create an ST Schema connector
283+
*
280284
* @param data definition of the connector
285+
* @param organizationId The organization to associate the connector with. You must be a member
286+
* of the organization. Overrides any organization header included when creating the
287+
* `SmartThingsClient`.
281288
*/
282-
public create(data: SchemaAppRequest): Promise<SchemaCreateResponse> {
283-
return this.client.post<SchemaCreateResponse>('apps', data)
289+
public create(data: SchemaAppRequest, organizationId?: string): Promise<SchemaCreateResponse> {
290+
const options = organizationId ? { headerOverrides: { 'X-ST-Organization': organizationId } } : undefined
291+
return this.client.post<SchemaCreateResponse>('apps', data, undefined, options)
284292
}
285293

286294
/**
287295
* Update an ST Schema connector
296+
*
288297
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
289298
* @param data new definition of the connector
299+
* @param organizationId The organization to associate the connector with. You must be a member
300+
* of the organization. The organization cannot be changed if the connector's `certificationStatus` is `wwst`.
290301
*/
291-
public async update(id: string, data: SchemaAppRequest): Promise<Status> {
292-
await this.client.put<SchemaApp>(`apps/${id}`, data)
302+
public async update(id: string, data: SchemaAppRequest, organizationId?: string): Promise<Status> {
303+
const options = organizationId ? { headerOverrides: { 'X-ST-Organization': organizationId } } : undefined
304+
await this.client.put<SchemaApp>(`apps/${id}`, data, undefined, options)
293305
return SuccessStatusValue
294306
}
295307

296308
/**
297309
* Re-generate the OAuth clientId and clientSecret for an ST Schema connector. The old clientId and clientSecret
298310
* will no longer be valid after this operation.
311+
*
299312
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
300313
*/
301314
public regenerateOauth(id: string): Promise<SchemaCreateResponse> {
@@ -304,6 +317,7 @@ export class SchemaEndpoint extends Endpoint {
304317

305318
/**
306319
* Delete an ST Schema connector
320+
*
307321
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
308322
*/
309323
public async delete(id: string): Promise<Status> {
@@ -313,6 +327,7 @@ export class SchemaEndpoint extends Endpoint {
313327

314328
/**
315329
* Get the page definition of an ST Schema installed instance in the specified location.
330+
*
316331
* @param id the "endpointApp" UUID of the connector, e.g. "viper_799ff3a0-8249-11e9-9bf1-b5c7d651c2c3"
317332
* @param locationId UUID of the location in which the connector is or is to be installed.
318333
*/
@@ -322,6 +337,7 @@ export class SchemaEndpoint extends Endpoint {
322337

323338
/**
324339
* Returns a list of the installed ST Schema connector instances in the specified location
340+
*
325341
* @param locationId UUID of the location
326342
*/
327343
public async installedApps(locationId?: string): Promise<InstalledSchemaApp[]> {
@@ -332,6 +348,7 @@ export class SchemaEndpoint extends Endpoint {
332348
/**
333349
* Returns a specific installed instance of an ST Schema connector. The returned object includes a list of the
334350
* devices created by the instance.
351+
*
335352
* @param id UUID of the installed app instance
336353
*/
337354
public getInstalledApp(id: string): Promise<InstalledSchemaApp> {
@@ -341,7 +358,6 @@ export class SchemaEndpoint extends Endpoint {
341358
/**
342359
* Deletes a specific installed instance of an ST Schema connector. This operation will also delete all
343360
* devices created by this instance
344-
* @param id
345361
*/
346362
public async deleteInstalledApp(id: string): Promise<Status> {
347363
await this.client.delete(`installedapps/${id}`)

test/unit/schema.test.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,67 @@ describe('Schema', () => {
4242
it('Create app', async () => {
4343
const app = { appName: 'Test app' } as SchemaAppRequest
4444
postSpy.mockResolvedValueOnce(app)
45+
4546
const response = await client.schema.create(app)
46-
expect(postSpy).toHaveBeenCalledWith('apps', app)
47+
48+
expect(postSpy).toHaveBeenCalledWith('apps', app, undefined, undefined)
49+
expect(response).toStrictEqual(app)
50+
})
51+
52+
it('Create app with organization', async () => {
53+
const app = { appName: 'Test app' } as SchemaAppRequest
54+
postSpy.mockResolvedValueOnce(app)
55+
56+
const response = await client.schema.create(app, 'organization-id')
57+
58+
expect(postSpy).toHaveBeenCalledWith(
59+
'apps',
60+
app,
61+
undefined,
62+
{ headerOverrides: { 'X-ST-Organization': 'organization-id' }},
63+
)
4764
expect(response).toStrictEqual(app)
4865
})
4966

5067
it('Update app', async () => {
5168
const app = { appName: 'Test app (modified)' } as SchemaAppRequest
52-
const id = 'viper_app_id'
69+
const id = 'schema-app-id'
5370
putSpy.mockResolvedValueOnce(app)
71+
5472
const response: Status = await client.schema.update(id, app as SchemaAppRequest)
55-
expect(putSpy).toHaveBeenCalledWith(`apps/${id}`, app)
73+
74+
expect(putSpy).toHaveBeenCalledWith(`apps/${id}`, app, undefined, undefined)
75+
expect(response).toEqual(SuccessStatusValue)
76+
})
77+
78+
it('Update app with organization', async () => {
79+
const app = { appName: 'Test app (modified)' } as SchemaAppRequest
80+
const id = 'schema-app-id'
81+
putSpy.mockResolvedValueOnce(app)
82+
83+
const response: Status = await client.schema.update(id, app as SchemaAppRequest, 'organization-id')
84+
85+
expect(putSpy).toHaveBeenCalledWith(
86+
`apps/${id}`,
87+
app, undefined,
88+
{ headerOverrides: { 'X-ST-Organization': 'organization-id' }},
89+
)
5690
expect(response).toEqual(SuccessStatusValue)
5791
})
5892

5993
it('Regenerate OAuth', async () => {
60-
const app = { endpointAppId: 'viper_app_id', stClientId: 'xxx', stClientSecret: 'yyy' } as SchemaCreateResponse
94+
const app = { endpointAppId: 'schema-app-id', stClientId: 'xxx', stClientSecret: 'yyy' } as SchemaCreateResponse
6195
postSpy.mockResolvedValueOnce(app)
62-
const response = await client.schema.regenerateOauth('viper_app_id')
63-
expect(postSpy).toHaveBeenCalledWith('oauth/stclient/credentials', { endpointAppId: 'viper_app_id' })
96+
const response = await client.schema.regenerateOauth('schema-app-id')
97+
expect(postSpy).toHaveBeenCalledWith('oauth/stclient/credentials', { endpointAppId: 'schema-app-id' })
6498
expect(response).toStrictEqual(app)
6599
})
66100

67101
it('Get page', async () => {
68102
const page = { pageType: 'requiresLogin' }
69103
getSpy.mockResolvedValueOnce(page)
70-
const response = await client.schema.getPage('viper_app_id', 'location_id')
71-
expect(getSpy).toHaveBeenCalledWith('install/viper_app_id?locationId=location_id&type=oauthLink')
104+
const response = await client.schema.getPage('schema-app-id', 'location_id')
105+
expect(getSpy).toHaveBeenCalledWith('install/schema-app-id?locationId=location_id&type=oauthLink')
72106
expect(response).toStrictEqual(page)
73107
})
74108

0 commit comments

Comments
 (0)