@distilled.cloud/core 1.0.0-rc.15 → 1.0.0-rc.17

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 (318) hide show
  1. package/lib/_bundle/aws.js +2 -0
  2. package/lib/_bundle/aws.js.map +1 -0
  3. package/lib/_bundle/base.js +5 -0
  4. package/lib/_bundle/base.js.map +1 -0
  5. package/lib/_bundle/graphql.js +3 -0
  6. package/lib/_bundle/graphql.js.map +1 -0
  7. package/lib/_bundle/rest.js +2 -0
  8. package/lib/_bundle/rest.js.map +1 -0
  9. package/lib/_bundle/xml.js +4 -0
  10. package/lib/_bundle/xml.js.map +1 -0
  11. package/lib/api.d.ts +118 -13
  12. package/lib/api.d.ts.map +1 -1
  13. package/lib/api.js +1 -188
  14. package/lib/category.d.ts.map +1 -1
  15. package/lib/category.js +1 -278
  16. package/lib/chunks.d.ts +3 -0
  17. package/lib/chunks.d.ts.map +1 -0
  18. package/lib/chunks.js +1 -0
  19. package/lib/codegen/boolean-string-enums.js +36 -36
  20. package/lib/codegen/chunk.d.ts +7 -0
  21. package/lib/codegen/chunk.d.ts.map +1 -0
  22. package/lib/codegen/chunk.js +139 -0
  23. package/lib/codegen/chunk.js.map +1 -0
  24. package/lib/codegen/cli.d.ts.map +1 -1
  25. package/lib/codegen/cli.js +28 -14
  26. package/lib/codegen/cli.js.map +1 -1
  27. package/lib/codegen/datafy.d.ts +14 -0
  28. package/lib/codegen/datafy.d.ts.map +1 -0
  29. package/lib/codegen/datafy.js +853 -0
  30. package/lib/codegen/datafy.js.map +1 -0
  31. package/lib/codegen/dedupe.d.ts +5 -0
  32. package/lib/codegen/dedupe.d.ts.map +1 -0
  33. package/lib/codegen/dedupe.js +153 -0
  34. package/lib/codegen/dedupe.js.map +1 -0
  35. package/lib/codegen/emit.js +56 -56
  36. package/lib/codegen/format.js +18 -18
  37. package/lib/codegen/generated-at.js +29 -29
  38. package/lib/codegen/generator.d.ts.map +1 -1
  39. package/lib/codegen/generator.js +92 -84
  40. package/lib/codegen/generator.js.map +1 -1
  41. package/lib/codegen/graph.js +36 -36
  42. package/lib/codegen/graphql-client.d.ts +2 -0
  43. package/lib/codegen/graphql-client.d.ts.map +1 -1
  44. package/lib/codegen/graphql-client.js +55 -26
  45. package/lib/codegen/graphql-client.js.map +1 -1
  46. package/lib/codegen/graphql.js +113 -113
  47. package/lib/codegen/latin1.d.ts +3 -0
  48. package/lib/codegen/latin1.d.ts.map +1 -0
  49. package/lib/codegen/latin1.js +126 -0
  50. package/lib/codegen/latin1.js.map +1 -0
  51. package/lib/codegen/members.d.ts +7 -0
  52. package/lib/codegen/members.d.ts.map +1 -1
  53. package/lib/codegen/members.js +26 -19
  54. package/lib/codegen/members.js.map +1 -1
  55. package/lib/codegen/model-diff.js +6 -6
  56. package/lib/codegen/naming.js +19 -19
  57. package/lib/codegen/openapi-cli.js +15 -15
  58. package/lib/codegen/openapi.d.ts +17 -0
  59. package/lib/codegen/openapi.d.ts.map +1 -1
  60. package/lib/codegen/openapi.js +323 -169
  61. package/lib/codegen/openapi.js.map +1 -1
  62. package/lib/codegen/operations.js +14 -14
  63. package/lib/codegen/pagination.js +11 -11
  64. package/lib/codegen/patch-audit.js +57 -57
  65. package/lib/codegen/patches.js +79 -79
  66. package/lib/codegen/prelude.js +19 -19
  67. package/lib/codegen/privatize.d.ts +21 -0
  68. package/lib/codegen/privatize.d.ts.map +1 -0
  69. package/lib/codegen/privatize.js +176 -0
  70. package/lib/codegen/privatize.js.map +1 -0
  71. package/lib/codegen/proto.js +25 -25
  72. package/lib/codegen/pure.d.ts +6 -0
  73. package/lib/codegen/pure.d.ts.map +1 -0
  74. package/lib/codegen/pure.js +91 -0
  75. package/lib/codegen/pure.js.map +1 -0
  76. package/lib/codegen/rewrite-operation-ids.js +108 -108
  77. package/lib/codegen/spec-path.js +57 -57
  78. package/lib/codegen/table.d.ts +5 -0
  79. package/lib/codegen/table.d.ts.map +1 -0
  80. package/lib/codegen/table.js +249 -0
  81. package/lib/codegen/table.js.map +1 -0
  82. package/lib/config.d.ts +27 -0
  83. package/lib/config.d.ts.map +1 -0
  84. package/lib/config.js +1 -0
  85. package/lib/crypto.d.ts +76 -0
  86. package/lib/crypto.d.ts.map +1 -0
  87. package/lib/crypto.js +1 -0
  88. package/lib/debug.d.ts +2 -0
  89. package/lib/debug.d.ts.map +1 -0
  90. package/lib/debug.js +1 -0
  91. package/lib/error-category.d.ts +1 -1
  92. package/lib/error-category.d.ts.map +1 -1
  93. package/lib/error-category.js +1 -46
  94. package/lib/errors.d.ts +62 -62
  95. package/lib/errors.d.ts.map +1 -1
  96. package/lib/errors.js +1 -195
  97. package/lib/graphql.d.ts +31 -20
  98. package/lib/graphql.d.ts.map +1 -1
  99. package/lib/graphql.js +1 -1027
  100. package/lib/graphql.types.js +1 -5
  101. package/lib/index.d.ts +3 -2
  102. package/lib/index.d.ts.map +1 -1
  103. package/lib/index.js +1 -15
  104. package/lib/json-patch.d.ts +2 -2
  105. package/lib/json-patch.js +1 -226
  106. package/lib/json-patch.js.map +1 -1
  107. package/lib/pagination.d.ts +9 -25
  108. package/lib/pagination.d.ts.map +1 -1
  109. package/lib/pagination.js +1 -266
  110. package/lib/plain-values.d.ts +10 -0
  111. package/lib/plain-values.d.ts.map +1 -0
  112. package/lib/plain-values.js +2 -0
  113. package/lib/plain-values.js.map +1 -0
  114. package/lib/protocol-http.d.ts +50 -14
  115. package/lib/protocol-http.d.ts.map +1 -1
  116. package/lib/protocol-http.js +1 -639
  117. package/lib/protocol-rest.d.ts +38 -25
  118. package/lib/protocol-rest.d.ts.map +1 -1
  119. package/lib/protocol-rest.js +1 -267
  120. package/lib/query.js +1 -27
  121. package/lib/response-validation-strict.d.ts +48 -0
  122. package/lib/response-validation-strict.d.ts.map +1 -0
  123. package/lib/response-validation-strict.js +1 -0
  124. package/lib/response-validation.d.ts +43 -25
  125. package/lib/response-validation.d.ts.map +1 -1
  126. package/lib/response-validation.js +1 -76
  127. package/lib/retry-after.d.ts +3 -3
  128. package/lib/retry-after.js +1 -112
  129. package/lib/retry.d.ts +48 -29
  130. package/lib/retry.d.ts.map +1 -1
  131. package/lib/retry.js +1 -209
  132. package/lib/schema-lite-impl.d.ts +158 -0
  133. package/lib/schema-lite-impl.d.ts.map +1 -0
  134. package/lib/schema-lite-impl.js +1 -0
  135. package/lib/schema-lite-typed.d.ts +41 -0
  136. package/lib/schema-lite-typed.d.ts.map +1 -0
  137. package/lib/schema-lite-typed.js +2 -0
  138. package/lib/schema-lite-typed.js.map +1 -0
  139. package/lib/schema-lite.d.ts +40 -0
  140. package/lib/schema-lite.d.ts.map +1 -0
  141. package/lib/schema-lite.js +1 -0
  142. package/lib/schema.d.ts +20 -6
  143. package/lib/schema.d.ts.map +1 -1
  144. package/lib/schema.js +1 -48
  145. package/lib/testing.d.ts +3 -2
  146. package/lib/testing.d.ts.map +1 -1
  147. package/lib/testing.js +1 -40
  148. package/lib/testing.js.map +1 -1
  149. package/lib/trait.d.ts +13 -13
  150. package/lib/trait.d.ts.map +1 -1
  151. package/lib/trait.js +1 -131
  152. package/lib/xml.d.ts +1 -1
  153. package/lib/xml.d.ts.map +1 -1
  154. package/lib/xml.js +1 -311
  155. package/package.json +9 -7
  156. package/src/api.ts +1080 -180
  157. package/src/category.ts +33 -33
  158. package/src/chunks.ts +30 -0
  159. package/src/codegen/chunk.ts +147 -0
  160. package/src/codegen/cli.ts +27 -1
  161. package/src/codegen/datafy.ts +874 -0
  162. package/src/codegen/dedupe.ts +173 -0
  163. package/src/codegen/generator.ts +10 -3
  164. package/src/codegen/graphql-client.ts +49 -15
  165. package/src/codegen/latin1.ts +131 -0
  166. package/src/codegen/members.ts +8 -0
  167. package/src/codegen/openapi.ts +194 -3
  168. package/src/codegen/privatize.ts +200 -0
  169. package/src/codegen/pure.ts +94 -0
  170. package/src/codegen/table.ts +254 -0
  171. package/src/config.ts +216 -0
  172. package/src/crypto.ts +202 -0
  173. package/src/debug.ts +8 -0
  174. package/src/error-category.ts +2 -1
  175. package/src/errors.ts +65 -56
  176. package/src/graphql.ts +109 -62
  177. package/src/graphql.types.ts +9 -7
  178. package/src/index.ts +3 -2
  179. package/src/json-patch.ts +2 -2
  180. package/src/pagination.ts +37 -18
  181. package/src/plain-values.ts +41 -0
  182. package/src/protocol-http.ts +378 -145
  183. package/src/protocol-rest.ts +337 -169
  184. package/src/response-validation-strict.ts +327 -0
  185. package/src/response-validation.ts +101 -50
  186. package/src/retry-after.ts +3 -3
  187. package/src/retry.ts +94 -93
  188. package/src/schema-lite-impl.ts +550 -0
  189. package/src/schema-lite-typed.ts +44 -0
  190. package/src/schema-lite.ts +44 -0
  191. package/src/schema.ts +258 -14
  192. package/src/testing.ts +4 -2
  193. package/src/trait.ts +36 -28
  194. package/src/xml.ts +31 -17
  195. package/lib/api.js.map +0 -1
  196. package/lib/category.js.map +0 -1
  197. package/lib/codegen/boolean-string-enums.test.d.ts +0 -2
  198. package/lib/codegen/boolean-string-enums.test.d.ts.map +0 -1
  199. package/lib/codegen/boolean-string-enums.test.js +0 -275
  200. package/lib/codegen/boolean-string-enums.test.js.map +0 -1
  201. package/lib/codegen/defer-naming.test.d.ts +0 -2
  202. package/lib/codegen/defer-naming.test.d.ts.map +0 -1
  203. package/lib/codegen/defer-naming.test.js +0 -105
  204. package/lib/codegen/defer-naming.test.js.map +0 -1
  205. package/lib/codegen/generated-at.test.d.ts +0 -2
  206. package/lib/codegen/generated-at.test.d.ts.map +0 -1
  207. package/lib/codegen/generated-at.test.js +0 -81
  208. package/lib/codegen/generated-at.test.js.map +0 -1
  209. package/lib/codegen/generator.test.d.ts +0 -2
  210. package/lib/codegen/generator.test.d.ts.map +0 -1
  211. package/lib/codegen/generator.test.js +0 -121
  212. package/lib/codegen/generator.test.js.map +0 -1
  213. package/lib/codegen/graphql-client.test.d.ts +0 -2
  214. package/lib/codegen/graphql-client.test.d.ts.map +0 -1
  215. package/lib/codegen/graphql-client.test.js +0 -171
  216. package/lib/codegen/graphql-client.test.js.map +0 -1
  217. package/lib/codegen/openapi-binary.test.d.ts +0 -2
  218. package/lib/codegen/openapi-binary.test.d.ts.map +0 -1
  219. package/lib/codegen/openapi-binary.test.js +0 -67
  220. package/lib/codegen/openapi-binary.test.js.map +0 -1
  221. package/lib/codegen/patch-audit.test.d.ts +0 -2
  222. package/lib/codegen/patch-audit.test.d.ts.map +0 -1
  223. package/lib/codegen/patch-audit.test.js +0 -141
  224. package/lib/codegen/patch-audit.test.js.map +0 -1
  225. package/lib/codegen/patches.test.d.ts +0 -2
  226. package/lib/codegen/patches.test.d.ts.map +0 -1
  227. package/lib/codegen/patches.test.js +0 -155
  228. package/lib/codegen/patches.test.js.map +0 -1
  229. package/lib/codegen/protocol-imports.test.d.ts +0 -2
  230. package/lib/codegen/protocol-imports.test.d.ts.map +0 -1
  231. package/lib/codegen/protocol-imports.test.js +0 -36
  232. package/lib/codegen/protocol-imports.test.js.map +0 -1
  233. package/lib/codegen/rewrite-operation-ids.test.d.ts +0 -2
  234. package/lib/codegen/rewrite-operation-ids.test.d.ts.map +0 -1
  235. package/lib/codegen/rewrite-operation-ids.test.js +0 -451
  236. package/lib/codegen/rewrite-operation-ids.test.js.map +0 -1
  237. package/lib/codegen/untagged-unions.test.d.ts +0 -2
  238. package/lib/codegen/untagged-unions.test.d.ts.map +0 -1
  239. package/lib/codegen/untagged-unions.test.js +0 -80
  240. package/lib/codegen/untagged-unions.test.js.map +0 -1
  241. package/lib/error-category.js.map +0 -1
  242. package/lib/errors.js.map +0 -1
  243. package/lib/fixtures/xml/upstream-validator.json +0 -397
  244. package/lib/graphql.js.map +0 -1
  245. package/lib/graphql.test.d.ts +0 -2
  246. package/lib/graphql.test.d.ts.map +0 -1
  247. package/lib/graphql.test.js +0 -151
  248. package/lib/graphql.test.js.map +0 -1
  249. package/lib/graphql.types.js.map +0 -1
  250. package/lib/index.js.map +0 -1
  251. package/lib/json-patch.test.d.ts +0 -2
  252. package/lib/json-patch.test.d.ts.map +0 -1
  253. package/lib/json-patch.test.js +0 -65
  254. package/lib/json-patch.test.js.map +0 -1
  255. package/lib/pagination.js.map +0 -1
  256. package/lib/pagination.test.d.ts +0 -2
  257. package/lib/pagination.test.d.ts.map +0 -1
  258. package/lib/pagination.test.js +0 -131
  259. package/lib/pagination.test.js.map +0 -1
  260. package/lib/protocol-http.js.map +0 -1
  261. package/lib/protocol-http.test.d.ts +0 -2
  262. package/lib/protocol-http.test.d.ts.map +0 -1
  263. package/lib/protocol-http.test.js +0 -228
  264. package/lib/protocol-http.test.js.map +0 -1
  265. package/lib/protocol-rest.js.map +0 -1
  266. package/lib/protocol-rest.test.d.ts +0 -2
  267. package/lib/protocol-rest.test.d.ts.map +0 -1
  268. package/lib/protocol-rest.test.js +0 -498
  269. package/lib/protocol-rest.test.js.map +0 -1
  270. package/lib/query.js.map +0 -1
  271. package/lib/response-validation.js.map +0 -1
  272. package/lib/response-validation.test.d.ts +0 -2
  273. package/lib/response-validation.test.d.ts.map +0 -1
  274. package/lib/response-validation.test.js +0 -72
  275. package/lib/response-validation.test.js.map +0 -1
  276. package/lib/retry-after.js.map +0 -1
  277. package/lib/retry-after.test.d.ts +0 -2
  278. package/lib/retry-after.test.d.ts.map +0 -1
  279. package/lib/retry-after.test.js +0 -204
  280. package/lib/retry-after.test.js.map +0 -1
  281. package/lib/retry.js.map +0 -1
  282. package/lib/retry.test.d.ts +0 -2
  283. package/lib/retry.test.d.ts.map +0 -1
  284. package/lib/retry.test.js +0 -407
  285. package/lib/retry.test.js.map +0 -1
  286. package/lib/schema.js.map +0 -1
  287. package/lib/sdks.test.d.ts +0 -2
  288. package/lib/sdks.test.d.ts.map +0 -1
  289. package/lib/sdks.test.js +0 -76
  290. package/lib/sdks.test.js.map +0 -1
  291. package/lib/trait.js.map +0 -1
  292. package/lib/xml.js.map +0 -1
  293. package/lib/xml.test.d.ts +0 -2
  294. package/lib/xml.test.d.ts.map +0 -1
  295. package/lib/xml.test.js +0 -210
  296. package/lib/xml.test.js.map +0 -1
  297. package/src/codegen/boolean-string-enums.test.ts +0 -299
  298. package/src/codegen/defer-naming.test.ts +0 -113
  299. package/src/codegen/generated-at.test.ts +0 -89
  300. package/src/codegen/generator.test.ts +0 -137
  301. package/src/codegen/graphql-client.test.ts +0 -189
  302. package/src/codegen/openapi-binary.test.ts +0 -91
  303. package/src/codegen/patch-audit.test.ts +0 -160
  304. package/src/codegen/patches.test.ts +0 -188
  305. package/src/codegen/protocol-imports.test.ts +0 -39
  306. package/src/codegen/rewrite-operation-ids.test.ts +0 -472
  307. package/src/codegen/untagged-unions.test.ts +0 -85
  308. package/src/fixtures/xml/upstream-validator.json +0 -397
  309. package/src/graphql.test.ts +0 -198
  310. package/src/json-patch.test.ts +0 -77
  311. package/src/pagination.test.ts +0 -169
  312. package/src/protocol-http.test.ts +0 -276
  313. package/src/protocol-rest.test.ts +0 -703
  314. package/src/response-validation.test.ts +0 -89
  315. package/src/retry-after.test.ts +0 -267
  316. package/src/retry.test.ts +0 -566
  317. package/src/sdks.test.ts +0 -87
  318. 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;