Files
opencode/js/src/id/id.ts
Dax Raad a34d020bc6 sync
2025-05-26 12:40:17 -04:00

24 lines
567 B
TypeScript

import { ulid } from "ulid";
import { z } from "zod";
export namespace Identifier {
const prefixes = {
session: "ses",
} 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]);
}
}