UNPKG

1.42 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createContext = undefined;
7
8var _nodeFetch = require('node-fetch');
9
10var _nodeFetch2 = _interopRequireDefault(_nodeFetch);
11
12var _log = require('./log');
13
14var _log2 = _interopRequireDefault(_log);
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18const createContext = exports.createContext = config => {
19 const callMainframe = (endpoint, data = {}) => {
20 const url = config.mainframe_url + endpoint;
21 (0, _log2.default)('call %s', url);
22 return (0, _nodeFetch2.default)(url, {
23 body: JSON.stringify(data),
24 method: 'post',
25 headers: {
26 Authorization: `Mainframe-Bot ${config.mainframe_secret}`,
27 'Content-Type': 'application/json'
28 }
29 }).then(res => {
30 if (res.ok) return res.json();else {
31 const error = new Error(res.statusText);
32 error.status = res.status;
33 error.enpoint = endpoint;
34 error.data = data;
35 throw error;
36 }
37 });
38 };
39
40 const sendMessage = payload => callMainframe('/send_message', payload);
41
42 const setupSubscription = payload => callMainframe('/setup_subscription', payload);
43
44 const editSubscription = payload => callMainframe('/edit_subscription', payload);
45
46 return {
47 config,
48 callMainframe,
49 sendMessage,
50 setupSubscription,
51 editSubscription
52 };
53};
\No newline at end of file