node-hue-api
Version:
Philips Hue API Library for Node.js
77 lines (76 loc) • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Remote = void 0;
const ApiError_1 = require("../ApiError");
class Remote {
constructor(hueApi) {
this._hueApi = hueApi;
}
/**
* Exchanges the code for a token on the remote API.
* @param code The code to exchange for a new token.
* @returns The token from the remote API.
*/
getToken(code) {
return this._getRemoteApi().getToken(code);
}
/**
* Will refresh the OAuth tokens on the remote API, exchanging the existing ones for new ones.
* @returns The new access and refresh tokens.
*/
refreshTokens() {
const self = this, remoteApi = self._getRemoteApi();
if (!remoteApi.refreshToken) {
return Promise.reject(new ApiError_1.ApiError('Cannot refresh tokens without a refresh token.'));
}
return remoteApi.refreshTokens(remoteApi.refreshToken)
.then((tokens) => {
// Update the authentication details for existing connections
self._getHueApi()._getTransport().refreshAuthorizationHeader(tokens.accessToken.value);
return tokens;
});
}
/**
* Creates a new remote user for the Hue Bridge.
*
* @param remoteBridgeId The is of the hue bridge on the remote portal
* @param deviceType The user device type identifier.
*/
createRemoteUser(remoteBridgeId, deviceType) {
return this._getRemoteApi().createRemoteUsername(remoteBridgeId, deviceType);
}
/** Obtains the remote access credentials that are in use for the remote connection. */
getRemoteAccessCredentials() {
const config = this._getHueApi()._getConfig();
const result = {
clientId: config.clientId,
clientSecret: config.clientSecret,
username: config.username,
tokens: {},
};
if (config.accessToken) {
// @ts-ignore it is set above
result.tokens.access = {
value: config.accessToken,
expiresAt: config.accessTokenExpiry
};
}
if (config.refreshToken) {
// @ts-ignore it is set above
result.tokens.refresh = {
value: config.refreshToken,
expiresAt: config.refreshTokenExpiry
};
}
return result;
}
/** @private */
_getHueApi() {
return this._hueApi;
}
/** @private */
_getRemoteApi() {
return this._getHueApi()._getRemote();
}
}
exports.Remote = Remote;