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

View File

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