UNPKG

@open-tender/store

Version:

A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API

35 lines (34 loc) 1.24 kB
import { capitalize } from '@open-tender/utils'; var makeMessage = function (event) { if (!event) return null; var msgType = event.replace('event: ', '').trim(); var msg = msgType .split('_') .map(function (s) { return capitalize(s); }) .join(' '); return { msgType: msgType, msg: msg }; }; var parseToken = function (data) { if (!data) return null; try { var tender = JSON.parse(data.replace('data: ', '')); var token = (tender.credit_card || {}).token; return token || null; } catch (err) { return null; } }; export var processMessage = function (value) { var decoded = new TextDecoder('utf-8').decode(value); var lines = decoded.split('\n'); var events = lines.filter(function (i) { return i.startsWith('event:'); }); var event = events[events.length - 1]; var _a = makeMessage(event) || {}, _b = _a.msgType, msgType = _b === void 0 ? null : _b, _c = _a.msg, msg = _c === void 0 ? null : _c; var datas = lines.filter(function (i) { return i.startsWith('data:'); }); var data = datas[datas.length - 1]; var token = parseToken(data); return { msgType: msgType, msg: msg, token: token }; };