Skip to content

Commit ecb8e3b

Browse files
committed
fix: fix return type of rules execute function; update dependencies
The API for executing rules returned more data than we were using. The method calling this API has been updated to match it. BREAKING CHANGE: the return type of RulesEndpoint.execute is different
1 parent 226d44c commit ecb8e3b

File tree

11 files changed

+2672
-9235
lines changed

11 files changed

+2672
-9235
lines changed

package-lock.json

Lines changed: 2336 additions & 8380 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,37 @@
1919
"dist/**/*"
2020
],
2121
"dependencies": {
22-
"async-mutex": "^0.2.1",
22+
"async-mutex": "^0.3.2",
2323
"axios": "^0.21.4",
24-
"http-signature": "^1.3.4",
25-
"qs": "^6.9.3",
26-
"sshpk": "^1.16.1",
27-
"underscore": "^1.10.2"
24+
"http-signature": "^1.3.6",
25+
"qs": "^6.10.3",
26+
"sshpk": "^1.17.0",
27+
"underscore": "^1.13.2"
2828
},
2929
"devDependencies": {
3030
"@commitlint/cli": "^16.0.2",
3131
"@commitlint/config-conventional": "^16.0.0",
3232
"@semantic-release/changelog": "^6.0.1",
3333
"@semantic-release/git": "^10.0.1",
34-
"@types/jest": "^26.0.10",
35-
"@types/node": "^12.11.7",
36-
"@types/qs": "^6.9.1",
37-
"@types/sshpk": "^1.10.4",
38-
"@types/underscore": "^1.9.4",
39-
"@typescript-eslint/eslint-plugin": "^5.8.1",
40-
"@typescript-eslint/parser": "^5.8.1",
34+
"@types/jest": "^27.4.0",
35+
"@types/node": "^17.0.9",
36+
"@types/qs": "^6.9.7",
37+
"@types/sshpk": "^1.10.7",
38+
"@types/underscore": "^1.11.4",
39+
"@typescript-eslint/eslint-plugin": "^5.10.0",
40+
"@typescript-eslint/parser": "^5.10.0",
4141
"conventional-changelog-conventionalcommits": "^4.6.3",
4242
"cz-conventional-changelog": "^3.3.0",
43-
"eslint": "^8.5.0",
43+
"eslint": "^8.7.0",
4444
"eslint-config-prettier": "^8.3.0",
4545
"eslint-plugin-eslint-comments": "^3.2.0",
46-
"eslint-plugin-import": "^2.25.3",
47-
"eslint-plugin-jest": "^23.8.2",
48-
"jest": "^26.4.2",
46+
"eslint-plugin-import": "^2.25.4",
47+
"eslint-plugin-jest": "^25.7.0",
48+
"jest": "^27.4.7",
4949
"prettier": "^2.5.1",
50-
"semantic-release": "^18.0.1",
51-
"ts-jest": "^26.3.0",
52-
"typedoc": "^0.20.36",
50+
"semantic-release": "^19.0.2",
51+
"ts-jest": "^27.1.3",
52+
"typedoc": "^0.22.11",
5353
"typescript": "^4.5.4"
5454
},
5555
"scripts": {

src/authenticator.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ export interface RefreshTokenStore {
8989
}
9090

9191
/**
92-
* An authenticator that supports refreshing of the access token using a refresh token by loading the refresh token,
93-
* client ID, and client secret from a token store, performing the refresh, and storing the new tokens.
92+
* An authenticator that supports refreshing of the access token using a refresh token by loading
93+
* the refresh token, client ID, and client secret from a token store, performing the refresh, and
94+
* storing the new tokens.
95+
*
96+
* Note that corruption of the refresh token is unlikely but possible if two of the same
97+
* authenticators refresh the same token at the same time.
9498
*/
9599
export class RefreshTokenAuthenticator implements Authenticator {
96100
constructor(public token: string, private tokenStore: RefreshTokenStore) {
@@ -129,14 +133,21 @@ export class RefreshTokenAuthenticator implements Authenticator {
129133
refreshToken: response.data.refresh_token,
130134
}
131135
this.token = authData.authToken
132-
requestConfig.headers.Authorization = `Bearer ${this.token}`
136+
requestConfig.headers = { ...(requestConfig.headers ?? {}), Authorization: `Bearer ${this.token}` }
133137
return this.tokenStore.putAuthData(authData)
134138
}
135139

136140
throw Error(`error ${response.status} refreshing token, with message ${response.data}`)
137141
}
138142
}
139143

144+
/**
145+
* A an authenticator that works like RefreshTokenAuthenticator but which can use a mutex to help
146+
* prevent corruption of the refresh token.
147+
*
148+
* Note that while `acquireRefreshMutex` is provided for you to use the mutex, the mutex is not
149+
* automatically used.
150+
*/
140151
export class SequentialRefreshTokenAuthenticator extends RefreshTokenAuthenticator {
141152
constructor(token: string, tokenStore: RefreshTokenStore, private refreshMutex: MutexInterface) {
142153
super(token, tokenStore)

src/endpoint/rules.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,41 @@ export interface Rule extends RuleRequest {
295295
id: string
296296
}
297297

298+
export type ExecutionResult = 'Success' | 'Failure' | 'Ignored'
299+
300+
export type IfExecutionResult = 'True' | 'False'
301+
export interface IfActionExecutionResult {
302+
result: IfExecutionResult
303+
}
304+
export interface LocationActionExecutionResult {
305+
result: ExecutionResult
306+
locationId: string
307+
}
308+
309+
export type CommandExecutionResult = 'Success' | 'Failure' | 'Offline'
310+
export interface CommandActionExecutionResult {
311+
result: CommandExecutionResult
312+
deviceId: string
313+
}
314+
315+
export interface SleepActionExecutionResult {
316+
result: ExecutionResult
317+
}
318+
319+
export interface ActionExecutionResult {
320+
actionId: string
321+
if?: IfActionExecutionResult
322+
location?: LocationActionExecutionResult
323+
command?: CommandActionExecutionResult[]
324+
sleep?: SleepActionExecutionResult
325+
}
326+
export interface ExecuteResponse {
327+
executionId: string
328+
id: string
329+
result: string
330+
actions?: ActionExecutionResult[]
331+
}
332+
298333
export class RulesEndpoint extends Endpoint {
299334
constructor(config: EndpointClientConfig) {
300335
super(new EndpointClient('rules', config))
@@ -308,7 +343,7 @@ export class RulesEndpoint extends Endpoint {
308343
* can be omitted
309344
*/
310345
public list(locationId?: string): Promise<Rule[]> {
311-
return this.client.getPagedItems<Rule>(undefined, {locationId: this.locationId(locationId)})
346+
return this.client.getPagedItems<Rule>(undefined, { locationId: this.locationId(locationId) })
312347
}
313348

314349
/**
@@ -318,7 +353,7 @@ export class RulesEndpoint extends Endpoint {
318353
* can be omitted
319354
*/
320355
public get(id: string, locationId?: string): Promise<Rule> {
321-
return this.client.get<Rule>(id, {locationId: this.locationId(locationId)})
356+
return this.client.get<Rule>(id, { locationId: this.locationId(locationId) })
322357
}
323358

324359
/**
@@ -328,7 +363,7 @@ export class RulesEndpoint extends Endpoint {
328363
* can be omitted
329364
*/
330365
public async delete(id: string, locationId?: string): Promise<Status> {
331-
await this.client.delete(id, {locationId: this.locationId(locationId)})
366+
await this.client.delete(id, { locationId: this.locationId(locationId) })
332367
return SuccessStatusValue
333368
}
334369

@@ -339,7 +374,7 @@ export class RulesEndpoint extends Endpoint {
339374
* can be omitted
340375
*/
341376
public create(data: RuleRequest, locationId?: string): Promise<Rule> {
342-
return this.client.post(undefined, data, {locationId: this.locationId(locationId)})
377+
return this.client.post(undefined, data, { locationId: this.locationId(locationId) })
343378
}
344379

345380
/**
@@ -350,7 +385,7 @@ export class RulesEndpoint extends Endpoint {
350385
* can be omitted
351386
*/
352387
public update(id: string, data: RuleRequest, locationId?: string): Promise<Rule> {
353-
return this.client.put(id, data, {locationId: this.locationId(locationId)})
388+
return this.client.put(id, data, { locationId: this.locationId(locationId) })
354389
}
355390

356391
/**
@@ -359,8 +394,7 @@ export class RulesEndpoint extends Endpoint {
359394
* @param locationId UUID of the location, If the client is configured with a location ID this parameter
360395
* can be omitted
361396
*/
362-
public async execute(id: string, locationId?: string): Promise<Status> {
363-
await this.client.post(`execute/${id}`, undefined, {locationId: this.locationId(locationId)})
364-
return SuccessStatusValue
397+
public async execute(id: string, locationId?: string): Promise<ExecuteResponse> {
398+
return this.client.post(`execute/${id}`, undefined, { locationId: this.locationId(locationId) })
365399
}
366400
}

0 commit comments

Comments
 (0)