UNPKG

homey-api

Version:
93 lines (81 loc) 1.97 kB
'use strict'; const API = require('./API'); class AthomIconsAPI extends API { static SPECIFICATION = { host: 'icons.athom.com', basePath: '/', operations: {}, }; static JSDOC_PRIVATE = true; async onCallResponseParseBody({ request, response }) { if (request.context.responseType === 'buffer') return response.buffer(); if (request.context.responseType === 'stream') return response.body; return super.onCallResponseParseBody({ request, response }); } /** * Get an icon as PNG buffer. * * @param {Object} opts * @param {String} opts.iconId * @param {Number} opts.size * @returns {Promise<Buffer>} */ async getIconAsBuffer({ iconId, size = 32 }) { return this.call({ method: 'get', path: `/icon/${iconId}?size=${size}`, context: { responseType: 'buffer', }, }); } /** * Get an icon as PNG stream. * * @param {Object} opts * @param {String} opts.iconId * @param {Number} opts.size * @returns {Promise<Stream>} */ async getIconAsStream({ iconId, size = 32 }) { return this.call({ method: 'get', path: `/icon/${iconId}?size=${size}`, context: { responseType: 'stream', }, }); } /** * Create an icon from an SVG string. * * @param {Object} opts * @param {String} opts.svg - SVG string * @returns {Promise<void>} */ async createIconFromSVG({ svg }) { return this.call({ method: 'post', path: '/icon', headers: { 'Content-Type': 'image/svg+xml', }, body: String(svg), bodyJSON: false, }); } /** * Create an icon from an URL. * * @param {Object} opts * @param {String} opts.url - URL to the icon * @returns {Promise<void>} */ async createIconFromURL({ url }) { return this.call({ method: 'post', path: `/icon?url=${encodeURIComponent(url)}`, }); } } module.exports = AthomIconsAPI;