chore: refactor url creation

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

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();
}