Cleanup duplicated slash handling

This commit is contained in:
Zoe Roux 2024-05-19 19:00:13 +02:00
parent e0935d7faa
commit 0124275d8c
No known key found for this signature in database
2 changed files with 8 additions and 4 deletions

View File

@ -33,6 +33,11 @@ import { type Page, Paged } from "./page";
export let lastUsedUrl: string = null!; export let lastUsedUrl: string = null!;
const cleanSlash = (str: string | null) => {
if (!str) return null;
return str.replace(/^\/|\/$/g, "");
};
export const queryFn = async <Parser extends z.ZodTypeAny>( export const queryFn = async <Parser extends z.ZodTypeAny>(
context: { context: {
apiUrl?: string | null; apiUrl?: string | null;
@ -54,17 +59,16 @@ export const queryFn = async <Parser extends z.ZodTypeAny>(
lastUsedUrl = url!; lastUsedUrl = url!;
const token = iToken === undefined && context.authenticated !== false ? await getToken() : iToken; const token = iToken === undefined && context.authenticated !== false ? await getToken() : iToken;
const path = [url] const path = [cleanSlash(url)]
.concat( .concat(
"path" in context "path" in context
? (context.path as string[]) ? (context.path as string[])
: "pageParam" in context && context.pageParam : "pageParam" in context && context.pageParam
? [context.pageParam as string] ? [cleanSlash(context.pageParam as string)]
: (context.queryKey as string[]), : (context.queryKey as string[]),
) )
.filter((x) => x) .filter((x) => x)
.join("/") .join("/")
.replace("//", "/")
.replace("/?", "?"); .replace("/?", "?");
let resp: Response; let resp: Response;
try { try {

View File

@ -48,7 +48,7 @@ const query: QueryIdentifier<ServerInfo> = {
export const ServerUrlPage: QueryPage = () => { export const ServerUrlPage: QueryPage = () => {
const [_apiUrl, setApiUrl] = useState(""); const [_apiUrl, setApiUrl] = useState("");
const apiUrl = cleanApiUrl(_apiUrl); const apiUrl = cleanApiUrl(_apiUrl);
const { data, error } = useFetch({ ...query, options: { apiUrl } }); const { data, error } = useFetch({ ...query, options: { apiUrl, authenticated: false } });
const router = useRouter(); const router = useRouter();
const { t } = useTranslation(); const { t } = useTranslation();
const { css } = useYoshiki(); const { css } = useYoshiki();