UNPKG

pushy-me

Version:

pushy-me is a simple push notification service for Node.js

41 lines (40 loc) 1.87 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PushyMeService = void 0; const constants_1 = require("../constants"); const node_fetch_1 = require("node-fetch"); class PushyMeService { setApiKey(apiKey) { this.apiKey = apiKey; } sendPushNotification(data, recipient, options) { return __awaiter(this, void 0, void 0, function* () { this.apiKeyIsSet(); const response = yield (0, node_fetch_1.default)(`${constants_1.API_END_POINT + constants_1.SEND_PUSH_NOTIFICATION}?api_key=${this.apiKey}`, { method: 'POST', body: JSON.stringify(Object.assign({ data, to: recipient }, options)), headers: { 'Content-Type': 'application/json' }, }); const result = (yield response.json()); if (result.success === false) { throw new Error('An invalid response code was received from the Pushy API.'); } return result; }); } apiKeyIsSet() { if (!this.apiKey) { throw new Error('API Key is not set'); } } } exports.PushyMeService = PushyMeService;