UNPKG

celeritas

Version:

This is an API service framework which supports API requests over HTTP & WebSockets.

136 lines (118 loc) 3.3 kB
const http = require('http'); const https = require('https'); const bluebird = require('bluebird'); const url = require('url'); class Webhook { /* @Webhook @params: { method: The HTTP method (GET, POST, PUT, etc.) url: The base URL to send a webhook to. (http://google.com/webhook), without query data. data: The GET or POST data for this request. auth: The full authorization bearer token. } */ constructor (method, url, data, auth) { this.status = this._status("Building"); this.createdOnUtc = new Date; this.completedOnUtc = null; this.response = null; this.options = { url: url, method: method.toUpperCase(), headers: { 'User-Agent': 'celeritas@' + require('../package.json').version, 'Content-Type': 'application/json', 'Accept': 'application/json' } } if (auth) { if (typeof auth == "string") this.options.headers['Authorization'] = auth; else if (typeof auth == "object") { if ('username' in auth && 'password' in auth) this.options.headers['Authorization'] = "Basic " + new Buffer(auth.username + ":" + auth.password, "base64").toString(); } } if (this.options.method == "GET") { for (var i in data) { var token = options.url.indexOf("?") === -1 ? "?" : "&"; options.url += token + i + "=" + encodeURIComponent(data[i]); } } else options.data = data; this.status = this._status("Ready"); } send () { if (this.status == this._status("Ready")) { var options = {}; Object.assign(options, this.options); var protocol = options.url.indexOf("http://") == 0 ? "http" : "https"; var uri = url.parse(options.url); options.hostname = uri.hostname; options.protocol = uri.protocol; options.path = uri.path; options.port = uri.port; return new Promise((resolve, reject) => { var lib = protocol == "https" ? https : http; this.request = lib.request(options, (response) => { var chunks = []; response.on("data", (chunk) => { chunks.push(chunk); }); response.on("error", (err) => { this.status = this._status("Failed"); this.completedOnUtc = new Date; this.response = err; resolve(this); }) response.on("end", () => { var body = Buffer.concat(chunks); body = body.toString('utf-8'); this.status = this._status("Complete"); this.completedOnUtc = new Date; try { this.response = JSON.parse(body); } catch (err) { this.response = body; } resolve(this); }); }); if (options.data != null) { try { var body = JSON.stringify(options.data); } catch (err) { var body = options.data; } this.request.write(body); this.request.end(); this.status = this._status("Pending"); } else { this.request.end(); this.status = this._status("Pending"); } }); } else { return new Promise((resolve, reject) => { reject(new Error("Webhooks must be in the 'READY' status to send!")); }); } } _status (value) { switch (value.toUpperCase()) { case "FAILED": return 0; case "BUILDING": return 1; case "READY": return 2; case "PENDING": return 3; case "COMPLETE": return 4; } return -1; } } module.exports = Webhook;