refactor: use bee-js for health endpoint (#38)

* refactor: use bee-js for health endpoint

* refactor: fix API URLs which could be undefined
This commit is contained in:
Vojtech Simetka
2021-04-01 18:20:27 +02:00
committed by GitHub
parent 0e4e9bcf68
commit decdd87bb7
3 changed files with 15 additions and 37 deletions
+2 -2
View File
@@ -6,7 +6,7 @@ import type { NodeAddresses, ChequebookAddressResponse, ChequebookBalanceRespons
import { beeDebugApi, beeApi } from '../services/bee';
export const useApiHealth = () => {
const [health, setHealth] = useState('')
const [health, setHealth] = useState<boolean>(false)
const [isLoadingHealth, setLoading] = useState<boolean>(false)
const [error, setError] = useState<Error | null>(null)
@@ -14,7 +14,7 @@ export const useApiHealth = () => {
setLoading(true)
beeApi.status.health()
.then(res => {
setHealth(res.data)
setHealth(res)
})
.catch(error => {
setError(error)
+4 -2
View File
@@ -8,6 +8,7 @@ import {DropzoneArea} from 'material-ui-dropzone'
import ClipboardCopy from '../../components/ClipboardCopy';
import TroubleshootConnectionCard from '../../components/TroubleshootConnectionCard';
import { Data, FileData } from '@ethersphere/bee-js';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
@@ -36,7 +37,7 @@ export default function Files(props: any) {
const [inputMode, setInputMode] = useState<'browse' | 'upload'>('browse');
const [searchInput, setSearchInput] = useState('');
const [searchResult, setSearchResult] = useState('');
const [searchResult, setSearchResult] = useState<FileData<Data> | null>(null);
const [loadingSearch, setLoadingSearch] = useState(false);
const [files, setFiles] = useState<File[]>([]);
@@ -47,7 +48,8 @@ export default function Files(props: any) {
setLoadingSearch(true)
beeApi.files.downloadFile(searchInput)
.then(res => {
setSearchResult(new TextDecoder("utf-8").decode(res.data))
setSearchResult(res)
const downloadUrl = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = downloadUrl;
+9 -33
View File
@@ -1,39 +1,21 @@
import axios, { AxiosInstance } from 'axios';
import { Bee } from "@ethersphere/bee-js";
import { Bee, Reference } from "@ethersphere/bee-js";
const beeJSClient = () => {
let apiHost
let apiHost = process.env.REACT_APP_BEE_HOST || 'http://localhost:1633'
if (sessionStorage.getItem('api_host')) {
apiHost = String(sessionStorage.getItem('api_host') || '')
} else {
apiHost = process.env.REACT_APP_BEE_HOST
apiHost = String(sessionStorage.getItem('api_host'))
}
return new Bee(`${apiHost}`)
}
const beeApiClient = (): AxiosInstance => {
let apiHost
if (sessionStorage.getItem('api_host')) {
apiHost = String(sessionStorage.getItem('api_host') || '')
} else {
apiHost = process.env.REACT_APP_BEE_HOST
}
return axios.create({
baseURL: apiHost
})
return new Bee(apiHost)
}
const beeDebugApiClient = (): AxiosInstance => {
let debugApiHost
let debugApiHost = process.env.REACT_APP_BEE_DEBUG_HOST || 'http://localhost:1635'
if (sessionStorage.getItem('debug_api_host')) {
debugApiHost = String(sessionStorage.getItem('debug_api_host') || '')
} else {
debugApiHost = process.env.REACT_APP_BEE_DEBUG_HOST
debugApiHost = String(sessionStorage.getItem('debug_api_host'))
}
return axios.create({
@@ -44,22 +26,16 @@ const beeDebugApiClient = (): AxiosInstance => {
export const beeApi = {
status: {
health() {
return beeApiClient().get('/')
return beeJSClient().isConnected()
}
},
files: {
uploadFile(file: any) {
uploadFile(file: File) {
return beeJSClient().uploadFile(file)
},
uploadData(file: any) {
return beeJSClient().uploadData(file)
},
downloadFile(hash: string) {
downloadFile(hash: string | Reference) {
return beeJSClient().downloadFile(hash)
},
downloadData(hash: string) {
return beeJSClient().downloadData(hash)
},
},
}