Skip to content

Commit 7030df1

Browse files
authored
Merge pull request #1228 from andypols/fix-invalid-api-base-url
fix: bug in using API_BASE with URL
2 parents db1debc + c6e3635 commit 7030df1

File tree

6 files changed

+26
-43
lines changed

6 files changed

+26
-43
lines changed

src/service/routes/users.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,8 @@ const db = require('../../db');
44
const { toPublicUser } = require('./publicApi');
55

66
router.get('/', async (req, res) => {
7-
const query = {};
8-
9-
console.log(`fetching users = query path =${JSON.stringify(req.query)}`);
10-
for (const k in req.query) {
11-
if (!k) continue;
12-
13-
if (k === 'limit') continue;
14-
if (k === 'skip') continue;
15-
let v = req.query[k];
16-
if (v === 'false') v = false;
17-
if (v === 'true') v = true;
18-
query[k] = v;
19-
}
20-
21-
const users = await db.getUsers(query);
7+
console.log(`fetching users`);
8+
const users = await db.getUsers({});
229
res.send(users.map(toPublicUser));
2310
});
2411

src/ui/apiBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ const stripTrailingSlashes = (s: string) => s.replace(/\/+$/, '');
88
*/
99
export const API_BASE = process.env.VITE_API_URI
1010
? stripTrailingSlashes(process.env.VITE_API_URI)
11-
: '';
11+
: location.origin;

src/ui/services/user.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ const getUsers = async (
4141
setData: SetStateCallback<UserData[]>,
4242
setAuth: SetStateCallback<boolean>,
4343
setErrorMessage: SetStateCallback<string>,
44-
query: Record<string, string> = {},
4544
): Promise<void> => {
46-
const url = new URL(`${API_BASE}/api/v1/user`);
47-
url.search = new URLSearchParams(query).toString();
48-
4945
setIsLoading(true);
5046

5147
try {
52-
const response: AxiosResponse<UserData[]> = await axios(url.toString(), getAxiosConfig());
48+
const response: AxiosResponse<UserData[]> = await axios(
49+
`${API_BASE}/api/v1/user`,
50+
getAxiosConfig(),
51+
);
5352
setData(response.data);
5453
} catch (error) {
5554
if (axios.isAxiosError(error)) {
@@ -70,10 +69,8 @@ const getUsers = async (
7069

7170
const updateUser = async (data: UserData): Promise<void> => {
7271
console.log(data);
73-
const url = new URL(`${API_BASE}/api/auth/gitAccount`);
74-
7572
try {
76-
await axios.post(url.toString(), data, getAxiosConfig());
73+
await axios.post(`${API_BASE}/api/auth/gitAccount`, data, getAxiosConfig());
7774
} catch (error) {
7875
const axiosError = error as AxiosError;
7976
if (axiosError.response) {
@@ -89,10 +86,11 @@ const getUserLoggedIn = async (
8986
setIsError: SetStateCallback<boolean>,
9087
setAuth: SetStateCallback<boolean>,
9188
): Promise<void> => {
92-
const url = new URL(`${API_BASE}/api/auth/me`);
93-
9489
try {
95-
const response: AxiosResponse<UserData> = await axios(url.toString(), getAxiosConfig());
90+
const response: AxiosResponse<UserData> = await axios(
91+
`${API_BASE}/api/auth/me`,
92+
getAxiosConfig(),
93+
);
9694
const data = response.data;
9795
setIsLoading(false);
9896
setIsAdmin(data.admin || false);

src/ui/views/RepoDetails/Components/AddUser.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const AddUserDialog: React.FC<AddUserDialogProps> = ({
7777
};
7878

7979
useEffect(() => {
80-
getUsers(setIsLoading, setData, setAuth, setErrorMessage, {});
80+
getUsers(setIsLoading, setData, setAuth, setErrorMessage);
8181
}, []);
8282

8383
if (errorMessage) return <Danger>{errorMessage}</Danger>;

src/ui/views/UserList/Components/UserList.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ import Search from '../../../components/Search/Search';
1919
import Danger from '../../../components/Typography/Danger';
2020
import { UserData } from '../../../../types/models';
2121

22-
interface UserListProps {
23-
[key: string]: any;
24-
}
25-
2622
const useStyles = makeStyles(styles as any);
2723

28-
const UserList: React.FC<UserListProps> = (props) => {
24+
const UserList: React.FC = () => {
2925
const classes = useStyles();
3026
const [data, setData] = useState<UserData[]>([]);
3127
const [, setAuth] = useState<boolean>(true);
@@ -40,14 +36,8 @@ const UserList: React.FC<UserListProps> = (props) => {
4036
navigate(`/dashboard/admin/user/${username}`, { replace: true });
4137

4238
useEffect(() => {
43-
const query: Record<string, any> = {};
44-
45-
for (const k in props) {
46-
if (!k) continue;
47-
query[k] = props[k];
48-
}
49-
getUsers(setIsLoading, setData, setAuth, setErrorMessage, query);
50-
}, [props]);
39+
getUsers(setIsLoading, setData, setAuth, setErrorMessage);
40+
});
5141

5242
if (isLoading) return <div>Loading...</div>;
5343
if (errorMessage) return <Danger>{errorMessage}</Danger>;

test/ui/apiBase.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ function loadApiBase() {
99
describe('apiBase', () => {
1010
let originalEnv;
1111

12+
before(() => {
13+
global.location = { origin: 'https://lovely-git-proxy.com' };
14+
});
15+
16+
after(() => {
17+
delete global.location;
18+
});
19+
1220
beforeEach(() => {
1321
originalEnv = process.env.VITE_API_URI;
1422
delete process.env.VITE_API_URI;
@@ -24,9 +32,9 @@ describe('apiBase', () => {
2432
delete require.cache[require.resolve('../../src/ui/apiBase')];
2533
});
2634

27-
it('uses empty string when VITE_API_URI is not set', () => {
35+
it('uses the location origin when VITE_API_URI is not set', () => {
2836
const { API_BASE } = loadApiBase();
29-
expect(API_BASE).to.equal('');
37+
expect(API_BASE).to.equal('https://lovely-git-proxy.com');
3038
});
3139

3240
it('returns the exact value when no trailing slash', () => {

0 commit comments

Comments
 (0)