UNPKG

@thiskyhan/bark.js

Version:

A simple JavaScript library for Bark.

112 lines (111 loc) 4.21 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BarkClient = void 0; const axios_1 = __importDefault(require("axios")); class BarkError extends Error { constructor(message, statusCode, data) { super(message); this.name = 'BarkError'; this.statusCode = statusCode; this.data = data; Object.setPrototypeOf(this, BarkError.prototype); } } /** * A client for interacting with the Bark push notification API. */ class BarkClient { validatePayload(payload) { if (!payload.body) throw new BarkError('The \'body\' field is required for a push notification.'); if (payload.icon) { try { new URL(payload.icon); if (!payload.icon.endsWith('.jpg')) throw new BarkError('The \'icon\' field must be a JPG image.'); } catch (_a) { throw new BarkError('The \'icon\' field must be a valid URL.'); } } if (payload.url) { try { new URL(payload.url); } catch (_b) { throw new BarkError('The \'url\' field must be a valid URL.'); } } } /** * Creates an instance of BarkClient. * @param options - Configuration options for the client */ constructor(options) { this.baseUrl = options.baseUrl.replace(/\/$/, ''); this.key = options.key; this.client = axios_1.default.create({ baseURL: this.baseUrl }); } /** * Sends a push notification via the Bark API. * @param payload - The notification payload to send * @returns The response from the Bark API */ pushMessage(payload) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; this.validatePayload(payload); try { const postData = Object.assign({ device_key: this.key }, payload); const response = yield this.client.post('/push', postData); if (response.status !== 200) throw new BarkError('Failed to send push notification.'); return response.data; } catch (error) { if (axios_1.default.isAxiosError(error)) { console.error('Failed to send push notification:', { status: (_a = error.response) === null || _a === void 0 ? void 0 : _a.status, data: (_b = error.response) === null || _b === void 0 ? void 0 : _b.data }); } else console.error('An unexpected error occurred:', error); throw error; } }); } /** * Sets the base URL for the client. * * This method updates both the local `baseUrl` property and the default base URL for * all HTTP requests made by the client. * * @param baseUrl - The new base URL to use. */ setBaseUrl(baseUrl) { this.baseUrl = baseUrl; this.client.defaults.baseURL = baseUrl; } /** * Sets the API key for the client. * * @param key - The API key that will be used for authentication. */ setKey(key) { this.key = key; } } exports.BarkClient = BarkClient;