UNPKG

ts-freebox

Version:

145 lines (140 loc) 4.7 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/lib/player.ts var player_exports = {}; __export(player_exports, { Player: () => Player }); module.exports = __toCommonJS(player_exports); // src/utils/fetch.ts var formatUrl = (url) => !url.toString().endsWith("/") ? `${url}/` : url; var fetchFBX = (url, token, method, options) => { return fetch(formatUrl(url), { ...options, method, body: JSON.stringify(options?.body), headers: { ...options?.headers, "Content-Type": "application/json", "X-Fbx-App-Auth": token || "", Host: "mafreebox.freebox.fr" } }).then((res) => res.json()).catch((error) => { throw new Error(`\u274C ~ error on call ${url}`, { cause: error }); }); }; var request = (url, token, options, method = "GET") => fetchFBX(url, token, method, options); var fetch_default = request; // src/lib/submodule.ts var Submodule = class { _freebox; baseUrl; token; constructor(freebox) { this._freebox = freebox; this.baseUrl = this._freebox._configuration.baseUrl; this.token = this._freebox.token; } }; var submodule_default = Submodule; // src/lib/player.ts var Player = class extends submodule_default { constructor(freebox) { super(freebox); } /** * Returns the list of all player devices registered on the local network * @link https://mafreebox.freebox.fr/doc/index.html?v=875e3d530bd643b2bd857397ff6c2984154acf58#list-every-player-devices * @returns {Promise<Response<TPlayer[]>>} */ async list() { return await fetch_default(`${this.baseUrl}/player/`, this.token); } /** * Get player device status * @link https://mafreebox.freebox.fr/doc/index.html?v=875e3d530bd643b2bd857397ff6c2984154acf58#get-player-device-status * @param id The ID of the player device to get the status for * @returns {Promise<Response<Partial<PlayerStatus>>>} */ async status(id) { return await fetch_default(`${this.baseUrl}/player/${id}/api/v6/status/`, this.token); } /** * Control the active media player of a device * @link https://mafreebox.freebox.fr/doc/index.html?v=875e3d530bd643b2bd857397ff6c2984154acf58#control-the-active-media-player-of-a-device * @param id The ID of the player device to control * @param cmd The command to send to the player device * @returns {Promise<VoidResponse>} */ async command(id, cmd) { return await fetch_default( `${this.baseUrl}/player/${id}/api/v6/control/mediactrl/`, this.token, { body: { cmd } }, "POST" ); } /** * Control the playback volume of the device * @link https://mafreebox.freebox.fr/doc/index.html?v=875e3d530bd643b2bd857397ff6c2984154acf58#control-the-playback-volume-of-the-device * @param id The ID of the player device to control * @param volume The volume to set on the player device or to mute the player device * @returns {Promise<Response<Partial<volume>>>} */ async volume(id, volume) { return await fetch_default( `${this.baseUrl}/player/${id}/api/v6/control/volume/`, this.token, volume && { body: volume }, volume ? "PUT" : "GET" ); } /** * Open a url on a player device * @link https://mafreebox.freebox.fr/doc/index.html?v=875e3d530bd643b2bd857397ff6c2984154acf58#open-a-url-on-a-player-device * @param id The ID of the player device to control * @param url Url to open on the Freebox Player * @param type Mime type of the content to open on the Freebox Player (optional: default is empty) * @returns {Promise<VoidResponse>} */ async open(id, url, type) { return await fetch_default( `${this.baseUrl}/player/${id}/api/v6/control/open/`, this.token, { body: { url, type } }, "POST" ); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Player });