@distilled.cloud/core 1.0.0-rc.2 → 1.0.0-rc.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (321) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +33 -0
  3. package/lib/_bundle/aws.js +2 -0
  4. package/lib/_bundle/aws.js.map +1 -0
  5. package/lib/_bundle/base.js +5 -0
  6. package/lib/_bundle/base.js.map +1 -0
  7. package/lib/_bundle/graphql.js +3 -0
  8. package/lib/_bundle/graphql.js.map +1 -0
  9. package/lib/_bundle/grpc.js +3 -0
  10. package/lib/_bundle/grpc.js.map +1 -0
  11. package/lib/_bundle/rest.js +2 -0
  12. package/lib/_bundle/rest.js.map +1 -0
  13. package/lib/_bundle/xml.js +4 -0
  14. package/lib/_bundle/xml.js.map +1 -0
  15. package/lib/annotations.d.ts +17 -0
  16. package/lib/annotations.d.ts.map +1 -0
  17. package/lib/annotations.js +2 -0
  18. package/lib/annotations.js.map +1 -0
  19. package/lib/api.d.ts +220 -51
  20. package/lib/api.d.ts.map +1 -1
  21. package/lib/api.js +1 -178
  22. package/lib/category.d.ts +19 -16
  23. package/lib/category.d.ts.map +1 -1
  24. package/lib/category.js +1 -280
  25. package/lib/chunks.d.ts +3 -0
  26. package/lib/chunks.d.ts.map +1 -0
  27. package/lib/chunks.js +1 -0
  28. package/lib/config.d.ts +81 -0
  29. package/lib/config.d.ts.map +1 -0
  30. package/lib/config.js +1 -0
  31. package/lib/crypto.d.ts +76 -0
  32. package/lib/crypto.d.ts.map +1 -0
  33. package/lib/crypto.js +1 -0
  34. package/lib/error-category.d.ts +1 -1
  35. package/lib/error-category.d.ts.map +1 -1
  36. package/lib/error-category.js +1 -46
  37. package/lib/errors.d.ts +99 -74
  38. package/lib/errors.d.ts.map +1 -1
  39. package/lib/errors.js +1 -186
  40. package/lib/graphql.d.ts +420 -0
  41. package/lib/graphql.d.ts.map +1 -0
  42. package/lib/graphql.js +1 -0
  43. package/lib/graphql.types.d.ts +11 -0
  44. package/lib/graphql.types.d.ts.map +1 -0
  45. package/lib/graphql.types.js +1 -0
  46. package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
  47. package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
  48. package/lib/grpc-transport-cloudflare-http2.js +6 -0
  49. package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
  50. package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
  51. package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
  52. package/lib/grpc-transport-cloudflare-sockets.js +5 -0
  53. package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
  54. package/lib/grpc-transport-node-http2.d.ts +19 -0
  55. package/lib/grpc-transport-node-http2.d.ts.map +1 -0
  56. package/lib/grpc-transport-node-http2.js +2 -0
  57. package/lib/grpc-transport-node-http2.js.map +1 -0
  58. package/lib/grpc-transport.d.ts +111 -0
  59. package/lib/grpc-transport.d.ts.map +1 -0
  60. package/lib/grpc-transport.js +1 -0
  61. package/lib/hpack.d.ts +34 -0
  62. package/lib/hpack.d.ts.map +1 -0
  63. package/lib/hpack.js +2 -0
  64. package/lib/hpack.js.map +1 -0
  65. package/lib/index.d.ts +32 -0
  66. package/lib/index.d.ts.map +1 -0
  67. package/lib/index.js +17 -0
  68. package/lib/max-message-bytes.d.ts +42 -0
  69. package/lib/max-message-bytes.d.ts.map +1 -0
  70. package/lib/max-message-bytes.js +2 -0
  71. package/lib/max-message-bytes.js.map +1 -0
  72. package/lib/ndjson.d.ts +3 -0
  73. package/lib/ndjson.d.ts.map +1 -0
  74. package/lib/ndjson.js +2 -0
  75. package/lib/ndjson.js.map +1 -0
  76. package/lib/pagination.d.ts +105 -25
  77. package/lib/pagination.d.ts.map +1 -1
  78. package/lib/pagination.js +1 -182
  79. package/lib/plain-values.d.ts +21 -0
  80. package/lib/plain-values.d.ts.map +1 -0
  81. package/lib/plain-values.js +2 -0
  82. package/lib/plain-values.js.map +1 -0
  83. package/lib/protobuf.d.ts +85 -0
  84. package/lib/protobuf.d.ts.map +1 -0
  85. package/lib/protobuf.js +1 -0
  86. package/lib/protocol-grpc.d.ts +123 -0
  87. package/lib/protocol-grpc.d.ts.map +1 -0
  88. package/lib/protocol-grpc.js +1 -0
  89. package/lib/protocol-http.d.ts +206 -25
  90. package/lib/protocol-http.d.ts.map +1 -1
  91. package/lib/protocol-http.js +1 -554
  92. package/lib/protocol-rest.d.ts +258 -64
  93. package/lib/protocol-rest.d.ts.map +1 -1
  94. package/lib/protocol-rest.js +1 -242
  95. package/lib/protocol.d.ts +113 -0
  96. package/lib/protocol.d.ts.map +1 -0
  97. package/lib/protocol.js +0 -0
  98. package/lib/query.d.ts +29 -0
  99. package/lib/query.d.ts.map +1 -0
  100. package/lib/query.js +1 -0
  101. package/lib/response-validation-strict.d.ts +48 -0
  102. package/lib/response-validation-strict.d.ts.map +1 -0
  103. package/lib/response-validation-strict.js +1 -0
  104. package/lib/response-validation.d.ts +86 -0
  105. package/lib/response-validation.d.ts.map +1 -0
  106. package/lib/response-validation.js +1 -0
  107. package/lib/retry-after.d.ts +4 -4
  108. package/lib/retry-after.d.ts.map +1 -1
  109. package/lib/retry-after.js +1 -112
  110. package/lib/retry-state.d.ts +26 -0
  111. package/lib/retry-state.d.ts.map +1 -0
  112. package/lib/retry-state.js +2 -0
  113. package/lib/retry-state.js.map +1 -0
  114. package/lib/retry.d.ts +141 -42
  115. package/lib/retry.d.ts.map +1 -1
  116. package/lib/retry.js +1 -210
  117. package/lib/schema-lite-impl.d.ts +179 -0
  118. package/lib/schema-lite-impl.d.ts.map +1 -0
  119. package/lib/schema-lite-impl.js +1 -0
  120. package/lib/schema-lite-typed.d.ts +41 -0
  121. package/lib/schema-lite-typed.d.ts.map +1 -0
  122. package/lib/schema-lite-typed.js +1 -0
  123. package/lib/schema-lite.d.ts +41 -0
  124. package/lib/schema-lite.d.ts.map +1 -0
  125. package/lib/schema-lite.js +1 -0
  126. package/lib/schema.d.ts +21 -7
  127. package/lib/schema.d.ts.map +1 -1
  128. package/lib/schema.js +1 -48
  129. package/lib/sdk-error.d.ts +31 -0
  130. package/lib/sdk-error.d.ts.map +1 -0
  131. package/lib/sdk-error.js +1 -0
  132. package/lib/send.d.ts +37 -0
  133. package/lib/send.d.ts.map +1 -0
  134. package/lib/send.js +2 -0
  135. package/lib/send.js.map +1 -0
  136. package/lib/sensitive.d.ts +28 -0
  137. package/lib/sensitive.d.ts.map +1 -0
  138. package/lib/sensitive.js +2 -0
  139. package/lib/sensitive.js.map +1 -0
  140. package/lib/telemetry-debug.d.ts +6 -0
  141. package/lib/telemetry-debug.d.ts.map +1 -0
  142. package/lib/telemetry-debug.js +2 -0
  143. package/lib/telemetry-debug.js.map +1 -0
  144. package/lib/telemetry-hooks.d.ts +32 -0
  145. package/lib/telemetry-hooks.d.ts.map +1 -0
  146. package/lib/telemetry-hooks.js +2 -0
  147. package/lib/telemetry-hooks.js.map +1 -0
  148. package/lib/telemetry-host-rules.d.ts +23 -0
  149. package/lib/telemetry-host-rules.d.ts.map +1 -0
  150. package/lib/telemetry-host-rules.js +2 -0
  151. package/lib/telemetry-host-rules.js.map +1 -0
  152. package/lib/telemetry-lambda-context.d.ts +17 -0
  153. package/lib/telemetry-lambda-context.d.ts.map +1 -0
  154. package/lib/telemetry-lambda-context.js +2 -0
  155. package/lib/telemetry-lambda-context.js.map +1 -0
  156. package/lib/telemetry-message-context.d.ts +52 -0
  157. package/lib/telemetry-message-context.d.ts.map +1 -0
  158. package/lib/telemetry-message-context.js +2 -0
  159. package/lib/telemetry-message-context.js.map +1 -0
  160. package/lib/telemetry-metrics.d.ts +6 -0
  161. package/lib/telemetry-metrics.d.ts.map +1 -0
  162. package/lib/telemetry-metrics.js +2 -0
  163. package/lib/telemetry-metrics.js.map +1 -0
  164. package/lib/telemetry-name-template.d.ts +13 -0
  165. package/lib/telemetry-name-template.d.ts.map +1 -0
  166. package/lib/telemetry-name-template.js +2 -0
  167. package/lib/telemetry-name-template.js.map +1 -0
  168. package/lib/telemetry-stream.d.ts +11 -0
  169. package/lib/telemetry-stream.d.ts.map +1 -0
  170. package/lib/telemetry-stream.js +2 -0
  171. package/lib/telemetry-stream.js.map +1 -0
  172. package/lib/telemetry-xray.d.ts +28 -0
  173. package/lib/telemetry-xray.d.ts.map +1 -0
  174. package/lib/telemetry-xray.js +2 -0
  175. package/lib/telemetry-xray.js.map +1 -0
  176. package/lib/telemetry.d.ts +317 -0
  177. package/lib/telemetry.d.ts.map +1 -0
  178. package/lib/telemetry.js +3 -0
  179. package/lib/telemetry.js.map +1 -0
  180. package/lib/token-cache.d.ts +31 -0
  181. package/lib/token-cache.d.ts.map +1 -0
  182. package/lib/token-cache.js +2 -0
  183. package/lib/token-cache.js.map +1 -0
  184. package/lib/trait.d.ts +94 -20
  185. package/lib/trait.d.ts.map +1 -1
  186. package/lib/trait.js +1 -107
  187. package/lib/unknown-error.d.ts +18 -0
  188. package/lib/unknown-error.d.ts.map +1 -0
  189. package/lib/unknown-error.js +1 -0
  190. package/lib/xml.d.ts +44 -0
  191. package/lib/xml.d.ts.map +1 -0
  192. package/lib/xml.js +1 -0
  193. package/package.json +13 -13
  194. package/src/annotations.ts +53 -0
  195. package/src/api.ts +1597 -234
  196. package/src/category.ts +97 -134
  197. package/src/chunks.ts +30 -0
  198. package/src/config.ts +261 -0
  199. package/src/crypto.ts +202 -0
  200. package/src/error-category.ts +16 -29
  201. package/src/errors.ts +99 -77
  202. package/src/graphql.ts +1817 -0
  203. package/src/graphql.types.ts +120 -0
  204. package/src/grpc-transport-cloudflare-http2.ts +569 -0
  205. package/src/grpc-transport-cloudflare-sockets.ts +368 -0
  206. package/src/grpc-transport-node-http2.ts +307 -0
  207. package/src/grpc-transport.ts +287 -0
  208. package/src/hpack.ts +331 -0
  209. package/src/index.ts +31 -0
  210. package/src/max-message-bytes.ts +58 -0
  211. package/src/ndjson.ts +55 -0
  212. package/src/pagination.ts +379 -94
  213. package/src/plain-values.ts +70 -0
  214. package/src/protobuf.ts +1367 -0
  215. package/src/protocol-grpc.ts +811 -0
  216. package/src/protocol-http.ts +1045 -371
  217. package/src/protocol-rest.ts +908 -255
  218. package/src/protocol.ts +150 -0
  219. package/src/query.ts +39 -0
  220. package/src/response-validation-strict.ts +333 -0
  221. package/src/response-validation.ts +161 -0
  222. package/src/retry-after.ts +8 -14
  223. package/src/retry-state.ts +105 -0
  224. package/src/retry.ts +260 -145
  225. package/src/schema-lite-impl.ts +621 -0
  226. package/src/schema-lite-typed.ts +44 -0
  227. package/src/schema-lite.ts +45 -0
  228. package/src/schema.ts +259 -15
  229. package/src/sdk-error.ts +37 -0
  230. package/src/send.ts +156 -0
  231. package/src/sensitive.ts +188 -0
  232. package/src/telemetry-debug.ts +179 -0
  233. package/src/telemetry-hooks.ts +33 -0
  234. package/src/telemetry-host-rules.ts +39 -0
  235. package/src/telemetry-lambda-context.ts +46 -0
  236. package/src/telemetry-message-context.ts +170 -0
  237. package/src/telemetry-metrics.ts +69 -0
  238. package/src/telemetry-name-template.ts +26 -0
  239. package/src/telemetry-stream.ts +100 -0
  240. package/src/telemetry-xray.ts +66 -0
  241. package/src/telemetry.ts +800 -0
  242. package/src/token-cache.ts +117 -0
  243. package/src/trait.ts +139 -67
  244. package/src/unknown-error.ts +38 -0
  245. package/src/xml.ts +346 -0
  246. package/lib/api.js.map +0 -1
  247. package/lib/category.js.map +0 -1
  248. package/lib/codegen/cli.d.ts +0 -29
  249. package/lib/codegen/cli.d.ts.map +0 -1
  250. package/lib/codegen/cli.js +0 -165
  251. package/lib/codegen/cli.js.map +0 -1
  252. package/lib/codegen/emit.d.ts +0 -129
  253. package/lib/codegen/emit.d.ts.map +0 -1
  254. package/lib/codegen/emit.js +0 -105
  255. package/lib/codegen/emit.js.map +0 -1
  256. package/lib/codegen/format.d.ts +0 -23
  257. package/lib/codegen/format.d.ts.map +0 -1
  258. package/lib/codegen/format.js +0 -28
  259. package/lib/codegen/format.js.map +0 -1
  260. package/lib/codegen/generator.d.ts +0 -334
  261. package/lib/codegen/generator.d.ts.map +0 -1
  262. package/lib/codegen/generator.js +0 -691
  263. package/lib/codegen/generator.js.map +0 -1
  264. package/lib/codegen/graph.d.ts +0 -36
  265. package/lib/codegen/graph.d.ts.map +0 -1
  266. package/lib/codegen/graph.js +0 -136
  267. package/lib/codegen/graph.js.map +0 -1
  268. package/lib/codegen/members.d.ts +0 -25
  269. package/lib/codegen/members.d.ts.map +0 -1
  270. package/lib/codegen/members.js +0 -55
  271. package/lib/codegen/members.js.map +0 -1
  272. package/lib/codegen/naming.d.ts +0 -29
  273. package/lib/codegen/naming.d.ts.map +0 -1
  274. package/lib/codegen/naming.js +0 -74
  275. package/lib/codegen/naming.js.map +0 -1
  276. package/lib/codegen/openapi-cli.d.ts +0 -38
  277. package/lib/codegen/openapi-cli.d.ts.map +0 -1
  278. package/lib/codegen/openapi-cli.js +0 -107
  279. package/lib/codegen/openapi-cli.js.map +0 -1
  280. package/lib/codegen/openapi.d.ts +0 -115
  281. package/lib/codegen/openapi.d.ts.map +0 -1
  282. package/lib/codegen/openapi.js +0 -1220
  283. package/lib/codegen/openapi.js.map +0 -1
  284. package/lib/codegen/operations.d.ts +0 -24
  285. package/lib/codegen/operations.d.ts.map +0 -1
  286. package/lib/codegen/operations.js +0 -56
  287. package/lib/codegen/operations.js.map +0 -1
  288. package/lib/codegen/pagination.d.ts +0 -39
  289. package/lib/codegen/pagination.d.ts.map +0 -1
  290. package/lib/codegen/pagination.js +0 -33
  291. package/lib/codegen/pagination.js.map +0 -1
  292. package/lib/codegen/prelude.d.ts +0 -15
  293. package/lib/codegen/prelude.d.ts.map +0 -1
  294. package/lib/codegen/prelude.js +0 -60
  295. package/lib/codegen/prelude.js.map +0 -1
  296. package/lib/error-category.js.map +0 -1
  297. package/lib/errors.js.map +0 -1
  298. package/lib/json-patch.d.ts +0 -44
  299. package/lib/json-patch.d.ts.map +0 -1
  300. package/lib/json-patch.js +0 -174
  301. package/lib/json-patch.js.map +0 -1
  302. package/lib/pagination.js.map +0 -1
  303. package/lib/protocol-http.js.map +0 -1
  304. package/lib/protocol-rest.js.map +0 -1
  305. package/lib/retry-after.js.map +0 -1
  306. package/lib/retry.js.map +0 -1
  307. package/lib/schema.js.map +0 -1
  308. package/lib/trait.js.map +0 -1
  309. package/src/codegen/cli.ts +0 -268
  310. package/src/codegen/emit.ts +0 -207
  311. package/src/codegen/format.ts +0 -47
  312. package/src/codegen/generator.ts +0 -1153
  313. package/src/codegen/graph.ts +0 -151
  314. package/src/codegen/members.ts +0 -71
  315. package/src/codegen/naming.ts +0 -86
  316. package/src/codegen/openapi-cli.ts +0 -166
  317. package/src/codegen/openapi.ts +0 -1450
  318. package/src/codegen/operations.ts +0 -76
  319. package/src/codegen/pagination.ts +0 -71
  320. package/src/codegen/prelude.ts +0 -70
  321. package/src/json-patch.ts +0 -222
@@ -0,0 +1,621 @@
1
+ /**
2
+ * Minimal schemas for generated SDK code: plain objects shaped like Effect
3
+ * Schema ASTs (`_tag`, `annotations`, `propertySignatures`, `types`,
4
+ * `thunk`, `encoding`), without Effect Schema's parser, issue and
5
+ * transformation machinery.
6
+ *
7
+ * Request encoding and lenient response decoding only walk the AST: they
8
+ * read wire names, HTTP bindings and the shape of the value. Importing
9
+ * `effect/Schema` to build those ASTs put about 37 KB gzip of decode
10
+ * machinery into every bundle that never decodes. These nodes carry the
11
+ * same fields protocol code reads, so it runs unchanged.
12
+ *
13
+ * When a caller asks for real validation (`ResponseValidation.strict` or
14
+ * `additionalProperties`), {@link toEffect} rebuilds the equivalent Effect
15
+ * schema from a lite node, loading `effect/Schema` only then.
16
+ *
17
+ * Every value here has the members generated code uses on schemas: `.ast`,
18
+ * `.pipe(...)`, `.annotate(...)`, `.fields` (structs) and `.key`/`.value`
19
+ * (records).
20
+ */
21
+ import type * as Cause from "effect/Cause";
22
+ import * as Data from "effect/Data";
23
+ import { pipeArguments, type Pipeable } from "effect/Pipeable";
24
+ import type * as EffectSchema from "effect/Schema";
25
+
26
+ export type Annotations = Record<string | symbol, unknown>;
27
+
28
+ /** A lite AST node. Field names match Effect's SchemaAST where protocols read them. */
29
+ export interface Node {
30
+ readonly _tag: string;
31
+ readonly annotations?: Annotations;
32
+ /**
33
+ * The wire-side node, for nodes whose value changes on the wire (AWS
34
+ * timestamps, blobs, sensitive values). Same shape as Effect's encoding
35
+ * chain, so code that follows `encoding[0].to` reads the wire type.
36
+ */
37
+ readonly encoding?: ReadonlyArray<{ readonly to: Node }>;
38
+ /** Declaration: the type guard strict validation applies (Unknown without one). */
39
+ readonly check?: (u: unknown) => boolean;
40
+ readonly context?: { readonly isOptional: boolean };
41
+ /** Objects */
42
+ readonly propertySignatures?: ReadonlyArray<PropertySignature>;
43
+ readonly indexSignatures?: ReadonlyArray<{ readonly parameter: Node; readonly type: Node }>;
44
+ /** Union */
45
+ readonly types?: ReadonlyArray<Node>;
46
+ /** Arrays */
47
+ readonly elements?: ReadonlyArray<Node>;
48
+ readonly rest?: ReadonlyArray<Node>;
49
+ /** Suspend */
50
+ readonly thunk?: () => Node;
51
+ /** Literal */
52
+ readonly literal?: unknown;
53
+ /** Declaration */
54
+ readonly typeParameters?: ReadonlyArray<Node>;
55
+ }
56
+
57
+ export interface PropertySignature {
58
+ readonly name: PropertyKey;
59
+ readonly type: Node;
60
+ }
61
+
62
+ /** A lite schema value: what generated code holds and passes around. */
63
+ export interface Lite {
64
+ readonly ast: Node;
65
+ readonly "~lite": true;
66
+ pipe(...fns: ReadonlyArray<(s: any) => any>): any;
67
+ annotate(annotations: Annotations): Lite;
68
+ }
69
+
70
+ /** Marks lite nodes so strict validation knows to convert them. */
71
+ export const liteKey = "~lite";
72
+ const node = <N extends Node>(n: N): N => Object.assign(n, { [liteKey]: true });
73
+
74
+ const Proto = {
75
+ "~lite": true,
76
+ pipe(this: Lite) {
77
+ return pipeArguments(this, arguments);
78
+ },
79
+ annotate(this: Lite, annotations: Annotations): Lite {
80
+ return make(
81
+ { ...this.ast, annotations: { ...this.ast.annotations, ...annotations } } as Node,
82
+ this,
83
+ );
84
+ },
85
+ };
86
+
87
+ const make = (ast: Node, from?: object): Lite => {
88
+ const self = Object.create(Proto) as { ast: Node };
89
+ self.ast = ast;
90
+ // Struct `.fields` and record `.key`/`.value` travel with annotate.
91
+ if (from)
92
+ for (const k of ["fields", "key", "value"]) if (k in from) (self as any)[k] = (from as any)[k];
93
+ return self as unknown as Lite;
94
+ };
95
+
96
+ /** A lite schema for a hand-built node (SDK-specific declarations). */
97
+ export const fromNode = (n: Node): Lite => make(node(n));
98
+
99
+ export const isLite = (u: unknown): u is Lite =>
100
+ (typeof u === "object" || typeof u === "function") && u !== null && (u as Lite)["~lite"] === true;
101
+
102
+ const astOf = (s: Lite | { readonly ast: Node }): Node => s.ast;
103
+
104
+ //#region primitives
105
+
106
+ export const String: Lite = /*@__PURE__*/ make(node({ _tag: "String" }));
107
+ export const Number: Lite = /*@__PURE__*/ make(node({ _tag: "Number" }));
108
+ export const Boolean: Lite = /*@__PURE__*/ make(node({ _tag: "Boolean" }));
109
+ export const Unknown: Lite = /*@__PURE__*/ make(node({ _tag: "Unknown" }));
110
+ export const Any: Lite = /*@__PURE__*/ make(node({ _tag: "Any" }));
111
+ export const Null: Lite = /*@__PURE__*/ make(node({ _tag: "Null" }));
112
+ export const Void: Lite = /*@__PURE__*/ make(node({ _tag: "Void" }));
113
+ export const Undefined: Lite = /*@__PURE__*/ make(node({ _tag: "Undefined" }));
114
+
115
+ //#endregion
116
+
117
+ //#region combinators
118
+
119
+ export const Literal = (value: unknown): Lite => make(node({ _tag: "Literal", literal: value }));
120
+
121
+ export const Literals = (values: ReadonlyArray<unknown>): Lite =>
122
+ make(node({ _tag: "Union", types: values.map((v) => node({ _tag: "Literal", literal: v })) }));
123
+
124
+ export const optional = (s: Lite): Lite => {
125
+ const inner = astOf(s);
126
+ return make(
127
+ node({
128
+ _tag: "Union",
129
+ types: [inner, node({ _tag: "Undefined" })],
130
+ context: { isOptional: true },
131
+ }),
132
+ );
133
+ };
134
+
135
+ export const NullOr = (s: Lite): Lite =>
136
+ make(node({ _tag: "Union", types: [astOf(s), node({ _tag: "Null" })] }));
137
+
138
+ export const Union = (members: ReadonlyArray<Lite>): Lite =>
139
+ make(node({ _tag: "Union", types: members.map(astOf) }));
140
+
141
+ export const Array = (item: Lite): Lite =>
142
+ make(node({ _tag: "Arrays", elements: [], rest: [astOf(item)] }));
143
+
144
+ export const Tuple = (elements: ReadonlyArray<Lite>): Lite =>
145
+ make(node({ _tag: "Arrays", elements: elements.map(astOf), rest: [] }));
146
+
147
+ export const Record = (key: Lite, value: Lite): Lite => {
148
+ const s = make(
149
+ node({
150
+ _tag: "Objects",
151
+ propertySignatures: [],
152
+ indexSignatures: [{ parameter: astOf(key), type: astOf(value) }],
153
+ }),
154
+ );
155
+ (s as any).key = key;
156
+ (s as any).value = value;
157
+ return s;
158
+ };
159
+
160
+ export const Struct = (fields: Readonly<Record<string, Lite>>): Lite => {
161
+ const s = make(
162
+ node({
163
+ _tag: "Objects",
164
+ propertySignatures: Object.keys(fields).map((name) => {
165
+ const type = astOf(fields[name]!);
166
+ return { name, type };
167
+ }),
168
+ indexSignatures: [],
169
+ }),
170
+ );
171
+ (s as any).fields = fields;
172
+ return s;
173
+ };
174
+
175
+ /**
176
+ * A struct that also allows keys it does not declare (OpenAPI
177
+ * `additionalProperties` next to `properties`): the struct's property
178
+ * signatures plus each record's index signature, as Effect's
179
+ * `StructWithRest` builds them.
180
+ */
181
+ export const StructWithRest = (struct: Lite, records: ReadonlyArray<Lite>): Lite => {
182
+ const base = resolveObjects(astOf(struct));
183
+ const s = make(
184
+ node({
185
+ _tag: "Objects",
186
+ propertySignatures: base.propertySignatures ?? [],
187
+ indexSignatures: [
188
+ ...(base.indexSignatures ?? []),
189
+ ...records.flatMap((r) => resolveObjects(astOf(r)).indexSignatures ?? []),
190
+ ],
191
+ }),
192
+ );
193
+ (s as any).fields = (struct as any).fields;
194
+ return s;
195
+ };
196
+
197
+ const resolveObjects = (n: Node): Node => (n._tag === "Suspend" ? resolveObjects(n.thunk!()) : n);
198
+
199
+ /**
200
+ * `struct`'s members, then the members of `base` (a struct, possibly
201
+ * suspended) that `struct` does not declare. An operation input that inlines
202
+ * its request body is the body struct extended with the path and query
203
+ * members, without a second copy of every body field. Resolves `base`, so
204
+ * call it inside the input's `suspend`.
205
+ */
206
+ export const Extend = (base: Lite, struct: Lite): Lite => {
207
+ const own = resolveObjects(astOf(struct));
208
+ const names = new Set((own.propertySignatures ?? []).map((p) => p.name));
209
+ const inherited = resolveObjects(astOf(base)).propertySignatures ?? [];
210
+ return make(
211
+ node({
212
+ _tag: "Objects",
213
+ propertySignatures: [
214
+ ...(own.propertySignatures ?? []),
215
+ ...inherited.filter((p) => !names.has(p.name)),
216
+ ],
217
+ indexSignatures: own.indexSignatures ?? [],
218
+ }),
219
+ );
220
+ };
221
+
222
+ /** A lazily resolved schema; `.ast` is a Suspend node whose thunk resolves the target. */
223
+ export const suspend = (f: () => Lite): Lite => {
224
+ let resolved: Node | undefined;
225
+ return make(node({ _tag: "Suspend", thunk: () => (resolved ??= astOf(f())) }));
226
+ };
227
+
228
+ /** Rename struct keys on the wire: `{ typeKey: "wire_key" }`. */
229
+ export const encodeKeys =
230
+ (mapping: Readonly<Record<string, string>>) =>
231
+ (s: Lite): Lite =>
232
+ s.annotate({ [encodeKeysKey]: mapping });
233
+ export const encodeKeysKey = "@distilled.cloud/core/schema-lite/encodeKeys";
234
+
235
+ //#endregion
236
+
237
+ //#region declarations and errors
238
+
239
+ /**
240
+ * Effect Schema's `representation` annotation for its built-in declaration
241
+ * `id` (`effect/schema/Date`, ...). A lite declaration of the same type
242
+ * carries it, so {@link toEffect} rebuilds Effect's own schema for it (and
243
+ * `effect/Arbitrary` can generate values for the result).
244
+ */
245
+ export const representation = (id: string) => ({ id, payload: null });
246
+
247
+ const declaration = (identifier: string): Lite =>
248
+ make(
249
+ node({
250
+ _tag: "Declaration",
251
+ typeParameters: [],
252
+ annotations: { identifier, representation: representation(`effect/schema/${identifier}`) },
253
+ }),
254
+ );
255
+
256
+ export const Date: Lite = /*@__PURE__*/ declaration("Date");
257
+ export const Duration: Lite = /*@__PURE__*/ declaration("Duration");
258
+ export const Redacted = (value: Lite): Lite =>
259
+ make(
260
+ node({
261
+ _tag: "Declaration",
262
+ typeParameters: [astOf(value)],
263
+ annotations: {
264
+ identifier: "Redacted",
265
+ representation: representation("effect/schema/Redacted"),
266
+ },
267
+ }),
268
+ );
269
+ export const instanceOf = (cls: Function, annotations?: Annotations): Lite =>
270
+ make(
271
+ node({
272
+ _tag: "Declaration",
273
+ typeParameters: [],
274
+ annotations: {
275
+ identifier: cls.name,
276
+ ...(cls === globalThis.Uint8Array && {
277
+ representation: representation("effect/schema/Uint8Array"),
278
+ }),
279
+ ...annotations,
280
+ },
281
+ }),
282
+ );
283
+ export const declare = (_is: (u: unknown) => boolean, annotations?: Annotations): Lite =>
284
+ make(node({ _tag: "Declaration", typeParameters: [], annotations: { ...annotations } }));
285
+
286
+ /**
287
+ * A tagged error class without Effect Schema: a yieldable `Data.TaggedError`
288
+ * that keeps only its declared fields (as the schema class did) and carries
289
+ * `.fields`, `.ast` and a static `.pipe` for category decorators. It does not
290
+ * validate on construction.
291
+ */
292
+ const tagKey = "~distilled/tag";
293
+ const keysKey = "~distilled/keys";
294
+ const astKey = "~distilled/ast";
295
+ const annotationsKey = "~distilled/annotations";
296
+ const annotationMetaKey = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/annotation-meta");
297
+
298
+ /**
299
+ * The annotations an error class declares: a plain object, or a pipeable
300
+ * trait (`T.all(...)`), whose own enumerable keys are its annotations.
301
+ */
302
+ const ownAnnotations = (from: object): Annotations => {
303
+ const out: Annotations = {};
304
+ for (const k of Reflect.ownKeys(from))
305
+ if (k !== annotationMetaKey && Object.prototype.propertyIsEnumerable.call(from, k))
306
+ out[k] = (from as any)[k];
307
+ return out;
308
+ };
309
+
310
+ /**
311
+ * Shared base of every lite error class: a yieldable `Data.Error` that keeps
312
+ * only the declared fields and sets `_tag` and `name` like
313
+ * `Data.TaggedError`. One class per error costs ~0.6 KB; building a
314
+ * `Data.TaggedError` base for each cost ~2.3 KB.
315
+ */
316
+ let liteErrorBase: any;
317
+ const LiteErrorBase = (): any =>
318
+ (liteErrorBase ??= class LiteError extends (Data.Error as any) {
319
+ constructor(props?: Record<string, unknown>) {
320
+ const keys: ReadonlyArray<string> = (new.target as any)[keysKey];
321
+ const picked: Record<string, unknown> = {};
322
+ if (props) for (const k of keys) if (props[k] !== undefined) picked[k] = props[k];
323
+ super(picked);
324
+ (this as any)._tag = (new.target as any)[tagKey];
325
+ }
326
+ /** The fields as a struct node carrying the tag (`identifier`) and class annotations. */
327
+ static get ast(): Node {
328
+ const C = this as any;
329
+ if (Object.hasOwn(C, astKey)) return C[astKey];
330
+ const fields: Record<string, Lite> = C.fields;
331
+ return (C[astKey] = node({
332
+ _tag: "Objects",
333
+ propertySignatures: Object.keys(fields).map((name) => ({
334
+ name,
335
+ type: astOf(fields[name]!),
336
+ })),
337
+ indexSignatures: [],
338
+ annotations: { identifier: C[tagKey], ...C[annotationsKey] },
339
+ }));
340
+ }
341
+ static pipe(this: unknown) {
342
+ return pipeArguments(this, arguments);
343
+ }
344
+ /** `new this(props)`, like Effect Schema's `TaggedError.make`. */
345
+ static make(
346
+ this: new (props?: Record<string, unknown>) => unknown,
347
+ props?: Record<string, unknown>,
348
+ ) {
349
+ return new this(props);
350
+ }
351
+ /**
352
+ * Whether `u` is an error of this class, by `_tag`. Matching the tag
353
+ * (not `instanceof`) holds across duplicate copies of a package.
354
+ */
355
+ static is(this: unknown, u: unknown): boolean {
356
+ return typeof u === "object" && u !== null && (u as any)._tag === (this as any)[tagKey];
357
+ }
358
+ });
359
+
360
+ /**
361
+ * The type of a lite tagged error class: what the class really has. It is
362
+ * not an Effect Schema, so `Schema.is(E)`, `Schema.decode(E)` and friends do
363
+ * not compile against it -- at runtime they would fail on the lite `.ast`.
364
+ * Match errors with `_tag` (`Effect.catchTag`) or `instanceof`.
365
+ */
366
+ export interface TaggedErrorClass<
367
+ Tag extends string,
368
+ Fields extends EffectSchema.Struct.Fields,
369
+ > extends Pipeable {
370
+ new (
371
+ ...args: {} extends EffectSchema.Struct.MakeIn<Fields>
372
+ ? [props?: EffectSchema.Struct.MakeIn<Fields>]
373
+ : [props: EffectSchema.Struct.MakeIn<Fields>]
374
+ ): Cause.YieldableError & { readonly _tag: Tag } & EffectSchema.Struct.Type<Fields>;
375
+ /** `new this(props)`. */
376
+ make<C extends abstract new (...args: any) => any>(
377
+ this: C,
378
+ ...args: ConstructorParameters<C>
379
+ ): InstanceType<C>;
380
+ /**
381
+ * Whether `u` is an error of this class, by `_tag`, so it holds across
382
+ * duplicate copies of a package (`instanceof` does not). Errors of another
383
+ * service with the same tag also match.
384
+ */
385
+ is<C extends abstract new (...args: any) => any>(this: C, u: unknown): u is InstanceType<C>;
386
+ readonly fields: Fields;
387
+ readonly identifier: Tag;
388
+ /** The lite struct node (not an Effect Schema AST). */
389
+ readonly ast: Node;
390
+ }
391
+
392
+ /**
393
+ * `TaggedError<Self>()(tag, fields)`, typed by the field schemas. `Self` is
394
+ * accepted for source compatibility with `Schema.TaggedError` and unused.
395
+ */
396
+ export type TaggedErrorFactory = <_Self = never>(
397
+ identifier?: string,
398
+ ) => <Tag extends string, const Fields extends EffectSchema.Struct.Fields = {}>(
399
+ tag: Tag,
400
+ fields?: Fields,
401
+ annotations?: object,
402
+ ) => TaggedErrorClass<Tag, Fields>;
403
+
404
+ /**
405
+ * A tagged error class without Effect Schema. It does not validate on
406
+ * construction. Carries `.fields` and `.identifier`, and a static `.pipe`
407
+ * for category decorators.
408
+ */
409
+ export const TaggedError =
410
+ (_identifier?: string) =>
411
+ (tag: string, fields: Readonly<Record<string, Lite>> = {}, annotations?: object): any => {
412
+ const C = class extends LiteErrorBase() {} as any;
413
+ C[tagKey] = tag;
414
+ C[keysKey] = Object.keys(fields);
415
+ if (annotations) C[annotationsKey] = ownAnnotations(annotations);
416
+ C.fields = fields;
417
+ C.identifier = tag;
418
+ C.prototype.name = tag;
419
+ return C;
420
+ };
421
+
422
+ /**
423
+ * Schema `j` of builder `build`, made on first use (the lite `chunks.at`).
424
+ * Its `.ast` is the built schema's own node, not a Suspend around it, so
425
+ * protocol code sees the same node shapes as with eager schemas.
426
+ */
427
+ export const at = (build: (j: number) => unknown, j: number): any => {
428
+ const self = Object.create(AtProto);
429
+ self.b = build;
430
+ self.j = j;
431
+ return self;
432
+ };
433
+
434
+ interface At {
435
+ readonly b: (j: number) => unknown;
436
+ readonly j: number;
437
+ s?: any;
438
+ }
439
+ const resolveAt = (self: At): any => (self.s ??= self.b(self.j));
440
+
441
+ const AtProto: object = /*@__PURE__*/ Object.create(Proto, {
442
+ ast: {
443
+ get(this: At) {
444
+ return resolveAt(this).ast;
445
+ },
446
+ },
447
+ fields: {
448
+ get(this: At) {
449
+ return resolveAt(this).fields;
450
+ },
451
+ },
452
+ key: {
453
+ get(this: At) {
454
+ return resolveAt(this).key;
455
+ },
456
+ },
457
+ value: {
458
+ get(this: At) {
459
+ return resolveAt(this).value;
460
+ },
461
+ },
462
+ });
463
+
464
+ //#endregion
465
+
466
+ //#region Effect Schema bridge
467
+
468
+ /**
469
+ * The Effect schema equivalent to a lite node: the reference for strict
470
+ * decoding, and what `effect/Arbitrary` generates values from
471
+ * (`Arbitrary.schema(toEffect(Schema, op.input.ast))`). Loaded on demand so
472
+ * `effect/Schema` is only bundled/imported by callers that use it.
473
+ */
474
+ /** The Effect Schema functions the bridge uses (passed in, so callers import only these). */
475
+ export type EffectSchemaApi = Pick<
476
+ typeof import("effect/Schema"),
477
+ | "Any"
478
+ | "Array"
479
+ | "Boolean"
480
+ | "Literal"
481
+ | "Null"
482
+ | "Number"
483
+ | "Record"
484
+ | "String"
485
+ | "Struct"
486
+ | "StructWithRest"
487
+ | "Tuple"
488
+ | "Undefined"
489
+ | "Union"
490
+ | "Unknown"
491
+ | "Void"
492
+ | "declare"
493
+ | "make"
494
+ | "optional"
495
+ | "suspend"
496
+ > &
497
+ /** Built-in declarations; without one, a lite node of that type becomes a plain check. */
498
+ Partial<Pick<typeof import("effect/Schema"), "Date" | "Duration" | "Redacted" | "Uint8Array">>;
499
+
500
+ export const toEffect = (S: EffectSchemaApi, ast: Node): unknown => {
501
+ const memo = new Map<Node, any>();
502
+ // Whether a lite node sits anywhere under `n` (cycles count as no).
503
+ const liteBelow = new Map<Node, boolean>();
504
+ const hasLite = (n: Node): boolean => {
505
+ const known = liteBelow.get(n);
506
+ if (known !== undefined) return known;
507
+ liteBelow.set(n, false);
508
+ const kids: Node[] = [
509
+ ...(n.types ?? []),
510
+ ...(n.elements ?? []),
511
+ ...(n.rest ?? []),
512
+ ...(n.typeParameters ?? []),
513
+ ...(n.propertySignatures ?? []).map((p) => p.type),
514
+ ...(n.indexSignatures ?? []).flatMap((i) => [i.parameter, i.type]),
515
+ ...(((n as any).encoding ?? []) as Array<{ to: Node }>).map((l) => l.to),
516
+ ...(n._tag === "Suspend" && n.thunk ? [n.thunk()] : []),
517
+ ];
518
+ const r = !!(n as any)[liteKey] || kids.some(hasLite);
519
+ liteBelow.set(n, r);
520
+ return r;
521
+ };
522
+ // Effect's own schema for a declaration whose `representation` names one.
523
+ const builtIn = (n: Node): unknown => {
524
+ switch ((n.annotations?.representation as { id?: unknown } | undefined)?.id) {
525
+ case "effect/schema/Date":
526
+ return S.Date;
527
+ case "effect/schema/Duration":
528
+ return S.Duration;
529
+ case "effect/schema/Uint8Array":
530
+ return S.Uint8Array;
531
+ case "effect/schema/Redacted": {
532
+ const value = n.typeParameters?.[0];
533
+ return value && S.Redacted?.(build(value));
534
+ }
535
+ }
536
+ return undefined;
537
+ };
538
+ const build = (n: Node): any => {
539
+ const hit = memo.get(n);
540
+ if (hit) return hit;
541
+ // A real Effect subtree with nothing lite below keeps its exact schema.
542
+ if (!(n as any)[liteKey] && !hasLite(n)) {
543
+ const real = S.make(n as any);
544
+ memo.set(n, real);
545
+ return real;
546
+ }
547
+ let out: any;
548
+ switch (n._tag) {
549
+ case "String":
550
+ out = S.String;
551
+ break;
552
+ case "Number":
553
+ out = S.Number;
554
+ break;
555
+ case "Boolean":
556
+ out = S.Boolean;
557
+ break;
558
+ case "Null":
559
+ out = S.Null;
560
+ break;
561
+ case "Undefined":
562
+ out = S.Undefined;
563
+ break;
564
+ case "Void":
565
+ out = S.Void;
566
+ break;
567
+ case "Any":
568
+ out = S.Any;
569
+ break;
570
+ case "Literal":
571
+ out = S.Literal(n.literal as any);
572
+ break;
573
+ case "Union":
574
+ out = n.context?.isOptional
575
+ ? S.optional(build(n.types![0]!))
576
+ : S.Union(n.types!.map(build));
577
+ break;
578
+ case "Arrays":
579
+ out = n.rest!.length ? S.Array(build(n.rest![0]!)) : S.Tuple(n.elements!.map(build));
580
+ break;
581
+ case "Objects": {
582
+ const props = n.propertySignatures ?? [];
583
+ const records = (n.indexSignatures ?? []).map((i) =>
584
+ S.Record(
585
+ build(i.parameter),
586
+ i.type._tag === "Union" && i.type.context?.isOptional
587
+ ? S.Union([build(i.type.types![0]!), S.Undefined])
588
+ : build(i.type),
589
+ ),
590
+ );
591
+ if (props.length === 0 && records.length === 1) {
592
+ out = records[0];
593
+ break;
594
+ }
595
+ const struct = S.Struct(Object.fromEntries(props.map((p) => [p.name, build(p.type)])));
596
+ out = records.length ? S.StructWithRest(struct, records as any) : struct;
597
+ break;
598
+ }
599
+ case "Suspend": {
600
+ const thunk = n.thunk!;
601
+ out = S.suspend(() => build(thunk()));
602
+ break;
603
+ }
604
+ case "Declaration":
605
+ out =
606
+ builtIn(n) ?? (n.check ? S.declare(n.check as (u: unknown) => u is unknown) : S.Unknown);
607
+ break;
608
+ case "Unknown":
609
+ out = S.Unknown;
610
+ break;
611
+ default:
612
+ // A lite tag with no case maps to Unknown; a real node keeps its schema.
613
+ out = (n as any)[liteKey] ? S.Unknown : S.make(n as any);
614
+ }
615
+ memo.set(n, out);
616
+ return out;
617
+ };
618
+ return build(ast);
619
+ };
620
+
621
+ //#endregion
@@ -0,0 +1,44 @@
1
+ /**
2
+ * `schema-lite` with Effect Schema's types, for hand-written modules.
3
+ *
4
+ * Generated services import `schema-lite`, whose builders are typed `any`
5
+ * because every generated schema is cast to its declared interface. Hand-
6
+ * written error classes have no such cast: built from untyped builders,
7
+ * their fields declare as `any` (`ValidationError.message: any`), which
8
+ * erases the field types for every consumer. This module exposes the same
9
+ * lite runtime values with the Effect Schema types, so `S.TaggedError` field
10
+ * types infer as they did with Effect Schema.
11
+ */
12
+ import type * as S from "effect/Schema";
13
+ import * as L from "./schema-lite-impl.ts";
14
+
15
+ export type * from "effect/Schema";
16
+
17
+ export const optional = L.optional as unknown as typeof S.optional;
18
+ export const Struct = L.Struct as unknown as typeof S.Struct;
19
+ export const StructWithRest = L.StructWithRest as unknown as typeof S.StructWithRest;
20
+ export const suspend = L.suspend as unknown as typeof S.suspend;
21
+ export const Array = L.Array as unknown as typeof S.Array;
22
+ export const Record = L.Record as unknown as typeof S.Record;
23
+ export const Union = L.Union as unknown as typeof S.Union;
24
+ export const Literal = L.Literal as unknown as typeof S.Literal;
25
+ export const Literals = L.Literals as unknown as typeof S.Literals;
26
+ export const NullOr = L.NullOr as unknown as typeof S.NullOr;
27
+ export const Tuple = L.Tuple as unknown as typeof S.Tuple;
28
+ export const declare = L.declare as unknown as typeof S.declare;
29
+ export const instanceOf = L.instanceOf as unknown as typeof S.instanceOf;
30
+ export const Redacted = L.Redacted as unknown as typeof S.Redacted;
31
+
32
+ export const String = L.String as unknown as typeof S.String;
33
+ export const Number = L.Number as unknown as typeof S.Number;
34
+ export const Int = L.Number as unknown as typeof S.Int;
35
+ export const Boolean = L.Boolean as unknown as typeof S.Boolean;
36
+ export const Date = L.Date as unknown as typeof S.Date;
37
+ export const Duration = L.Duration as unknown as typeof S.Duration;
38
+ export const Unknown = L.Unknown as unknown as typeof S.Unknown;
39
+ export const Null = L.Null as unknown as typeof S.Null;
40
+ export const Void = L.Void as unknown as typeof S.Void;
41
+ export const Any = L.Any as unknown as typeof S.Any;
42
+
43
+ export const TaggedError = L.TaggedError as unknown as L.TaggedErrorFactory;
44
+ export type { TaggedErrorClass, TaggedErrorFactory } from "./schema-lite-impl.ts";