balena-register-device
Version:
Balena device registration utilities
125 lines (121 loc) • 4.52 kB
JavaScript
;
/*
Copyright 2016-2020 Balena Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiError = exports.getRegisterDevice = void 0;
const tslib_1 = require("tslib");
const uuid_1 = require("uuid");
const typed_error_1 = require("typed-error");
/**
* @summary Creates a Balena Register Device instance
* @function
* @public
*
* @returns {Object} Balena Register Device instance { generateUniqueKey: ..., register: ... }
*/
const getRegisterDevice = ({ request, }) => ({
/**
* @summary Generate a random key, useful for both uuid and api key.
* @function
* @public
*
* @returns {String} A generated key
*
* @example
* randomKey = deviceRegister.generateUniqueKey()
* # randomKey is a randomly generated key that can be used as either a uuid or an api key
* console.log(randomKey)
*/
generateUniqueKey() {
return (0, uuid_1.v4)().replace(/-/g, '');
},
/**
* @summary Register a device with Balena
* @function
* @public
*
* @param {Object} options - options
* @param {Number} [options.userId] - user id
* @param {Number} options.applicationId - application id
* @param {String} options.uuid - device uuid
* @param {String} options.deviceType - device type
* @param {String} [options.deviceApiKey] - api key to create for the device
* @param {String} options.provisioningApiKey - provisioning api key
* @param {String} options.apiEndpoint - api endpoint
* @param {String} [options.supervisorVersion] - supervisor version of the device
* @param {String} [options.osVersion] - os version of the device
* @param {String} [options.osVariant] - os variant of the device
* @param {String} [options.macAddress] - mac address of the device
*
* @example
* deviceRegister.register
* userId: 199
* applicationId: 10350
* uuid: '...'
* deviceType: 'raspberry-pi'
* deviceApiKey: '...'
* provisioningApiKey: '...'
* apiEndpoint: 'https://api.balena-cloud.com'
* .then (deviceInfo) ->
* console.log(deviceInfo) # { id }
*/
register(options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
for (const opt of [
'applicationId',
'uuid',
'deviceType',
'provisioningApiKey',
'apiEndpoint',
]) {
if (options[opt] == null) {
throw new Error(`Options must contain a '${opt}' entry.`);
}
}
const response = yield request.send({
method: 'POST',
baseUrl: options.apiEndpoint,
url: '/device/register',
refreshToken: false,
sendToken: false,
headers: {
Authorization: `Bearer ${options.provisioningApiKey}`,
},
timeout: 30000,
body: {
user: options.userId,
application: options.applicationId,
uuid: options.uuid,
device_type: options.deviceType,
api_key: options.deviceApiKey,
supervisor_version: options.supervisorVersion,
os_version: options.osVersion,
os_variant: options.osVariant,
mac_address: options.macAddress,
},
});
if (response.statusCode !== 201) {
throw new ApiError(response.body, response);
}
return response.body;
});
},
});
exports.getRegisterDevice = getRegisterDevice;
class ApiError extends typed_error_1.TypedError {
constructor(message = 'Error with API request', response) {
super(message);
this.response = response;
}
}
exports.ApiError = ApiError;