@dual-os/cli 0.0.0-stage → 0.11.0-next.0

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 (254) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +15 -2
  3. package/dist/_virtual/_rolldown/runtime.js +13 -0
  4. package/dist/auth/credentials.d.ts +17 -0
  5. package/dist/auth/credentials.d.ts.map +1 -0
  6. package/dist/auth/credentials.js +57 -0
  7. package/dist/auth/credentials.js.map +1 -0
  8. package/dist/auth/destination.d.ts +22 -0
  9. package/dist/auth/destination.d.ts.map +1 -0
  10. package/dist/auth/destination.js +49 -0
  11. package/dist/auth/destination.js.map +1 -0
  12. package/dist/auth/http.d.ts +12 -0
  13. package/dist/auth/http.d.ts.map +1 -0
  14. package/dist/auth/http.js +34 -0
  15. package/dist/auth/http.js.map +1 -0
  16. package/dist/auth/login.d.ts +22 -0
  17. package/dist/auth/login.d.ts.map +1 -0
  18. package/dist/auth/login.js +98 -0
  19. package/dist/auth/login.js.map +1 -0
  20. package/dist/auth/protocol.d.ts +26 -0
  21. package/dist/auth/protocol.d.ts.map +1 -0
  22. package/dist/auth/protocol.js +31 -0
  23. package/dist/auth/protocol.js.map +1 -0
  24. package/dist/baseline.d.ts +18 -0
  25. package/dist/baseline.d.ts.map +1 -0
  26. package/dist/baseline.js +22 -0
  27. package/dist/baseline.js.map +1 -0
  28. package/dist/bin.d.ts +1 -0
  29. package/dist/bin.js +7 -0
  30. package/dist/bin.js.map +1 -0
  31. package/dist/cli.d.ts +19 -0
  32. package/dist/cli.d.ts.map +1 -0
  33. package/dist/cli.js +492 -0
  34. package/dist/cli.js.map +1 -0
  35. package/dist/create/choices.d.ts +58 -0
  36. package/dist/create/choices.d.ts.map +1 -0
  37. package/dist/create/choices.js +121 -0
  38. package/dist/create/choices.js.map +1 -0
  39. package/dist/create/deployment.d.ts +48 -0
  40. package/dist/create/deployment.d.ts.map +1 -0
  41. package/dist/create/deployment.js +66 -0
  42. package/dist/create/deployment.js.map +1 -0
  43. package/dist/create/scaffold.d.ts +57 -0
  44. package/dist/create/scaffold.d.ts.map +1 -0
  45. package/dist/create/scaffold.js +198 -0
  46. package/dist/create/scaffold.js.map +1 -0
  47. package/dist/create/source-reference.d.ts +39 -0
  48. package/dist/create/source-reference.d.ts.map +1 -0
  49. package/dist/create/source-reference.js +185 -0
  50. package/dist/create/source-reference.js.map +1 -0
  51. package/dist/create/template.d.ts +116 -0
  52. package/dist/create/template.d.ts.map +1 -0
  53. package/dist/create/template.js +211 -0
  54. package/dist/create/template.js.map +1 -0
  55. package/dist/diagnostics.d.ts +6 -0
  56. package/dist/diagnostics.d.ts.map +1 -0
  57. package/dist/diagnostics.js +18 -0
  58. package/dist/diagnostics.js.map +1 -0
  59. package/dist/help.d.ts +16 -0
  60. package/dist/help.d.ts.map +1 -0
  61. package/dist/help.js +73 -0
  62. package/dist/help.js.map +1 -0
  63. package/dist/openapi/bundle.d.ts +18 -0
  64. package/dist/openapi/bundle.d.ts.map +1 -0
  65. package/dist/openapi/bundle.js +201 -0
  66. package/dist/openapi/bundle.js.map +1 -0
  67. package/dist/openapi/discovery.d.ts +30 -0
  68. package/dist/openapi/discovery.d.ts.map +1 -0
  69. package/dist/openapi/discovery.js +325 -0
  70. package/dist/openapi/discovery.js.map +1 -0
  71. package/dist/openapi/emit.d.ts +52 -0
  72. package/dist/openapi/emit.d.ts.map +1 -0
  73. package/dist/openapi/emit.js +596 -0
  74. package/dist/openapi/emit.js.map +1 -0
  75. package/dist/openapi/graphql.d.ts +102 -0
  76. package/dist/openapi/graphql.d.ts.map +1 -0
  77. package/dist/openapi/graphql.js +294 -0
  78. package/dist/openapi/graphql.js.map +1 -0
  79. package/dist/openapi/identity.d.ts +88 -0
  80. package/dist/openapi/identity.d.ts.map +1 -0
  81. package/dist/openapi/identity.js +98 -0
  82. package/dist/openapi/identity.js.map +1 -0
  83. package/dist/openapi/mcp.d.ts +21 -0
  84. package/dist/openapi/mcp.d.ts.map +1 -0
  85. package/dist/openapi/mcp.js +133 -0
  86. package/dist/openapi/mcp.js.map +1 -0
  87. package/dist/openapi/operations.d.ts +165 -0
  88. package/dist/openapi/operations.d.ts.map +1 -0
  89. package/dist/openapi/operations.js +361 -0
  90. package/dist/openapi/operations.js.map +1 -0
  91. package/dist/openapi/overlay.d.ts +23 -0
  92. package/dist/openapi/overlay.d.ts.map +1 -0
  93. package/dist/openapi/overlay.js +115 -0
  94. package/dist/openapi/overlay.js.map +1 -0
  95. package/dist/openapi/plugin.d.ts +83 -0
  96. package/dist/openapi/plugin.d.ts.map +1 -0
  97. package/dist/openapi/plugin.js +468 -0
  98. package/dist/openapi/plugin.js.map +1 -0
  99. package/dist/openapi/polymorphism.d.ts +21 -0
  100. package/dist/openapi/polymorphism.d.ts.map +1 -0
  101. package/dist/openapi/polymorphism.js +96 -0
  102. package/dist/openapi/polymorphism.js.map +1 -0
  103. package/dist/openapi/postman.d.ts +21 -0
  104. package/dist/openapi/postman.d.ts.map +1 -0
  105. package/dist/openapi/postman.js +286 -0
  106. package/dist/openapi/postman.js.map +1 -0
  107. package/dist/openapi/schema.d.ts +178 -0
  108. package/dist/openapi/schema.d.ts.map +1 -0
  109. package/dist/openapi/schema.js +1234 -0
  110. package/dist/openapi/schema.js.map +1 -0
  111. package/dist/openapi/select.d.ts +22 -0
  112. package/dist/openapi/select.d.ts.map +1 -0
  113. package/dist/openapi/select.js +79 -0
  114. package/dist/openapi/select.js.map +1 -0
  115. package/dist/openapi/spec.d.ts +147 -0
  116. package/dist/openapi/spec.d.ts.map +1 -0
  117. package/dist/openapi/spec.js +263 -0
  118. package/dist/openapi/spec.js.map +1 -0
  119. package/dist/openapi/swagger.d.ts +166 -0
  120. package/dist/openapi/swagger.d.ts.map +1 -0
  121. package/dist/openapi/swagger.js +298 -0
  122. package/dist/openapi/swagger.js.map +1 -0
  123. package/dist/openapi/versions.d.ts +52 -0
  124. package/dist/openapi/versions.d.ts.map +1 -0
  125. package/dist/openapi/versions.js +120 -0
  126. package/dist/openapi/versions.js.map +1 -0
  127. package/dist/plugin/archive.d.ts +27 -0
  128. package/dist/plugin/archive.d.ts.map +1 -0
  129. package/dist/plugin/archive.js +170 -0
  130. package/dist/plugin/archive.js.map +1 -0
  131. package/dist/plugin/bundle.d.ts +27 -0
  132. package/dist/plugin/bundle.d.ts.map +1 -0
  133. package/dist/plugin/bundle.js +328 -0
  134. package/dist/plugin/bundle.js.map +1 -0
  135. package/dist/plugin/init.d.ts +18 -0
  136. package/dist/plugin/init.d.ts.map +1 -0
  137. package/dist/plugin/init.js +121 -0
  138. package/dist/plugin/init.js.map +1 -0
  139. package/dist/plugin/inspection.d.ts +24 -0
  140. package/dist/plugin/inspection.d.ts.map +1 -0
  141. package/dist/plugin/inspection.js +24 -0
  142. package/dist/plugin/inspection.js.map +1 -0
  143. package/dist/plugin/package.d.ts +26 -0
  144. package/dist/plugin/package.d.ts.map +1 -0
  145. package/dist/plugin/package.js +73 -0
  146. package/dist/plugin/package.js.map +1 -0
  147. package/dist/plugin/publication.d.ts +29 -0
  148. package/dist/plugin/publication.d.ts.map +1 -0
  149. package/dist/plugin/publication.js +65 -0
  150. package/dist/plugin/publication.js.map +1 -0
  151. package/dist/plugin/validation.d.ts +15 -0
  152. package/dist/plugin/validation.d.ts.map +1 -0
  153. package/dist/plugin/validation.js +119 -0
  154. package/dist/plugin/validation.js.map +1 -0
  155. package/dist/release/archives.d.ts +33 -0
  156. package/dist/release/archives.d.ts.map +1 -0
  157. package/dist/release/archives.js +81 -0
  158. package/dist/release/archives.js.map +1 -0
  159. package/dist/release/release-manifest.d.ts +182 -0
  160. package/dist/release/release-manifest.d.ts.map +1 -0
  161. package/dist/release/release-manifest.js +79 -0
  162. package/dist/release/release-manifest.js.map +1 -0
  163. package/dist/release/releases.d.ts +36 -0
  164. package/dist/release/releases.d.ts.map +1 -0
  165. package/dist/release/releases.js +122 -0
  166. package/dist/release/releases.js.map +1 -0
  167. package/dist/skills.d.ts +35 -0
  168. package/dist/skills.d.ts.map +1 -0
  169. package/dist/skills.js +257 -0
  170. package/dist/skills.js.map +1 -0
  171. package/package.json +73 -4
  172. package/skills/dual-fde/SKILL.md +38 -0
  173. package/skills/dual-fde/references/access.md +62 -0
  174. package/skills/dual-fde/references/author-plugins.md +54 -0
  175. package/skills/dual-fde/references/cli.md +97 -0
  176. package/skills/dual-fde/references/manage-plugins.md +78 -0
  177. package/skills/dual-fde/references/migrations-deployment.md +84 -0
  178. package/skills/dual-fde/references/openapi-plugins.md +47 -0
  179. package/skills/dual-fde/references/sdk-docs.md +56 -0
  180. package/skills/dual-fde/references/upgrades.md +115 -0
  181. package/skills/dual-fde/references/working-context.md +69 -0
  182. package/src/auth/credentials.ts +84 -0
  183. package/src/auth/destination.ts +83 -0
  184. package/src/auth/http.ts +39 -0
  185. package/src/auth/login.ts +177 -0
  186. package/src/auth/protocol.ts +37 -0
  187. package/src/baseline.ts +15 -0
  188. package/src/bin.ts +4 -0
  189. package/src/cli.ts +969 -0
  190. package/src/create/choices.ts +177 -0
  191. package/src/create/deployment.ts +96 -0
  192. package/src/create/scaffold.ts +334 -0
  193. package/src/create/source-reference.ts +212 -0
  194. package/src/create/template.ts +300 -0
  195. package/src/diagnostics.ts +8 -0
  196. package/src/help.ts +142 -0
  197. package/src/openapi/bundle.ts +290 -0
  198. package/src/openapi/discovery.ts +438 -0
  199. package/src/openapi/emit.ts +1031 -0
  200. package/src/openapi/graphql.ts +375 -0
  201. package/src/openapi/identity.ts +153 -0
  202. package/src/openapi/mcp.ts +165 -0
  203. package/src/openapi/operations.ts +781 -0
  204. package/src/openapi/overlay.ts +195 -0
  205. package/src/openapi/plugin.ts +856 -0
  206. package/src/openapi/polymorphism.ts +207 -0
  207. package/src/openapi/postman.ts +403 -0
  208. package/src/openapi/schema.ts +2138 -0
  209. package/src/openapi/select.ts +113 -0
  210. package/src/openapi/spec.ts +401 -0
  211. package/src/openapi/swagger.ts +422 -0
  212. package/src/openapi/versions.ts +174 -0
  213. package/src/plugin/archive.ts +244 -0
  214. package/src/plugin/bundle.ts +572 -0
  215. package/src/plugin/init.ts +178 -0
  216. package/src/plugin/inspection.ts +19 -0
  217. package/src/plugin/package.ts +140 -0
  218. package/src/plugin/publication.ts +128 -0
  219. package/src/plugin/validation.ts +213 -0
  220. package/src/release/archives.ts +133 -0
  221. package/src/release/release-manifest.ts +79 -0
  222. package/src/release/releases.ts +210 -0
  223. package/src/skills.ts +434 -0
  224. package/templates/compose/.env.compose.example.hbs +3 -0
  225. package/templates/compose/.env.host.example.hbs +4 -0
  226. package/templates/compose/Dockerfile.hbs +12 -0
  227. package/templates/compose/OPERATIONS.md.hbs +159 -0
  228. package/templates/compose/_dockerignore +14 -0
  229. package/templates/compose/compose.external.yaml +7 -0
  230. package/templates/compose/compose.host-linux.yaml +5 -0
  231. package/templates/compose/compose.host.yaml.hbs +15 -0
  232. package/templates/compose/compose.yaml.hbs +85 -0
  233. package/templates/compose/release.env.hbs +2 -0
  234. package/templates/default/.env.example.hbs +79 -0
  235. package/templates/default/README.md.hbs +150 -0
  236. package/templates/default/_gitignore +11 -0
  237. package/templates/default/packages/customer-plugin/src/Customer.ts.hbs +17 -0
  238. package/templates/default/packages/customer-plugin/tsconfig.json +15 -0
  239. package/templates/default/packages/execution-layer/src/App.ts.hbs +7 -0
  240. package/templates/default/packages/execution-layer/src/Host.ts.hbs +31 -0
  241. package/templates/default/packages/execution-layer/src/Telemetry.ts.hbs +23 -0
  242. package/templates/default/packages/execution-layer/src/api.ts.hbs +21 -0
  243. package/templates/default/packages/execution-layer/src/bootstrap.ts +22 -0
  244. package/templates/default/packages/execution-layer/src/migrate.ts +11 -0
  245. package/templates/default/packages/execution-layer/src/registry-check.ts +29 -0
  246. package/templates/default/packages/execution-layer/src/registry-sync.ts +14 -0
  247. package/templates/default/packages/execution-layer/src/worker.ts.hbs +16 -0
  248. package/templates/default/packages/execution-layer/test/app.host-test.ts.hbs +58 -0
  249. package/templates/default/packages/execution-layer/test/app.test.ts.hbs +16 -0
  250. package/templates/default/tsconfig.json +5 -0
  251. package/templates/default/vitest.config.ts +5 -0
  252. package/templates/default/vitest.host.config.ts +6 -0
  253. package/templates/opensandbox/OPEN-SANDBOX.md +25 -0
  254. package/templates/opensandbox/docker/agent-sandbox.Dockerfile +41 -0
@@ -0,0 +1,422 @@
1
+ import {
2
+ Array,
3
+ Effect,
4
+ Match,
5
+ Option,
6
+ Predicate,
7
+ Record,
8
+ Result,
9
+ Schema,
10
+ String,
11
+ pipe,
12
+ } from "effect";
13
+
14
+ type JsonObject = typeof JsonObject.Type;
15
+ const JsonObject = Schema.Record(Schema.String, Schema.Unknown);
16
+
17
+ type MediaTypes = typeof MediaTypes.Type;
18
+ const MediaTypes = Schema.Array(Schema.String);
19
+
20
+ type Parameter = typeof Parameter.Type;
21
+ const Parameter = Schema.StructWithRest(
22
+ Schema.Struct({
23
+ collectionFormat: Schema.optionalKey(Schema.String),
24
+ description: Schema.optionalKey(Schema.String),
25
+ in: Schema.String,
26
+ name: Schema.String,
27
+ required: Schema.optionalKey(Schema.Boolean),
28
+ schema: Schema.optionalKey(Schema.Unknown),
29
+ type: Schema.optionalKey(Schema.String),
30
+ }),
31
+ [JsonObject],
32
+ );
33
+
34
+ type Response = typeof Response.Type;
35
+ const Response = Schema.Struct({
36
+ description: Schema.optionalKey(Schema.String),
37
+ headers: Schema.optionalKey(Schema.Record(Schema.String, JsonObject)),
38
+ schema: Schema.optionalKey(Schema.Unknown),
39
+ });
40
+
41
+ type Operation = typeof Operation.Type;
42
+ const Operation = Schema.StructWithRest(
43
+ Schema.Struct({
44
+ consumes: Schema.optionalKey(MediaTypes),
45
+ parameters: Schema.optionalKey(Schema.Array(JsonObject)),
46
+ produces: Schema.optionalKey(MediaTypes),
47
+ responses: Schema.Record(Schema.String, JsonObject),
48
+ }),
49
+ [JsonObject],
50
+ );
51
+
52
+ type SecurityDefinition = typeof SecurityDefinition.Type;
53
+ const SecurityDefinition = Schema.Struct({
54
+ authorizationUrl: Schema.optionalKey(Schema.String),
55
+ description: Schema.optionalKey(Schema.String),
56
+ flow: Schema.optionalKey(Schema.String),
57
+ in: Schema.optionalKey(Schema.String),
58
+ name: Schema.optionalKey(Schema.String),
59
+ scopes: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
60
+ tokenUrl: Schema.optionalKey(Schema.String),
61
+ type: Schema.String,
62
+ });
63
+
64
+ type ParameterizedHost = typeof ParameterizedHost.Type;
65
+ const ParameterizedHost = Schema.Struct({
66
+ hostTemplate: Schema.String,
67
+ parameters: Schema.optionalKey(
68
+ Schema.Array(
69
+ Schema.Struct({
70
+ default: Schema.optionalKey(Schema.String),
71
+ description: Schema.optionalKey(Schema.String),
72
+ enum: Schema.optionalKey(Schema.Array(Schema.String)),
73
+ format: Schema.optionalKey(Schema.String),
74
+ name: Schema.String,
75
+ }),
76
+ ),
77
+ ),
78
+ useSchemePrefix: Schema.optionalKey(Schema.Boolean),
79
+ });
80
+
81
+ type Swagger = typeof Swagger.Type;
82
+ const Swagger = Schema.StructWithRest(
83
+ Schema.Struct({
84
+ basePath: Schema.optionalKey(Schema.String),
85
+ consumes: Schema.optionalKey(MediaTypes),
86
+ definitions: Schema.optionalKey(JsonObject),
87
+ host: Schema.optionalKey(Schema.String),
88
+ parameters: Schema.optionalKey(Schema.Record(Schema.String, JsonObject)),
89
+ paths: Schema.Record(Schema.String, JsonObject),
90
+ produces: Schema.optionalKey(MediaTypes),
91
+ responses: Schema.optionalKey(Schema.Record(Schema.String, JsonObject)),
92
+ schemes: Schema.optionalKey(Schema.Array(Schema.String)),
93
+ securityDefinitions: Schema.optionalKey(Schema.Record(Schema.String, SecurityDefinition)),
94
+ swagger: Schema.Literal("2.0"),
95
+ "x-ms-parameterized-host": Schema.optionalKey(ParameterizedHost),
96
+ }),
97
+ [JsonObject],
98
+ );
99
+
100
+ const methods = ["delete", "get", "head", "options", "patch", "post", "put"];
101
+ const formTypes = ["application/x-www-form-urlencoded", "multipart/form-data"];
102
+ const schemaKeys = [
103
+ "default",
104
+ "enum",
105
+ "exclusiveMaximum",
106
+ "exclusiveMinimum",
107
+ "format",
108
+ "items",
109
+ "maxItems",
110
+ "maxLength",
111
+ "maximum",
112
+ "minItems",
113
+ "minLength",
114
+ "minimum",
115
+ "multipleOf",
116
+ "pattern",
117
+ "type",
118
+ "uniqueItems",
119
+ ];
120
+
121
+ function schema(value: unknown): unknown {
122
+ if (Array.isArray(value)) return Array.map(value, schema);
123
+ return Predicate.isObject(value) ? schemaObject(value) : value;
124
+ }
125
+
126
+ function schemaObject(value: JsonObject): JsonObject {
127
+ const converted = Record.map(Record.remove(value, "x-nullable"), (entry, key) => {
128
+ if (!Predicate.isString(entry)) return schema(entry);
129
+ if (key === "$ref") return String.replace(/^#\/definitions\//u, "#/components/schemas/")(entry);
130
+ return key === "discriminator" ? { propertyName: entry } : entry;
131
+ });
132
+ const base: JsonObject = {
133
+ ...converted,
134
+ ...(value["x-nullable"] === true ? { nullable: true } : {}),
135
+ ...(value["type"] === "file" ? { format: "binary", type: "string" } : {}),
136
+ };
137
+ if (!Array.isArray(converted["items"])) return base;
138
+ return value["type"] === "array"
139
+ ? { ...base, items: { anyOf: converted["items"] } }
140
+ : { ...Record.remove(base, "items"), anyOf: converted["items"] };
141
+ }
142
+
143
+ function parameterSchema(parameter: Parameter) {
144
+ return schemaObject(Record.filter(parameter, (_, key) => Array.contains(schemaKeys, key)));
145
+ }
146
+
147
+ function described(description?: string) {
148
+ return description === undefined ? {} : { description };
149
+ }
150
+
151
+ function mediaTypes(declared: MediaTypes, fallback: string) {
152
+ return Array.isReadonlyArrayNonEmpty(declared) ? declared : [fallback];
153
+ }
154
+
155
+ function requestBody(parameters: Parameter[], consumes: MediaTypes) {
156
+ const body = Option.getOrUndefined(
157
+ Array.findFirst(parameters, parameter => parameter.in === "body"),
158
+ );
159
+ if (body !== undefined) {
160
+ return {
161
+ content: Record.fromIterableWith(
162
+ mediaTypes(
163
+ Array.filter(consumes, type => !Array.contains(formTypes, type)),
164
+ "application/json",
165
+ ),
166
+ type => [type, { schema: schema(body.schema) }],
167
+ ),
168
+ ...described(body.description),
169
+ required: body.required === true,
170
+ };
171
+ }
172
+
173
+ const fields = Array.filter(parameters, parameter => parameter.in === "formData");
174
+ if (Array.isReadonlyArrayEmpty(fields)) return undefined;
175
+ const fallback = Array.some(fields, field => field.type === "file")
176
+ ? "multipart/form-data"
177
+ : "application/x-www-form-urlencoded";
178
+ const properties = Record.fromIterableWith(fields, field => [
179
+ field.name,
180
+ { ...parameterSchema(field), ...described(field.description) },
181
+ ]);
182
+ const encoding = Record.fromIterableWith(
183
+ Array.filter(fields, field => field.type === "array"),
184
+ field => [
185
+ field.name,
186
+ field.collectionFormat === "tsv"
187
+ ? { "x-dual-delimiter": "\t" }
188
+ : collection(field.collectionFormat ?? "csv"),
189
+ ],
190
+ );
191
+ return {
192
+ content: Record.fromIterableWith(
193
+ mediaTypes(
194
+ Array.filter(consumes, type => Array.contains(formTypes, type)),
195
+ fallback,
196
+ ),
197
+ type => [
198
+ type,
199
+ {
200
+ encoding,
201
+ schema: {
202
+ properties,
203
+ required: Array.filterMap(fields, field =>
204
+ field.required === true ? Result.succeed(field.name) : Result.failVoid,
205
+ ),
206
+ type: "object",
207
+ },
208
+ },
209
+ ],
210
+ ),
211
+ required: Array.some(fields, field => field.required === true),
212
+ };
213
+ }
214
+
215
+ function collection(format: string) {
216
+ if (format === "multi") return { explode: true, style: "form" };
217
+ if (format === "csv") return { explode: false, style: "form" };
218
+ if (format === "ssv") return { explode: false, style: "spaceDelimited" };
219
+ if (format === "pipes") return { explode: false, style: "pipeDelimited" };
220
+ return { explode: false, style: format };
221
+ }
222
+
223
+ function openApiParameter(value: Parameter) {
224
+ return {
225
+ ...described(value.description),
226
+ ...(value.type === "array" ? collection(value.collectionFormat ?? "csv") : {}),
227
+ in: value.in,
228
+ name: value.name,
229
+ required: value.in === "path" || value.required === true,
230
+ schema: parameterSchema(value),
231
+ };
232
+ }
233
+
234
+ function response(value: Response, produces: MediaTypes) {
235
+ return {
236
+ description: value.description ?? "",
237
+ ...(value.headers === undefined
238
+ ? {}
239
+ : {
240
+ headers: Record.map(value.headers, header => ({
241
+ ...described(
242
+ Predicate.isString(header["description"]) ? header["description"] : undefined,
243
+ ),
244
+ schema: schemaObject(Record.remove(header, "description")),
245
+ })),
246
+ }),
247
+ ...(value.schema === undefined
248
+ ? {}
249
+ : {
250
+ content: Record.fromIterableWith(mediaTypes(produces, "application/json"), type => [
251
+ type,
252
+ { schema: schema(value.schema) },
253
+ ]),
254
+ }),
255
+ };
256
+ }
257
+
258
+ function securityScheme(definition: SecurityDefinition) {
259
+ const scopes = definition.scopes ?? {};
260
+ return {
261
+ ...described(definition.description),
262
+ ...pipe(
263
+ Match.value(definition.type),
264
+ Match.when("basic", () => ({ scheme: "basic", type: "http" })),
265
+ Match.when("apiKey", () => ({ in: definition.in, name: definition.name, type: "apiKey" })),
266
+ Match.orElse(() => ({
267
+ flows: pipe(
268
+ Match.value(definition.flow),
269
+ Match.when("accessCode", () => ({
270
+ authorizationCode: {
271
+ authorizationUrl: definition.authorizationUrl,
272
+ scopes,
273
+ tokenUrl: definition.tokenUrl,
274
+ },
275
+ })),
276
+ Match.when("application", () => ({
277
+ clientCredentials: { scopes, tokenUrl: definition.tokenUrl },
278
+ })),
279
+ Match.when("password", () => ({ password: { scopes, tokenUrl: definition.tokenUrl } })),
280
+ Match.orElse(() => ({
281
+ implicit: { authorizationUrl: definition.authorizationUrl, scopes },
282
+ })),
283
+ ),
284
+ type: definition.type,
285
+ })),
286
+ ),
287
+ };
288
+ }
289
+
290
+ function server(document: Swagger) {
291
+ const scheme = pipe(
292
+ document.schemes ?? [],
293
+ Array.head,
294
+ Option.getOrElse(() => "https"),
295
+ );
296
+ const host = document["x-ms-parameterized-host"];
297
+ if (host === undefined) {
298
+ return {
299
+ url:
300
+ document.host === undefined
301
+ ? (document.basePath ?? "/")
302
+ : `${scheme}://${document.host}${document.basePath ?? ""}`,
303
+ };
304
+ }
305
+ return {
306
+ url: `${host.useSchemePrefix === false ? "" : `${scheme}://`}${host.hostTemplate}${document.basePath ?? ""}`,
307
+ variables: Record.fromIterableWith(host.parameters ?? [], parameter => [
308
+ parameter.name,
309
+ {
310
+ default:
311
+ parameter.default ??
312
+ (parameter.format === "uri" ? `https://${parameter.name}.invalid` : parameter.name),
313
+ ...described(parameter.description),
314
+ ...(parameter.enum === undefined ? {} : { enum: parameter.enum }),
315
+ },
316
+ ]),
317
+ };
318
+ }
319
+
320
+ const operation = Effect.fnUntraced(function* (
321
+ document: Swagger,
322
+ shared: JsonObject[],
323
+ value: unknown,
324
+ ) {
325
+ const decoded = yield* Schema.decodeUnknownEffect(Operation)(value);
326
+ const declared = yield* Effect.forEach([...shared, ...(decoded.parameters ?? [])], entry =>
327
+ Schema.decodeUnknownEffect(Parameter)(
328
+ Predicate.isString(entry["$ref"])
329
+ ? document.parameters?.[String.replace(/^#\/parameters\//u, "")(entry["$ref"])]
330
+ : entry,
331
+ ),
332
+ );
333
+ const parameters = Array.filter(
334
+ declared,
335
+ (entry, index) =>
336
+ !Array.some(
337
+ Array.drop(declared, index + 1),
338
+ later => later.name === entry.name && later.in === entry.in,
339
+ ),
340
+ );
341
+ const produces = decoded.produces ?? document.produces ?? [];
342
+ const body = requestBody(parameters, decoded.consumes ?? document.consumes ?? []);
343
+ function referenced(listed: JsonObject) {
344
+ if (!Predicate.isString(listed["$ref"])) return listed;
345
+ if (pipe(listed["$ref"], String.startsWith("#/responses/"))) {
346
+ return document.responses?.[String.replace(/^#\/responses\//u, "")(listed["$ref"])];
347
+ }
348
+ return { schema: { $ref: listed["$ref"] } };
349
+ }
350
+ const responses = yield* Effect.forEach(Record.toEntries(decoded.responses), entry =>
351
+ Effect.map(
352
+ Schema.decodeUnknownEffect(Response)(referenced(entry[1])),
353
+ decodedResponse => [entry[0], response(decodedResponse, produces)] as const,
354
+ ),
355
+ );
356
+ return {
357
+ ...Record.filter(
358
+ decoded,
359
+ (_, key) => !Array.contains(["consumes", "parameters", "produces", "responses"], key),
360
+ ),
361
+ parameters: pipe(
362
+ parameters,
363
+ Array.filter(entry => entry.in !== "body" && entry.in !== "formData"),
364
+ Array.map(openApiParameter),
365
+ ),
366
+ ...(body === undefined ? {} : { requestBody: body }),
367
+ responses: Record.fromEntries(responses),
368
+ };
369
+ });
370
+
371
+ export const toOpenApi = Effect.fn("OpenApiSwagger.toOpenApi")(function* (input: unknown) {
372
+ const document = yield* Schema.decodeUnknownEffect(Swagger)(input);
373
+ const paths = yield* Effect.forEach(Record.toEntries(document.paths), entry => {
374
+ const shared = Array.filter(
375
+ Array.isArray(entry[1]["parameters"]) ? entry[1]["parameters"] : [],
376
+ Predicate.isObject,
377
+ );
378
+ return Effect.map(
379
+ Effect.forEach(
380
+ Array.filter(Record.toEntries(entry[1]), item => Array.contains(methods, item[0])),
381
+ item =>
382
+ Effect.map(
383
+ operation(document, shared, item[1]),
384
+ converted => [item[0], converted] as const,
385
+ ),
386
+ ),
387
+ operations => [entry[0], Record.fromEntries(operations)] as const,
388
+ );
389
+ });
390
+ return {
391
+ ...Record.filter(
392
+ document,
393
+ (_, key) =>
394
+ !Array.contains(
395
+ [
396
+ "basePath",
397
+ "consumes",
398
+ "definitions",
399
+ "host",
400
+ "parameters",
401
+ "paths",
402
+ "produces",
403
+ "responses",
404
+ "schemes",
405
+ "securityDefinitions",
406
+ "swagger",
407
+ "x-ms-parameterized-host",
408
+ ],
409
+ key,
410
+ ),
411
+ ),
412
+ components: {
413
+ schemas: Record.map(document.definitions ?? {}, schema),
414
+ securitySchemes: Record.map(document.securityDefinitions ?? {}, securityScheme),
415
+ },
416
+ openapi: "3.0.3",
417
+ paths: Record.fromEntries(paths),
418
+ servers: [server(document)],
419
+ };
420
+ });
421
+
422
+ export * as OpenApiSwagger from "./swagger.ts";
@@ -0,0 +1,174 @@
1
+ import {
2
+ Array,
3
+ Effect,
4
+ HashMap,
5
+ Match,
6
+ MutableList,
7
+ Option,
8
+ Order,
9
+ Record,
10
+ Result,
11
+ Schema,
12
+ Struct,
13
+ pipe,
14
+ } from "effect";
15
+
16
+ import { OpenApiIdentity } from "./identity.ts";
17
+ import { OpenApiSchema } from "./schema.ts";
18
+
19
+ export type LockFile = typeof LockFile.Type;
20
+ export const LockFile = Schema.fromJsonString(
21
+ Schema.Struct({
22
+ actions: Schema.Record(Schema.String, Schema.Struct({ hash: Schema.String })),
23
+ identities: OpenApiIdentity.Ledger,
24
+ }),
25
+ );
26
+
27
+ const StoredLockFile = Schema.fromJsonString(
28
+ Schema.Struct({
29
+ actions: Schema.Record(
30
+ Schema.String,
31
+ Schema.Union([
32
+ Schema.Struct({ hash: Schema.String }),
33
+ Schema.NonEmptyArray(
34
+ Schema.Union([
35
+ Schema.Struct({ hash: Schema.String, version: Schema.String }),
36
+ Schema.Struct({ removed: Schema.Literal(true), version: Schema.String }),
37
+ ]),
38
+ ),
39
+ ]),
40
+ ),
41
+ identities: OpenApiIdentity.Ledger,
42
+ }),
43
+ );
44
+
45
+ /** Read the previous generator's ledger once, then write only current contract hashes. */
46
+ export const decodeLock = Effect.fn("OpenApiVersions.decodeLock")(function* (text: string) {
47
+ const stored = yield* Schema.decodeEffect(StoredLockFile)(text);
48
+ return {
49
+ actions: Record.filterMap(stored.actions, entry => {
50
+ const current = "hash" in entry ? entry : Array.lastNonEmpty(entry);
51
+ return "hash" in current ? Result.succeed({ hash: current.hash }) : Result.failVoid;
52
+ }),
53
+ identities: stored.identities,
54
+ };
55
+ });
56
+
57
+ /** A JSON value for a node that ignores descriptions, field order and inlined spreads. */
58
+ function canonical(
59
+ node: OpenApiSchema.Node,
60
+ spreads: (name: string) => readonly OpenApiSchema.Field[],
61
+ ref: (target: string) => unknown,
62
+ ): unknown {
63
+ const checks = node.checks === undefined ? {} : { checks: node.checks };
64
+ return pipe(
65
+ Match.value(node),
66
+ Match.tag("Ref", current => ref(current.target)),
67
+ Match.tag("Array", current => ({ array: canonical(current.item, spreads, ref), ...checks })),
68
+ Match.tag("Record", current => ({ record: canonical(current.value, spreads, ref), ...checks })),
69
+ Match.tag("NullOr", current => ({ nullOr: canonical(current.node, spreads, ref) })),
70
+ Match.tag("Literals", current => ({
71
+ literals: Array.sort(
72
+ Array.map(current.literals, value => JSON.stringify(value)),
73
+ Order.String,
74
+ ),
75
+ })),
76
+ Match.tag("Union", current => ({
77
+ union: pipe(
78
+ Array.map(current.members, member => canonical(member, spreads, ref)),
79
+ Array.sort(Order.mapInput(Order.String, (member: unknown) => JSON.stringify(member))),
80
+ ),
81
+ ...(current.preserve === true ? { preserve: true } : {}),
82
+ ...(current.exclusive === true ? { exclusive: true } : {}),
83
+ })),
84
+ Match.tag("Struct", current => {
85
+ const fields = Record.fromEntries(
86
+ Array.map(
87
+ [...Array.flatMap(current.spreads, spreads), ...current.fields],
88
+ field => [field.name, field] as const,
89
+ ),
90
+ );
91
+ return {
92
+ fields: Array.map(Array.sort(Record.keys(fields), Order.String), name => {
93
+ const field = Option.getOrThrow(Record.get(fields, name));
94
+ return [name, field.optional, field.default ?? null, canonical(field.node, spreads, ref)];
95
+ }),
96
+ rest: current.rest,
97
+ ...(current.excluded === undefined
98
+ ? {}
99
+ : { excluded: Array.sort(current.excluded, Order.String) }),
100
+ ...(current.additionalProperties === undefined
101
+ ? {}
102
+ : { additional: canonical(current.additionalProperties, spreads, ref) }),
103
+ ...checks,
104
+ };
105
+ }),
106
+ Match.tag("Bytes", current => ({ bytes: current.alphabet, direction: current.direction })),
107
+ Match.tag("Instant", current => ({ direction: current.direction, instant: current.clock })),
108
+ Match.orElse(current => ({
109
+ type: current._tag,
110
+ ...checks,
111
+ ...(current.format === undefined ? {} : { format: current.format }),
112
+ })),
113
+ );
114
+ }
115
+
116
+ /**
117
+ * Content hashes of each action's HTTP request and input/output schemas. Descriptions, key order
118
+ * and schema names do not count, so hashes describe only what is sent or accepted.
119
+ */
120
+ export function hashes(catalogued: {
121
+ readonly contracts: readonly OpenApiSchema.Contract[];
122
+ readonly declarations: readonly OpenApiSchema.Declaration[];
123
+ }) {
124
+ const nodes = HashMap.fromIterable(
125
+ Array.map(
126
+ catalogued.declarations,
127
+ declaration => [declaration.name, declaration.node] as const,
128
+ ),
129
+ );
130
+ function spreads(name: string): readonly OpenApiSchema.Field[] {
131
+ const node = HashMap.getUnsafe(nodes, name);
132
+ return node._tag === "Struct" ? [...Array.flatMap(node.spreads, spreads), ...node.fields] : [];
133
+ }
134
+ function hashable(declaration: OpenApiSchema.Declaration) {
135
+ const refs = MutableList.make<string>();
136
+ const content = JSON.stringify(
137
+ canonical(declaration.node, spreads, target => {
138
+ MutableList.append(refs, target);
139
+ return "#";
140
+ }),
141
+ );
142
+ return { content, key: declaration.name, refs: MutableList.toArray(refs) };
143
+ }
144
+ const identity = OpenApiSchema.identities(Array.map(catalogued.declarations, hashable));
145
+ function ref(target: string) {
146
+ return { ref: HashMap.getUnsafe(identity, target) };
147
+ }
148
+ return Array.map(catalogued.contracts, contract =>
149
+ OpenApiSchema.hash(
150
+ JSON.stringify(
151
+ {
152
+ input: canonical(contract.input, spreads, ref),
153
+ output: canonical(contract.output, spreads, ref),
154
+ // Parameter order in the source is not a change.
155
+ request: {
156
+ ...Struct.omit(contract, ["input", "output"]),
157
+ commaSeparated: Array.sort(contract.commaSeparated, Order.String),
158
+ ...(contract.exploded === undefined
159
+ ? {}
160
+ : { exploded: Array.sort(contract.exploded, Order.String) }),
161
+ ...(contract.deepObject === undefined
162
+ ? {}
163
+ : { deepObject: Array.sort(contract.deepObject, Order.String) }),
164
+ headers: Array.sort(contract.headers, Order.String),
165
+ query: Array.sort(contract.query, Order.String),
166
+ },
167
+ },
168
+ (key, value: unknown) => (key === "schema" ? undefined : value),
169
+ ),
170
+ ),
171
+ );
172
+ }
173
+
174
+ export * as OpenApiVersions from "./versions.ts";