Skip to content

Commit 7e98a97

Browse files
committed
Add option to override hostname
1 parent 015610d commit 7e98a97

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

src/settings.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@
33
*
44
* © 2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
55
*/
6-
/**
7-
* This is the main url used to scan for Devices SwitchBot API
8-
*/
9-
export const baseURL = 'https://api.switch-bot.com/v1.1'
10-
11-
/**
12-
* This is the main url used to scan for Devices SwitchBot API
13-
*/
14-
export const devicesURL = 'https://api.switch-bot.com/v1.1/devices'
15-
16-
/**
17-
* This is the updateWebhook url used to access SwitchBot API
18-
*/
19-
export const setupWebhook = 'https://api.switch-bot.com/v1.1/webhook/setupWebhook'
6+
let baseURL = 'https://api.switch-bot.com'
207

21-
/**
22-
* This is the updateWebhook url used to access SwitchBot API
23-
*/
24-
export const queryWebhook = 'https://api.switch-bot.com/v1.1/webhook/queryWebhook'
8+
let devicesURL = `${baseURL}/v1.1/devices`
9+
let setupWebhook = `${baseURL}/v1.1/webhook/setupWebhook`
10+
let queryWebhook = `${baseURL}/v1.1/webhook/queryWebhook`
11+
let updateWebhook = `${baseURL}/v1.1/webhook/updateWebhook`
12+
let deleteWebhook = `${baseURL}/v1.1/webhook/deleteWebhook`
2513

2614
/**
27-
* This is the updateWebhook url used to access SwitchBot API
15+
* Updates the base URL for the SwitchBot API endpoints.
16+
* @param {string} newBaseURL - The new base URL to use.
2817
*/
29-
export const updateWebhook = 'https://api.switch-bot.com/v1.1/webhook/updateWebhook'
18+
export function updateBaseURL(newBaseURL: string): void {
19+
baseURL = newBaseURL
20+
devicesURL = `${baseURL}/v1.1/devices`
21+
setupWebhook = `${baseURL}/v1.1/webhook/setupWebhook`
22+
queryWebhook = `${baseURL}/v1.1/webhook/queryWebhook`
23+
updateWebhook = `${baseURL}/v1.1/webhook/updateWebhook`
24+
deleteWebhook = `${baseURL}/v1.1/webhook/deleteWebhook`
25+
}
3026

31-
/**
32-
* This is the deleteWebhook url used to access SwitchBot API
33-
*/
34-
export const deleteWebhook = 'https://api.switch-bot.com/v1.1/webhook/deleteWebhook'
27+
export const urls = {
28+
get baseURL() { return baseURL },
29+
get devicesURL() { return devicesURL },
30+
get setupWebhook() { return setupWebhook },
31+
get queryWebhook() { return queryWebhook },
32+
get updateWebhook() { return updateWebhook },
33+
get deleteWebhook() { return deleteWebhook },
34+
}
3535

3636
/**
3737
* constants used to access SwitchBot BLE API

src/switchbot-openapi.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { createServer } from 'node:http'
1616

1717
import { request } from 'undici'
1818

19-
import { baseURL, deleteWebhook, devicesURL, queryWebhook, setupWebhook, updateWebhook } from './settings.js'
19+
import { updateBaseURL, urls } from './settings.js'
2020

2121
/**
2222
* The `SwitchBotOpenAPI` class provides methods to interact with the SwitchBot OpenAPI.
@@ -66,11 +66,15 @@ export class SwitchBotOpenAPI extends EventEmitter {
6666
* @param token - The API token used for authentication.
6767
* @param secret - The secret key used for signing requests.
6868
*/
69-
constructor(token: string, secret: string) {
69+
constructor(token: string, secret: string, hostname?: string) {
7070
super()
7171
this.token = token
7272
this.secret = secret
73-
this.baseURL = baseURL
73+
this.baseURL = urls.baseURL
74+
75+
if (hostname) {
76+
updateBaseURL(hostname)
77+
}
7478
}
7579

7680
/**
@@ -91,7 +95,7 @@ export class SwitchBotOpenAPI extends EventEmitter {
9195
*/
9296
async getDevices(): Promise<{ response: devices, statusCode: number }> {
9397
try {
94-
const { body, statusCode } = await request(devicesURL, { headers: this.generateHeaders() })
98+
const { body, statusCode } = await request(urls.devicesURL, { headers: this.generateHeaders() })
9599
const response = await body.json() as devices
96100
this.emitLog('debug', `Got devices: ${JSON.stringify(response)}`)
97101
this.emitLog('debug', `statusCode: ${statusCode}`)
@@ -233,7 +237,7 @@ export class SwitchBotOpenAPI extends EventEmitter {
233237
}
234238

235239
try {
236-
const { body, statusCode } = await request(setupWebhook, {
240+
const { body, statusCode } = await request(urls.setupWebhook, {
237241
method: 'POST',
238242
headers: this.generateHeaders(),
239243
body: JSON.stringify({
@@ -252,7 +256,7 @@ export class SwitchBotOpenAPI extends EventEmitter {
252256
}
253257

254258
try {
255-
const { body, statusCode } = await request(updateWebhook, {
259+
const { body, statusCode } = await request(urls.updateWebhook, {
256260
method: 'POST',
257261
headers: this.generateHeaders(),
258262
body: JSON.stringify({
@@ -273,7 +277,7 @@ export class SwitchBotOpenAPI extends EventEmitter {
273277
}
274278

275279
try {
276-
const { body, statusCode } = await request(queryWebhook, {
280+
const { body, statusCode } = await request(urls.queryWebhook, {
277281
method: 'POST',
278282
headers: this.generateHeaders(),
279283
body: JSON.stringify({
@@ -302,7 +306,7 @@ export class SwitchBotOpenAPI extends EventEmitter {
302306
*/
303307
async deleteWebhook(url: string): Promise<void> {
304308
try {
305-
const { body, statusCode } = await request(deleteWebhook, {
309+
const { body, statusCode } = await request(urls.deleteWebhook, {
306310
method: 'POST',
307311
headers: this.generateHeaders(),
308312
body: JSON.stringify({

src/test/settings.test.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,33 @@ import {
55
CHAR_UUID_NOTIFY,
66
CHAR_UUID_WRITE,
77
COMMAND_TIMEOUT_MSEC,
8-
deleteWebhook,
9-
devicesURL,
10-
queryWebhook,
118
READ_TIMEOUT_MSEC,
129
SERV_UUID_PRIMARY,
13-
setupWebhook,
14-
updateWebhook,
10+
urls,
1511
WoSmartLockCommands,
1612
WoSmartLockProCommands,
1713
WRITE_TIMEOUT_MSEC,
1814
} from '../settings.js'
1915

2016
describe('switchBot API Settings', () => {
2117
it('should have correct Devices URL', () => {
22-
expect(devicesURL).toBe('https://api.switch-bot.com/v1.1/devices')
18+
expect(urls.devicesURL).toBe('https://api.switch-bot.com/v1.1/devices')
2319
})
2420

2521
it('should have correct setupWebhook URL', () => {
26-
expect(setupWebhook).toBe('https://api.switch-bot.com/v1.1/webhook/setupWebhook')
22+
expect(urls.setupWebhook).toBe('https://api.switch-bot.com/v1.1/webhook/setupWebhook')
2723
})
2824

2925
it('should have correct queryWebhook URL', () => {
30-
expect(queryWebhook).toBe('https://api.switch-bot.com/v1.1/webhook/queryWebhook')
26+
expect(urls.queryWebhook).toBe('https://api.switch-bot.com/v1.1/webhook/queryWebhook')
3127
})
3228

3329
it('should have correct updateWebhook URL', () => {
34-
expect(updateWebhook).toBe('https://api.switch-bot.com/v1.1/webhook/updateWebhook')
30+
expect(urls.updateWebhook).toBe('https://api.switch-bot.com/v1.1/webhook/updateWebhook')
3531
})
3632

3733
it('should have correct deleteWebhook URL', () => {
38-
expect(deleteWebhook).toBe('https://api.switch-bot.com/v1.1/webhook/deleteWebhook')
34+
expect(urls.deleteWebhook).toBe('https://api.switch-bot.com/v1.1/webhook/deleteWebhook')
3935
})
4036

4137
it('should have correct BLE API constants', () => {

0 commit comments

Comments
 (0)