diff --git a/net/web/src/api/createAccount.js b/net/web/src/api/createAccount.js new file mode 100644 index 00000000..659f9287 --- /dev/null +++ b/net/web/src/api/createAccount.js @@ -0,0 +1,11 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; +var base64 = require('base-64'); + +export async function createAccount(username, password) { + let headers = new Headers() + headers.append('Credentials', 'Basic ' + base64.encode(username + ":" + password)); + let profile = await fetchWithTimeout("/account/profile", { method: 'POST', headers: headers }) + checkResponse(profile); + return await profile.json() +} + diff --git a/net/web/src/api/getAvailable.js b/net/web/src/api/getAvailable.js new file mode 100644 index 00000000..f16bd1a6 --- /dev/null +++ b/net/web/src/api/getAvailable.js @@ -0,0 +1,8 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function getAvailable() { + let available = await fetchWithTimeout("/account/available", { method: 'GET' }) + checkResponse(available) + return await available.json() +} + diff --git a/net/web/src/api/setLogin.js b/net/web/src/api/setLogin.js new file mode 100644 index 00000000..c7593d0b --- /dev/null +++ b/net/web/src/api/setLogin.js @@ -0,0 +1,11 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; +var base64 = require('base-64'); + +export async function setLogin(username, password) { + let headers = new Headers() + headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password)); + let app = { Name: "indicom", Description: "decentralized communication" } + let login = await fetchWithTimeout('/account/apps', { method: 'POST', body: JSON.stringify(app), headers: headers }) + checkResponse(login) + return await login.json() +} diff --git a/net/web/src/context/fetchUtil.js b/net/web/src/context/fetchUtil.js deleted file mode 100644 index 1e943267..00000000 --- a/net/web/src/context/fetchUtil.js +++ /dev/null @@ -1,41 +0,0 @@ -var base64 = require('base-64'); - -const FETCH_TIMEOUT = 15000; - -function checkResponse(response) { - if(response.status >= 400 && response.status < 600) { - throw new Error(response.url + " failed"); - } -} - -async function fetchWithTimeout(url, options) { - return Promise.race([ - fetch(url, options).catch(err => { throw new Error(url + ' failed'); }), - new Promise((_, reject) => setTimeout(() => reject(new Error(url + ' timeout')), FETCH_TIMEOUT)) - ]); -} - -export async function getAvailable() { - let available = await fetchWithTimeout("/account/available", { method: 'GET', timeout: FETCH_TIMEOUT }) - checkResponse(available) - return await available.json() -} - -export async function setLogin(username, password) { - let headers = new Headers() - headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password)); - let app = { Name: "indicom", Description: "decentralized communication" } - let login = await fetchWithTimeout('/account/apps', { method: 'POST', timeout: FETCH_TIMEOUT, body: JSON.stringify(app), headers: headers }) - checkResponse(login) - return await login.json() -} - -export async function createAccount(username, password) { - let headers = new Headers() - headers.append('Credentials', 'Basic ' + base64.encode(username + ":" + password)); - let profile = await fetchWithTimeout("/account/profile", { method: 'POST', timeout: FETCH_TIMEOUT, headers: headers }) - checkResponse(profile); - return await profile.json() -} - - diff --git a/net/web/src/context/useAppContext.hook.js b/net/web/src/context/useAppContext.hook.js index b6c860cc..54424252 100644 --- a/net/web/src/context/useAppContext.hook.js +++ b/net/web/src/context/useAppContext.hook.js @@ -1,6 +1,7 @@ import { useEffect, useState, useRef, useContext } from 'react'; import { useNavigate, useLocation, useParams } from "react-router-dom"; -import { getAvailable, setLogin } from './fetchUtil'; +import { getAvailable } from 'api/getAvailable'; +import { setLogin } from 'api/setLogin'; import { setAccountAccess } from 'api/setAccountAccess'; import { addAccount } from 'api/addAccount'; import { getUsername } from 'api/getUsername';