Skip to content

Commit aa9b735

Browse files
authored
Refactor to swagger ts API (#2860)
1 parent 0904f83 commit aa9b735

25 files changed

+272
-303
lines changed

portal-ui/src/api/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const errorToHandler = (e: Error): ErrorResponseHandler => {
2929
}
3030
return {
3131
statusCode: e.code,
32-
errorMessage: e.message,
33-
detailedError: e.detailedMessage,
32+
errorMessage: e.message || "",
33+
detailedError: e.detailedMessage || "",
3434
};
3535
};

portal-ui/src/screens/Console/Account/Account.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ import {
2929
} from "mds";
3030
import { useSelector } from "react-redux";
3131
import { useNavigate } from "react-router-dom";
32-
import api from "../../../common/api";
3332
import { stringSort } from "../../../utils/sortFunctions";
3433
import { actionsTray } from "../Common/FormComponents/common/styleLibrary";
3534

36-
import { ErrorResponseHandler } from "../../../common/types";
3735
import ChangePasswordModal from "./ChangePasswordModal";
3836
import SearchBox from "../Common/SearchBox";
3937
import withSuspense from "../Common/Components/withSuspense";
@@ -56,6 +54,8 @@ import { selFeatures } from "../consoleSlice";
5654
import { useAppDispatch } from "../../../store";
5755
import TooltipWrapper from "../Common/TooltipWrapper/TooltipWrapper";
5856
import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper";
57+
import { api } from "api";
58+
import { errorToHandler } from "api/errors";
5959
import HelpMenu from "../HelpMenu";
6060

6161
const DeleteServiceAccount = withSuspense(
@@ -94,16 +94,16 @@ const Account = () => {
9494

9595
useEffect(() => {
9696
if (loading) {
97-
api
98-
.invoke("GET", `/api/v1/service-accounts`)
99-
.then((res: string[]) => {
100-
const serviceAccounts = res.sort(stringSort);
97+
api.serviceAccounts
98+
.listUserServiceAccounts()
99+
.then((res) => {
100+
const serviceAccounts = res.data.sort(stringSort);
101101

102102
setLoading(false);
103103
setRecords(serviceAccounts);
104104
})
105-
.catch((err: ErrorResponseHandler) => {
106-
dispatch(setErrorSnackMessage(err));
105+
.catch((err) => {
106+
dispatch(setErrorSnackMessage(errorToHandler(err.error)));
107107
setLoading(false);
108108
});
109109
}

portal-ui/src/screens/Console/Account/AddServiceAccountScreen.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ import AddServiceAccountHelpBox from "./AddServiceAccountHelpBox";
3535

3636
import { NewServiceAccount } from "../Common/CredentialsPrompt/types";
3737
import { IAM_PAGES } from "../../../common/SecureComponent/permissions";
38-
import { ErrorResponseHandler } from "../../../../src/common/types";
39-
import api from "../../../../src/common/api";
4038
import CredentialsPrompt from "../Common/CredentialsPrompt/CredentialsPrompt";
4139

4240
import PanelTitle from "../Common/PanelTitle/PanelTitle";
@@ -45,7 +43,10 @@ import { setErrorSnackMessage, setHelpName } from "../../../systemSlice";
4543
import { useAppDispatch } from "../../../store";
4644
import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper";
4745
import { getRandomString } from "../../../common/utils";
46+
import { api } from "api";
47+
import { errorToHandler } from "api/errors";
4848
import HelpMenu from "../HelpMenu";
49+
import { ContentType } from "api/consoleApi";
4950

5051
const AddServiceAccount = () => {
5152
const dispatch = useAppDispatch();
@@ -67,32 +68,35 @@ const AddServiceAccount = () => {
6768

6869
useEffect(() => {
6970
if (addSending) {
70-
api
71-
.invoke("POST", `/api/v1/service-account-credentials`, {
72-
policy: policyJSON,
73-
accessKey: accessKey,
74-
secretKey: secretKey,
75-
})
71+
api.serviceAccountCredentials
72+
.createServiceAccountCreds(
73+
{
74+
policy: policyJSON,
75+
accessKey: accessKey,
76+
secretKey: secretKey,
77+
},
78+
{ type: ContentType.Json }
79+
)
7680
.then((res) => {
7781
setAddSending(false);
7882
setNewServiceAccount({
79-
accessKey: res.accessKey || "",
80-
secretKey: res.secretKey || "",
83+
accessKey: res.data.accessKey || "",
84+
secretKey: res.data.secretKey || "",
8185
url: res.url || "",
8286
});
8387
})
8488

85-
.catch((err: ErrorResponseHandler) => {
89+
.catch((err) => {
8690
setAddSending(false);
87-
dispatch(setErrorSnackMessage(err));
91+
dispatch(setErrorSnackMessage(errorToHandler(err.error)));
8892
});
8993
}
9094
}, [addSending, setAddSending, dispatch, policyJSON, accessKey, secretKey]);
9195

9296
useEffect(() => {
9397
if (isRestrictedByPolicy) {
94-
api.invoke("GET", `/api/v1/user/policy`).then((res: string) => {
95-
setPolicyJSON(JSON.stringify(JSON.parse(res), null, 4));
98+
api.user.getUserPolicy().then((res) => {
99+
setPolicyJSON(JSON.stringify(JSON.parse(res.data), null, 4));
96100
});
97101
}
98102
}, [isRestrictedByPolicy]);
@@ -146,6 +150,7 @@ const AddServiceAccount = () => {
146150
noValidate
147151
autoComplete="off"
148152
onSubmit={(e: React.FormEvent<HTMLFormElement>) => {
153+
e.preventDefault();
149154
addServiceAccount(e);
150155
}}
151156
>

portal-ui/src/screens/Console/Account/ChangePasswordModal.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import { Button, ChangePasswordIcon, InputBox, Grid, FormLayout } from "mds";
1919
import ModalWrapper from "../Common/ModalWrapper/ModalWrapper";
2020
import { LinearProgress } from "@mui/material";
2121
import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary";
22-
import { ChangePasswordRequest } from "../Buckets/types";
23-
import { ErrorResponseHandler } from "../../../common/types";
24-
import api from "../../../common/api";
2522
import { setModalErrorSnackMessage } from "../../../systemSlice";
2623
import { useAppDispatch } from "../../../store";
24+
import { api } from "api";
25+
import { AccountChangePasswordRequest } from "api/consoleApi";
26+
import { errorToHandler } from "api/errors";
2727

2828
interface IChangePasswordProps {
2929
open: boolean;
@@ -67,26 +67,26 @@ const ChangePassword = ({ open, closeModal }: IChangePasswordProps) => {
6767
}
6868
setLoading(true);
6969

70-
let request: ChangePasswordRequest = {
70+
let request: AccountChangePasswordRequest = {
7171
current_secret_key: currentPassword,
7272
new_secret_key: newPassword,
7373
};
7474

75-
api
76-
.invoke("POST", "/api/v1/account/change-password", request)
75+
api.account
76+
.accountChangePassword(request)
7777
.then(() => {
7878
setLoading(false);
7979
setNewPassword("");
8080
setReNewPassword("");
8181
setCurrentPassword("");
8282
closeModal();
8383
})
84-
.catch((err: ErrorResponseHandler) => {
84+
.catch((err) => {
8585
setLoading(false);
8686
setNewPassword("");
8787
setReNewPassword("");
8888
setCurrentPassword("");
89-
dispatch(setModalErrorSnackMessage(err));
89+
dispatch(setModalErrorSnackMessage(errorToHandler(err)));
9090
});
9191
};
9292

portal-ui/src/screens/Console/Account/ChangeUserPasswordModal.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import {
2929
modalStyleUtils,
3030
spacingUtils,
3131
} from "../Common/FormComponents/common/styleLibrary";
32-
import { ChangeUserPasswordRequest } from "../Buckets/types";
3332

34-
import { ErrorResponseHandler } from "../../../common/types";
35-
import api from "../../../common/api";
3633
import { setModalErrorSnackMessage } from "../../../systemSlice";
3734
import { useAppDispatch } from "../../../store";
35+
import { api } from "api";
36+
import { ChangeUserPasswordRequest } from "api/consoleApi";
37+
import { errorToHandler } from "api/errors";
3838

3939
const styles = (theme: Theme) =>
4040
createStyles({
@@ -89,19 +89,19 @@ const ChangeUserPassword = ({
8989
newSecretKey: newPassword,
9090
};
9191

92-
api
93-
.invoke("POST", "/api/v1/account/change-user-password", request)
92+
api.account
93+
.changeUserPassword(request)
9494
.then((res) => {
9595
setLoading(false);
9696
setNewPassword("");
9797
setReNewPassword("");
9898
closeModal();
9999
})
100-
.catch((err: ErrorResponseHandler) => {
100+
.catch((err) => {
101101
setLoading(false);
102102
setNewPassword("");
103103
setReNewPassword("");
104-
dispatch(setModalErrorSnackMessage(err));
104+
dispatch(setModalErrorSnackMessage(errorToHandler(err)));
105105
});
106106
};
107107

portal-ui/src/screens/Console/Account/ServiceAccountPolicy.tsx

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ import {
2727
spacingUtils,
2828
} from "../Common/FormComponents/common/styleLibrary";
2929

30-
import { ErrorResponseHandler } from "../../../common/types";
31-
import api from "../../../common/api";
3230
import ModalWrapper from "../Common/ModalWrapper/ModalWrapper";
3331
import CodeMirrorWrapper from "../Common/FormComponents/CodeMirrorWrapper/CodeMirrorWrapper";
3432
import { encodeURLString } from "../../../common/utils";
3533
import { setModalErrorSnackMessage } from "../../../systemSlice";
3634
import { useAppDispatch } from "../../../store";
35+
import { api } from "api";
36+
import { errorToHandler } from "api/errors";
3737

3838
const styles = (theme: Theme) =>
3939
createStyles({
@@ -73,39 +73,30 @@ const ServiceAccountPolicy = ({
7373
const [policyDefinition, setPolicyDefinition] = useState<string>("");
7474
useEffect(() => {
7575
if (loading) {
76-
api
77-
.invoke(
78-
"GET",
79-
`/api/v1/service-accounts/${encodeURLString(
80-
selectedAccessKey
81-
)}/policy`
82-
)
76+
api.serviceAccounts
77+
.getServiceAccountPolicy(encodeURLString(selectedAccessKey))
8378
.then((res) => {
8479
setLoading(false);
85-
setPolicyDefinition(res);
80+
setPolicyDefinition(res.data);
8681
})
87-
.catch((err: ErrorResponseHandler) => {
82+
.catch((err) => {
8883
setLoading(false);
89-
dispatch(setModalErrorSnackMessage(err));
84+
dispatch(setModalErrorSnackMessage(errorToHandler(err)));
9085
});
9186
}
9287
}, [loading, setLoading, dispatch, selectedAccessKey]);
9388

9489
const setPolicy = (event: React.FormEvent, newPolicy: string) => {
9590
event.preventDefault();
96-
api
97-
.invoke(
98-
"PUT",
99-
`/api/v1/service-accounts/${encodeURLString(selectedAccessKey)}/policy`,
100-
{
101-
policy: newPolicy,
102-
}
103-
)
104-
.then((res) => {
91+
api.serviceAccounts
92+
.setServiceAccountPolicy(encodeURLString(selectedAccessKey), {
93+
policy: newPolicy,
94+
})
95+
.then(() => {
10596
closeModalAndRefresh();
10697
})
107-
.catch((err: ErrorResponseHandler) => {
108-
dispatch(setModalErrorSnackMessage(err));
98+
.catch((err) => {
99+
dispatch(setModalErrorSnackMessage(errorToHandler(err)));
109100
});
110101
};
111102

portal-ui/src/screens/Console/Buckets/BucketDetails/AccessDetailsPanel.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ import Tabs from "@mui/material/Tabs";
2222
import Tab from "@mui/material/Tab";
2323

2424
import { TabPanel } from "../../../shared/tabs";
25-
import { User } from "../../Users/types";
26-
import { ErrorResponseHandler } from "../../../../common/types";
2725
import TableWrapper from "../../Common/TableWrapper/TableWrapper";
28-
import api from "../../../../common/api";
2926
import {
3027
CONSOLE_UI_RESOURCE,
3128
IAM_PAGES,
@@ -40,7 +37,9 @@ import { encodeURLString } from "../../../../common/utils";
4037
import { setErrorSnackMessage, setHelpName } from "../../../../systemSlice";
4138
import { selBucketDetailsLoading } from "./bucketDetailsSlice";
4239
import { useAppDispatch } from "../../../../store";
43-
import { Policy } from "../../../../api/consoleApi";
40+
import { Policy, ServiceAccounts } from "../../../../api/consoleApi";
41+
import { api } from "api";
42+
import { errorToHandler } from "api/errors";
4443

4544
function a11yProps(index: any) {
4645
return {
@@ -58,9 +57,9 @@ const AccessDetails = () => {
5857

5958
const [curTab, setCurTab] = useState<number>(0);
6059
const [loadingPolicies, setLoadingPolicies] = useState<boolean>(true);
61-
const [bucketPolicy, setBucketPolicy] = useState<Policy[]>([]);
60+
const [bucketPolicy, setBucketPolicy] = useState<Policy[] | undefined>([]);
6261
const [loadingUsers, setLoadingUsers] = useState<boolean>(true);
63-
const [bucketUsers, setBucketUsers] = useState<User[]>([]);
62+
const [bucketUsers, setBucketUsers] = useState<ServiceAccounts>([]);
6463

6564
const bucketName = params.bucketName || "";
6665

@@ -117,14 +116,14 @@ const AccessDetails = () => {
117116
useEffect(() => {
118117
if (loadingUsers) {
119118
if (displayUsersList) {
120-
api
121-
.invoke("GET", `/api/v1/bucket-users/${bucketName}`)
122-
.then((res: any) => {
123-
setBucketUsers(res);
119+
api.bucketUsers
120+
.listUsersWithAccessToBucket(bucketName)
121+
.then((res) => {
122+
setBucketUsers(res.data);
124123
setLoadingUsers(false);
125124
})
126-
.catch((err: ErrorResponseHandler) => {
127-
dispatch(setErrorSnackMessage(err));
125+
.catch((err) => {
126+
dispatch(setErrorSnackMessage(errorToHandler(err)));
128127
setLoadingUsers(false);
129128
});
130129
} else {
@@ -141,14 +140,14 @@ const AccessDetails = () => {
141140
useEffect(() => {
142141
if (loadingPolicies) {
143142
if (displayPoliciesList) {
144-
api
145-
.invoke("GET", `/api/v1/bucket-policy/${bucketName}`)
146-
.then((res: any) => {
147-
setBucketPolicy(res.policies);
143+
api.bucketPolicy
144+
.listPoliciesWithBucket(bucketName)
145+
.then((res) => {
146+
setBucketPolicy(res.data.policies);
148147
setLoadingPolicies(false);
149148
})
150-
.catch((err: ErrorResponseHandler) => {
151-
dispatch(setErrorSnackMessage(err));
149+
.catch((err) => {
150+
dispatch(setErrorSnackMessage(errorToHandler(err)));
152151
setLoadingPolicies(false);
153152
});
154153
} else {
@@ -181,15 +180,17 @@ const AccessDetails = () => {
181180
resource={bucketName}
182181
errorProps={{ disabled: true }}
183182
>
184-
<TableWrapper
185-
noBackground={true}
186-
itemActions={PolicyActions}
187-
columns={[{ label: "Name", elementKey: "name" }]}
188-
isLoading={loadingPolicies}
189-
records={bucketPolicy}
190-
entityName="Policies"
191-
idField="name"
192-
/>
183+
{bucketPolicy && (
184+
<TableWrapper
185+
noBackground={true}
186+
itemActions={PolicyActions}
187+
columns={[{ label: "Name", elementKey: "name" }]}
188+
isLoading={loadingPolicies}
189+
records={bucketPolicy}
190+
entityName="Policies"
191+
idField="name"
192+
/>
193+
)}
193194
</SecureComponent>
194195
</TabPanel>
195196

0 commit comments

Comments
 (0)