@@ -1,67 +1,36 @@
1−import * as http from 'http';
2−import Maybe from 'folktale/maybe';
4//NOTE if accessing this in a localhost configuration fails with "CORS request
1//NOTE if accessing this in a localhost configuration fails with "CORS request
5// did not succeed", you might need to visit localhost:3001 or whatever
2// did not succeed", you might need to visit localhost:3001 or whatever
6// explicitly and tell your browser that's safe to access.
3// explicitly and tell your browser that's safe to access.
7// https://stackoverflow.com/a/53011185/1334324
4// https://stackoverflow.com/a/53011185/1334324
8−const baseOptions = {
5+const baseUrl = 'https://localhost:3001';
9− hostname: 'localhost',
12− headers: { 'Content-Type': 'application/json' }, //TODO text/plain vs application/json
16−const reqHandler = (resolve, reject) => {
7+function sendRequest(where, what) {
17− return (response) => {
8+ return new Promise((resolve, reject) => {
18− console.log('xx got response', response)
9+ fetch(baseUrl + where, {
19− // handle http errors
10+ method: 'POST',
20− if (response.statusCode < 200 || response.statusCode > 299) {
11+ cache: 'no-cache',
21− reject(new Error('Failed to load, status code: ' + response.statusCode));
12+ headers: {
22− }
13+ 'Content-Type': 'application/json'
23− // temporary data holder
14+ },
24− const body = [];
15+ body: JSON.stringify(what)
25− // on every content chunk, push it to the data array
16+ })
26− response.on('data', (chunk) => body.push(chunk));
17+ .then(response => {
27− // we are done, resolve promise with those joined chunks
18+ if (response.ok) {
28− response.on('end', () => {
19+ resolve(response.json());
29− return resolve(JSON.parse(body.join('')))
20+ } else {
34const remainingTransactions = point => {
28const remainingTransactions = point => {
35− return new Promise((resolve, reject) => {
29+ return sendRequest('/point', {point:point});
40− const request = http.request(
41− Object.assign(baseOptions, options),
42− reqHandler(resolve, reject)
44− request.on('error', (e) => reject(new Error(e)));
45− request.write(JSON.stringify({point:point}));
50−//TODO refactor, add sendRequest(path, json)
51const fundTransactions = signedTxs => {
32const fundTransactions = signedTxs => {
52− return new Promise((resolve, reject) => {
33+ return sendRequest('/request', {txs:signedTxs});
57− const request = http.request(
58− Object.assign(baseOptions, options),
59− reqHandler(resolve, reject)
61− request.on('error', (e) => reject(new Error(e)));
62− request.write(JSON.stringify({txs:signedTxs}));