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

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 (276) hide show
  1. package/lib/api.d.ts +4 -4
  2. package/lib/api.d.ts.map +1 -1
  3. package/lib/api.js +3 -5
  4. package/lib/api.js.map +1 -1
  5. package/lib/category.d.ts.map +1 -1
  6. package/lib/category.js +3 -5
  7. package/lib/category.js.map +1 -1
  8. package/lib/codegen/boolean-string-enums.d.ts +8 -8
  9. package/lib/codegen/boolean-string-enums.d.ts.map +1 -1
  10. package/lib/codegen/boolean-string-enums.js +83 -30
  11. package/lib/codegen/boolean-string-enums.js.map +1 -1
  12. package/lib/codegen/boolean-string-enums.test.js +130 -2
  13. package/lib/codegen/boolean-string-enums.test.js.map +1 -1
  14. package/lib/codegen/cli.d.ts +5 -4
  15. package/lib/codegen/cli.d.ts.map +1 -1
  16. package/lib/codegen/cli.js +12 -9
  17. package/lib/codegen/cli.js.map +1 -1
  18. package/lib/codegen/defer-naming.test.d.ts +2 -0
  19. package/lib/codegen/defer-naming.test.d.ts.map +1 -0
  20. package/lib/codegen/defer-naming.test.js +105 -0
  21. package/lib/codegen/defer-naming.test.js.map +1 -0
  22. package/lib/codegen/emit.d.ts.map +1 -1
  23. package/lib/codegen/emit.js +1 -3
  24. package/lib/codegen/emit.js.map +1 -1
  25. package/lib/codegen/format.d.ts +0 -12
  26. package/lib/codegen/format.d.ts.map +1 -1
  27. package/lib/codegen/format.js +10 -6
  28. package/lib/codegen/format.js.map +1 -1
  29. package/lib/codegen/generated-at.d.ts +29 -0
  30. package/lib/codegen/generated-at.d.ts.map +1 -0
  31. package/lib/codegen/generated-at.js +78 -0
  32. package/lib/codegen/generated-at.js.map +1 -0
  33. package/lib/codegen/generated-at.test.d.ts +2 -0
  34. package/lib/codegen/generated-at.test.d.ts.map +1 -0
  35. package/lib/codegen/generated-at.test.js +81 -0
  36. package/lib/codegen/generated-at.test.js.map +1 -0
  37. package/lib/codegen/generator.d.ts +24 -5
  38. package/lib/codegen/generator.d.ts.map +1 -1
  39. package/lib/codegen/generator.js +69 -80
  40. package/lib/codegen/generator.js.map +1 -1
  41. package/lib/codegen/generator.test.d.ts +2 -0
  42. package/lib/codegen/generator.test.d.ts.map +1 -0
  43. package/lib/codegen/generator.test.js +121 -0
  44. package/lib/codegen/generator.test.js.map +1 -0
  45. package/lib/codegen/graphql-client.d.ts +7 -1
  46. package/lib/codegen/graphql-client.d.ts.map +1 -1
  47. package/lib/codegen/graphql-client.js +264 -148
  48. package/lib/codegen/graphql-client.js.map +1 -1
  49. package/lib/codegen/graphql-client.test.js +98 -238
  50. package/lib/codegen/graphql-client.test.js.map +1 -1
  51. package/lib/codegen/graphql.d.ts.map +1 -1
  52. package/lib/codegen/graphql.js +7 -20
  53. package/lib/codegen/graphql.js.map +1 -1
  54. package/lib/codegen/model-diff.d.ts +10 -0
  55. package/lib/codegen/model-diff.d.ts.map +1 -0
  56. package/lib/codegen/model-diff.js +67 -0
  57. package/lib/codegen/model-diff.js.map +1 -0
  58. package/lib/codegen/naming.d.ts.map +1 -1
  59. package/lib/codegen/naming.js.map +1 -1
  60. package/lib/codegen/openapi-binary.test.d.ts +2 -0
  61. package/lib/codegen/openapi-binary.test.d.ts.map +1 -0
  62. package/lib/codegen/openapi-binary.test.js +67 -0
  63. package/lib/codegen/openapi-binary.test.js.map +1 -0
  64. package/lib/codegen/openapi-cli.d.ts +11 -11
  65. package/lib/codegen/openapi-cli.d.ts.map +1 -1
  66. package/lib/codegen/openapi-cli.js +26 -51
  67. package/lib/codegen/openapi-cli.js.map +1 -1
  68. package/lib/codegen/openapi.d.ts +16 -2
  69. package/lib/codegen/openapi.d.ts.map +1 -1
  70. package/lib/codegen/openapi.js +56 -39
  71. package/lib/codegen/openapi.js.map +1 -1
  72. package/lib/codegen/operations.d.ts.map +1 -1
  73. package/lib/codegen/operations.js.map +1 -1
  74. package/lib/codegen/pagination.d.ts.map +1 -1
  75. package/lib/codegen/pagination.js +2 -4
  76. package/lib/codegen/pagination.js.map +1 -1
  77. package/lib/codegen/patch-audit.d.ts +84 -0
  78. package/lib/codegen/patch-audit.d.ts.map +1 -0
  79. package/lib/codegen/patch-audit.js +366 -0
  80. package/lib/codegen/patch-audit.js.map +1 -0
  81. package/lib/codegen/patch-audit.test.d.ts +2 -0
  82. package/lib/codegen/patch-audit.test.d.ts.map +1 -0
  83. package/lib/codegen/patch-audit.test.js +141 -0
  84. package/lib/codegen/patch-audit.test.js.map +1 -0
  85. package/lib/codegen/patches.d.ts +75 -11
  86. package/lib/codegen/patches.d.ts.map +1 -1
  87. package/lib/codegen/patches.js +301 -65
  88. package/lib/codegen/patches.js.map +1 -1
  89. package/lib/codegen/patches.test.js +55 -5
  90. package/lib/codegen/patches.test.js.map +1 -1
  91. package/lib/codegen/prelude.js +1 -1
  92. package/lib/codegen/prelude.js.map +1 -1
  93. package/lib/codegen/proto.d.ts.map +1 -1
  94. package/lib/codegen/proto.js +2 -4
  95. package/lib/codegen/proto.js.map +1 -1
  96. package/lib/codegen/protocol-imports.test.d.ts +2 -0
  97. package/lib/codegen/protocol-imports.test.d.ts.map +1 -0
  98. package/lib/codegen/protocol-imports.test.js +36 -0
  99. package/lib/codegen/protocol-imports.test.js.map +1 -0
  100. package/lib/codegen/rewrite-operation-ids.d.ts +23 -0
  101. package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -1
  102. package/lib/codegen/rewrite-operation-ids.js +97 -59
  103. package/lib/codegen/rewrite-operation-ids.js.map +1 -1
  104. package/lib/codegen/rewrite-operation-ids.test.js +19 -101
  105. package/lib/codegen/rewrite-operation-ids.test.js.map +1 -1
  106. package/lib/codegen/spec-path.d.ts +1 -1
  107. package/lib/codegen/spec-path.d.ts.map +1 -1
  108. package/lib/codegen/spec-path.js +1 -1
  109. package/lib/codegen/spec-path.js.map +1 -1
  110. package/lib/codegen/untagged-unions.test.d.ts +2 -0
  111. package/lib/codegen/untagged-unions.test.d.ts.map +1 -0
  112. package/lib/codegen/untagged-unions.test.js +80 -0
  113. package/lib/codegen/untagged-unions.test.js.map +1 -0
  114. package/lib/error-category.d.ts.map +1 -1
  115. package/lib/error-category.js.map +1 -1
  116. package/lib/errors.d.ts +2 -1
  117. package/lib/errors.d.ts.map +1 -1
  118. package/lib/errors.js +9 -4
  119. package/lib/errors.js.map +1 -1
  120. package/lib/fixtures/xml/upstream-validator.json +397 -0
  121. package/lib/graphql.d.ts +292 -205
  122. package/lib/graphql.d.ts.map +1 -1
  123. package/lib/graphql.js +945 -636
  124. package/lib/graphql.js.map +1 -1
  125. package/lib/graphql.test.js +135 -764
  126. package/lib/graphql.test.js.map +1 -1
  127. package/lib/graphql.types.d.ts +10 -0
  128. package/lib/graphql.types.d.ts.map +1 -1
  129. package/lib/graphql.types.js +4 -44
  130. package/lib/graphql.types.js.map +1 -1
  131. package/lib/index.d.ts +27 -0
  132. package/lib/index.d.ts.map +1 -0
  133. package/lib/index.js +27 -0
  134. package/lib/index.js.map +1 -0
  135. package/lib/json-patch.d.ts +13 -6
  136. package/lib/json-patch.d.ts.map +1 -1
  137. package/lib/json-patch.js +34 -19
  138. package/lib/json-patch.js.map +1 -1
  139. package/lib/json-patch.test.d.ts +2 -0
  140. package/lib/json-patch.test.d.ts.map +1 -0
  141. package/lib/json-patch.test.js +65 -0
  142. package/lib/json-patch.test.js.map +1 -0
  143. package/lib/pagination.d.ts +2 -1
  144. package/lib/pagination.d.ts.map +1 -1
  145. package/lib/pagination.js +28 -30
  146. package/lib/pagination.js.map +1 -1
  147. package/lib/pagination.test.d.ts +2 -0
  148. package/lib/pagination.test.d.ts.map +1 -0
  149. package/lib/pagination.test.js +131 -0
  150. package/lib/pagination.test.js.map +1 -0
  151. package/lib/protocol-http.d.ts +30 -16
  152. package/lib/protocol-http.d.ts.map +1 -1
  153. package/lib/protocol-http.js +133 -84
  154. package/lib/protocol-http.js.map +1 -1
  155. package/lib/protocol-http.test.js +141 -1
  156. package/lib/protocol-http.test.js.map +1 -1
  157. package/lib/protocol-rest.d.ts +18 -1
  158. package/lib/protocol-rest.d.ts.map +1 -1
  159. package/lib/protocol-rest.js +32 -21
  160. package/lib/protocol-rest.js.map +1 -1
  161. package/lib/protocol-rest.test.d.ts +2 -0
  162. package/lib/protocol-rest.test.d.ts.map +1 -0
  163. package/lib/protocol-rest.test.js +498 -0
  164. package/lib/protocol-rest.test.js.map +1 -0
  165. package/lib/query.d.ts +29 -0
  166. package/lib/query.d.ts.map +1 -0
  167. package/lib/query.js +27 -0
  168. package/lib/query.js.map +1 -0
  169. package/lib/response-validation.d.ts +67 -0
  170. package/lib/response-validation.d.ts.map +1 -0
  171. package/lib/response-validation.js +76 -0
  172. package/lib/response-validation.js.map +1 -0
  173. package/lib/response-validation.test.d.ts +2 -0
  174. package/lib/response-validation.test.d.ts.map +1 -0
  175. package/lib/response-validation.test.js +72 -0
  176. package/lib/response-validation.test.js.map +1 -0
  177. package/lib/retry-after.d.ts.map +1 -1
  178. package/lib/retry-after.js +1 -1
  179. package/lib/retry-after.js.map +1 -1
  180. package/lib/retry-after.test.d.ts +2 -0
  181. package/lib/retry-after.test.d.ts.map +1 -0
  182. package/lib/retry-after.test.js +204 -0
  183. package/lib/retry-after.test.js.map +1 -0
  184. package/lib/retry.d.ts.map +1 -1
  185. package/lib/retry.js +1 -2
  186. package/lib/retry.js.map +1 -1
  187. package/lib/retry.test.d.ts +2 -0
  188. package/lib/retry.test.d.ts.map +1 -0
  189. package/lib/retry.test.js +407 -0
  190. package/lib/retry.test.js.map +1 -0
  191. package/lib/sdks.test.d.ts +2 -0
  192. package/lib/sdks.test.d.ts.map +1 -0
  193. package/lib/sdks.test.js +76 -0
  194. package/lib/sdks.test.js.map +1 -0
  195. package/lib/testing.d.ts +39 -0
  196. package/lib/testing.d.ts.map +1 -0
  197. package/lib/testing.js +41 -0
  198. package/lib/testing.js.map +1 -0
  199. package/lib/trait.d.ts +29 -9
  200. package/lib/trait.d.ts.map +1 -1
  201. package/lib/trait.js +8 -0
  202. package/lib/trait.js.map +1 -1
  203. package/lib/xml.d.ts +44 -0
  204. package/lib/xml.d.ts.map +1 -0
  205. package/lib/xml.js +311 -0
  206. package/lib/xml.js.map +1 -0
  207. package/lib/xml.test.d.ts +2 -0
  208. package/lib/xml.test.d.ts.map +1 -0
  209. package/lib/xml.test.js +210 -0
  210. package/lib/xml.test.js.map +1 -0
  211. package/package.json +8 -11
  212. package/src/api.ts +38 -55
  213. package/src/category.ts +21 -50
  214. package/src/codegen/boolean-string-enums.test.ts +148 -17
  215. package/src/codegen/boolean-string-enums.ts +81 -32
  216. package/src/codegen/cli.ts +29 -47
  217. package/src/codegen/defer-naming.test.ts +113 -0
  218. package/src/codegen/emit.ts +2 -5
  219. package/src/codegen/format.ts +12 -10
  220. package/src/codegen/generated-at.test.ts +89 -0
  221. package/src/codegen/generated-at.ts +88 -0
  222. package/src/codegen/generator.test.ts +137 -0
  223. package/src/codegen/generator.ts +132 -193
  224. package/src/codegen/graphql-client.test.ts +100 -297
  225. package/src/codegen/graphql-client.ts +302 -210
  226. package/src/codegen/graphql.ts +22 -94
  227. package/src/codegen/model-diff.ts +67 -0
  228. package/src/codegen/naming.ts +1 -2
  229. package/src/codegen/openapi-binary.test.ts +91 -0
  230. package/src/codegen/openapi-cli.ts +38 -79
  231. package/src/codegen/openapi.ts +98 -133
  232. package/src/codegen/operations.ts +2 -5
  233. package/src/codegen/pagination.ts +4 -11
  234. package/src/codegen/patch-audit.test.ts +160 -0
  235. package/src/codegen/patch-audit.ts +480 -0
  236. package/src/codegen/patches.test.ts +73 -15
  237. package/src/codegen/patches.ts +389 -99
  238. package/src/codegen/prelude.ts +1 -1
  239. package/src/codegen/proto.ts +18 -54
  240. package/src/codegen/protocol-imports.test.ts +39 -0
  241. package/src/codegen/rewrite-operation-ids.test.ts +23 -114
  242. package/src/codegen/rewrite-operation-ids.ts +111 -98
  243. package/src/codegen/spec-path.ts +3 -6
  244. package/src/codegen/untagged-unions.test.ts +85 -0
  245. package/src/error-category.ts +5 -17
  246. package/src/errors.ts +18 -30
  247. package/src/fixtures/xml/upstream-validator.json +397 -0
  248. package/src/graphql.test.ts +173 -949
  249. package/src/graphql.ts +1468 -1186
  250. package/src/graphql.types.ts +78 -164
  251. package/src/index.ts +26 -0
  252. package/src/json-patch.test.ts +77 -0
  253. package/src/json-patch.ts +54 -60
  254. package/src/pagination.test.ts +169 -0
  255. package/src/pagination.ts +46 -103
  256. package/src/protocol-http.test.ts +178 -9
  257. package/src/protocol-http.ts +174 -163
  258. package/src/protocol-rest.test.ts +703 -0
  259. package/src/protocol-rest.ts +76 -65
  260. package/src/query.ts +39 -0
  261. package/src/response-validation.test.ts +89 -0
  262. package/src/response-validation.ts +107 -0
  263. package/src/retry-after.test.ts +267 -0
  264. package/src/retry-after.ts +4 -10
  265. package/src/retry.test.ts +566 -0
  266. package/src/retry.ts +6 -21
  267. package/src/sdks.test.ts +87 -0
  268. package/src/testing.ts +72 -0
  269. package/src/trait.ts +48 -63
  270. package/src/xml.test.ts +251 -0
  271. package/src/xml.ts +323 -0
  272. package/lib/graphql.fixture.d.ts +0 -249
  273. package/lib/graphql.fixture.d.ts.map +0 -1
  274. package/lib/graphql.fixture.js +0 -240
  275. package/lib/graphql.fixture.js.map +0 -1
  276. package/src/graphql.fixture.ts +0 -371
@@ -1,8 +1,5 @@
1
- import { describe, expect, test } from "bun:test";
2
- import {
3
- booleanStringEnums,
4
- STRING_ENCODED_TRAIT,
5
- } from "./boolean-string-enums.ts";
1
+ import { describe, expect, test } from "vitest";
2
+ import { booleanStringEnums, STRING_ENCODED_TRAIT } from "./boolean-string-enums.ts";
6
3
  import { generateService } from "./generator.ts";
7
4
 
8
5
  const boolEnum = {
@@ -46,9 +43,7 @@ describe("booleanStringEnums", () => {
46
43
  },
47
44
  });
48
45
  expect(booleanStringEnums(m)).toEqual({ members: 0, lists: 0 });
49
- expect(m.shapes["com.example.x#Response"].members.active.target).toBe(
50
- "com.example.x#Flag",
51
- );
46
+ expect(m.shapes["com.example.x#Response"].members.active.target).toBe("com.example.x#Flag");
52
47
  });
53
48
 
54
49
  test("retargets a request-only list's element type", () => {
@@ -65,9 +60,7 @@ describe("booleanStringEnums", () => {
65
60
  },
66
61
  });
67
62
  expect(booleanStringEnums(m)).toEqual({ members: 1, lists: 1 });
68
- expect(m.shapes["com.example.x#FlagList"].member.target).toBe(
69
- "smithy.api#Boolean",
70
- );
63
+ expect(m.shapes["com.example.x#FlagList"].member.target).toBe("smithy.api#Boolean");
71
64
  });
72
65
 
73
66
  test("leaves a list a response also uses", () => {
@@ -88,9 +81,7 @@ describe("booleanStringEnums", () => {
88
81
  },
89
82
  });
90
83
  expect(booleanStringEnums(m)).toEqual({ members: 0, lists: 0 });
91
- expect(m.shapes["com.example.x#FlagList"].member.target).toBe(
92
- "com.example.x#Flag",
93
- );
84
+ expect(m.shapes["com.example.x#FlagList"].member.target).toBe("com.example.x#Flag");
94
85
  });
95
86
 
96
87
  test("leaves an enum that is not exactly true/false", () => {
@@ -123,6 +114,106 @@ describe("booleanStringEnums", () => {
123
114
  });
124
115
 
125
116
  describe("generateService", () => {
117
+ test("member schema overrides retain nullability, query bindings and protocol traits", () => {
118
+ const { code } = generateService(
119
+ model({
120
+ "ns#Example": {
121
+ type: "service",
122
+ version: "1",
123
+ operations: [{ target: "ns#GetThing" }],
124
+ },
125
+ "ns#GetThing": {
126
+ type: "operation",
127
+ input: { target: "ns#Input" },
128
+ output: { target: "smithy.api#Unit" },
129
+ traits: { "smithy.api#http": { method: "GET", uri: "/things" } },
130
+ },
131
+ "ns#Input": {
132
+ type: "structure",
133
+ members: {
134
+ secret: {
135
+ target: "smithy.api#String",
136
+ traits: {
137
+ "smithy.api#httpQuery": "secret",
138
+ "smithy.api#sensitive": {},
139
+ "ns#nullable": {},
140
+ },
141
+ },
142
+ },
143
+ },
144
+ }),
145
+ {
146
+ nullableTrait: "ns#nullable",
147
+ memberSchema: (member, ref) =>
148
+ `S.Union([${ref(member.target)}, S.Redacted(${ref(member.target)})])`,
149
+ memberTraitPipes: { "smithy.api#sensitive": "T.SensitiveValue" },
150
+ operationDecl: {
151
+ contextType: "ExampleOpContext",
152
+ commonErrorType: "ExampleOpError",
153
+ commonErrorClasses: [],
154
+ protocol: "ExampleProtocol",
155
+ retry: "Retry.Retry",
156
+ },
157
+ },
158
+ );
159
+ expect(code).toContain(
160
+ '"secret": S.optional(S.NullOr(S.Union([S.String, S.Redacted(S.String)])).pipe(T.Query(), T.SensitiveValue({})))',
161
+ );
162
+ });
163
+
164
+ test("service-free schemas preserve Codec annotations across structures, collections and raw outputs", () => {
165
+ const input = model({
166
+ "ns#Example": {
167
+ type: "service",
168
+ version: "1",
169
+ operations: [{ target: "ns#GetThing" }],
170
+ },
171
+ "ns#GetThing": {
172
+ type: "operation",
173
+ input: { target: "ns#Input" },
174
+ output: { target: "ns#Output" },
175
+ traits: { "smithy.api#http": { method: "GET", uri: "/things" } },
176
+ },
177
+ "ns#Input": {
178
+ type: "structure",
179
+ members: { list: { target: "ns#List" }, map: { target: "ns#Map" } },
180
+ },
181
+ "ns#List": { type: "list", member: { target: "smithy.api#String" } },
182
+ "ns#Map": {
183
+ type: "map",
184
+ key: { target: "smithy.api#String" },
185
+ value: { target: "smithy.api#String" },
186
+ },
187
+ "ns#Output": {
188
+ type: "structure",
189
+ members: { body: { target: "ns#List", traits: { "ns#raw": {} } } },
190
+ },
191
+ });
192
+ const spec = {
193
+ extraBindings: [
194
+ {
195
+ trait: "ns#raw",
196
+ binding: "rawResponse",
197
+ pipe: "T.RawResponse()",
198
+ rootPipe: "T.RawResponseRoot()",
199
+ },
200
+ ],
201
+ operationDecl: {
202
+ contextType: "ExampleOpContext",
203
+ commonErrorType: "ExampleOpError",
204
+ commonErrorClasses: [],
205
+ protocol: "ExampleProtocol",
206
+ retry: "Retry.Retry",
207
+ },
208
+ } as const;
209
+ const { code } = generateService(input, { ...spec, schemaType: "Codec" });
210
+ for (const name of ["Input", "List", "Map", "Output"]) {
211
+ expect(code).toContain(`as any as S.Codec<${name}>`);
212
+ }
213
+ expect(code).not.toContain("S.Schema<");
214
+ expect(generateService(input, spec).code).toContain("as any as S.Schema<Input>");
215
+ });
216
+
126
217
  test("emits a boolean piped through T.StringEncoded()", () => {
127
218
  const { code } = generateService(
128
219
  model({
@@ -159,10 +250,50 @@ describe("generateService", () => {
159
250
  },
160
251
  },
161
252
  );
162
- expect(code).toContain(
163
- '"verbose": S.optional(S.Boolean.pipe(T.Query(), T.StringEncoded())),',
164
- );
253
+ expect(code).toContain('"verbose": S.optional(S.Boolean.pipe(T.Query(), T.StringEncoded())),');
165
254
  expect(code).toContain("verbose?: boolean;");
166
255
  expect(code).not.toContain('"true" | "false"');
167
256
  });
168
257
  });
258
+
259
+ describe("nested structures", () => {
260
+ const nested = (sharedWithResponse: boolean) =>
261
+ model({
262
+ "com.example.x#GetThing": {
263
+ type: "operation",
264
+ input: { target: "com.example.x#GetThingRequest" },
265
+ output: { target: "com.example.x#GetThingResponse" },
266
+ },
267
+ "com.example.x#GetThingRequest": {
268
+ type: "structure",
269
+ traits: { "smithy.api#input": {} },
270
+ members: { profile: { target: "com.example.x#Profile" } },
271
+ },
272
+ "com.example.x#GetThingResponse": {
273
+ type: "structure",
274
+ traits: { "smithy.api#output": {} },
275
+ members: sharedWithResponse ? { profile: { target: "com.example.x#Profile" } } : {},
276
+ },
277
+ "com.example.x#Profile": {
278
+ type: "structure",
279
+ members: { dynamicMemoryEnabled: { target: "com.example.x#Flag" } },
280
+ },
281
+ "com.example.x#Flag": boolEnum,
282
+ });
283
+
284
+ test("retargets a member of a structure only requests reach", () => {
285
+ const m = nested(false);
286
+ expect(booleanStringEnums(m)).toEqual({ members: 1, lists: 0 });
287
+ const member = m.shapes["com.example.x#Profile"].members.dynamicMemoryEnabled;
288
+ expect(member.target).toBe("smithy.api#Boolean");
289
+ expect(member.traits[STRING_ENCODED_TRAIT]).toEqual({});
290
+ });
291
+
292
+ test("leaves a structure a response also delivers", () => {
293
+ const m = nested(true);
294
+ expect(booleanStringEnums(m)).toEqual({ members: 0, lists: 0 });
295
+ expect(m.shapes["com.example.x#Profile"].members.dynamicMemoryEnabled.target).toBe(
296
+ "com.example.x#Flag",
297
+ );
298
+ });
299
+ });
@@ -10,17 +10,17 @@
10
10
  * {@link generateService} runs this pass over the loaded model before
11
11
  * emitting: REQUEST members of such an enum are retargeted to a real
12
12
  * `smithy.api#Boolean` and stamped {@link STRING_ENCODED_TRAIT}, which the
13
- * generator emits as `T.StringEncoded()` and the protocol's `buildRequest`
14
- * honors by sending the value's string spelling. The TS surface becomes
15
- * `boolean` with the wire unchanged. The model on disk keeps the string
16
- * enum the description documents.
13
+ * generator emits as `T.StringEncoded()` and the protocol honors by sending
14
+ * the value's string spelling. The TS surface becomes `boolean` with the
15
+ * wire unchanged. The model on disk keeps the string enum the description
16
+ * documents.
17
17
  *
18
18
  * Deliberately narrow:
19
19
  *
20
- * • request shapes only (`smithy.api#input`) — a response member would
21
- * need the mirror-image decode, which the protocol does not do;
22
- * • a list member is retargeted only when EVERY reference to that list
23
- * comes from a request shape, so a shared list is never rewritten;
20
+ * • request-only shapes — an operation input, or a structure or list the
21
+ * inputs reach and no output or error does. A member a response can
22
+ * deliver would need the mirror-image decode, which the protocol does
23
+ * not do, so a shape used both ways keeps its documented string type;
24
24
  * • the enum must be exactly `{"true", "false"}` — a three-value enum
25
25
  * that happens to include them is left alone.
26
26
  */
@@ -45,51 +45,100 @@ const booleanEnumIds = (shapes: Record<string, any>): Set<string> => {
45
45
  const values = Object.values(shape.members ?? {}).map(
46
46
  (m: any) => m?.traits?.["smithy.api#enumValue"],
47
47
  );
48
- if (
49
- values.length === 2 &&
50
- values.includes("true") &&
51
- values.includes("false")
52
- ) {
48
+ if (values.length === 2 && values.includes("true") && values.includes("false")) {
53
49
  out.add(id);
54
50
  }
55
51
  }
56
52
  return out;
57
53
  };
58
54
 
59
- const isRequestShape = (shape: any): boolean =>
60
- shape?.type === "structure" && "smithy.api#input" in (shape.traits ?? {});
55
+ /** Every shape id a shape points at: members, list/map entries, mixins. */
56
+ const targetsOf = (shape: any): string[] => {
57
+ const out: string[] = [];
58
+ for (const member of Object.values<any>(shape?.members ?? {})) {
59
+ if (member?.target) out.push(member.target);
60
+ }
61
+ for (const key of ["member", "value", "key"] as const) {
62
+ const target = shape?.[key]?.target;
63
+ if (target) out.push(target);
64
+ }
65
+ for (const mixin of shape?.mixins ?? []) {
66
+ if (mixin?.target) out.push(mixin.target);
67
+ }
68
+ return out;
69
+ };
70
+
71
+ const reachable = (
72
+ shapes: Record<string, any>,
73
+ roots: ReadonlyArray<string | undefined>,
74
+ ): Set<string> => {
75
+ const seen = new Set<string>();
76
+ const stack = roots.filter((r): r is string => r !== undefined);
77
+ while (stack.length > 0) {
78
+ const id = stack.pop()!;
79
+ if (seen.has(id) || shapes[id] === undefined) continue;
80
+ seen.add(id);
81
+ stack.push(...targetsOf(shapes[id]));
82
+ }
83
+ return seen;
84
+ };
85
+
86
+ /**
87
+ * Shapes an operation input reaches that no output or error does. Operation
88
+ * I/O is the spine; the `smithy.api#input`/`#output`/`#error` traits cover
89
+ * models whose shapes are not wired through an operation, and a structure
90
+ * the inputs never reach counts as a response root — unreachable from a
91
+ * request, it can only describe something the API hands back.
92
+ */
93
+ const requestOnlyShapes = (shapes: Record<string, any>): Set<string> => {
94
+ const requestRoots: Array<string | undefined> = [];
95
+ const responseRoots: Array<string | undefined> = [];
96
+ for (const [id, shape] of Object.entries<any>(shapes)) {
97
+ if (shape?.type === "operation") {
98
+ requestRoots.push(shape.input?.target);
99
+ responseRoots.push(shape.output?.target);
100
+ for (const error of shape.errors ?? []) responseRoots.push(error?.target);
101
+ continue;
102
+ }
103
+ const traits = shape?.traits ?? {};
104
+ if ("smithy.api#input" in traits) requestRoots.push(id);
105
+ if ("smithy.api#output" in traits || "smithy.api#error" in traits) {
106
+ responseRoots.push(id);
107
+ }
108
+ }
109
+ const fromRequests = reachable(shapes, requestRoots);
110
+ for (const [id, shape] of Object.entries<any>(shapes)) {
111
+ if (shape?.type === "structure" && !fromRequests.has(id)) {
112
+ responseRoots.push(id);
113
+ }
114
+ }
115
+ const fromResponses = reachable(shapes, responseRoots);
116
+ const out = new Set<string>();
117
+ for (const id of fromRequests) {
118
+ if (!fromResponses.has(id)) out.add(id);
119
+ }
120
+ return out;
121
+ };
61
122
 
62
123
  export const booleanStringEnums = (model: any): BooleanStringEnumResult => {
63
124
  const shapes: Record<string, any> = model?.shapes ?? {};
64
125
  const boolEnums = booleanEnumIds(shapes);
65
126
  if (boolEnums.size === 0) return { members: 0, lists: 0 };
66
127
 
67
- // Lists of a boolean enum, and whether anything outside a request shape
68
- // refers to them (a shared list keeps its documented string type).
128
+ const requestOnly = requestOnlyShapes(shapes);
69
129
  const boolLists = new Set<string>();
70
130
  for (const [id, shape] of Object.entries(shapes)) {
71
- if (shape?.type === "list" && boolEnums.has(shape.member?.target)) {
131
+ if (shape?.type === "list" && boolEnums.has(shape.member?.target) && requestOnly.has(id)) {
72
132
  boolLists.add(id);
73
133
  }
74
134
  }
75
- const sharedLists = new Set<string>();
76
- for (const shape of Object.values(shapes)) {
77
- if (isRequestShape(shape)) continue;
78
- for (const member of Object.values<any>(shape?.members ?? {})) {
79
- if (boolLists.has(member?.target)) sharedLists.add(member.target);
80
- }
81
- if (boolLists.has(shape?.member?.target)) {
82
- sharedLists.add(shape.member.target);
83
- }
84
- }
85
135
 
86
136
  let members = 0;
87
137
  const retargeted = new Set<string>();
88
- for (const shape of Object.values<any>(shapes)) {
89
- if (!isRequestShape(shape)) continue;
138
+ for (const [id, shape] of Object.entries<any>(shapes)) {
139
+ if (shape?.type !== "structure" || !requestOnly.has(id)) continue;
90
140
  for (const member of Object.values<any>(shape.members ?? {})) {
91
- const isList =
92
- boolLists.has(member?.target) && !sharedLists.has(member.target);
141
+ const isList = boolLists.has(member?.target);
93
142
  if (!boolEnums.has(member?.target) && !isList) continue;
94
143
  if (isList) {
95
144
  retargeted.add(member.target);
@@ -4,26 +4,27 @@
4
4
  * Owns the smithy→SDK pipeline every SDK package shares: scan the model
5
5
  * directory (plus optional hand-authored manual specs), compile each model
6
6
  * through {@link generateService} with the provider's {@link SdkSpec}, write
7
- * the service modules and the namespaced barrel. RFC-6902 patches apply in
8
- * convert so `.generated-specs` is already the patched model.
7
+ * the service modules and the namespaced barrel. Patches are already in
8
+ * `.generated-specs` (convert applies them, see `codegen/patches`).
9
9
  *
10
10
  * A provider's `scripts/generate.ts` is: trait consts + an SdkSpec + a
11
11
  * `runGeneratorCli` call.
12
12
  */
13
- import { BunRuntime, BunServices } from "@effect/platform-bun";
13
+ import { NodeRuntime, NodeServices } from "@effect/platform-node";
14
14
  import { Console, Effect } from "effect";
15
+ import { Flag } from "effect/cli";
16
+ import { Command } from "effect/cli";
15
17
  import * as FileSystem from "effect/FileSystem";
16
18
  import * as Path from "effect/Path";
17
- import { Flag } from "effect/unstable/cli";
18
- import { Command } from "effect/unstable/cli";
19
19
  import { barrel } from "./emit.ts";
20
20
  import { formatGenerated } from "./format.ts";
21
+ import { stampGeneratedAt } from "./generated-at.ts";
21
22
  import { generateService, type SdkSpec } from "./generator.ts";
22
23
 
23
24
  export interface GeneratorCliOptions {
24
25
  /** Command description shown in --help. */
25
26
  readonly description: string;
26
- /** Absolute package root (usually `path.resolve(import.meta.dir, "..")`). */
27
+ /** Absolute package root (usually `path.resolve(import.meta.dirname, "..")`). */
27
28
  readonly root: string;
28
29
  /** Model directory default (relative to root). Default `.generated-specs`. */
29
30
  readonly smithyDir?: string;
@@ -59,10 +60,7 @@ export interface GeneratorCliOptions {
59
60
  * modules after the service's `aws.api#service` sdkId (`amazon-s3.json` →
60
61
  * `s3.ts`), so the public surface reads `AWS.S3.getObject`.
61
62
  */
62
- readonly resourceName?: (ctx: {
63
- readonly model: any;
64
- readonly file: string;
65
- }) => string;
63
+ readonly resourceName?: (ctx: { readonly model: any; readonly file: string }) => string;
66
64
  /** Barrel export name for a resource. Default: the resource name itself. */
67
65
  readonly barrelExportName?: (resource: string) => string;
68
66
  /**
@@ -80,8 +78,9 @@ export interface GeneratorCliOptions {
80
78
  /** Directory of hand-authored models merged after the generated ones. */
81
79
  readonly manualSpecsDir?: string;
82
80
  /**
83
- * RFC-6902 patches apply in convert, never here. Only `false` is accepted
84
- * so a string is a type error rather than a silently ignored setting.
81
+ * Generate never applies patches: convert does, so `.generated-specs` is
82
+ * the patched model. Only `false` is accepted so a path is a type error
83
+ * rather than a silently ignored setting.
85
84
  */
86
85
  readonly patchesDir?: false;
87
86
  /**
@@ -94,7 +93,7 @@ export interface GeneratorCliOptions {
94
93
  readonly spec: (model: any) => SdkSpec;
95
94
  }
96
95
 
97
- /** Run the generator CLI (BunRuntime main — call at module top level). */
96
+ /** Run the generator CLI (NodeRuntime main — call at module top level). */
98
97
  export const runGeneratorCli = (options: GeneratorCliOptions): void => {
99
98
  const command = Command.make(
100
99
  "generate",
@@ -132,10 +131,7 @@ export const runGeneratorCli = (options: GeneratorCliOptions): void => {
132
131
  const generated = options.discoverModels
133
132
  ? yield* options.discoverModels({ smithyDir })
134
133
  : (yield* fs.readDirectory(smithyDir))
135
- .filter(
136
- (f) =>
137
- f.endsWith(".json") && !(options.excludeModel?.(f) ?? false),
138
- )
134
+ .filter((f) => f.endsWith(".json") && !(options.excludeModel?.(f) ?? false))
139
135
  .map((f) => ({ file: f, dir: smithyDir }));
140
136
  const manualDir = options.manualSpecsDir
141
137
  ? path.resolve(root, options.manualSpecsDir)
@@ -155,9 +151,7 @@ export const runGeneratorCli = (options: GeneratorCliOptions): void => {
155
151
  );
156
152
  }
157
153
  }
158
- const entries = [...generated, ...manual].sort((a, b) =>
159
- a.file.localeCompare(b.file),
160
- );
154
+ const entries = [...generated, ...manual].sort((a, b) => a.file.localeCompare(b.file));
161
155
 
162
156
  yield* fs.makeDirectory(outDir, { recursive: true });
163
157
 
@@ -166,14 +160,10 @@ export const runGeneratorCli = (options: GeneratorCliOptions): void => {
166
160
  let totalOps = 0;
167
161
 
168
162
  for (const { file, dir } of entries) {
169
- const model = JSON.parse(
170
- yield* fs.readFileString(path.join(dir, file)),
171
- );
163
+ const model = JSON.parse(yield* fs.readFileString(path.join(dir, file)));
172
164
  // The module's name — the model's filename unless the provider
173
165
  // derives it from the model itself (AWS: the service's sdkId).
174
- const resource =
175
- options.resourceName?.({ model, file }) ??
176
- file.replace(/\.json$/, "");
166
+ const resource = options.resourceName?.({ model, file }) ?? file.replace(/\.json$/, "");
177
167
  if (config.resource && resource !== config.resource) continue;
178
168
 
179
169
  if (options.transformModel) {
@@ -190,9 +180,7 @@ export const runGeneratorCli = (options: GeneratorCliOptions): void => {
190
180
  generated = generateService(model, options.spec(model));
191
181
  } catch (e) {
192
182
  if (!options.continueOnModelError) throw e;
193
- failedModels.push(
194
- `${resource}: ${e instanceof Error ? e.message : String(e)}`,
195
- );
183
+ failedModels.push(`${resource}: ${e instanceof Error ? e.message : String(e)}`);
196
184
  yield* Console.error(`❌ ${resource}`);
197
185
  continue;
198
186
  }
@@ -220,19 +208,14 @@ export const runGeneratorCli = (options: GeneratorCliOptions): void => {
220
208
  // Sorted, not in generation order: a provider that discovers models
221
209
  // in some other order (AWS walks Amazon's directory tree) would
222
210
  // otherwise reshuffle the barrel on every run.
223
- let resources = [...written].sort((a, b) =>
224
- `${a}.json`.localeCompare(`${b}.json`),
225
- );
211
+ let resources = [...written].sort((a, b) => `${a}.json`.localeCompare(`${b}.json`));
226
212
  if (filtered && (yield* fs.exists(barrelPath))) {
227
213
  // Recover the RESOURCE from each export line's path, not its name:
228
214
  // the two differ when barrelExportName renames (AWS exports
229
215
  // `S3` from `./s3.ts`).
230
216
  const existing = (yield* fs.readFileString(barrelPath))
231
217
  .split("\n")
232
- .map(
233
- (line) =>
234
- /^export \* as \S+ from "\.\/(.+)\.ts";/.exec(line)?.[1],
235
- )
218
+ .map((line) => /^export \* as \S+ from "\.\/(.+)\.ts";/.exec(line)?.[1])
236
219
  .filter((name): name is string => name !== undefined);
237
220
  // Ordered exactly as a full run orders it: by MODEL FILENAME, so
238
221
  // the `.json` takes part in the collation (`ai_gateway.json` sorts
@@ -265,21 +248,20 @@ export const runGeneratorCli = (options: GeneratorCliOptions): void => {
265
248
  // at the end, and the run FAILS — a generate that couldn't produce
266
249
  // a module must not exit 0 with the failure buried in the log.
267
250
  if (failedModels.length) {
268
- yield* Console.error(
269
- `\n❌ ${failedModels.length} model(s) failed to generate:`,
270
- );
251
+ yield* Console.error(`\n❌ ${failedModels.length} model(s) failed to generate:`);
271
252
  for (const f of failedModels) yield* Console.error(` ${f}`);
272
- return yield* Effect.die(
273
- new Error(`${failedModels.length} model(s) failed to generate`),
274
- );
253
+ return yield* Effect.die(new Error(`${failedModels.length} model(s) failed to generate`));
275
254
  }
255
+
256
+ const stamp = yield* Effect.sync(() => stampGeneratedAt(root));
257
+ yield* Console.log(` package.json distilled.generatedAt = ${stamp.generatedAt}`);
258
+ yield* Console.log(
259
+ ` package.json distilled.specUpdatedAt = ${stamp.specUpdatedAt ?? "(unknown, unchanged)"}`,
260
+ );
276
261
  }),
277
262
  ).pipe(Command.withDescription(options.description));
278
263
 
279
- BunRuntime.runMain(
280
- Effect.provide(
281
- Command.run(command, { version: "1.0.0" }),
282
- BunServices.layer,
283
- ),
264
+ NodeRuntime.runMain(
265
+ Effect.provide(Command.run(command, { version: "1.0.0" }), NodeServices.layer),
284
266
  );
285
267
  };
@@ -0,0 +1,113 @@
1
+ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { afterEach, describe, expect, test } from "vitest";
5
+ import { convertOpenApiToSmithy, RENAME_METADATA_KEY } from "./openapi.ts";
6
+ import { FINALIZED_KEY, finalizeConvert } from "./patches.ts";
7
+
8
+ const spec = {
9
+ openapi: "3.0.3",
10
+ info: { title: "Apps", version: "1" },
11
+ paths: {
12
+ "/apps": {
13
+ get: {
14
+ operationId: "Apps_list",
15
+ responses: {
16
+ "200": {
17
+ content: {
18
+ "application/json": {
19
+ schema: { type: "array", items: { $ref: "#/components/schemas/App" } },
20
+ },
21
+ },
22
+ },
23
+ },
24
+ },
25
+ },
26
+ "/apps/{id}": {
27
+ get: {
28
+ operationId: "Apps_get",
29
+ parameters: [{ name: "id", in: "path", required: true, schema: { type: "string" } }],
30
+ responses: {
31
+ "200": {
32
+ content: { "application/json": { schema: { $ref: "#/components/schemas/App" } } },
33
+ },
34
+ },
35
+ },
36
+ },
37
+ },
38
+ components: {
39
+ schemas: {
40
+ App: { type: "object", properties: { name: { type: "string" } } },
41
+ // Takes the name the final-named run gives `Apps_list`'s response, so
42
+ // the two runs disagree on which shape gets the `2` suffix.
43
+ ListAppsResponse: { type: "object", properties: { n: { type: "integer" } } },
44
+ },
45
+ },
46
+ };
47
+
48
+ const options = { namespace: "ns", serviceName: "Apps" };
49
+
50
+ const dirs: string[] = [];
51
+ afterEach(() => {
52
+ for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });
53
+ });
54
+
55
+ /** Write `model` as `.generated-specs/apps.json`, with optional patch files. */
56
+ const finalize = async (model: unknown, patches: Record<string, unknown[]> = {}) => {
57
+ const root = mkdtempSync(join(tmpdir(), "defer-naming-"));
58
+ dirs.push(root);
59
+ mkdirSync(join(root, ".generated-specs"));
60
+ writeFileSync(join(root, ".generated-specs", "apps.json"), JSON.stringify(model));
61
+ mkdirSync(join(root, "patches", "apps"), { recursive: true });
62
+ for (const [name, ops] of Object.entries(patches)) {
63
+ writeFileSync(join(root, "patches", "apps", name), JSON.stringify({ patches: ops }));
64
+ }
65
+ await finalizeConvert({ root, operationNaming: "as-is" });
66
+ return JSON.parse(readFileSync(join(root, ".generated-specs", "apps.json"), "utf8"));
67
+ };
68
+
69
+ describe("deferNaming", () => {
70
+ test("names shapes from the spec and records the final names", () => {
71
+ const model = convertOpenApiToSmithy(spec, { ...options, deferNaming: true });
72
+ expect(model.shapes["ns#AppsList"]?.type).toBe("operation");
73
+ expect(model.shapes["ns#ListApps"]).toBeUndefined();
74
+ expect(model.metadata[RENAME_METADATA_KEY]).toMatchObject({
75
+ "ns#AppsList": "ns#ListApps",
76
+ "ns#AppsGet": "ns#GetApp",
77
+ });
78
+ });
79
+
80
+ test("finalize renames to exactly the model converted with final names", async () => {
81
+ const named = convertOpenApiToSmithy(spec, options);
82
+ const deferred = convertOpenApiToSmithy(spec, { ...options, deferNaming: true });
83
+ const finalized = await finalize(deferred);
84
+ expect(finalized).toEqual({
85
+ ...named,
86
+ metadata: { ...named.metadata, [FINALIZED_KEY]: true },
87
+ });
88
+ expect(Object.keys(finalized.shapes)).toEqual(Object.keys(named.shapes));
89
+ });
90
+
91
+ test("a Smithy patch targets the upstream name", async () => {
92
+ const deferred = convertOpenApiToSmithy(spec, { ...options, deferNaming: true });
93
+ const finalized = await finalize(deferred, {
94
+ "doc.json": [
95
+ {
96
+ op: "add",
97
+ path: "/shapes/ns#AppsList/traits/smithy.api#documentation",
98
+ value: "Every app.",
99
+ },
100
+ ],
101
+ });
102
+ expect(finalized.shapes["ns#ListApps"].traits["smithy.api#documentation"]).toBe("Every app.");
103
+ });
104
+
105
+ test("a patch that takes a final name fails the rename", async () => {
106
+ const deferred = convertOpenApiToSmithy(spec, { ...options, deferNaming: true });
107
+ await expect(
108
+ finalize(deferred, {
109
+ "clash.json": [{ op: "add", path: "/shapes/ns#GetApp", value: { type: "structure" } }],
110
+ }),
111
+ ).rejects.toThrow(/already taken/);
112
+ });
113
+ });
@@ -190,14 +190,11 @@ export const errorUnionAlias = (
190
190
  opName: string,
191
191
  errorNames: readonly string[],
192
192
  commonRef: string,
193
- ): string =>
194
- `export type ${opName}Error = ${[...errorNames, commonRef].join(" | ")};`;
193
+ ): string => `export type ${opName}Error = ${[...errorNames, commonRef].join(" | ")};`;
195
194
 
196
195
  /** Namespaced barrel: `export * as name from "./file.ts";` per entry. */
197
196
  export const barrel = (
198
197
  header: string,
199
198
  entries: ReadonlyArray<{ name: string; path: string }>,
200
199
  ): string =>
201
- header +
202
- entries.map((e) => `export * as ${e.name} from ${q(e.path)};`).join("\n") +
203
- "\n";
200
+ header + entries.map((e) => `export * as ${e.name} from ${q(e.path)};`).join("\n") + "\n";