chore: refactor url creation

This commit is contained in:
Ramires Viana committed 2022-05-02 13:47:22 +00:00
1 parent f663237a16
commit 9734f707f0
12 files changed
+106 -69

No files matched your search

+16
View File
@@ -1,6 +1,7 @@
import store from "@/store";
import { renew } from "@/utils/auth";
import { baseURL } from "@/utils/constants";
import { encodePath } from "@/utils/url";
export async function fetchURL(url, opts) {
opts = opts || {};
@@ -45,3 +46,18 @@ export function removePrefix(url) {
if (url[0] !== "/") url = "/" + url;
return url;
}
export function createURL(endpoint, params = {}, auth = true) {
const url = new URL(encodePath(endpoint), origin + baseURL);
const searchParams = {
...(auth && { auth: store.state.jwt }),
...params,
};
for (const key in searchParams) {
url.searchParams.set(key, searchParams[key]);
}
return url.toString();
}