158 lines
5.4 KiB
TypeScript
158 lines
5.4 KiB
TypeScript
const progress = new Set(["day", "type", "loi", "screen"]);
|
|
/** Browser inputs select only these implemented read operations, never an arbitrary upstream URL. */
|
|
export function isProjectReadPath(slug: string[]): boolean {
|
|
if (slug.length === 1) return slug[0] === "list" || slug[0] === "summary";
|
|
if (slug.length === 3 && slug[2] === "access")
|
|
return isProjectReadPath([slug[0], slug[1], "question", "list"]);
|
|
return (
|
|
slug.length === 4 &&
|
|
(slug[0] === "0" || slug[0] === "1") &&
|
|
slug[1] !== "." &&
|
|
slug[1] !== ".." &&
|
|
slug[1].length > 0 &&
|
|
slug[1].length <= 20 &&
|
|
!/[|/\\]/.test(slug[1]) &&
|
|
((slug[2] === "progress" && progress.has(slug[3])) ||
|
|
(slug[2] === "question" && slug[3] === "list"))
|
|
);
|
|
}
|
|
export function projectReadQuery(
|
|
slug: string[],
|
|
input: URLSearchParams,
|
|
): Record<string, string> {
|
|
const allowed =
|
|
slug[0] === "list"
|
|
? ["projectType", "intranetCode", "userTeam", "keyword"]
|
|
: slug.length === 4 && slug[2] === "progress" && slug[3] !== "day"
|
|
? ["startDate", "endDate"]
|
|
: [];
|
|
const result: Record<string, string> = {};
|
|
for (const key of allowed) {
|
|
const value = input.get(key);
|
|
if (value !== null) result[key] = value;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** Quota BFF: `{type}/{pkey}/list` (GET) and `{type}/{pkey}/{quotaOrder}/result` (POST) only. */
|
|
export function isQuotaListPath(slug: string[]): boolean {
|
|
return (
|
|
slug.length === 3 &&
|
|
slug[2] === "list" &&
|
|
isProjectReadPath([slug[0], slug[1], "question", "list"])
|
|
);
|
|
}
|
|
export function isQuotaResultPath(slug: string[]): boolean {
|
|
return (
|
|
slug.length === 4 &&
|
|
/^\d{1,4}$/.test(slug[2]) &&
|
|
slug[3] === "result" &&
|
|
isProjectReadPath([slug[0], slug[1], "question", "list"])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Bookmark BFF: `{type}/{pkey}/list?page=` (GET), `{type}/{pkey}` (POST) and
|
|
* `{type}/{pkey}/{bookmarkId}` (PUT/DELETE), forwarded under `/project/{type}/{pkey}/bookmark`.
|
|
*/
|
|
const isProjectSlug = (slug: string[]) =>
|
|
isProjectReadPath([slug[0], slug[1], "question", "list"]);
|
|
export function isBookmarkListPath(slug: string[]): boolean {
|
|
return slug.length === 3 && slug[2] === "list" && isProjectSlug(slug);
|
|
}
|
|
export function isBookmarkCreatePath(slug: string[]): boolean {
|
|
return slug.length === 2 && isProjectSlug(slug);
|
|
}
|
|
export function isBookmarkItemPath(slug: string[]): boolean {
|
|
const id = slug[2] ?? "";
|
|
return (
|
|
slug.length === 3 &&
|
|
id !== "list" &&
|
|
id !== "." &&
|
|
id !== ".." &&
|
|
id.length > 0 &&
|
|
id.length <= 100 &&
|
|
!/[\s|/\\]/.test(id) &&
|
|
isProjectSlug(slug)
|
|
);
|
|
}
|
|
export function bookmarkUpstreamPath(slug: string[]): string {
|
|
return (
|
|
"/project/" +
|
|
slug.slice(0, 2).map(encodeURIComponent).join("/") +
|
|
"/bookmark" +
|
|
slug.slice(2).map((part) => "/" + encodeURIComponent(part)).join("")
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Derived variable BFF: `{type}/{pkey}/list` (GET), `{type}/{pkey}` (POST) and
|
|
* `{type}/{pkey}/{variableId}` (PUT/DELETE) under `/project/{type}/{pkey}/derived`.
|
|
* Recalculation endpoints stay closed (the legacy screen never calls them).
|
|
*/
|
|
export function isDerivedListPath(slug: string[]): boolean {
|
|
return slug.length === 3 && slug[2] === "list" && isProjectSlug(slug);
|
|
}
|
|
export function isDerivedCreatePath(slug: string[]): boolean {
|
|
return slug.length === 2 && isProjectSlug(slug);
|
|
}
|
|
export function isDerivedItemPath(slug: string[]): boolean {
|
|
return (
|
|
slug.length === 3 &&
|
|
slug[2] !== "list" &&
|
|
slug[2] !== "recalculate" &&
|
|
/^[A-Za-z][A-Za-z0-9_]{0,127}$/.test(slug[2]) &&
|
|
isProjectSlug(slug)
|
|
);
|
|
}
|
|
export function derivedUpstreamPath(slug: string[]): string {
|
|
return (
|
|
"/project/" +
|
|
slug.slice(0, 2).map(encodeURIComponent).join("/") +
|
|
"/derived" +
|
|
slug.slice(2).map((part) => "/" + encodeURIComponent(part)).join("")
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Download BFF (`/api/export/...`): fixed file endpoints only. Returns the upstream path and the
|
|
* request body kind, or null when the method/path is not an implemented download.
|
|
*/
|
|
export type ExportTarget = { path: string; body: "none" | "excel" | "data" };
|
|
export function exportTarget(method: "GET" | "POST", slug: string[]): ExportTarget | null {
|
|
if (method === "GET" && slug.join("/") === "form/question")
|
|
return { path: "/export/form/question", body: "none" };
|
|
if (slug.length !== 3 && slug.length !== 4) return null;
|
|
if (!isProjectSlug(slug)) return null;
|
|
const project = slug.slice(0, 2).map(encodeURIComponent).join("/");
|
|
const action = slug.slice(2).join("/");
|
|
if (method === "GET") {
|
|
if (action === "form/data" || action === "form/title" || action === "guide")
|
|
return { path: "/project/" + project + "/export/" + action, body: "none" };
|
|
return null;
|
|
}
|
|
if (action === "table")
|
|
return { path: "/project/" + project + "/export/excel/table", body: "excel" };
|
|
if (action === "quota") return { path: "/quota/" + project + "/export/excel", body: "excel" };
|
|
if (action === "data") return { path: "/project/" + project + "/export/data", body: "data" };
|
|
return null;
|
|
}
|
|
|
|
/** Import BFF: `{type}/{pkey}/{title|question|data}` multipart uploads. */
|
|
export function isImportPath(slug: string[]): boolean {
|
|
return (
|
|
slug.length === 3 &&
|
|
(slug[2] === "title" || slug[2] === "question" || slug[2] === "data") &&
|
|
isProjectSlug(slug)
|
|
);
|
|
}
|
|
|
|
export function isReportStreamPath(slug: string[]): boolean {
|
|
return (
|
|
slug.length === 4 &&
|
|
slug[2] === "statistics" &&
|
|
slug[3] === "result" &&
|
|
isProjectReadPath([slug[0], slug[1], "question", "list"])
|
|
);
|
|
}
|