diff options
| author | Dax Raad <[email protected]> | 2025-05-17 21:31:42 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-05-26 12:40:17 -0400 |
| commit | a34d020bc6b252e842f042d935c7a0e6444460cf (patch) | |
| tree | ea3484499dff80e82d421e879ab639133ae9c3b4 /js/src/id | |
| parent | 96fbc37f0175052291f8a096d530bd4480f6cb19 (diff) | |
| download | opencode-a34d020bc6b252e842f042d935c7a0e6444460cf.tar.gz opencode-a34d020bc6b252e842f042d935c7a0e6444460cf.zip | |
sync
Diffstat (limited to 'js/src/id')
| -rw-r--r-- | js/src/id/id.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/js/src/id/id.ts b/js/src/id/id.ts new file mode 100644 index 000000000..a63716041 --- /dev/null +++ b/js/src/id/id.ts @@ -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]); + } +} |
