UNPKG

koa-neo4j

Version:

Rapidly create REST APIs, powered by Koa and Neo4j -- batteries included with built-in role based authentication via JWT and reusable backend components

85 lines (73 loc) 4.21 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.contains = exports.httpCall = exports.httpPost = exports.httpGet = exports.areSameDay = exports.compareFnFromArray = exports.pipe = exports.haveIntersection = undefined;var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;}; /** * Created by keyvan on 8/22/16. */ var _http = require('http');var _http2 = _interopRequireDefault(_http);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} const contains = (iterable, element) => { for (const arrayElement of iterable) if (element === arrayElement) return true; return false; }; const haveIntersection = (arrayFirst, arraySecond) => { if (!arrayFirst || !arraySecond) return false; const first = new Set(arrayFirst); const second = new Set(arraySecond); for (const element of first) if (second.has(element)) return true; return false; }; const pipe = (...functions) => (...args) => { for (const func of functions) if (Array.isArray(args)) args = func.apply(undefined, args);else args = func.apply(undefined, [args]); return args; }; const compareFnFromArray = (array, fn) => (first, second) => { if (!fn) fn = x => x; first = fn.apply(null, [first]); second = fn.apply(null, [second]); first = array.indexOf(first); second = array.indexOf(second); return first - second; }; const areSameDay = (dateFirst, dateSecond) => dateFirst.getFullYear() === dateSecond.getFullYear() && dateFirst.getMonth() === dateSecond.getMonth() && dateFirst.getDate() === dateSecond.getDate(); const httpCall = (method, host, route, port, data, headers) => { return new Promise((resolve, reject) => { headers = headers || {}; if (typeof data === 'object') { data = JSON.stringify(data); headers = _extends({}, headers, { 'Content-Type': 'application/json' }); } const request = _http2.default.request({ hostname: host, port: port, path: route, method: method, headers: headers }, resolve); request.on('error', reject); request.end(data); }). then(response => {response.setEncoding('utf8');return response;}). then(response => new Promise(resolve => response.on('data', resolve))). then(chunk => chunk.toString('utf8')). then(str => { try { return JSON.parse(str); } catch (error) { return str; } }); }; const httpGet = (route, port) => httpCall('GET', 'localhost', route, port); const httpPost = (route, port, data, headers) => httpCall('POST', 'localhost', route, port, data, headers);exports. haveIntersection = haveIntersection;exports.pipe = pipe;exports.compareFnFromArray = compareFnFromArray;exports. areSameDay = areSameDay;exports.httpGet = httpGet;exports.httpPost = httpPost;exports.httpCall = httpCall;exports.contains = contains;