This commit is contained in:
Dax Raad
2025-05-17 21:31:42 -04:00
parent 96fbc37f01
commit a34d020bc6
26 changed files with 979 additions and 54 deletions

23
js/src/id/id.ts Normal file
View File

@@ -0,0 +1,23 @@
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]);
}
}