@distilled.cloud/core 1.0.0-rc.14 → 1.0.0-rc.16

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 (312) hide show
  1. package/lib/api.d.ts +118 -13
  2. package/lib/api.d.ts.map +1 -1
  3. package/lib/api.js +755 -147
  4. package/lib/api.js.map +1 -1
  5. package/lib/category.d.ts.map +1 -1
  6. package/lib/category.js +146 -146
  7. package/lib/category.js.map +1 -1
  8. package/lib/chunks.d.ts +3 -0
  9. package/lib/chunks.d.ts.map +1 -0
  10. package/lib/chunks.js +11 -0
  11. package/lib/chunks.js.map +1 -0
  12. package/lib/codegen/boolean-string-enums.js +36 -36
  13. package/lib/codegen/chunk.d.ts +7 -0
  14. package/lib/codegen/chunk.d.ts.map +1 -0
  15. package/lib/codegen/chunk.js +139 -0
  16. package/lib/codegen/chunk.js.map +1 -0
  17. package/lib/codegen/cli.d.ts.map +1 -1
  18. package/lib/codegen/cli.js +28 -14
  19. package/lib/codegen/cli.js.map +1 -1
  20. package/lib/codegen/datafy.d.ts +14 -0
  21. package/lib/codegen/datafy.d.ts.map +1 -0
  22. package/lib/codegen/datafy.js +853 -0
  23. package/lib/codegen/datafy.js.map +1 -0
  24. package/lib/codegen/dedupe.d.ts +5 -0
  25. package/lib/codegen/dedupe.d.ts.map +1 -0
  26. package/lib/codegen/dedupe.js +153 -0
  27. package/lib/codegen/dedupe.js.map +1 -0
  28. package/lib/codegen/emit.js +56 -56
  29. package/lib/codegen/format.js +18 -18
  30. package/lib/codegen/generated-at.js +29 -29
  31. package/lib/codegen/generator.d.ts.map +1 -1
  32. package/lib/codegen/generator.js +92 -84
  33. package/lib/codegen/generator.js.map +1 -1
  34. package/lib/codegen/graph.js +36 -36
  35. package/lib/codegen/graphql-client.d.ts +2 -0
  36. package/lib/codegen/graphql-client.d.ts.map +1 -1
  37. package/lib/codegen/graphql-client.js +55 -26
  38. package/lib/codegen/graphql-client.js.map +1 -1
  39. package/lib/codegen/graphql.js +113 -113
  40. package/lib/codegen/latin1.d.ts +3 -0
  41. package/lib/codegen/latin1.d.ts.map +1 -0
  42. package/lib/codegen/latin1.js +126 -0
  43. package/lib/codegen/latin1.js.map +1 -0
  44. package/lib/codegen/members.d.ts +7 -0
  45. package/lib/codegen/members.d.ts.map +1 -1
  46. package/lib/codegen/members.js +26 -19
  47. package/lib/codegen/members.js.map +1 -1
  48. package/lib/codegen/model-diff.js +6 -6
  49. package/lib/codegen/naming.js +19 -19
  50. package/lib/codegen/openapi-cli.js +15 -15
  51. package/lib/codegen/openapi.d.ts +17 -0
  52. package/lib/codegen/openapi.d.ts.map +1 -1
  53. package/lib/codegen/openapi.js +323 -169
  54. package/lib/codegen/openapi.js.map +1 -1
  55. package/lib/codegen/operations.js +14 -14
  56. package/lib/codegen/pagination.js +11 -11
  57. package/lib/codegen/patch-audit.js +57 -57
  58. package/lib/codegen/patches.js +79 -79
  59. package/lib/codegen/prelude.js +19 -19
  60. package/lib/codegen/privatize.d.ts +21 -0
  61. package/lib/codegen/privatize.d.ts.map +1 -0
  62. package/lib/codegen/privatize.js +176 -0
  63. package/lib/codegen/privatize.js.map +1 -0
  64. package/lib/codegen/proto.js +25 -25
  65. package/lib/codegen/pure.d.ts +6 -0
  66. package/lib/codegen/pure.d.ts.map +1 -0
  67. package/lib/codegen/pure.js +91 -0
  68. package/lib/codegen/pure.js.map +1 -0
  69. package/lib/codegen/rewrite-operation-ids.js +108 -108
  70. package/lib/codegen/spec-path.js +57 -57
  71. package/lib/codegen/table.d.ts +5 -0
  72. package/lib/codegen/table.d.ts.map +1 -0
  73. package/lib/codegen/table.js +249 -0
  74. package/lib/codegen/table.js.map +1 -0
  75. package/lib/config.d.ts +27 -0
  76. package/lib/config.d.ts.map +1 -0
  77. package/lib/config.js +161 -0
  78. package/lib/config.js.map +1 -0
  79. package/lib/crypto.d.ts +76 -0
  80. package/lib/crypto.d.ts.map +1 -0
  81. package/lib/crypto.js +130 -0
  82. package/lib/crypto.js.map +1 -0
  83. package/lib/debug.d.ts +2 -0
  84. package/lib/debug.d.ts.map +1 -0
  85. package/lib/debug.js +8 -0
  86. package/lib/debug.js.map +1 -0
  87. package/lib/error-category.d.ts +1 -1
  88. package/lib/error-category.d.ts.map +1 -1
  89. package/lib/error-category.js +7 -6
  90. package/lib/error-category.js.map +1 -1
  91. package/lib/errors.d.ts +62 -62
  92. package/lib/errors.d.ts.map +1 -1
  93. package/lib/errors.js +117 -114
  94. package/lib/errors.js.map +1 -1
  95. package/lib/graphql.d.ts +31 -20
  96. package/lib/graphql.d.ts.map +1 -1
  97. package/lib/graphql.js +150 -119
  98. package/lib/graphql.js.map +1 -1
  99. package/lib/graphql.types.js +4 -3
  100. package/lib/graphql.types.js.map +1 -1
  101. package/lib/index.d.ts +3 -2
  102. package/lib/index.d.ts.map +1 -1
  103. package/lib/index.js +15 -14
  104. package/lib/index.js.map +1 -1
  105. package/lib/json-patch.d.ts +2 -2
  106. package/lib/json-patch.js +28 -28
  107. package/lib/pagination.d.ts +9 -25
  108. package/lib/pagination.d.ts.map +1 -1
  109. package/lib/pagination.js +95 -90
  110. package/lib/pagination.js.map +1 -1
  111. package/lib/plain-values.d.ts +10 -0
  112. package/lib/plain-values.d.ts.map +1 -0
  113. package/lib/plain-values.js +40 -0
  114. package/lib/plain-values.js.map +1 -0
  115. package/lib/protocol-http.d.ts +50 -14
  116. package/lib/protocol-http.d.ts.map +1 -1
  117. package/lib/protocol-http.js +351 -178
  118. package/lib/protocol-http.js.map +1 -1
  119. package/lib/protocol-rest.d.ts +38 -25
  120. package/lib/protocol-rest.d.ts.map +1 -1
  121. package/lib/protocol-rest.js +238 -130
  122. package/lib/protocol-rest.js.map +1 -1
  123. package/lib/query.js +16 -16
  124. package/lib/response-validation-strict.d.ts +48 -0
  125. package/lib/response-validation-strict.d.ts.map +1 -0
  126. package/lib/response-validation-strict.js +284 -0
  127. package/lib/response-validation-strict.js.map +1 -0
  128. package/lib/response-validation.d.ts +43 -25
  129. package/lib/response-validation.d.ts.map +1 -1
  130. package/lib/response-validation.js +112 -70
  131. package/lib/response-validation.js.map +1 -1
  132. package/lib/retry-after.d.ts +3 -3
  133. package/lib/retry-after.js +49 -49
  134. package/lib/retry.d.ts +48 -29
  135. package/lib/retry.d.ts.map +1 -1
  136. package/lib/retry.js +142 -154
  137. package/lib/retry.js.map +1 -1
  138. package/lib/schema-lite-impl.d.ts +158 -0
  139. package/lib/schema-lite-impl.d.ts.map +1 -0
  140. package/lib/schema-lite-impl.js +341 -0
  141. package/lib/schema-lite-impl.js.map +1 -0
  142. package/lib/schema-lite-typed.d.ts +41 -0
  143. package/lib/schema-lite-typed.d.ts.map +1 -0
  144. package/lib/schema-lite-typed.js +27 -0
  145. package/lib/schema-lite-typed.js.map +1 -0
  146. package/lib/schema-lite.d.ts +40 -0
  147. package/lib/schema-lite.d.ts.map +1 -0
  148. package/lib/schema-lite.js +31 -0
  149. package/lib/schema-lite.js.map +1 -0
  150. package/lib/schema.d.ts +20 -6
  151. package/lib/schema.d.ts.map +1 -1
  152. package/lib/schema.js +202 -30
  153. package/lib/schema.js.map +1 -1
  154. package/lib/testing.d.ts +3 -2
  155. package/lib/testing.d.ts.map +1 -1
  156. package/lib/testing.js +22 -21
  157. package/lib/testing.js.map +1 -1
  158. package/lib/trait.d.ts +13 -13
  159. package/lib/trait.d.ts.map +1 -1
  160. package/lib/trait.js +80 -80
  161. package/lib/trait.js.map +1 -1
  162. package/lib/xml.d.ts +1 -1
  163. package/lib/xml.d.ts.map +1 -1
  164. package/lib/xml.js +38 -26
  165. package/lib/xml.js.map +1 -1
  166. package/package.json +9 -7
  167. package/src/api.ts +1079 -180
  168. package/src/category.ts +33 -33
  169. package/src/chunks.ts +30 -0
  170. package/src/codegen/chunk.ts +147 -0
  171. package/src/codegen/cli.ts +27 -1
  172. package/src/codegen/datafy.ts +874 -0
  173. package/src/codegen/dedupe.ts +173 -0
  174. package/src/codegen/generator.ts +10 -3
  175. package/src/codegen/graphql-client.ts +49 -15
  176. package/src/codegen/latin1.ts +131 -0
  177. package/src/codegen/members.ts +8 -0
  178. package/src/codegen/openapi.ts +194 -3
  179. package/src/codegen/privatize.ts +200 -0
  180. package/src/codegen/pure.ts +94 -0
  181. package/src/codegen/table.ts +254 -0
  182. package/src/config.ts +216 -0
  183. package/src/crypto.ts +202 -0
  184. package/src/debug.ts +8 -0
  185. package/src/error-category.ts +2 -1
  186. package/src/errors.ts +65 -56
  187. package/src/graphql.ts +109 -62
  188. package/src/graphql.types.ts +9 -7
  189. package/src/index.ts +3 -2
  190. package/src/json-patch.ts +2 -2
  191. package/src/pagination.ts +37 -18
  192. package/src/plain-values.ts +41 -0
  193. package/src/protocol-http.ts +378 -145
  194. package/src/protocol-rest.ts +337 -169
  195. package/src/response-validation-strict.ts +327 -0
  196. package/src/response-validation.ts +101 -50
  197. package/src/retry-after.ts +3 -3
  198. package/src/retry.ts +94 -93
  199. package/src/schema-lite-impl.ts +550 -0
  200. package/src/schema-lite-typed.ts +44 -0
  201. package/src/schema-lite.ts +44 -0
  202. package/src/schema.ts +258 -14
  203. package/src/testing.ts +4 -2
  204. package/src/trait.ts +36 -28
  205. package/src/xml.ts +31 -17
  206. package/lib/codegen/boolean-string-enums.test.d.ts +0 -2
  207. package/lib/codegen/boolean-string-enums.test.d.ts.map +0 -1
  208. package/lib/codegen/boolean-string-enums.test.js +0 -275
  209. package/lib/codegen/boolean-string-enums.test.js.map +0 -1
  210. package/lib/codegen/defer-naming.test.d.ts +0 -2
  211. package/lib/codegen/defer-naming.test.d.ts.map +0 -1
  212. package/lib/codegen/defer-naming.test.js +0 -105
  213. package/lib/codegen/defer-naming.test.js.map +0 -1
  214. package/lib/codegen/generated-at.test.d.ts +0 -2
  215. package/lib/codegen/generated-at.test.d.ts.map +0 -1
  216. package/lib/codegen/generated-at.test.js +0 -81
  217. package/lib/codegen/generated-at.test.js.map +0 -1
  218. package/lib/codegen/generator.test.d.ts +0 -2
  219. package/lib/codegen/generator.test.d.ts.map +0 -1
  220. package/lib/codegen/generator.test.js +0 -121
  221. package/lib/codegen/generator.test.js.map +0 -1
  222. package/lib/codegen/graphql-client.test.d.ts +0 -2
  223. package/lib/codegen/graphql-client.test.d.ts.map +0 -1
  224. package/lib/codegen/graphql-client.test.js +0 -171
  225. package/lib/codegen/graphql-client.test.js.map +0 -1
  226. package/lib/codegen/openapi-binary.test.d.ts +0 -2
  227. package/lib/codegen/openapi-binary.test.d.ts.map +0 -1
  228. package/lib/codegen/openapi-binary.test.js +0 -67
  229. package/lib/codegen/openapi-binary.test.js.map +0 -1
  230. package/lib/codegen/patch-audit.test.d.ts +0 -2
  231. package/lib/codegen/patch-audit.test.d.ts.map +0 -1
  232. package/lib/codegen/patch-audit.test.js +0 -141
  233. package/lib/codegen/patch-audit.test.js.map +0 -1
  234. package/lib/codegen/patches.test.d.ts +0 -2
  235. package/lib/codegen/patches.test.d.ts.map +0 -1
  236. package/lib/codegen/patches.test.js +0 -155
  237. package/lib/codegen/patches.test.js.map +0 -1
  238. package/lib/codegen/protocol-imports.test.d.ts +0 -2
  239. package/lib/codegen/protocol-imports.test.d.ts.map +0 -1
  240. package/lib/codegen/protocol-imports.test.js +0 -36
  241. package/lib/codegen/protocol-imports.test.js.map +0 -1
  242. package/lib/codegen/rewrite-operation-ids.test.d.ts +0 -2
  243. package/lib/codegen/rewrite-operation-ids.test.d.ts.map +0 -1
  244. package/lib/codegen/rewrite-operation-ids.test.js +0 -451
  245. package/lib/codegen/rewrite-operation-ids.test.js.map +0 -1
  246. package/lib/codegen/untagged-unions.test.d.ts +0 -2
  247. package/lib/codegen/untagged-unions.test.d.ts.map +0 -1
  248. package/lib/codegen/untagged-unions.test.js +0 -80
  249. package/lib/codegen/untagged-unions.test.js.map +0 -1
  250. package/lib/fixtures/xml/upstream-validator.json +0 -397
  251. package/lib/graphql.test.d.ts +0 -2
  252. package/lib/graphql.test.d.ts.map +0 -1
  253. package/lib/graphql.test.js +0 -151
  254. package/lib/graphql.test.js.map +0 -1
  255. package/lib/json-patch.test.d.ts +0 -2
  256. package/lib/json-patch.test.d.ts.map +0 -1
  257. package/lib/json-patch.test.js +0 -65
  258. package/lib/json-patch.test.js.map +0 -1
  259. package/lib/pagination.test.d.ts +0 -2
  260. package/lib/pagination.test.d.ts.map +0 -1
  261. package/lib/pagination.test.js +0 -131
  262. package/lib/pagination.test.js.map +0 -1
  263. package/lib/protocol-http.test.d.ts +0 -2
  264. package/lib/protocol-http.test.d.ts.map +0 -1
  265. package/lib/protocol-http.test.js +0 -228
  266. package/lib/protocol-http.test.js.map +0 -1
  267. package/lib/protocol-rest.test.d.ts +0 -2
  268. package/lib/protocol-rest.test.d.ts.map +0 -1
  269. package/lib/protocol-rest.test.js +0 -498
  270. package/lib/protocol-rest.test.js.map +0 -1
  271. package/lib/response-validation.test.d.ts +0 -2
  272. package/lib/response-validation.test.d.ts.map +0 -1
  273. package/lib/response-validation.test.js +0 -72
  274. package/lib/response-validation.test.js.map +0 -1
  275. package/lib/retry-after.test.d.ts +0 -2
  276. package/lib/retry-after.test.d.ts.map +0 -1
  277. package/lib/retry-after.test.js +0 -204
  278. package/lib/retry-after.test.js.map +0 -1
  279. package/lib/retry.test.d.ts +0 -2
  280. package/lib/retry.test.d.ts.map +0 -1
  281. package/lib/retry.test.js +0 -407
  282. package/lib/retry.test.js.map +0 -1
  283. package/lib/sdks.test.d.ts +0 -2
  284. package/lib/sdks.test.d.ts.map +0 -1
  285. package/lib/sdks.test.js +0 -76
  286. package/lib/sdks.test.js.map +0 -1
  287. package/lib/xml.test.d.ts +0 -2
  288. package/lib/xml.test.d.ts.map +0 -1
  289. package/lib/xml.test.js +0 -210
  290. package/lib/xml.test.js.map +0 -1
  291. package/src/codegen/boolean-string-enums.test.ts +0 -299
  292. package/src/codegen/defer-naming.test.ts +0 -113
  293. package/src/codegen/generated-at.test.ts +0 -89
  294. package/src/codegen/generator.test.ts +0 -137
  295. package/src/codegen/graphql-client.test.ts +0 -189
  296. package/src/codegen/openapi-binary.test.ts +0 -91
  297. package/src/codegen/patch-audit.test.ts +0 -160
  298. package/src/codegen/patches.test.ts +0 -188
  299. package/src/codegen/protocol-imports.test.ts +0 -39
  300. package/src/codegen/rewrite-operation-ids.test.ts +0 -472
  301. package/src/codegen/untagged-unions.test.ts +0 -85
  302. package/src/fixtures/xml/upstream-validator.json +0 -397
  303. package/src/graphql.test.ts +0 -198
  304. package/src/json-patch.test.ts +0 -77
  305. package/src/pagination.test.ts +0 -169
  306. package/src/protocol-http.test.ts +0 -276
  307. package/src/protocol-rest.test.ts +0 -703
  308. package/src/response-validation.test.ts +0 -89
  309. package/src/retry-after.test.ts +0 -267
  310. package/src/retry.test.ts +0 -566
  311. package/src/sdks.test.ts +0 -87
  312. package/src/xml.test.ts +0 -251
@@ -0,0 +1,173 @@
1
+ /**
2
+ * Merge structurally identical schema consts in a generated module
3
+ * (dev-time only).
4
+ *
5
+ * Specs repeat shapes under different names: Stripe has hundreds of
6
+ * `{ object: string; url: string }`-style structs, AWS has dozens of empty
7
+ * `*Response` shapes and identical lists. Each copy is its own schema at
8
+ * runtime. This pass keeps the first schema of every structural class and
9
+ * turns the others into aliases (`export const B = A as any as S.Schema<B>;`),
10
+ * so the exports and types are unchanged and only one schema is built.
11
+ *
12
+ * Two consts are equal when their initializers are the same text after
13
+ * (1) dropping their own name from `identifier: "<name>"` annotations and
14
+ * casts, and (2) replacing every reference to another schema const, and every
15
+ * `identifier: "<other>"` string, with that const's current class. This is
16
+ * repeated until the classes stop changing (bottom-up merging; members of a
17
+ * reference cycle merge only when they were already textually identical).
18
+ *
19
+ * Schemas passed directly as an operation's `input:` keep their own object:
20
+ * AWS query protocols derive the action name from the input identifier.
21
+ * Modules on AWS XML / query protocols are left alone entirely: those
22
+ * protocols use a shape's identifier as its XML element name, so a merged
23
+ * shape would change the wire format.
24
+ */
25
+ const IDENTIFIER_ON_WIRE =
26
+ /^const proto = (?:\/\*[@#]__PURE__\*\/ )?T\.AwsProtocols(?:RestXml|AwsQuery|Ec2Query)\(/m;
27
+ import { parseAst } from "rolldown/parseAst";
28
+
29
+ type Node = { readonly type: string; readonly start: number; readonly end: number } & Record<
30
+ string,
31
+ unknown
32
+ >;
33
+
34
+ const isNode = (value: unknown): value is Node =>
35
+ value !== null && typeof value === "object" && typeof (value as Node).type === "string";
36
+
37
+ interface Candidate {
38
+ readonly name: string;
39
+ /** Initializer span. */
40
+ readonly start: number;
41
+ readonly end: number;
42
+ /** Positions [start, end, name] of identifier references to other consts, in the initializer. */
43
+ readonly refs: Array<readonly [number, number, string]>;
44
+ /** Positions of `"<name>"` string literals under an `identifier` key. */
45
+ readonly idStrings: Array<readonly [number, number, string]>;
46
+ }
47
+
48
+ const SCHEMA_CALL = /\bS\.(suspend|Struct|Array|Record|Union|Tuple|Literals|NullOr|optional)\(/;
49
+
50
+ export const dedupeSchemas = (
51
+ code: string,
52
+ filename = "generated.ts",
53
+ ): { code: string; merged: number } => {
54
+ if (IDENTIFIER_ON_WIRE.test(code)) return { code, merged: 0 };
55
+ const program = parseAst(code, { sourceType: "module", lang: "ts" }, filename);
56
+ const decls: Array<{ name: string; init: Node }> = [];
57
+ // `export const Status = S.String;`: enum aliases and other plain leaf
58
+ // schemas. References to them compare by what they alias, so two structs
59
+ // that differ only in which string enum a field names still merge.
60
+ const leafAliases = new Map<string, string>();
61
+ const operationSchemas = new Set<string>();
62
+
63
+ for (let statement of program.body as unknown as Node[]) {
64
+ if (statement.type !== "ExportNamedDeclaration" || !isNode(statement.declaration)) continue;
65
+ statement = statement.declaration;
66
+ if (statement.type !== "VariableDeclaration") continue;
67
+ for (const d of statement.declarations as Node[]) {
68
+ const id = d.id as Node;
69
+ if (id.type !== "Identifier" || !isNode(d.init)) continue;
70
+ const text = code.slice(d.init.start, d.init.end);
71
+ if (/\bAPI\.make(Paginated)?\(/.test(text)) {
72
+ for (const m of text.matchAll(/\binput: (\w+)\b/g)) operationSchemas.add(m[1]!);
73
+ continue;
74
+ }
75
+ if (SCHEMA_CALL.test(text)) decls.push({ name: id.name as string, init: d.init });
76
+ else if (/^S\.[A-Z]\w*$/.test(text)) leafAliases.set(id.name as string, text);
77
+ }
78
+ }
79
+
80
+ const names = new Set([...decls.map((d) => d.name), ...leafAliases.keys()]);
81
+ const candidates: Candidate[] = decls
82
+ .filter((d) => !operationSchemas.has(d.name))
83
+ .map(({ name, init }) => {
84
+ const refs: Array<readonly [number, number, string]> = [];
85
+ const idStrings: Array<readonly [number, number, string]> = [];
86
+ const visit = (node: unknown, parentKey?: string, parent?: Node): void => {
87
+ if (Array.isArray(node)) {
88
+ for (const child of node) visit(child, parentKey, parent);
89
+ return;
90
+ }
91
+ if (!isNode(node)) return;
92
+ if (node.type === "Identifier" && names.has(node.name as string)) {
93
+ // Skip object keys and member property names: they are not references.
94
+ const isKey =
95
+ (parent?.type === "Property" && parentKey === "key" && !parent.computed) ||
96
+ (parent?.type === "MemberExpression" && parentKey === "property" && !parent.computed);
97
+ if (!isKey) refs.push([node.start, node.end, node.name as string]);
98
+ }
99
+ if (
100
+ node.type === "Property" &&
101
+ isNode(node.key) &&
102
+ ((node.key.type === "Identifier" && node.key.name === "identifier") ||
103
+ (node.key.type === "Literal" && node.key.value === "identifier")) &&
104
+ isNode(node.value) &&
105
+ node.value.type === "Literal" &&
106
+ typeof node.value.value === "string"
107
+ ) {
108
+ idStrings.push([node.value.start, node.value.end, node.value.value]);
109
+ }
110
+ for (const [key, value] of Object.entries(node)) {
111
+ if (key === "type" || key === "start" || key === "end") continue;
112
+ if (value !== null && typeof value === "object") visit(value, key, node);
113
+ }
114
+ };
115
+ visit(init);
116
+ return { name, start: init.start, end: init.end, refs, idStrings };
117
+ });
118
+
119
+ // Class of every schema const; operation schemas are their own class.
120
+ const cls = new Map<string, string>([...names].map((n) => [n, leafAliases.get(n) ?? n]));
121
+ const keyOf = (c: Candidate): string => {
122
+ const edits = [
123
+ ...c.refs.map(([s, e, n]) => [s, e, n === c.name ? "@self" : `@${cls.get(n) ?? n}`] as const),
124
+ ...c.idStrings.map(
125
+ ([s, e, n]) => [s, e, n === c.name ? `"@self"` : `"@${cls.get(n) ?? n}"`] as const,
126
+ ),
127
+ ].sort((a, b) => a[0] - b[0]);
128
+ let out = "";
129
+ let pos = c.start;
130
+ for (const [s, e, text] of edits) {
131
+ if (s < pos) continue;
132
+ out += code.slice(pos, s) + text;
133
+ pos = e;
134
+ }
135
+ return out + code.slice(pos, c.end);
136
+ };
137
+
138
+ for (let round = 0; round < 50; round++) {
139
+ const first = new Map<string, string>();
140
+ let changed = false;
141
+ for (const c of candidates) {
142
+ const key = keyOf(c);
143
+ const canon = first.get(key);
144
+ if (canon === undefined) first.set(key, c.name);
145
+ const next = canon ?? c.name;
146
+ if (cls.get(c.name) !== next) {
147
+ cls.set(c.name, next);
148
+ changed = true;
149
+ }
150
+ }
151
+ if (!changed) break;
152
+ }
153
+
154
+ // Rewrite duplicates as aliases (back to front so offsets stay valid).
155
+ const aliases = candidates
156
+ .filter((c) => cls.get(c.name) !== c.name)
157
+ .sort((a, b) => b.start - a.start);
158
+ let out = code;
159
+ for (const c of aliases) {
160
+ // Follow the class to a const that is still a real schema.
161
+ let target = cls.get(c.name)!;
162
+ while (cls.get(target) !== target) target = cls.get(target)!;
163
+ out = `${out.slice(0, c.start)}${target} as any as S.Schema<${c.name}>${out.slice(c.end)}`;
164
+ }
165
+ // `X as any as S.Schema<X>` with a leftover trailing cast: `A as any as S.Schema<B> as any as S.Schema<B>`.
166
+ out = out.replace(
167
+ / as any as S\.Schema<(\w+)> as any as S\.Schema<\1>/g,
168
+ " as any as S.Schema<$1>",
169
+ );
170
+ // An alias is not a call; a PURE marker before it would only draw bundler warnings.
171
+ out = out.replace(/\/\*[@#]__PURE__\*\/\s*(\w+ as any as S\.Schema<)/g, "$1");
172
+ return { code: out, merged: aliases.length };
173
+ };
@@ -10,7 +10,7 @@ import {
10
10
  suspendConst,
11
11
  } from "./emit.ts";
12
12
  import { orderIndex, reachableFrom, shapeDeps, topoOrder, type ShapeMap } from "./graph.ts";
13
- import { memberBases, smithyWireName } from "./members.ts";
13
+ import { ADDITIONAL_PROPERTIES_TRAIT, memberBases, smithyWireName } from "./members.ts";
14
14
  /**
15
15
  * The generic smithy→SDK service generator (dev-time only).
16
16
  *
@@ -878,7 +878,13 @@ export const generateService = (model: any, spec: SdkSpec): GeneratedService =>
878
878
  fields.push(...inject.interfaceLines);
879
879
  members.push(inject.structLine);
880
880
  }
881
- const struct = members.length ? `S.Struct({\n${members.join("\n")}\n})` : `S.Struct({})`;
881
+ const closed = members.length ? `S.Struct({\n${members.join("\n")}\n})` : `S.Struct({})`;
882
+ // Open object: keys beyond the declared members are typed `unknown`.
883
+ // A typed value (OpenAPI `additionalProperties: {schema}`) would have
884
+ // to accept every declared member's type too, so it stays `unknown`.
885
+ const open = ADDITIONAL_PROPERTIES_TRAIT in (d.traits ?? {});
886
+ if (open) fields.push(` [key: string]: unknown;`);
887
+ const struct = open ? `S.StructWithRest(${closed}, [S.Record(S.String, S.Unknown)])` : closed;
882
888
  const structCtx = {
883
889
  id,
884
890
  isOpIo: opIoShapes.has(id),
@@ -1180,7 +1186,8 @@ export const generateService = (model: any, spec: SdkSpec): GeneratedService =>
1180
1186
  `// AUTO-GENERATED by scripts/generate.ts${
1181
1187
  spec.sourceNote ? ` from ${spec.sourceNote}` : ""
1182
1188
  }. Do not edit.\n` +
1183
- `import * as S from "@distilled.cloud/core/schema";\n` +
1189
+ // Lite schemas: Effect Schema is bundled only with ResponseValidation.strict.
1190
+ `import * as S from "@distilled.cloud/core/schema-lite";\n` +
1184
1191
  `import * as API from "@distilled.cloud/core/api";\n` +
1185
1192
  (usesCategories ? `import * as C from "@distilled.cloud/core/category";\n` : "") +
1186
1193
  `import * as T from "../traits.ts";\n` +
@@ -12,6 +12,8 @@ export interface GraphQLField {
12
12
  readonly errors: readonly string[];
13
13
  readonly description?: string;
14
14
  readonly deprecated?: string | true;
15
+ /** The value is a secret: typed `Redacted<string>` and wrapped at runtime. Set by patch. */
16
+ readonly sensitive?: true;
15
17
  }
16
18
  export interface GraphQLType {
17
19
  readonly kind: string;
@@ -149,6 +151,10 @@ export const validateGraphQLModel = (model: GraphQLModel): void => {
149
151
  const checkError = (error: string, coordinate: string) => {
150
152
  if (!model.errors[error]) throw new Error(`${coordinate}: unknown GraphQL error ${error}`);
151
153
  };
154
+ const checkInput = (input: GraphQLArgument, coordinate: string) => {
155
+ checkType(input.type, coordinate);
156
+ if ("sensitive" in input) throw new Error(`${coordinate}: only output fields can be sensitive`);
157
+ };
152
158
  for (const name of [model.queryType, model.mutationType, model.subscriptionType]) {
153
159
  if (name && !model.types[name]) throw new Error(`Unknown GraphQL root ${name}`);
154
160
  }
@@ -158,11 +164,13 @@ export const validateGraphQLModel = (model: GraphQLModel): void => {
158
164
  const coordinate = `${name}.${fieldName}`;
159
165
  checkType(field.type, coordinate);
160
166
  for (const [argName, arg] of Object.entries(field.args))
161
- checkType(arg.type, `${coordinate}(${argName})`);
167
+ checkInput(arg, `${coordinate}(${argName})`);
162
168
  for (const error of field.errors) checkError(error, coordinate);
169
+ if (field.sensitive && model.types[peelRef(field.type).name]?.scalar !== "string")
170
+ throw new Error(`${coordinate}: only string scalar fields can be sensitive`);
163
171
  }
164
172
  for (const [fieldName, field] of Object.entries(type.inputFields ?? {}))
165
- checkType(field.type, `${name}.${fieldName}`);
173
+ checkInput(field, `${name}.${fieldName}`);
166
174
  for (const member of [...(type.possibleTypes ?? []), ...(type.interfaces ?? [])])
167
175
  checkType(member, name);
168
176
  }
@@ -253,12 +261,14 @@ export const generateGraphQLClient = (
253
261
  return nonNull ? ts : `${ts} | null`;
254
262
  };
255
263
  /** Response type: nullable GraphQL types (no `!`) include `null`. */
256
- const tsOutput = (ref: string): string => {
264
+ const tsOutput = (ref: string, sensitive = false): string => {
257
265
  const nonNull = ref.endsWith("!");
258
266
  const inner = nonNull ? ref.slice(0, -1) : ref;
259
267
  let ts: string;
260
268
  if (inner.startsWith("[") && inner.endsWith("]")) {
261
- ts = `ReadonlyArray<${tsOutput(inner.slice(1, -1))}>`;
269
+ ts = `ReadonlyArray<${tsOutput(inner.slice(1, -1), sensitive)}>`;
270
+ } else if (sensitive) {
271
+ ts = "Redacted.Redacted<string>";
262
272
  } else {
263
273
  const node = connectionNode(inner);
264
274
  ts = node ? `ReadonlyArray<${tsNamed(node)}>` : tsNamed(inner);
@@ -290,7 +300,9 @@ export const generateGraphQLClient = (
290
300
  if (isObjectLike(name) || model.types[name]?.kind === "UNION") {
291
301
  return `objectField(${JSON.stringify(fieldName)}, ${name})`;
292
302
  }
293
- return `scalarField(${JSON.stringify(fieldName)})`;
303
+ return field.sensitive
304
+ ? `scalarField(${JSON.stringify(fieldName)}, { sensitive: true })`
305
+ : `scalarField(${JSON.stringify(fieldName)})`;
294
306
  };
295
307
  // Errors: one tagged class per declared error, a runtime spec per class,
296
308
  // and per-root error unions (field errors ∪ global errors).
@@ -301,6 +313,10 @@ export const generateGraphQLClient = (
301
313
  sdkName,
302
314
  `${sdkName}Error`,
303
315
  globalErrorType,
316
+ "S",
317
+ "Category",
318
+ "Redacted",
319
+ "errorFields",
304
320
  ]);
305
321
  for (const tag of errorTags) {
306
322
  if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(tag))
@@ -323,7 +339,7 @@ export const generateGraphQLClient = (
323
339
  const errors = rootErrors(field);
324
340
  const { name, list } = peelRef(field.type);
325
341
  const node = !list ? connectionNode(name) : undefined;
326
- const resultTs = tsOutput(field.type);
342
+ const resultTs = tsOutput(field.type, field.sensitive);
327
343
  const argEntries = Object.entries(field.args);
328
344
  const argTypes =
329
345
  argEntries.length > 0
@@ -343,7 +359,7 @@ export const generateGraphQLClient = (
343
359
  : `args: { ${argFields} }`;
344
360
  const argsExpr = argEntries.length === 0 ? ", undefined" : ", args";
345
361
  const argTypesExpr = argTypes ? `, ${argTypes}` : ", undefined";
346
- const rest = `${argsExpr}${argTypesExpr}, ${errors.specs}`;
362
+ const rest = `${argsExpr}${argTypesExpr}, ${errors.specs}${field.sensitive ? ", { sensitive: true }" : ""}`;
347
363
  const result = `Query<${resultTs}, ${errors.type}>`;
348
364
  const call = (helper: string, target: string) =>
349
365
  `${helper}(${JSON.stringify(kind)}, ${JSON.stringify(fieldName)}, ${target}${rest})`;
@@ -370,10 +386,24 @@ export const generateGraphQLClient = (
370
386
  enums.sort();
371
387
  inputs.sort();
372
388
  unions.sort();
389
+ // Field tables are module-private, so a taken name gets a suffix rather than an error.
390
+ const taken = new Set([...reserved, ...errorTags.flatMap((tag) => [tag, `${tag}Spec`])]);
391
+ const tables = new Map<string, string>();
392
+ for (const name of objects) {
393
+ let table = `${name}Fields`;
394
+ while (taken.has(table)) table += "_";
395
+ taken.add(table);
396
+ tables.set(name, table);
397
+ }
373
398
 
374
399
  const lines: string[] = [
375
400
  "// Generated by @distilled.cloud/core/codegen/graphql-client. DO NOT EDIT.",
376
401
  `import * as S from "effect/Schema";`,
402
+ ...(Object.values(model.types).some((type) =>
403
+ Object.values(type.fields ?? {}).some((field) => field.sensitive),
404
+ )
405
+ ? [`import type * as Redacted from "effect/Redacted";`]
406
+ : []),
377
407
  `import * as Category from "@distilled.cloud/core/category";`,
378
408
  `import {`,
379
409
  ` connectionField,`,
@@ -463,26 +493,30 @@ export const generateGraphQLClient = (
463
493
  for (const name of objects) {
464
494
  const type = model.types[name]!;
465
495
  const fields = Object.entries(type.fields ?? {}).map(
466
- ([fieldName, field]) => ` readonly ${fieldName}: ${tsOutput(field.type)};`,
496
+ ([fieldName, field]) => ` readonly ${fieldName}: ${tsOutput(field.type, field.sensitive)};`,
467
497
  );
468
498
  lines.push(documentation(type.description) + `export interface ${name} {`, ...fields, "}", "");
469
499
  }
470
500
 
471
- for (const name of [...objects, ...unions]) {
472
- lines.push(`export const ${name}: TypeMeta = { name: ${JSON.stringify(name)}, fields: {} };`);
501
+ for (const name of objects) {
502
+ lines.push(
503
+ `export const ${name}: TypeMeta = { name: ${JSON.stringify(name)}, get fields() { return ${tables.get(name)}; } };`,
504
+ );
505
+ }
506
+ for (const name of unions) {
507
+ lines.push(
508
+ `export const ${name}: TypeMeta = { name: ${JSON.stringify(name)}, fields: { __typename: scalarField("__typename") } };`,
509
+ );
473
510
  }
474
511
  lines.push("");
475
512
 
476
513
  for (const name of objects) {
477
514
  const type = model.types[name]!;
478
- lines.push(`Object.assign(${name}.fields, {`);
515
+ lines.push(`const ${tables.get(name)}: TypeMeta["fields"] = {`);
479
516
  for (const [fieldName, field] of Object.entries(type.fields ?? {})) {
480
517
  lines.push(` ${fieldName}: ${fieldMeta(fieldName, field)},`);
481
518
  }
482
- lines.push("});", "");
483
- }
484
- for (const name of unions) {
485
- lines.push(`Object.assign(${name}.fields, { __typename: scalarField("__typename") });`, "");
519
+ lines.push("};", "");
486
520
  }
487
521
 
488
522
  lines.push(`export const ${sdkName} = {`);
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Keep module source Latin-1 only (dev-time only).
3
+ *
4
+ * V8 keeps every module's source text alive (for lazy compilation and
5
+ * `Function.prototype.toString`), and stores a string as one byte per
6
+ * character only while every character is Latin-1. A single `—` in a doc
7
+ * comment doubles the whole file: importing AWS S3's compiled service module
8
+ * retained 2,240 KB of heap, and 1,570 KB with its 3 non-Latin-1 comment
9
+ * characters replaced.
10
+ *
11
+ * {@link toLatin1} leaves values intact: inside string and template literals
12
+ * a character becomes a `\u` escape (the same value). Elsewhere — comments,
13
+ * in practice — typographic punctuation becomes its ASCII form and anything
14
+ * else becomes `U+XXXX`.
15
+ */
16
+ import { parseAst } from "rolldown/parseAst";
17
+
18
+ const NON_LATIN1 = /[^\x00-\xff]/;
19
+ const NON_LATIN1_ALL = /[^\x00-\xff]/g;
20
+
21
+ /** ASCII forms for the punctuation that shows up in API descriptions. */
22
+ const ASCII: Record<string, string> = {
23
+ "\u2010": "-",
24
+ "\u2011": "-",
25
+ "\u2012": "-",
26
+ "\u2013": "-",
27
+ "\u2014": "--",
28
+ "\u2015": "--",
29
+ "\u2018": "'",
30
+ "\u2019": "'",
31
+ "\u201a": "'",
32
+ "\u201b": "'",
33
+ "\u201c": '"',
34
+ "\u201d": '"',
35
+ "\u201e": '"',
36
+ "\u2022": "*",
37
+ "\u2026": "...",
38
+ "\u2032": "'",
39
+ "\u2033": '"',
40
+ "\u2122": "(TM)",
41
+ "\u2190": "<-",
42
+ "\u2192": "->",
43
+ "\u2194": "<->",
44
+ "\u21d2": "=>",
45
+ "\u2212": "-",
46
+ "\u2248": "~",
47
+ "\u2260": "!=",
48
+ "\u2264": "<=",
49
+ "\u2265": ">=",
50
+ "\u00a0": " ",
51
+ "\u200b": "",
52
+ "\u200d": "",
53
+ "\ufe0f": "",
54
+ "\ufeff": "",
55
+ };
56
+
57
+ const escapeChar = (c: string): string => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`;
58
+
59
+ const describeChar = (c: string): string => {
60
+ const ascii = ASCII[c];
61
+ if (ascii !== undefined) return ascii;
62
+ const cp = c.codePointAt(0)!;
63
+ return `U+${cp.toString(16).toUpperCase().padStart(4, "0")}`;
64
+ };
65
+
66
+ type Node = { readonly type: string; readonly start: number; readonly end: number } & Record<
67
+ string,
68
+ unknown
69
+ >;
70
+
71
+ const isNode = (value: unknown): value is Node =>
72
+ value !== null && typeof value === "object" && typeof (value as Node).type === "string";
73
+
74
+ /** [start, end) ranges of string, template and regex literals. */
75
+ const literalRanges = (code: string, filename: string): Array<[number, number]> => {
76
+ const program = parseAst(code, { sourceType: "module", lang: "ts" }, filename);
77
+ const ranges: Array<[number, number]> = [];
78
+ const visit = (node: unknown): void => {
79
+ if (Array.isArray(node)) {
80
+ for (const child of node) visit(child);
81
+ return;
82
+ }
83
+ if (!isNode(node)) return;
84
+ if (node.type === "Literal" || node.type === "TemplateElement" || node.type === "Directive") {
85
+ ranges.push([node.start, node.end]);
86
+ if (node.type !== "Directive") return;
87
+ }
88
+ for (const [key, value] of Object.entries(node)) {
89
+ if (key === "type" || key === "start" || key === "end") continue;
90
+ if (value !== null && typeof value === "object") visit(value);
91
+ }
92
+ };
93
+ visit(program);
94
+ return ranges.sort((a, b) => a[0] - b[0]);
95
+ };
96
+
97
+ /** `code` with every non-Latin-1 character escaped (literals) or spelled in ASCII (elsewhere). */
98
+ export const toLatin1 = (code: string, filename = "module.ts"): string => {
99
+ if (!NON_LATIN1.test(code)) return code;
100
+ const ranges = literalRanges(code, filename);
101
+ let out = "";
102
+ let pos = 0;
103
+ let r = 0;
104
+ for (const match of code.matchAll(NON_LATIN1_ALL)) {
105
+ const i = match.index;
106
+ while (r < ranges.length && ranges[r]![1] <= i) r++;
107
+ const inLiteral = r < ranges.length && ranges[r]![0] <= i;
108
+ let c = match[0];
109
+ // A lone low surrogate was handled with its high surrogate below.
110
+ if (
111
+ c >= "\udc00" &&
112
+ c <= "\udfff" &&
113
+ i > 0 &&
114
+ code.charCodeAt(i - 1) >= 0xd800 &&
115
+ code.charCodeAt(i - 1) <= 0xdbff
116
+ ) {
117
+ pos = i + 1;
118
+ continue;
119
+ }
120
+ if (c >= "\ud800" && c <= "\udbff" && i + 1 < code.length) c = code.slice(i, i + 2);
121
+ out += code.slice(pos, i);
122
+ out += inLiteral
123
+ ? [...c]
124
+ .flatMap((ch) => (ch.length === 2 ? [ch[0]!, ch[1]!] : [ch]))
125
+ .map(escapeChar)
126
+ .join("")
127
+ : describeChar(c);
128
+ pos = i + c.length;
129
+ }
130
+ return out + code.slice(pos);
131
+ };
@@ -7,6 +7,14 @@
7
7
  */
8
8
  import { oneLine } from "./naming.ts";
9
9
 
10
+ /**
11
+ * Structure-level marker: the object also carries keys it does not declare
12
+ * (OpenAPI `additionalProperties` next to `properties`). The generator adds
13
+ * `[key: string]: unknown` to the interface and an `unknown`-valued index
14
+ * signature to the schema (`S.StructWithRest`).
15
+ */
16
+ export const ADDITIONAL_PROPERTIES_TRAIT = "com.distilled.openapi#additionalProperties";
17
+
10
18
  export interface MemberBase {
11
19
  /** Original smithy member name. */
12
20
  readonly name: string;