Skip to content

Commit

Permalink
Use ES6 fetch()
Browse files Browse the repository at this point in the history
  • Loading branch information
Fang- committed Apr 30, 2019
1 parent 54003b2 commit 0bf12cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 58 deletions.
8 changes: 3 additions & 5 deletions src/bridge/lib/inviteMail.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ const baseUrl = 'https://localhost:3002'
function sendMail(recipient, ticket) {
return new Promise((resolve, reject) => {
fetch(baseUrl + '/send-ticket', {
method: "POST",
// mode: "cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
// credentials: "omit", // include, *same-origin, omit
method: 'POST',
cache: 'no-cache',
headers: {
"Content-Type": "application/json",
'Content-Type': 'application/json'
},
body: JSON.stringify({recipient,ticket}),
})
Expand Down
75 changes: 22 additions & 53 deletions src/bridge/lib/tank.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,36 @@
import * as http from 'http';
import Maybe from 'folktale/maybe';

//NOTE if accessing this in a localhost configuration fails with "CORS request
// did not succeed", you might need to visit localhost:3001 or whatever
// explicitly and tell your browser that's safe to access.
// https://stackoverflow.com/a/53011185/1334324
const baseOptions = {
hostname: 'localhost',
port: 3001,
protocol: 'https:',
headers: { 'Content-Type': 'application/json' }, //TODO text/plain vs application/json
json: true
}
const baseUrl = 'https://localhost:3001';

const reqHandler = (resolve, reject) => {
return (response) => {
console.log('xx got response', response)
// handle http errors
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load, status code: ' + response.statusCode));
}
// temporary data holder
const body = [];
// on every content chunk, push it to the data array
response.on('data', (chunk) => body.push(chunk));
// we are done, resolve promise with those joined chunks
response.on('end', () => {
return resolve(JSON.parse(body.join('')))
});
}
function sendRequest(where, what) {
return new Promise((resolve, reject) => {
fetch(baseUrl + where, {
method: 'POST',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(what)
})
.then(response => {
if (response.ok) {
resolve(response.json());
} else {
reject(response);
}
})
.catch(reject);
});
}

const remainingTransactions = point => {
return new Promise((resolve, reject) => {
const options = {
path: '/point',
method: 'POST'
}
const request = http.request(
Object.assign(baseOptions, options),
reqHandler(resolve, reject)
);
request.on('error', (e) => reject(new Error(e)));
request.write(JSON.stringify({point:point}));
request.end();
});
return sendRequest('/point', {point:point});
};

//TODO refactor, add sendRequest(path, json)
const fundTransactions = signedTxs => {
return new Promise((resolve, reject) => {
const options = {
path: '/request',
method: 'POST'
}
const request = http.request(
Object.assign(baseOptions, options),
reqHandler(resolve, reject)
);
request.on('error', (e) => reject(new Error(e)));
request.write(JSON.stringify({txs:signedTxs}));
request.end();
});
return sendRequest('/request', {txs:signedTxs});
};

export {
Expand Down

0 comments on commit 0bf12cd

Please sign in to comment.