on master help sign in
Use ES6 fetch()
0bf12cdFang
src/bridge/lib/inviteMail.js+3−5
@@ -7,12 +7,10 @@ const baseUrl = 'https://localhost:3002'
7function sendMail(recipient, ticket) {
7function sendMail(recipient, ticket) {
8 return new Promise((resolve, reject) => {
8 return new Promise((resolve, reject) => {
9 fetch(baseUrl + '/send-ticket', {
9 fetch(baseUrl + '/send-ticket', {
10− method: "POST",
10+ method: 'POST',
11− // mode: "cors", // no-cors, cors, *same-origin
11+ cache: 'no-cache',
12− cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
13− // credentials: "omit", // include, *same-origin, omit
14 headers: {
12 headers: {
15− "Content-Type": "application/json",
13+ 'Content-Type': 'application/json'
16 },
14 },
17 body: JSON.stringify({recipient,ticket}),
15 body: JSON.stringify({recipient,ticket}),
18 })
16 })
src/bridge/lib/tank.js+22−53
@@ -1,67 +1,36 @@
1−import * as http from 'http';
2−import Maybe from 'folktale/maybe';
3−
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',
10− port: 3001,
11− protocol: 'https:',
12− headers: { 'Content-Type': 'application/json' }, //TODO text/plain vs application/json
13− json: true
14−}
15
6
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 {
21+ reject(response);
22+ }
30− });
23+ })
24+ .catch(reject);
31− }
25+ });
32}
26}
33
27
34const remainingTransactions = point => {
28const remainingTransactions = point => {
35− return new Promise((resolve, reject) => {
29+ return sendRequest('/point', {point:point});
36− const options = {
37− path: '/point',
38− method: 'POST'
39− }
40− const request = http.request(
41− Object.assign(baseOptions, options),
42− reqHandler(resolve, reject)
43− );
44− request.on('error', (e) => reject(new Error(e)));
45− request.write(JSON.stringify({point:point}));
46− request.end();
47− });
48};
30};
49
31
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});
53− const options = {
54− path: '/request',
55− method: 'POST'
56− }
57− const request = http.request(
58− Object.assign(baseOptions, options),
59− reqHandler(resolve, reject)
60− );
61− request.on('error', (e) => reject(new Error(e)));
62− request.write(JSON.stringify({txs:signedTxs}));
63− request.end();
64− });
65};
34};
66
35
67export {
36export {