cloudaxe-opencode/packages/console/core/src/identifier.ts

34 lines
801 B
TypeScript
Raw Permalink Normal View History

2025-08-08 17:22:54 +00:00
import { ulid } from "ulid"
import { z } from "zod"
export namespace Identifier {
const prefixes = {
account: "acc",
2025-10-16 21:50:30 +00:00
auth: "aut",
2025-12-28 18:54:09 +00:00
benchmark: "ben",
2025-08-08 17:22:54 +00:00
billing: "bil",
key: "key",
2026-02-24 09:45:39 +00:00
lite: "lit",
2025-10-08 04:03:34 +00:00
model: "mod",
2025-08-08 17:22:54 +00:00
payment: "pay",
2025-10-08 17:31:12 +00:00
provider: "prv",
referral: "ref",
2026-01-09 00:24:20 +00:00
subscription: "sub",
2025-08-08 17:22:54 +00:00
usage: "usg",
user: "usr",
workspace: "wrk",
} as const
export function create(prefix: keyof typeof prefixes, given?: string): string {
if (given) {
if (given.startsWith(prefixes[prefix])) return given
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
}
return [prefixes[prefix], ulid()].join("_")
}
export function schema(prefix: keyof typeof prefixes) {
return z.string().startsWith(prefixes[prefix])
}
}