mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 09:00:06 +00:00
moving remainder of endpoints to api folder
This commit is contained in:
parent
9020cd9375
commit
184c12aa7e
11
net/web/src/api/createAccount.js
Normal file
11
net/web/src/api/createAccount.js
Normal file
@ -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()
|
||||
}
|
||||
|
8
net/web/src/api/getAvailable.js
Normal file
8
net/web/src/api/getAvailable.js
Normal file
@ -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()
|
||||
}
|
||||
|
11
net/web/src/api/setLogin.js
Normal file
11
net/web/src/api/setLogin.js
Normal file
@ -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()
|
||||
}
|
@ -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()
|
||||
}
|
||||
|
||||
|
@ -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';
|
||||
|
Loading…
Reference in New Issue
Block a user