Skip to content

Commit 9eb651c

Browse files
committed
maybe a fix?
1 parent 8d7c906 commit 9eb651c

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"readmeFilename": "README.md",
5252
"dependencies": {
53-
"@stoprocent/noble": "^1.17.0",
53+
"@stoprocent/noble": "^1.17.1",
5454
"async-mutex": "^0.5.0",
5555
"undici": "^7.2.0"
5656
},

src/switchbot-openapi.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ export class SwitchBotOpenAPI extends EventEmitter {
9090

9191
/**
9292
* Retrieves the list of devices from the SwitchBot OpenAPI.
93-
*
93+
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
94+
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
9495
* @returns {Promise<{ response: body, statusCode: number }>} A promise that resolves to an object containing the API response.
9596
* @throws {Error} Throws an error if the request to get devices fails.
9697
*/
@@ -112,11 +113,13 @@ export class SwitchBotOpenAPI extends EventEmitter {
112113
/**
113114
* Controls a device by sending a command to the SwitchBot API.
114115
*
115-
* @param deviceId - The unique identifier of the device to control.
116+
* @param deviceId - The ID of the device to control.
116117
* @param command - The command to send to the device.
117118
* @param parameter - The parameter for the command.
118-
* @param commandType - The type of the command, defaults to 'command'.
119-
* @returns {Promise<{ response: pushResponse['body'], statusCode: pushResponse['statusCode'] }>} A promise that resolves to an object containing the API response.
119+
* @param commandType - The type of the command (default is 'command').
120+
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
121+
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
122+
* @returns A promise that resolves to an object containing the response body and status code.
120123
* @throws An error if the device control fails.
121124
*/
122125
async controlDevice(deviceId: string, command: string, parameter: string, commandType: string = 'command', token?: string, secret?: string): Promise<{ response: pushResponse['body'], statusCode: pushResponse['statusCode'] }> {
@@ -146,7 +149,9 @@ export class SwitchBotOpenAPI extends EventEmitter {
146149
* Retrieves the status of a specific device.
147150
*
148151
* @param deviceId - The unique identifier of the device.
149-
* @returns {Promise<{ response: deviceStatus, statusCode: deviceStatusRequest['statusCode'] }>} A promise that resolves to the device status.
152+
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
153+
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
154+
* @returns A promise that resolves to an object containing the device status and the status code of the request.
150155
* @throws An error if the request fails.
151156
*/
152157
async getDeviceStatus(deviceId: string, token?: string, secret?: string): Promise<{ response: deviceStatus, statusCode: deviceStatusRequest['statusCode'] }> {
@@ -170,22 +175,23 @@ export class SwitchBotOpenAPI extends EventEmitter {
170175
/**
171176
* Generates the headers required for authentication with the SwitchBot OpenAPI.
172177
*
173-
* @returns An object containing the following headers:
174-
* - `Authorization`: The token used for authorization.
175-
* - `sign`: The HMAC-SHA256 signature of the concatenated token, timestamp, and nonce.
176-
* - `nonce`: A unique identifier for the request, formatted as a UUID.
177-
* - `t`: The current timestamp in milliseconds since the Unix epoch.
178-
* - `Content-Type`: The content type of the request, set to `application/json`.
178+
* @param configToken - The token used for authorization.
179+
* @param configSecret - The secret key used to sign the request.
180+
* @returns An object containing the necessary headers:
181+
* - `Authorization`: The authorization token.
182+
* - `sign`: The HMAC-SHA256 signature of the token, timestamp, and nonce.
183+
* - `nonce`: A unique identifier for the request.
184+
* - `t`: The current timestamp in milliseconds.
185+
* - `Content-Type`: The content type of the request, set to 'application/json'.
179186
*/
180-
private generateHeaders = (configToken: string, configSecret: string): { 'Authorization': string, 'sign': string, 'nonce': `${string}-${string}-${string}-${string}-${string}`, 't': string, 'Content-Type': string } => {
187+
private generateHeaders = (configToken: string, configSecret: string): { 'Authorization': string, 'sign': string, 'nonce': string, 't': string, 'Content-Type': string } => {
181188
const t = `${Date.now()}`
182189
const nonce = randomUUID()
183190
const data = configToken + t + nonce
184-
const signTerm = crypto
191+
const sign = crypto
185192
.createHmac('sha256', configSecret)
186-
.update(Buffer.from(data, 'utf-8'))
187-
.digest()
188-
const sign = signTerm.toString('base64')
193+
.update(data)
194+
.digest('base64')
189195

190196
return {
191197
'Authorization': configToken,
@@ -206,6 +212,8 @@ export class SwitchBotOpenAPI extends EventEmitter {
206212
* 4. Sends a request to query the current webhook URL.
207213
*
208214
* @param url - The URL to which the webhook events will be sent.
215+
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
216+
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
209217
* @returns A promise that resolves when the webhook setup is complete.
210218
*
211219
* @throws Will log an error if any step in the webhook setup process fails.
@@ -313,6 +321,8 @@ export class SwitchBotOpenAPI extends EventEmitter {
313321
* Deletes a webhook by sending a request to the specified URL.
314322
*
315323
* @param url - The URL of the webhook to be deleted.
324+
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
325+
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
316326
* @returns A promise that resolves when the webhook is successfully deleted.
317327
*
318328
* @throws Will log an error if the deletion fails.

0 commit comments

Comments
 (0)