// @flow import fetch from 'node-fetch' import type { Config, BotContext, Handlers } from './types' export const createContext = ( config: Config, handlers: Handlers, ): BotContext => { const callMainframe = (endpoint: string, data: ?Object = {}) => { return fetch(config.mainframe_url + endpoint, { body: JSON.stringify(data), method: 'post', headers: { Authorization: `Mainframe-Bot ${config.mainframe_secret}`, 'Content-Type': 'application/json', }, }).then(res => { if (res.ok) return res.json() else { const error: Object = new Error(res.statusText) error.status = res.status error.enpoint = endpoint error.data = data throw error } }) } const sendEvent = (payload: ?Object) => callMainframe('/event', payload) return { config, callMainframe, sendEvent } }