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

33 lines
780 B
TypeScript
Raw Normal View History

2025-08-09 01:22:54 +08:00
import { ulid } from "ulid"
import { z } from "zod"
export namespace Identifier {
const prefixes = {
account: "acc",
2025-10-17 05:50:30 +08:00
auth: "aut",
2025-12-29 02:54:09 +08:00
benchmark: "ben",
2025-08-09 01:22:54 +08:00
billing: "bil",
key: "key",
2026-02-24 17:45:39 +08:00
lite: "lit",
2025-10-08 12:03:34 +08:00
model: "mod",
2025-08-09 01:22:54 +08:00
payment: "pay",
2025-10-09 01:31:12 +08:00
provider: "prv",
2026-01-09 08:24:20 +08:00
subscription: "sub",
2025-08-09 01:22:54 +08: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])
}
}