homey-api
Version:
106 lines (92 loc) • 1.97 kB
JavaScript
const Item = require('../Item');
/**
* @class
* @hideconstructor
* @extends HomeyAPIV3.Item
* @memberof HomeyAPIV3.ManagerApps
*/
class App extends Item {
/**
* Call the app's API endpoint.
* @param {Object} opts
* @param {('GET'|'POST'|'PUT'|'DELETE')} opts.method HTTP Method of the API endpoint.
* @param {String} opts.path - Path to the API endpoint.
* @param {mixed} opts.body
* @returns {Promise<any>}
*/
async call({
method,
path,
body,
}) {
if (typeof method !== 'string') {
throw new Error('Invalid Path');
}
if (typeof path !== 'string') {
throw new Error('Invalid Path');
}
if (!path.startsWith('/')) {
path = `/${path}`;
}
this.__debug(method, path);
return this.homey.call({
method,
path: `/api/app/${this.id}${path}`,
body,
});
}
/**
* Make a GET request to the App's Web API.
* @param {object} opts
* @param {string} opts.path
* @returns {Promise<any>}
*/
async get({ path }) {
return this.call({
path,
method: 'GET',
});
}
/**
* Make a POST request to the App's Web API.
* @param {object} opts
* @param {string} opts.path
* @param {object} opts.body
* @returns {Promise<any>}
*/
async post({ path, body }) {
return this.call({
path,
body,
method: 'POST',
});
}
/**
* Make a PUT request to the App's Web API.
* @param {object} opts
* @param {string} opts.path
* @param {object} opts.body
* @returns {Promise<any>}
*/
async put({ path, body }) {
return this.call({
path,
body,
method: 'PUT',
});
}
/**
* Make a DELETE request to the App's Web API.
* @param {object} opts
* @param {string} opts.path
* @returns {Promise<any>}
*/
async delete({ path }) {
return this.call({
path,
method: 'DELETE',
});
}
}
module.exports = App;
;