UNPKG

fritzbox-api

Version:

Straightforward, lightweight and extendable Node.js library to communicate with FRITZ!Box devices

72 lines (62 loc) 1.92 kB
// Copyright (c) 2024, Thorsten A. Weintz. All rights reserved. // Licensed under the MIT license. See LICENSE in the project root for license information. import { stringify } from 'querystring'; import axios, { toFormData } from 'axios'; /** * HttpClient is an abstract base class to handle HTTP request. */ class HttpClient { /** * Initializes new instance of @see HttpClient. * * @param {string} baseUrl Base URL of device. */ constructor(baseUrl) { if (baseUrl) { axios.defaults.baseURL = baseUrl; } } /** * Performs HTTP requests by passing configuration values * to related HTTP client. * * @param {object} options Object with request configuration values. * @returns Object with response of HTTP client. */ async request(options) { try { this.reqDate = new Date(); return await axios(options); } catch { this.reqDate = null; return {}; } } /** * Performs GET request by given URL. * * @param {string} url Target URL of endpoint. * @returns Object with response of HTTP client. */ get = async (url) => this.request({ method: 'GET', url }); /** * Performs POST request by given URL and data. * * @param {string} url Target URL of endpoint. * @param {object} data Data to be sent as body to server. * @param {boolean} isFormData Contains whether data is formdata. * @returns Object with response of HTTP client. */ post = async (url, data, isFormData) => this.request({ method: 'POST', url, data: isFormData ? toFormData(data) : stringify(data) }); } /** * Exports @see HttpClient as default class. */ export default HttpClient;