bananas-commerce-admin
Version:
What's this, an admin for apes?
60 lines • 1.95 kB
JavaScript
import { capitalize, nthIndexOf } from "../util";
/**
* Parses an `operationId` returning app, view and action
*
* ## Examples
*
* `bananas.me:list` -> `{ app: "bananas", view: "me", action: "list" }`
*
* `bananas.login:create` -> `{ app: "bananas", view: "login", action: "create" }`
*
* `bananas.i18n:list` -> `{ app: "bananas", view: "i18n", action: "list" }`
*/
export function parseOperationId(operationId) {
const match = operationId.match(/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+):([a-zA-Z0-9_-]+)$/);
if (match !== null) {
return {
app: match[1],
view: match[2],
action: match[3],
};
}
return undefined;
}
export function isNavigation(tags) {
return tags.includes("navigation");
}
export function getPath(endpoint, method, action) {
return (method.toLowerCase() === "get" ? endpoint : `${endpoint}${action ?? ""}/`).replace(/{([^}]*)}/, ":$1");
}
function getBasePath(path) {
return path.substring(0, nthIndexOf(path, "/", 2, 1) + 1 || undefined);
}
export function getPage(path, action) {
const basePath = getBasePath(path);
const relativeBasePath = basePath.slice(1);
return `${relativeBasePath}${action}`;
}
export function getAppLabel(tags) {
return tags.filter((tag) => tag.startsWith("app:"))[0]?.split(":")[1];
}
export function getView(id) {
return id.substring(id.indexOf(".") + 1, id.indexOf(":"));
}
export function getTitle(view, summary) {
return summary ?? (view && capitalize(view.replace("_", " ")));
}
export function stripPathPrefix(path, prefix) {
if (prefix == null) {
return path;
}
if (path.startsWith(prefix)) {
return path.slice(prefix.length);
}
console.warn(`Path "${path}" does not start with api prefix "${prefix}"`);
return path;
}
export function parentPath(schema) {
return schema.substring(0, schema.lastIndexOf("/"));
}
//# sourceMappingURL=routes.js.map