UNPKG

node-hue-api

Version:
124 lines (123 loc) 3.6 kB
import { ApiError } from '../ApiError'; export class HueApiConfig { constructor(config, transport, remoteApi) { this._config = config; this._remoteApi = remoteApi; this._transport = transport; this._isRemote = !!config.remote && remoteApi != undefined; } /** * Is the connection to the hue bridge remote. */ get isRemote() { return this._isRemote; } /** * Gets the transport implementation that is used to connect with the Hue Bridge */ get transport() { return this._transport; } /** * Gets the remote API in use that was used to bootstrap the remote connection. * @throws ApiError if the connection is local network. */ get remote() { if (this.isRemote) { // @ts-ignore return this._remoteApi; } else { throw new ApiError('This API has not been set up as a remote API'); } } /** * Gets the current username used to connect/interact with the Hue Bridge. */ get username() { return this._config.username; } /** * Gets the client id for the remote OAuth connection. * @throws ApiError if the connection is not remote. */ get clientId() { this._requireRemote(); // @ts-ignore return this._config.clientId; } /** * Gets the client secret for the remote OAuth connection. * @throws ApiError if the connection is not remote. */ get clientSecret() { this._requireRemote(); return this._config.clientSecret; } /** * The Base URL for communication with the bridge. * @returns The base URL for the hue bridge. */ get baseUrl() { return this._config.baseUrl; } /** * Gets the name of the hue bridge. * @returns string The name for the bridge. */ get bridgeName() { return this._config.bridgeName; } /** * Gets the client key for the entertainment API/streaming endpoints * @throws ApiError if the connection is not local network. */ get clientKey() { this._requireLocal(); return this._config.clientKey; } /** * Gets the current access token. * @returns {String} * @throws ApiError if the connection is not remote. */ get accessToken() { return this.getRequiredRemote().accessToken; } /** * Gets the expiry timestamp of the access token. * @returns {number} The timestamp for the expiry or -1 if not known */ get accessTokenExpiry() { return this.getRequiredRemote().accessTokenExpiry; } /** * Gets the current refresh token. * @throws ApiError if the connection is not remote. */ get refreshToken() { return this.getRequiredRemote().refreshToken; } /** * Gets the expiry timestamp of the refresh token. * @returns {number} The timestamp for the expiry or -1 if not known */ get refreshTokenExpiry() { return this.getRequiredRemote().refreshTokenExpiry; } getRequiredRemote() { this._requireRemote(); // The above call will throw an error if we are not remote return this._remoteApi; } _requireRemote() { if (!this.isRemote) { throw new ApiError('The function is only valid on a remote Hue API instance'); } } _requireLocal() { if (this.isRemote) { throw new ApiError('The function is only valid on a local Hue API instance'); } } }