Skip to content

Commit

Permalink
Normalize double slashes in urls
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-becker committed Dec 23, 2023
1 parent 5a0cac6 commit 44bfe81
Show file tree
Hide file tree
Showing 5 changed files with 635 additions and 4 deletions.
6 changes: 4 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
"preview": "vite preview",
"testt": "vitest"
},
"dependencies": {
"@chakra-ui/react": "2.8.0",
Expand Down Expand Up @@ -39,6 +40,7 @@
"js-cookie": "^3.0.5",
"prettier": "3.0.3",
"typescript": "5.2.2",
"vite": "4.4.9"
"vite": "4.4.9",
"vitest": "^1.1.0"
}
}
37 changes: 37 additions & 0 deletions frontend/src/hooks/api/normalize-url-middleware.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, expect, vi } from 'vitest';
import { normalizeUrlMiddleware } from './normalize-url-middleware.tsx';

describe('normalizeUrlMiddleware', () => {
it('should remove double slash from http url', () => {
const next = vi.fn();
normalizeUrlMiddleware(next)('http://localhost:3000//path/to/endpoint', {});
expect(next).toBeCalledWith('http://localhost:3000/path/to/endpoint', {});
});

it('should remove double slash from https url', () => {
const next = vi.fn();
normalizeUrlMiddleware(next)(
'https://localhost:3000//path/to/endpoint',
{},
);
expect(next).toBeCalledWith('https://localhost:3000/path/to/endpoint', {});
});

it('should keep single slash at http url', () => {
const next = vi.fn();
normalizeUrlMiddleware(next)('http://localhost:3000/path/to/endpoint', {});
expect(next).toBeCalledWith('http://localhost:3000/path/to/endpoint', {});
});

it('should not modify the url when there is no path and no trailing slash', () => {
const next = vi.fn();
normalizeUrlMiddleware(next)('http://localhost:3000', {});
expect(next).toBeCalledWith('http://localhost:3000', {});
});

it('should no modify the url when there is no path and a trailing slash', () => {
const next = vi.fn();
normalizeUrlMiddleware(next)('http://localhost:3000/', {});
expect(next).toBeCalledWith('http://localhost:3000/', {});
});
});
8 changes: 8 additions & 0 deletions frontend/src/hooks/api/normalize-url-middleware.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ConfiguredMiddleware } from 'wretch';

export const normalizeUrlMiddleware: ConfiguredMiddleware = (next) => {
const matchDoubleSlashes = /^(http(s)?:\/\/[^/]*)(\/\/|\/)/;
return (url, opts) => {
return next(url.replace(matchDoubleSlashes, '$1/'), opts);
};
};
5 changes: 4 additions & 1 deletion frontend/src/hooks/api/use-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { useContext, useMemo } from 'react';
import wretch from 'wretch';
import { UserContext } from '../../state/user.context.tsx';
import QueryStringAddon from 'wretch/addons/queryString';
import { normalizeUrlMiddleware } from './normalize-url-middleware.tsx';

const instance = wretch(import.meta.env.VITE_API_URL).addon(QueryStringAddon);
const instance = wretch(import.meta.env.VITE_API_URL)
.middlewares([normalizeUrlMiddleware])
.addon(QueryStringAddon);

export const useApi = () => {
const user = useContext(UserContext);
Expand Down
Loading

0 comments on commit 44bfe81

Please sign in to comment.