Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

baseSchemas.ts

60 lines2.6 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import * as SchemaTransformation from "effect/SchemaTransformation";
4​
5export const TrimmedString = Schema.String.pipe(
6 Schema.decodeTo(
7 Schema.String,
8 SchemaTransformation.transformOrFail({
9 decode: (value) => Effect.succeed(value.trim()),
10 encode: (value) => Effect.succeed(value.trim()),
11 }),
12 ),
13);
14export const TrimmedNonEmptyString = TrimmedString.check(Schema.isNonEmpty());
15​
16export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
17export const PositiveInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1));
18export const PortSchema = Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65535 }));
19​
20export const IsoDateTime = Schema.String;
21export type IsoDateTime = typeof IsoDateTime.Type;
22​
23/**
24 * Construct a branded identifier. Enforces non-empty trimmed strings
25 */
26const makeEntityId = <Brand extends string>(brand: Brand) => {
27 return TrimmedNonEmptyString.pipe(Schema.brand(brand));
28};
29​
30export const ThreadId = makeEntityId("ThreadId");
31export type ThreadId = typeof ThreadId.Type;
32export const ProjectId = makeEntityId("ProjectId");
33export type ProjectId = typeof ProjectId.Type;
34export const EnvironmentId = makeEntityId("EnvironmentId");
35export type EnvironmentId = typeof EnvironmentId.Type;
36export const CommandId = makeEntityId("CommandId");
37export type CommandId = typeof CommandId.Type;
38export const EventId = makeEntityId("EventId");
39export type EventId = typeof EventId.Type;
40export const MessageId = makeEntityId("MessageId");
41export type MessageId = typeof MessageId.Type;
42export const TurnId = makeEntityId("TurnId");
43export type TurnId = typeof TurnId.Type;
44export const AuthSessionId = makeEntityId("AuthSessionId");
45export type AuthSessionId = typeof AuthSessionId.Type;
46​
47export const ProviderItemId = makeEntityId("ProviderItemId");
48export type ProviderItemId = typeof ProviderItemId.Type;
49export const RuntimeSessionId = makeEntityId("RuntimeSessionId");
50export type RuntimeSessionId = typeof RuntimeSessionId.Type;
51export const RuntimeItemId = makeEntityId("RuntimeItemId");
52export type RuntimeItemId = typeof RuntimeItemId.Type;
53export const RuntimeRequestId = makeEntityId("RuntimeRequestId");
54export type RuntimeRequestId = typeof RuntimeRequestId.Type;
55export const RuntimeTaskId = makeEntityId("RuntimeTaskId");
56export type RuntimeTaskId = typeof RuntimeTaskId.Type;
57export const ApprovalRequestId = makeEntityId("ApprovalRequestId");
58export type ApprovalRequestId = typeof ApprovalRequestId.Type;
59export const CheckpointRef = makeEntityId("CheckpointRef");
60export type CheckpointRef = typeof CheckpointRef.Type;