2024-11-21 00:15:30 +00:00
|
|
|
import { baseURL } from "@/utils/constants.js";
|
|
|
|
|
2022-05-02 13:47:22 +00:00
|
|
|
export function removeLastDir(url) {
|
2021-03-21 11:51:58 +00:00
|
|
|
var arr = url.split("/");
|
|
|
|
if (arr.pop() === "") {
|
|
|
|
arr.pop();
|
2018-02-01 12:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 11:51:58 +00:00
|
|
|
return arr.join("/");
|
2018-02-01 12:17:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 11:13:14 +00:00
|
|
|
// this code borrow from mozilla
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#Examples
|
2022-05-02 13:47:22 +00:00
|
|
|
export function encodeRFC5987ValueChars(str) {
|
2021-03-21 11:51:58 +00:00
|
|
|
return (
|
|
|
|
encodeURIComponent(str)
|
2019-07-05 11:13:14 +00:00
|
|
|
// Note that although RFC3986 reserves "!", RFC5987 does not,
|
|
|
|
// so we do not need to escape it
|
2021-03-21 11:51:58 +00:00
|
|
|
.replace(/['()]/g, escape) // i.e., %27 %28 %29
|
|
|
|
.replace(/\*/g, "%2A")
|
|
|
|
// The following are not required for percent-encoding per RFC5987,
|
|
|
|
// so we can allow for a little better readability over the wire: |`^
|
|
|
|
.replace(/%(?:7C|60|5E)/g, unescape)
|
|
|
|
);
|
2019-07-05 11:13:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 13:47:22 +00:00
|
|
|
export function encodePath(str) {
|
2021-03-21 11:51:58 +00:00
|
|
|
return str
|
|
|
|
.split("/")
|
|
|
|
.map((v) => encodeURIComponent(v))
|
|
|
|
.join("/");
|
2020-08-05 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 17:45:27 +00:00
|
|
|
|
|
|
|
export function pathsMatch(url1, url2) {
|
|
|
|
return removeTrailingSlash(url1) == removeTrailingSlash(url2);
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:17:04 +00:00
|
|
|
export default {
|
2024-07-30 17:45:27 +00:00
|
|
|
pathsMatch,
|
|
|
|
removeTrailingSlash,
|
2022-05-02 13:47:22 +00:00
|
|
|
encodeRFC5987ValueChars,
|
|
|
|
removeLastDir,
|
|
|
|
encodePath,
|
2024-11-21 00:15:30 +00:00
|
|
|
removePrefix,
|
|
|
|
getApiPath
|
2021-03-21 11:51:58 +00:00
|
|
|
};
|
2024-11-21 00:15:30 +00:00
|
|
|
|
|
|
|
export function removePrefix(path, prefix) {
|
2024-11-27 13:30:03 +00:00
|
|
|
if (path === undefined) {
|
|
|
|
return ""
|
|
|
|
}
|
2024-11-21 00:15:30 +00:00
|
|
|
if (prefix != "") {
|
|
|
|
prefix = "/" + trimSlashes(prefix)
|
|
|
|
}
|
|
|
|
const combined = trimSlashes(baseURL) + prefix
|
|
|
|
// Remove combined (baseURL + prefix) from the start of the path if present
|
|
|
|
if (path.startsWith(combined)) {
|
|
|
|
path = path.slice(combined.length);
|
|
|
|
} else if (path.startsWith(prefix)) {
|
|
|
|
// Fallback: remove only the prefix if the combined string isn't present
|
|
|
|
path = path.slice(prefix.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure path starts with '/'
|
|
|
|
if (!path.startsWith('/')) {
|
|
|
|
path = '/' + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get path with parameters
|
|
|
|
export function getApiPath(path, params = {}) {
|
|
|
|
if (path.startsWith("/")) {
|
|
|
|
path = path.slice(1);
|
|
|
|
}
|
|
|
|
path = `${baseURL}${path}`;
|
|
|
|
if (Object.keys(params).length > 0) {
|
|
|
|
path += "?";
|
|
|
|
}
|
|
|
|
for (const key in params) {
|
|
|
|
if (params[key] === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
path += `${key}=${params[key]}&`;
|
|
|
|
}
|
|
|
|
// remove trailing &
|
|
|
|
if (path.endsWith("&")) {
|
|
|
|
path = path.slice(0, -1);
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeTrailingSlash(str) {
|
|
|
|
if (str.endsWith('/')) {
|
|
|
|
return str.slice(0, -1);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeLeadingSlash(str) {
|
|
|
|
if (str.startsWith('/')) {
|
|
|
|
return str.slice(1);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function trimSlashes(str) {
|
|
|
|
return removeLeadingSlash(removeTrailingSlash(str))
|
|
|
|
}
|