diff --git a/src/main/webui/package-lock.json b/src/main/webui/package-lock.json index 3b6f8ed..655dbb9 100644 --- a/src/main/webui/package-lock.json +++ b/src/main/webui/package-lock.json @@ -9,7 +9,6 @@ "version": "2.0.0", "license": "ASLv2", "dependencies": { - "axios": "^1.5.1", "bootstrap": "^5.3.2", "prop-types": "^15.8.1", "react": "^18.2.0", @@ -4451,7 +4450,8 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true }, "node_modules/available-typed-arrays": { "version": "1.0.5", @@ -4465,16 +4465,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/axios": { - "version": "1.6.0", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.0.tgz", - "integrity": "sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==", - "dependencies": { - "follow-redirects": "^1.15.0", - "form-data": "^4.0.0", - "proxy-from-env": "^1.1.0" - } - }, "node_modules/babel-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz", @@ -5216,6 +5206,7 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -5742,6 +5733,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true, "engines": { "node": ">=0.4.0" } @@ -7088,6 +7080,7 @@ "version": "1.15.2", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", + "dev": true, "funding": [ { "type": "individual", @@ -7116,6 +7109,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dev": true, "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -10708,6 +10702,7 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "dev": true, "engines": { "node": ">= 0.6" } @@ -10716,6 +10711,7 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dev": true, "dependencies": { "mime-db": "1.52.0" }, @@ -11618,11 +11614,6 @@ "node": ">= 0.10" } }, - "node_modules/proxy-from-env": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" - }, "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", diff --git a/src/main/webui/package.json b/src/main/webui/package.json index 5664bc8..6673d90 100644 --- a/src/main/webui/package.json +++ b/src/main/webui/package.json @@ -6,7 +6,6 @@ "repository": "https://github.com/Commonjava/indy-ui-service", "license": "ASLv2", "dependencies": { - "axios": "^1.5.1", "bootstrap": "^5.3.2", "prop-types": "^15.8.1", "react": "^18.2.0", diff --git a/src/main/webui/src/app/RestClient.js b/src/main/webui/src/app/RestClient.js index b36db05..5e75859 100644 --- a/src/main/webui/src/app/RestClient.js +++ b/src/main/webui/src/app/RestClient.js @@ -14,20 +14,24 @@ * limitations under the License. */ -export const jsonGet = async function(payload){ - const response = await fetch(payload.url, { - method: "GET", - credentials: 'same-origin', - headers: { - "Content-Type": "application/json", - }, - body: payload.data ? payload.data : undefined - }); - if(response.ok && payload.done){ - await response.json().then(data=>payload.done(data)); - }else if(!response.ok && payload.fail){ - await response.text().then(data=>{ - payload.fail(data, response.status, response.statusText); - }); - } +const httpCall = (url, method, headers={}, payload) => fetch(url, { + method, + credentials: 'same-origin', + headers, + body: payload? payload : undefined +}); + +const http = { + get: (url, headers, payload) => httpCall(url, "GET", headers, payload), + post: (url, headers, payload) => httpCall(url, "POST", headers, payload), + put: (url, headers, payload) => httpCall(url, "PUT", headers, payload), + delete: (url, headers, payload) => httpCall(url, "DELETE", headers, payload) +}; + +const jsonRest ={ + get: (url, payload) => httpCall(url, "GET", {"Content-type": "application/json"}, payload), + post: (url, payload) => httpCall(url, "POST", {"Content-type": "application/json"}, payload), + put: (url, payload) => httpCall(url, "PUT", {"Content-type": "application/json"}, payload) }; + +export {http, jsonRest}; diff --git a/src/main/webui/src/app/components/CompUtils.js b/src/main/webui/src/app/components/CompUtils.js index 54b3641..2086b43 100644 --- a/src/main/webui/src/app/components/CompUtils.js +++ b/src/main/webui/src/app/components/CompUtils.js @@ -104,11 +104,11 @@ export const Utils = { return false; }, reConstituents: store => { - let oldConstituents = store.constituents; - let constituents = [oldConstituents.length]; + const oldConstituents = store.constituents; + const constituents = [oldConstituents.length]; for(let j=0; j Object.keys(obj).length === 0 && obj.constructor === Object, cloneObj: src => { let target = {}; - for (let prop in src) { + for (const prop in src) { if (prop in src) { target[prop] = src[prop]; } diff --git a/src/main/webui/src/app/components/content/RemoteEdit.jsx b/src/main/webui/src/app/components/content/RemoteEdit.jsx index 3059255..f1c4ea1 100644 --- a/src/main/webui/src/app/components/content/RemoteEdit.jsx +++ b/src/main/webui/src/app/components/content/RemoteEdit.jsx @@ -18,7 +18,6 @@ import React, {useState, useEffect} from 'react'; import {useLocation, useNavigate, useParams} from 'react-router-dom'; import {PropTypes} from 'prop-types'; -import axios from 'axios'; import {StoreEditControlPanel as EditControlPanel} from './StoreControlPanels.jsx'; import {DisableTimeoutHint, DurationHint, PrefetchHint, Hint} from './Hints.jsx'; // import ViewJsonDebugger from './Debugger.jsx'; @@ -26,7 +25,7 @@ import {Utils} from '../CompUtils.js'; // import Filters from '../Filters.js'; import {TimeUtils} from '../../TimeUtils.js'; import {PACKAGE_TYPES} from '../ComponentConstants.js'; -// import jsonGet from '../../RestClient.js'; +import {jsonRest} from '../../RestClient.js'; const init = (pkgType, storeName, setState) => { const getUrl = `/api/admin/stores/${pkgType}/remote/${storeName}`; @@ -34,20 +33,9 @@ const init = (pkgType, storeName, setState) => { const fetchStore = async () =>{ // get Store data let isError = false; - const response = await axios.get(getUrl).catch(error =>{ - isError = true; - let message = ""; - if (error.response) { - message = JSON.parse(error.response.data).error; - }else{ - message = error; - } - setState({ - message - }); - }); - if (!isError){ - let raw = response.data; + const response = await jsonRest.get(getUrl); + if (response.ok){ + let raw = await response.json(); let storeView = Utils.cloneObj(raw); storeView.disabled = raw.disabled === undefined ? false : raw.disabled; storeView.useX509 = raw.server_certificate_pem || raw.key_certificate_pem; @@ -57,19 +45,24 @@ const init = (pkgType, storeName, setState) => { // get Store disablement data const timeoutUrl = `/api/admin/schedule/store/${storeView.packageType}/${storeView.type}/${storeView.name}/disable-timeout`; - const timeoutResponse = await axios.get(timeoutUrl).catch(error=>{ - isError = true; - Utils.logMessage(`disable timeout getting failed! Error reason: ${error}`); - }); + const timeoutResponse = await jsonRest.get(timeoutUrl); let cloned = Utils.cloneObj(storeView); - if (!isError){ - cloned.disableExpiration = timeoutResponse.data.expiration; + if (timeoutResponse.ok){ + const timeout = await timeoutResponse.json(); + cloned.disableExpiration = timeout.expiration; + }else{ + response.text().then(error=>Utils.logMessage(`disable timeout getting failed! Error reason: ${error}`)); } + // Change state and re-rendering setState({ storeView: cloned, store: raw }); + }else{ + response.text().then(error=>setState({ + message: error + })); } }; @@ -82,16 +75,10 @@ const saveStore = store => { const postStore = async () =>{ // get Store data let isError = false; - const response = await axios.post(getUrl, store).catch(error =>{ - isError = true; - let message = ""; - if (error.response) { - message = JSON.parse(error.response.data).error; - }else{ - message = error; - } - Utils.logMessage(message); - }); + const response = await jsonRest.post(getUrl, store); + if (!response.ok){ + response.text().then(error=>Utils.logMessage(error)); + } const navigate = useNavigate(); if(!isError && (response.status >= 200 || response.status < 300)){ navigate(`/remote/${store.packageType}/view/${store.name}`); diff --git a/src/main/webui/src/app/components/content/RemoteList.jsx b/src/main/webui/src/app/components/content/RemoteList.jsx index 76f9924..96c8efc 100644 --- a/src/main/webui/src/app/components/content/RemoteList.jsx +++ b/src/main/webui/src/app/components/content/RemoteList.jsx @@ -15,43 +15,41 @@ */ import React, {useEffect, useState} from 'react'; -import axios from 'axios'; import {ListJsonDebugger} from './Debugger.jsx'; import ListControl from "./ListControl.jsx"; import {remoteOptionLegend as options} from "../ComponentConstants.js"; import {Utils} from '../CompUtils.js'; import {StoreListingWidget} from './CommonPageWidget.jsx'; +import {jsonRest} from '../../RestClient.js'; +import {DisableTimeoutHint} from './Hints.jsx'; const init = (state, setState) => { useEffect(()=>{ const fetchdData = async ()=>{ - let isError = false; - const response = await axios.get(`/api/admin/stores/_all/remote`).catch(error=>{ - isError = true; - let message = ""; - if (error.response) { - message = JSON.parse(error.response.data).error; - }else{ - message = error; - } - setState({ - message - }); - }); - if (!isError){ - const timeoutResponse = await axios.get('/api/admin/schedule/store/all/disable-timeout').catch(error=>{ - isError=true; - Utils.logMessage(`disable timeout get failed in remote listing! Error reason: ${error}`); - }); + const response = await jsonRest.get(`/api/admin/stores/_all/remote`); + if (response.ok){ + const timeoutResponse = await jsonRest.get('/api/admin/schedule/store/all/disable-timeout'); let disabledMap = {}; - if (!isError){ - disabledMap = Utils.setDisableMap(timeoutResponse.data, state.listing); + Utils.logMessage(timeoutResponse); + if (timeoutResponse.ok){ + const timeoutData = await timeoutResponse.json(); + Utils.logMessage(timeoutData); + disabledMap = Utils.setDisableMap(timeoutData); + }else{ + timeoutResponse.text().then(data=>Utils.logMessage(`disable timeout get failed in remote listing! Error reason: ${data}`)); } + const data = await response.json(); setState({ - listing: response.data.items, - rawListing: response.data.items, + listing: data.items, + rawListing: data.items, disabledMap }); + }else{ + response.text().then(data=>{ + setState({ + message: data + }); + }); } }; fetchdData(); diff --git a/src/main/webui/src/app/components/content/RemoteView.jsx b/src/main/webui/src/app/components/content/RemoteView.jsx index dd2cdd0..be520b1 100644 --- a/src/main/webui/src/app/components/content/RemoteView.jsx +++ b/src/main/webui/src/app/components/content/RemoteView.jsx @@ -24,28 +24,15 @@ import {DisableTimeoutHint, PrefetchHint, Hint, PasswordMask} from './Hints.jsx' import {Utils} from '../CompUtils.js'; import {Filters} from '../Filters.js'; import {TimeUtils} from '../../TimeUtils.js'; -import axios from 'axios'; +import {jsonRest} from '../../RestClient.js'; const init = (pkgType, storeName, setState) => { const storeUrl = `/api/admin/stores/${pkgType}/remote/${storeName}`; useEffect(()=>{ - const fetchStore = async () =>{ - // get Store data - let isError = false; - const response = await axios.get(storeUrl).catch(error =>{ - isError = true; - let message = ""; - if (error.response) { - message = JSON.parse(error.response.data).error; - }else{ - message = error; - } - setState({ - message - }); - }); - if (!isError){ - let raw = response.data; + const fetchStore = async () => { + const response = await jsonRest.get(storeUrl); + if (response.ok){ + let raw = await response.json(); let store = Utils.cloneObj(raw); store.disabled = raw.disabled === undefined ? false : raw.disabled; store.useX509 = raw.server_certificate_pem || raw.key_certificate_pem; @@ -55,18 +42,20 @@ const init = (pkgType, storeName, setState) => { // get Store disablement data const timeoutUrl = `/api/admin/schedule/store/${store.packageType}/${store.type}/${store.name}/disable-timeout`; - const timeoutResponse = await axios.get(timeoutUrl).catch(error=>{ - isError = true; - Utils.logMessage(`disable timeout getting failed! Error reason: ${error}`); - }); + const timeoutResponse = await jsonRest.get(timeoutUrl); let newStore = Utils.cloneObj(store); - if (!isError){ - newStore.disableExpiration = timeoutResponse.data.expiration; + if(timeoutResponse.ok){ + const timeoutData = await timeoutResponse.json(); + newStore.disableExpiration = timeoutData.expiration; } // Change state and re-rendering setState({ store: newStore }); + }else{ + response.text().then(data=>{ + Utils.logMessage(`Failed to get store data. Error reason: ${response.status}->${response.statusText}`); + }); } }; diff --git a/src/main/webui/src/app/components/content/StoreControlPanels.jsx b/src/main/webui/src/app/components/content/StoreControlPanels.jsx index 0b88a6e..9ad221a 100644 --- a/src/main/webui/src/app/components/content/StoreControlPanels.jsx +++ b/src/main/webui/src/app/components/content/StoreControlPanels.jsx @@ -17,8 +17,8 @@ import React from 'react'; import {useNavigate} from 'react-router-dom'; import {PropTypes} from 'prop-types'; -import axios from 'axios'; import {Utils} from '../CompUtils'; +import {http} from '../../RestClient'; const StoreEditControlPanel = ({handleSave, handleCancel, handleRemove}) =>
{' '} @@ -40,11 +40,12 @@ const StoreViewControlPanel = function({enabled, storeObj, handleDisable, handle const [pkgType, storeType, storeName] = [storeObj.packageType, storeObj.type, storeObj.name]; const storeUrl = `/api/admin/stores/${pkgType}/${storeType}/${storeName}`; const handleRemove = async ()=>{ - const response = await axios.delete(storeUrl).catch(error =>{ + const response = await http.delete(storeUrl); + if(!response.ok && response.status >= 400){ // TODO: Some other way to handle errors? - Utils.logMessage(error); - }); - if(response && response.status===204){ + response.text().then(error=>Utils.logMessage(error)); + } + if(response.status===204){ // TODO: Some other way to show deletion success? Utils.logMessage("Store deleted."); } diff --git a/src/main/webui/src/content-browse/DirectoryListing.jsx b/src/main/webui/src/content-browse/DirectoryListing.jsx index 35be3ae..7aa03df 100644 --- a/src/main/webui/src/content-browse/DirectoryListing.jsx +++ b/src/main/webui/src/content-browse/DirectoryListing.jsx @@ -16,8 +16,8 @@ import React, {useState, useEffect} from 'react'; import {PropTypes} from 'prop-types'; -import axios from 'axios'; import {styles} from './style.js'; +import {jsonRest} from '../app/RestClient.js'; const replaceUrl = url =>{ if (url.includes("api/browse")){ @@ -106,24 +106,18 @@ const init = setState => { const url =`/api${document.location.pathname}`; useEffect(()=>{ const fetchData = async () => { - const response = await axios.get(url).catch(error=>{ - if (error.response) { - setState({ - isLoaded: true, - error: JSON.parse(error.response.data).error - }); - }else{ - setState({ - isLoaded: true, - error - }); - } - }); - if(response.status === 200){ + const response = await jsonRest.get(url); + if(response.ok){ + const data = await response.json(); setState({ isLoaded: true, - data: response.data + data }); + }else{ + response.text().then(error=>setState({ + isLoaded: true, + error + })); } }; fetchData();