UNPKG

balena-sdk

Version:
168 lines (164 loc) 6.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); /* Copyright 2016 Balena 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. */ const errors = tslib_1.__importStar(require("balena-errors")); const getConfigModel = function (deps, opts) { const { request } = deps; const { apiUrl } = opts; // TODO: Drop when `instructions` is no longer returned by the `/config` and `/device-types/v1` endpoints const normalizeDeviceTypes = (deviceTypes) => deviceTypes.map(function (deviceType) { // Remove the device-type.json instructions to enforce // users to use the contract based ones. delete deviceType.instructions; return deviceType; }); const exports = { /** * @summary Get all configuration * @name getAll * @public * @function * @memberof balena.models.config * * @fulfil {Object} - configuration * @returns {Promise} * * @example * balena.models.config.getAll().then(function(config) { * console.log(config); * }); */ getAll: async () => { const { body } = await request.send({ method: 'GET', url: '/config', baseUrl: apiUrl, sendToken: false, }); if ('deviceTypes' in body) { delete body.deviceTypes; } return body; }, /** * @summary Get device types * @name getDeviceTypes * @public * @function * @memberof balena.models.config * * @deprecated use balena.models.deviceType.getAll * @fulfil {Object[]} - device types * @returns {Promise} * * @example * balena.models.config.getDeviceTypes().then(function(deviceTypes) { * console.log(deviceTypes); * }); */ getDeviceTypes: async () => { const { body: deviceTypes } = await request.send({ method: 'GET', url: '/device-types/v1', baseUrl: apiUrl, }); if (deviceTypes == null) { throw new Error('No device types'); } return normalizeDeviceTypes(deviceTypes); }, /** * @summary Get a device type manifest by slug * @name getDeviceTypeManifestBySlug * @public * @function * @memberof balena.models.config * * @deprecated use balena.models.deviceType.getBySlugOrName * @param {String} slugOrName - device type slug * @fulfil {Object} - device type manifest * @returns {Promise} * * @example * balena.models.config.getDeviceTypeManifestBySlug('raspberry-pi').then(function(manifest) { * console.log(manifest); * }); */ getDeviceTypeManifestBySlug: async (slugOrName) => { const deviceTypes = await exports.getDeviceTypes(); const deviceManifest = deviceTypes.find((deviceType) => { var _a; return deviceType.name === slugOrName || deviceType.slug === slugOrName || ((_a = deviceType.aliases) === null || _a === void 0 ? void 0 : _a.includes(slugOrName)); }); if (deviceManifest == null) { throw new errors.BalenaInvalidDeviceType(slugOrName); } return deviceManifest; }, /** * @summary Get configuration/initialization options for a device type * @name getDeviceOptions * @public * @function * @memberof balena.models.config * * @param {String} deviceType - device type slug * @fulfil {Object[]} - configuration options * @returns {Promise} * * @example * balena.models.config.getDeviceOptions('raspberry-pi').then(function(options) { * console.log(options); * }); */ getDeviceOptions: async (deviceType) => { var _a, _b, _c; const manifest = await exports.getDeviceTypeManifestBySlug(deviceType); return Array.from(new Set([ ...((_a = manifest.options) !== null && _a !== void 0 ? _a : []), ...((_c = (_b = manifest.initialization) === null || _b === void 0 ? void 0 : _b.options) !== null && _c !== void 0 ? _c : []), ])); }, /** * @summary Get configuration variables schema for a device type * @name getConfigVarSchema * @public * @function * @memberof balena.models.config * * @param {String} deviceType - device type slug * @fulfil {Object[]} - configuration options * @returns {Promise} * * @example * balena.models.config.getConfigVarSchema('raspberry-pi').then(function(options) { * console.log(options); * }); */ getConfigVarSchema: async (deviceType) => { const { body } = await request.send({ method: 'GET', url: `/config/vars${typeof deviceType === 'string' ? `?deviceType=${deviceType}` : ''}`, baseUrl: apiUrl, sendToken: typeof deviceType === 'string', }); return body; }, }; return exports; }; exports.default = getConfigModel;