mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-31 20:43:41 +00:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { literals, some, string } from "ts-matches"
|
|
|
|
type NestedPath<A extends string, B extends string> = `/${A}/${string}/${B}`
|
|
type NestedPaths =
|
|
| NestedPath<"actions", "run" | "get">
|
|
| NestedPath<"dependencies", "query" | "update">
|
|
// prettier-ignore
|
|
type UnNestPaths<A> =
|
|
A extends `${infer A}/${infer B}` ? [...UnNestPaths<A>, ... UnNestPaths<B>] :
|
|
[A]
|
|
|
|
export function unNestPath<A extends string>(a: A): UnNestPaths<A> {
|
|
return a.split("/") as UnNestPaths<A>
|
|
}
|
|
function isNestedPath(path: string): path is NestedPaths {
|
|
const paths = path.split("/")
|
|
if (paths.length !== 4) return false
|
|
if (paths[1] === "actions" && (paths[3] === "run" || paths[3] === "get"))
|
|
return true
|
|
if (
|
|
paths[1] === "dependencies" &&
|
|
(paths[3] === "query" || paths[3] === "update")
|
|
)
|
|
return true
|
|
return false
|
|
}
|
|
export const jsonPath = some(
|
|
literals(
|
|
"/init",
|
|
"/uninit",
|
|
"/main/start",
|
|
"/main/stop",
|
|
"/config/set",
|
|
"/config/get",
|
|
"/backup/create",
|
|
"/backup/restore",
|
|
"/actions/metadata",
|
|
"/properties",
|
|
),
|
|
string.refine(isNestedPath, "isNestedPath"),
|
|
)
|
|
|
|
export type JsonPath = typeof jsonPath._TYPE
|