2021-03-18 22:19:35 -05:00
|
|
|
interface Result {
|
|
|
|
type?: string;
|
|
|
|
objectBody?: {
|
|
|
|
id: string;
|
|
|
|
self: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseDocumentsPath(resourcePath: string): Result {
|
|
|
|
if (typeof resourcePath !== "string") {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resourcePath.length === 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resourcePath[resourcePath.length - 1] !== "/") {
|
|
|
|
resourcePath = resourcePath + "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resourcePath[0] !== "/") {
|
|
|
|
resourcePath = "/" + resourcePath;
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:19:35 -05:00
|
|
|
let id: string;
|
|
|
|
let type: string;
|
|
|
|
const pathParts = resourcePath.split("/");
|
|
|
|
|
|
|
|
if (pathParts.length % 2 === 0) {
|
|
|
|
id = pathParts[pathParts.length - 2];
|
|
|
|
type = pathParts[pathParts.length - 3];
|
|
|
|
} else {
|
|
|
|
id = pathParts[pathParts.length - 3];
|
|
|
|
type = pathParts[pathParts.length - 2];
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2021-03-18 22:19:35 -05:00
|
|
|
const result = {
|
|
|
|
type: type,
|
|
|
|
objectBody: {
|
|
|
|
id: id,
|
|
|
|
self: resourcePath,
|
|
|
|
},
|
|
|
|
};
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2021-03-18 22:19:35 -05:00
|
|
|
return result;
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2021-03-18 22:19:35 -05:00
|
|
|
export function createUri(baseUri: string, relativeUri: string): string {
|
|
|
|
if (!baseUri) {
|
|
|
|
throw new Error("baseUri is null or empty");
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
2021-03-18 22:19:35 -05:00
|
|
|
|
|
|
|
const slashAtEndOfUriRegex = /\/$/,
|
|
|
|
slashAtStartOfUriRegEx = /^\//;
|
|
|
|
|
|
|
|
const normalizedBaseUri = baseUri.replace(slashAtEndOfUriRegex, "") + "/",
|
|
|
|
normalizedRelativeUri = (relativeUri && relativeUri.replace(slashAtStartOfUriRegEx, "")) || "";
|
|
|
|
|
|
|
|
return normalizedBaseUri + normalizedRelativeUri;
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|