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

baseSchemas.ts

96 lines4.2 KB

1import * as Effect from "effect/Effect";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4import * as SchemaTransformation from "effect/SchemaTransformation";
5​
6export const TrimmedString = Schema.String.pipe(
7 Schema.decodeTo(
8 Schema.String,
9 SchemaTransformation.transformOrFail({
10 decode: (value) => Effect.succeed(value.trim()),
11 encode: (value) => Effect.succeed(value.trim()),
12 }),
13 ),
14);
15export const TrimmedNonEmptyString = TrimmedString.check(Schema.isNonEmpty());
16​
17export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
18export const PositiveInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1));
19export const PortSchema = Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65535 }));
20​
21export const IsoDateTime = Schema.String;
22export type IsoDateTime = typeof IsoDateTime.Type;
23​
24/**
25 * Wire codec for server→client arrays whose element unions grow over time
26 * (new literal members, new struct variants). Decoding drops elements the
27 * current build cannot decode instead of failing the whole payload — a client
28 * has to keep decoding configs sent by servers newer than itself, and
29 * rejecting the payload would take down the connection over data the client
30 * couldn't act on anyway. Encoding is the plain array encoding.
31 */
32export const ForwardCompatibleArray = <Element extends Schema.Top>(element: Element) => {
33 const decodeElement = Schema.decodeUnknownOption(element as never);
34 return Schema.Array(Schema.Unknown).pipe(
35 Schema.decodeTo(
36 Schema.Array(element),
37 SchemaTransformation.transform<ReadonlyArray<Element["Encoded"]>, ReadonlyArray<unknown>>({
38 decode: (values) =>
39 values.filter((value) => Option.isSome(decodeElement(value))) as ReadonlyArray<
40 Element["Encoded"]
41 >,
42 encode: (values) => values,
43 }),
44 ),
45 );
46};
47​
48/**
49 * Construct a branded identifier. Enforces non-empty trimmed strings
50 */
51const makeEntityId = <Brand extends string>(brand: Brand) => {
52 return TrimmedNonEmptyString.pipe(Schema.brand(brand));
53};
54​
55export const ThreadId = makeEntityId("ThreadId");
56export type ThreadId = typeof ThreadId.Type;
57export const ProjectId = makeEntityId("ProjectId");
58export type ProjectId = typeof ProjectId.Type;
59export const EnvironmentId = makeEntityId("EnvironmentId");
60export type EnvironmentId = typeof EnvironmentId.Type;
61export const CommandId = makeEntityId("CommandId");
62export type CommandId = typeof CommandId.Type;
63export const EventId = makeEntityId("EventId");
64export type EventId = typeof EventId.Type;
65export const MessageId = makeEntityId("MessageId");
66export type MessageId = typeof MessageId.Type;
67export const TurnId = makeEntityId("TurnId");
68export type TurnId = typeof TurnId.Type;
69export const AuthSessionId = makeEntityId("AuthSessionId");
70export type AuthSessionId = typeof AuthSessionId.Type;
71export const RpcClientId = NonNegativeInt.pipe(Schema.brand("RpcClientId"));
72export type RpcClientId = typeof RpcClientId.Type;
73​
74/**
75 * Which client app a connection comes from. Unlike
76 * `AuthClientMetadataDeviceType` (a UA-style device class where web and
77 * desktop are both "desktop"), this names the actual product surface.
78 * Optional everywhere it appears: old clients never send it.
79 */
80export const ClientSurface = Schema.Literals(["web", "desktop", "mobile"]);
81export type ClientSurface = typeof ClientSurface.Type;
82​
83export const ProviderItemId = makeEntityId("ProviderItemId");
84export type ProviderItemId = typeof ProviderItemId.Type;
85export const RuntimeSessionId = makeEntityId("RuntimeSessionId");
86export type RuntimeSessionId = typeof RuntimeSessionId.Type;
87export const RuntimeItemId = makeEntityId("RuntimeItemId");
88export type RuntimeItemId = typeof RuntimeItemId.Type;
89export const RuntimeRequestId = makeEntityId("RuntimeRequestId");
90export type RuntimeRequestId = typeof RuntimeRequestId.Type;
91export const RuntimeTaskId = makeEntityId("RuntimeTaskId");
92export type RuntimeTaskId = typeof RuntimeTaskId.Type;
93export const ApprovalRequestId = makeEntityId("ApprovalRequestId");
94export type ApprovalRequestId = typeof ApprovalRequestId.Type;
95export const CheckpointRef = makeEntityId("CheckpointRef");
96export type CheckpointRef = typeof CheckpointRef.Type;