UNPKG

@piarre/ts-freebox

Version:

179 lines (174 loc) 5.72 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/lan.ts var lan_exports = {}; __export(lan_exports, { LAN: () => LAN }); module.exports = __toCommonJS(lan_exports); // 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/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/lan.ts var LAN = class extends submodule_default { constructor(freebox) { super(freebox); } /** * Returns the current LanConfig * @link https://mafreebox.freebox.fr/doc/index.html#get-the-current-lan-configuration * @returns {Promise<Response<LanConfig>>} */ async config() { return await fetch_default(`${this.baseUrl}/lan/config/`, this.token); } /** * Update the current LanConfig * @link https://mafreebox.freebox.fr/doc/index.html#update-the-current-lan-configuration * @param config The new partial LanConfig * @returns {Promise<Response<Partial<LanConfig>>>} */ async update(config) { if (!config || Object.keys(config).length === 0) throw new Error("config is required"); return await fetch_default( `${this.baseUrl}/lan/config/`, this.token, { body: config }, "PUT" ); } /** * Getting the list of browsable LAN interfaces * @link https://mafreebox.freebox.fr/doc/index.html#getting-the-list-of-browsable-lan-interfaces * @returns {Promise<Response<LanInterface[]>>} */ async interfaces() { return await fetch_default(`${this.baseUrl}/lan/browser/interfaces/`, this.token); } /** * Getting the list of hosts on a given interface * @link https://mafreebox.freebox.fr/doc/index.html#getting-the-list-of-hosts-on-a-given-interface * @param _interface Returns the list of LanHost on this interface * @returns {Promise<Response<LanHost[]>>} */ async hosts(_interface = "pub") { if (!_interface) throw new Error("interface is required"); return await fetch_default(`${this.baseUrl}/lan/browser/${_interface}/`, this.token); } /** * Getting an host information * @link https://mafreebox.freebox.fr/doc/index.html#getting-an-host-information * @param _interface - The interface to which the host is connected * @param id - The id of the host * @returns {Promise<Response<LanHost>>} */ async host(_interface = "pub", id) { if (!_interface) throw new Error("interface is required"); if (!id) throw new Error("id is required"); return await fetch_default(`${this.baseUrl}/lan/browser/${_interface}/${id}/`, this.token); } /** * Updating an host information * @link https://mafreebox.freebox.fr/doc/index.html#updating-an-host-information * @param _interface The interface to which the host is connected * @param id The id of the host * @param data The data to update * @returns {Promise<Response<Partial<LanHost>>>} */ async updateHost(_interface = "pub", id, data) { if (!_interface) throw new Error("interface is required"); if (!id) throw new Error("id is required"); if (!data || Object.keys(data).length === 0) throw new Error("data is required"); return await fetch_default( `${this.baseUrl}/lan/browser/${_interface}/${id}/`, this.token, { body: data }, "PUT" ); } /** * Send a wake on LAN packet to the specified host with an optional password * @link https://mafreebox.freebox.fr/doc/index.html#send-wake-ok-lan-packet-to-an-host * @param _interface The interface to which the host is connected * @param mac The MAC address of the host * @param password - [OPTIONAL] The password to use to send the WOL packet * @returns */ async WOL(_interface = "pub", mac, password = "") { if (!_interface) throw new Error("interface is required"); if (!mac) throw new Error("mac is required"); return await fetch_default( `${this.baseUrl}/lan/wol/${_interface}/`, this.token, { body: { mac, password } }, "POST" ); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { LAN });