Files
start-os/sdk/lib/store/PathBuilder.ts
2024-03-23 09:03:56 -06:00

39 lines
1.0 KiB
TypeScript

import { Affine } from "../util"
const pathValue = Symbol("pathValue")
export type PathValue = typeof pathValue
export type PathBuilderStored<AllStore, Store> = {
[K in PathValue]: [AllStore, Store]
}
export type PathBuilder<AllStore, Store = AllStore> = (Store extends Record<
string,
unknown
>
? {
[K in keyof Store]: PathBuilder<AllStore, Store[K]>
}
: {}) &
PathBuilderStored<AllStore, Store>
export type StorePath = string & Affine<"StorePath">
const privateSymbol = Symbol("jsonPath")
export const extractJsonPath = (builder: PathBuilder<unknown>) => {
return (builder as any)[privateSymbol] as StorePath
}
export const pathBuilder = <Store, StorePath = Store>(
paths: string[] = [],
): PathBuilder<Store, StorePath> => {
return new Proxy({} as PathBuilder<Store, StorePath>, {
get(target, prop) {
if (prop === privateSymbol) {
if (paths.length === 0) return ""
return `/${paths.join("/")}`
}
return pathBuilder<any>([...paths, prop as string])
},
}) as PathBuilder<Store, StorePath>
}