@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.
- package/LICENSE +4 -0
- package/README.md +15 -2
- package/dist/_virtual/_rolldown/runtime.js +13 -0
- package/dist/auth/credentials.d.ts +17 -0
- package/dist/auth/credentials.d.ts.map +1 -0
- package/dist/auth/credentials.js +57 -0
- package/dist/auth/credentials.js.map +1 -0
- package/dist/auth/destination.d.ts +22 -0
- package/dist/auth/destination.d.ts.map +1 -0
- package/dist/auth/destination.js +49 -0
- package/dist/auth/destination.js.map +1 -0
- package/dist/auth/http.d.ts +12 -0
- package/dist/auth/http.d.ts.map +1 -0
- package/dist/auth/http.js +34 -0
- package/dist/auth/http.js.map +1 -0
- package/dist/auth/login.d.ts +22 -0
- package/dist/auth/login.d.ts.map +1 -0
- package/dist/auth/login.js +98 -0
- package/dist/auth/login.js.map +1 -0
- package/dist/auth/protocol.d.ts +26 -0
- package/dist/auth/protocol.d.ts.map +1 -0
- package/dist/auth/protocol.js +31 -0
- package/dist/auth/protocol.js.map +1 -0
- package/dist/baseline.d.ts +18 -0
- package/dist/baseline.d.ts.map +1 -0
- package/dist/baseline.js +22 -0
- package/dist/baseline.js.map +1 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +7 -0
- package/dist/bin.js.map +1 -0
- package/dist/cli.d.ts +19 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +492 -0
- package/dist/cli.js.map +1 -0
- package/dist/create/choices.d.ts +58 -0
- package/dist/create/choices.d.ts.map +1 -0
- package/dist/create/choices.js +121 -0
- package/dist/create/choices.js.map +1 -0
- package/dist/create/deployment.d.ts +48 -0
- package/dist/create/deployment.d.ts.map +1 -0
- package/dist/create/deployment.js +66 -0
- package/dist/create/deployment.js.map +1 -0
- package/dist/create/scaffold.d.ts +57 -0
- package/dist/create/scaffold.d.ts.map +1 -0
- package/dist/create/scaffold.js +198 -0
- package/dist/create/scaffold.js.map +1 -0
- package/dist/create/source-reference.d.ts +39 -0
- package/dist/create/source-reference.d.ts.map +1 -0
- package/dist/create/source-reference.js +185 -0
- package/dist/create/source-reference.js.map +1 -0
- package/dist/create/template.d.ts +116 -0
- package/dist/create/template.d.ts.map +1 -0
- package/dist/create/template.js +211 -0
- package/dist/create/template.js.map +1 -0
- package/dist/diagnostics.d.ts +6 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +18 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/help.d.ts +16 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +73 -0
- package/dist/help.js.map +1 -0
- package/dist/openapi/bundle.d.ts +18 -0
- package/dist/openapi/bundle.d.ts.map +1 -0
- package/dist/openapi/bundle.js +201 -0
- package/dist/openapi/bundle.js.map +1 -0
- package/dist/openapi/discovery.d.ts +30 -0
- package/dist/openapi/discovery.d.ts.map +1 -0
- package/dist/openapi/discovery.js +325 -0
- package/dist/openapi/discovery.js.map +1 -0
- package/dist/openapi/emit.d.ts +52 -0
- package/dist/openapi/emit.d.ts.map +1 -0
- package/dist/openapi/emit.js +596 -0
- package/dist/openapi/emit.js.map +1 -0
- package/dist/openapi/graphql.d.ts +102 -0
- package/dist/openapi/graphql.d.ts.map +1 -0
- package/dist/openapi/graphql.js +294 -0
- package/dist/openapi/graphql.js.map +1 -0
- package/dist/openapi/identity.d.ts +88 -0
- package/dist/openapi/identity.d.ts.map +1 -0
- package/dist/openapi/identity.js +98 -0
- package/dist/openapi/identity.js.map +1 -0
- package/dist/openapi/mcp.d.ts +21 -0
- package/dist/openapi/mcp.d.ts.map +1 -0
- package/dist/openapi/mcp.js +133 -0
- package/dist/openapi/mcp.js.map +1 -0
- package/dist/openapi/operations.d.ts +165 -0
- package/dist/openapi/operations.d.ts.map +1 -0
- package/dist/openapi/operations.js +361 -0
- package/dist/openapi/operations.js.map +1 -0
- package/dist/openapi/overlay.d.ts +23 -0
- package/dist/openapi/overlay.d.ts.map +1 -0
- package/dist/openapi/overlay.js +115 -0
- package/dist/openapi/overlay.js.map +1 -0
- package/dist/openapi/plugin.d.ts +83 -0
- package/dist/openapi/plugin.d.ts.map +1 -0
- package/dist/openapi/plugin.js +468 -0
- package/dist/openapi/plugin.js.map +1 -0
- package/dist/openapi/polymorphism.d.ts +21 -0
- package/dist/openapi/polymorphism.d.ts.map +1 -0
- package/dist/openapi/polymorphism.js +96 -0
- package/dist/openapi/polymorphism.js.map +1 -0
- package/dist/openapi/postman.d.ts +21 -0
- package/dist/openapi/postman.d.ts.map +1 -0
- package/dist/openapi/postman.js +286 -0
- package/dist/openapi/postman.js.map +1 -0
- package/dist/openapi/schema.d.ts +178 -0
- package/dist/openapi/schema.d.ts.map +1 -0
- package/dist/openapi/schema.js +1234 -0
- package/dist/openapi/schema.js.map +1 -0
- package/dist/openapi/select.d.ts +22 -0
- package/dist/openapi/select.d.ts.map +1 -0
- package/dist/openapi/select.js +79 -0
- package/dist/openapi/select.js.map +1 -0
- package/dist/openapi/spec.d.ts +147 -0
- package/dist/openapi/spec.d.ts.map +1 -0
- package/dist/openapi/spec.js +263 -0
- package/dist/openapi/spec.js.map +1 -0
- package/dist/openapi/swagger.d.ts +166 -0
- package/dist/openapi/swagger.d.ts.map +1 -0
- package/dist/openapi/swagger.js +298 -0
- package/dist/openapi/swagger.js.map +1 -0
- package/dist/openapi/versions.d.ts +52 -0
- package/dist/openapi/versions.d.ts.map +1 -0
- package/dist/openapi/versions.js +120 -0
- package/dist/openapi/versions.js.map +1 -0
- package/dist/plugin/archive.d.ts +27 -0
- package/dist/plugin/archive.d.ts.map +1 -0
- package/dist/plugin/archive.js +170 -0
- package/dist/plugin/archive.js.map +1 -0
- package/dist/plugin/bundle.d.ts +27 -0
- package/dist/plugin/bundle.d.ts.map +1 -0
- package/dist/plugin/bundle.js +328 -0
- package/dist/plugin/bundle.js.map +1 -0
- package/dist/plugin/init.d.ts +18 -0
- package/dist/plugin/init.d.ts.map +1 -0
- package/dist/plugin/init.js +121 -0
- package/dist/plugin/init.js.map +1 -0
- package/dist/plugin/inspection.d.ts +24 -0
- package/dist/plugin/inspection.d.ts.map +1 -0
- package/dist/plugin/inspection.js +24 -0
- package/dist/plugin/inspection.js.map +1 -0
- package/dist/plugin/package.d.ts +26 -0
- package/dist/plugin/package.d.ts.map +1 -0
- package/dist/plugin/package.js +73 -0
- package/dist/plugin/package.js.map +1 -0
- package/dist/plugin/publication.d.ts +29 -0
- package/dist/plugin/publication.d.ts.map +1 -0
- package/dist/plugin/publication.js +65 -0
- package/dist/plugin/publication.js.map +1 -0
- package/dist/plugin/validation.d.ts +15 -0
- package/dist/plugin/validation.d.ts.map +1 -0
- package/dist/plugin/validation.js +119 -0
- package/dist/plugin/validation.js.map +1 -0
- package/dist/release/archives.d.ts +33 -0
- package/dist/release/archives.d.ts.map +1 -0
- package/dist/release/archives.js +81 -0
- package/dist/release/archives.js.map +1 -0
- package/dist/release/release-manifest.d.ts +182 -0
- package/dist/release/release-manifest.d.ts.map +1 -0
- package/dist/release/release-manifest.js +79 -0
- package/dist/release/release-manifest.js.map +1 -0
- package/dist/release/releases.d.ts +36 -0
- package/dist/release/releases.d.ts.map +1 -0
- package/dist/release/releases.js +122 -0
- package/dist/release/releases.js.map +1 -0
- package/dist/skills.d.ts +35 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +257 -0
- package/dist/skills.js.map +1 -0
- package/package.json +73 -4
- package/skills/dual-fde/SKILL.md +38 -0
- package/skills/dual-fde/references/access.md +62 -0
- package/skills/dual-fde/references/author-plugins.md +54 -0
- package/skills/dual-fde/references/cli.md +97 -0
- package/skills/dual-fde/references/manage-plugins.md +78 -0
- package/skills/dual-fde/references/migrations-deployment.md +84 -0
- package/skills/dual-fde/references/openapi-plugins.md +47 -0
- package/skills/dual-fde/references/sdk-docs.md +56 -0
- package/skills/dual-fde/references/upgrades.md +115 -0
- package/skills/dual-fde/references/working-context.md +69 -0
- package/src/auth/credentials.ts +84 -0
- package/src/auth/destination.ts +83 -0
- package/src/auth/http.ts +39 -0
- package/src/auth/login.ts +177 -0
- package/src/auth/protocol.ts +37 -0
- package/src/baseline.ts +15 -0
- package/src/bin.ts +4 -0
- package/src/cli.ts +969 -0
- package/src/create/choices.ts +177 -0
- package/src/create/deployment.ts +96 -0
- package/src/create/scaffold.ts +334 -0
- package/src/create/source-reference.ts +212 -0
- package/src/create/template.ts +300 -0
- package/src/diagnostics.ts +8 -0
- package/src/help.ts +142 -0
- package/src/openapi/bundle.ts +290 -0
- package/src/openapi/discovery.ts +438 -0
- package/src/openapi/emit.ts +1031 -0
- package/src/openapi/graphql.ts +375 -0
- package/src/openapi/identity.ts +153 -0
- package/src/openapi/mcp.ts +165 -0
- package/src/openapi/operations.ts +781 -0
- package/src/openapi/overlay.ts +195 -0
- package/src/openapi/plugin.ts +856 -0
- package/src/openapi/polymorphism.ts +207 -0
- package/src/openapi/postman.ts +403 -0
- package/src/openapi/schema.ts +2138 -0
- package/src/openapi/select.ts +113 -0
- package/src/openapi/spec.ts +401 -0
- package/src/openapi/swagger.ts +422 -0
- package/src/openapi/versions.ts +174 -0
- package/src/plugin/archive.ts +244 -0
- package/src/plugin/bundle.ts +572 -0
- package/src/plugin/init.ts +178 -0
- package/src/plugin/inspection.ts +19 -0
- package/src/plugin/package.ts +140 -0
- package/src/plugin/publication.ts +128 -0
- package/src/plugin/validation.ts +213 -0
- package/src/release/archives.ts +133 -0
- package/src/release/release-manifest.ts +79 -0
- package/src/release/releases.ts +210 -0
- package/src/skills.ts +434 -0
- package/templates/compose/.env.compose.example.hbs +3 -0
- package/templates/compose/.env.host.example.hbs +4 -0
- package/templates/compose/Dockerfile.hbs +12 -0
- package/templates/compose/OPERATIONS.md.hbs +159 -0
- package/templates/compose/_dockerignore +14 -0
- package/templates/compose/compose.external.yaml +7 -0
- package/templates/compose/compose.host-linux.yaml +5 -0
- package/templates/compose/compose.host.yaml.hbs +15 -0
- package/templates/compose/compose.yaml.hbs +85 -0
- package/templates/compose/release.env.hbs +2 -0
- package/templates/default/.env.example.hbs +79 -0
- package/templates/default/README.md.hbs +150 -0
- package/templates/default/_gitignore +11 -0
- package/templates/default/packages/customer-plugin/src/Customer.ts.hbs +17 -0
- package/templates/default/packages/customer-plugin/tsconfig.json +15 -0
- package/templates/default/packages/execution-layer/src/App.ts.hbs +7 -0
- package/templates/default/packages/execution-layer/src/Host.ts.hbs +31 -0
- package/templates/default/packages/execution-layer/src/Telemetry.ts.hbs +23 -0
- package/templates/default/packages/execution-layer/src/api.ts.hbs +21 -0
- package/templates/default/packages/execution-layer/src/bootstrap.ts +22 -0
- package/templates/default/packages/execution-layer/src/migrate.ts +11 -0
- package/templates/default/packages/execution-layer/src/registry-check.ts +29 -0
- package/templates/default/packages/execution-layer/src/registry-sync.ts +14 -0
- package/templates/default/packages/execution-layer/src/worker.ts.hbs +16 -0
- package/templates/default/packages/execution-layer/test/app.host-test.ts.hbs +58 -0
- package/templates/default/packages/execution-layer/test/app.test.ts.hbs +16 -0
- package/templates/default/tsconfig.json +5 -0
- package/templates/default/vitest.config.ts +5 -0
- package/templates/default/vitest.host.config.ts +6 -0
- package/templates/opensandbox/OPEN-SANDBOX.md +25 -0
- package/templates/opensandbox/docker/agent-sandbox.Dockerfile +41 -0
|
@@ -0,0 +1,2138 @@
|
|
|
1
|
+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- content hashes are taken synchronously inside pure planning; Effect's Crypto digests only as an effect
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
Array,
|
|
6
|
+
Boolean,
|
|
7
|
+
Graph,
|
|
8
|
+
HashMap,
|
|
9
|
+
HashSet,
|
|
10
|
+
Match,
|
|
11
|
+
MutableRef,
|
|
12
|
+
Number,
|
|
13
|
+
Option,
|
|
14
|
+
Order,
|
|
15
|
+
Predicate,
|
|
16
|
+
Record,
|
|
17
|
+
Result,
|
|
18
|
+
String,
|
|
19
|
+
Struct,
|
|
20
|
+
pipe,
|
|
21
|
+
} from "effect";
|
|
22
|
+
|
|
23
|
+
import type { OpenApiOperations } from "./operations.ts";
|
|
24
|
+
import { OpenApiSpec } from "./spec.ts";
|
|
25
|
+
|
|
26
|
+
type Primitive = "String" | "Number" | "Int" | "Boolean" | "Null" | "Unknown" | "Never";
|
|
27
|
+
|
|
28
|
+
export interface Field {
|
|
29
|
+
readonly name: string;
|
|
30
|
+
readonly optional: boolean;
|
|
31
|
+
readonly node: Node;
|
|
32
|
+
readonly default?: unknown;
|
|
33
|
+
readonly readOnly?: boolean;
|
|
34
|
+
readonly writeOnly?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type Alphabet = "base64" | "base64url" | "data-uri";
|
|
38
|
+
|
|
39
|
+
export type Direction = "input" | "output";
|
|
40
|
+
|
|
41
|
+
export type Clock = "unix-seconds" | "rfc-2822";
|
|
42
|
+
|
|
43
|
+
export type Node = (
|
|
44
|
+
| { readonly _tag: Primitive | "File" | "ToolFile" }
|
|
45
|
+
| { readonly _tag: "Bytes"; readonly alphabet: Alphabet; readonly direction?: Direction }
|
|
46
|
+
| { readonly _tag: "Instant"; readonly clock: Clock; readonly direction?: Direction }
|
|
47
|
+
| { readonly _tag: "Literals"; readonly literals: readonly (string | number | boolean)[] }
|
|
48
|
+
| { readonly _tag: "Array"; readonly item: Node }
|
|
49
|
+
| { readonly _tag: "Record"; readonly value: Node }
|
|
50
|
+
| { readonly _tag: "NullOr"; readonly node: Node }
|
|
51
|
+
// Retained in canonical contracts to keep existing generated version identities stable.
|
|
52
|
+
| {
|
|
53
|
+
readonly _tag: "Union";
|
|
54
|
+
readonly members: readonly Node[];
|
|
55
|
+
readonly preserve?: boolean;
|
|
56
|
+
readonly exclusive?: boolean;
|
|
57
|
+
}
|
|
58
|
+
| {
|
|
59
|
+
readonly _tag: "Struct";
|
|
60
|
+
readonly additionalProperties?: Node;
|
|
61
|
+
readonly excluded?: readonly string[];
|
|
62
|
+
readonly spreads: readonly string[];
|
|
63
|
+
readonly fields: readonly Field[];
|
|
64
|
+
readonly rest: boolean;
|
|
65
|
+
}
|
|
66
|
+
| { readonly _tag: "Ref"; readonly target: string; readonly lazy: boolean }
|
|
67
|
+
) & {
|
|
68
|
+
readonly description?: string;
|
|
69
|
+
readonly checks?: readonly string[];
|
|
70
|
+
readonly format?: string;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
interface Degraded {
|
|
74
|
+
readonly pointer: string;
|
|
75
|
+
readonly reason: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface Contract {
|
|
79
|
+
readonly pathSegments?: readonly string[];
|
|
80
|
+
readonly preauthenticatedUpload?: boolean;
|
|
81
|
+
readonly googleDownload?: boolean;
|
|
82
|
+
readonly nextLink?: boolean;
|
|
83
|
+
readonly alias?: OpenApiOperations.Alias;
|
|
84
|
+
readonly googleUpload?: OpenApiOperations.Operation["googleUpload"];
|
|
85
|
+
readonly graphql?: OpenApiOperations.Operation["graphql"];
|
|
86
|
+
readonly mcp?: OpenApiOperations.Operation["mcp"];
|
|
87
|
+
readonly method: string;
|
|
88
|
+
readonly url: string;
|
|
89
|
+
readonly headers: readonly string[];
|
|
90
|
+
readonly query: readonly string[];
|
|
91
|
+
readonly commaSeparated: readonly string[];
|
|
92
|
+
readonly delimited?: readonly [string, string][];
|
|
93
|
+
readonly deepObject?: readonly string[];
|
|
94
|
+
readonly exploded?: readonly string[];
|
|
95
|
+
readonly bodies: readonly OpenApiOperations.Body[];
|
|
96
|
+
readonly responses: readonly OpenApiOperations.Response[];
|
|
97
|
+
readonly input: Node;
|
|
98
|
+
readonly output: Node;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface Action {
|
|
102
|
+
readonly provider: string;
|
|
103
|
+
readonly document: number;
|
|
104
|
+
readonly operation: OpenApiOperations.Operation;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface Declaration {
|
|
108
|
+
readonly name: string;
|
|
109
|
+
readonly node: Node;
|
|
110
|
+
readonly spread: boolean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const unknown: Node = { _tag: "Unknown" };
|
|
114
|
+
|
|
115
|
+
interface Context {
|
|
116
|
+
readonly spec: OpenApiSpec.Spec;
|
|
117
|
+
readonly document: number;
|
|
118
|
+
readonly degraded: MutableRef.MutableRef<readonly Degraded[]>;
|
|
119
|
+
readonly visiting: HashSet.HashSet<string>;
|
|
120
|
+
readonly multipart?: boolean;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function degrade(context: Context, pointer: string, reason: string, node: Node) {
|
|
124
|
+
MutableRef.update(context.degraded, Array.append({ pointer, reason }));
|
|
125
|
+
return node;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function nullOr(node: Node): Node {
|
|
129
|
+
return node._tag === "Null" || node._tag === "NullOr" || node._tag === "Unknown"
|
|
130
|
+
? node
|
|
131
|
+
: { _tag: "NullOr", node };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function withoutNull(node: Node) {
|
|
135
|
+
if (node._tag === "NullOr") {
|
|
136
|
+
return node.node;
|
|
137
|
+
}
|
|
138
|
+
return node;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function withChecks(checks: string[]) {
|
|
142
|
+
if (Array.isArrayNonEmpty(checks)) {
|
|
143
|
+
return { checks };
|
|
144
|
+
}
|
|
145
|
+
return {};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function keywordChecks(schema: OpenApiSpec.JsonObject, keywords: [string, string][]) {
|
|
149
|
+
return Array.flatMap(keywords, keyword => {
|
|
150
|
+
const value = schema[keyword[0]];
|
|
151
|
+
if (!Predicate.isNumber(value)) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
return [`Schema.${keyword[1]}(${value})`];
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function isBinary(schema: OpenApiSpec.JsonObject) {
|
|
159
|
+
return (
|
|
160
|
+
schema["format"] === "binary" ||
|
|
161
|
+
schema["contentEncoding"] === "binary" ||
|
|
162
|
+
Predicate.isString(schema["contentMediaType"])
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function alphabetOf(schema: OpenApiSpec.JsonObject) {
|
|
167
|
+
if (schema["format"] === "data-uri") {
|
|
168
|
+
return "data-uri" as const;
|
|
169
|
+
}
|
|
170
|
+
if (schema["format"] === "base64url" || schema["contentEncoding"] === "base64url") {
|
|
171
|
+
return "base64url" as const;
|
|
172
|
+
}
|
|
173
|
+
if (
|
|
174
|
+
schema["format"] === "byte" ||
|
|
175
|
+
schema["format"] === "binary" ||
|
|
176
|
+
schema["contentEncoding"] === "base64"
|
|
177
|
+
) {
|
|
178
|
+
return "base64" as const;
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function patternOf(pattern: unknown) {
|
|
184
|
+
if (!Predicate.isString(pattern)) {
|
|
185
|
+
return Option.none();
|
|
186
|
+
}
|
|
187
|
+
const inline = pipe(pattern, String.match(/^\(\?([ims]+)\)/u));
|
|
188
|
+
const source = Option.match(inline, {
|
|
189
|
+
onNone: () => pattern,
|
|
190
|
+
onSome: match => pipe(pattern, String.slice(Array.getUnsafe(match, 0).length)),
|
|
191
|
+
});
|
|
192
|
+
const flags = Option.match(inline, {
|
|
193
|
+
onNone: () => "",
|
|
194
|
+
onSome: match => Array.getUnsafe(match, 1),
|
|
195
|
+
});
|
|
196
|
+
return Option.map(Option.liftThrowable(() => new RegExp(source, flags))(), () => ({
|
|
197
|
+
flags,
|
|
198
|
+
source,
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function formatted(schema: OpenApiSpec.JsonObject) {
|
|
203
|
+
const format = schema["format"];
|
|
204
|
+
if (!Predicate.isString(format) || String.isEmpty(format)) {
|
|
205
|
+
return {};
|
|
206
|
+
}
|
|
207
|
+
return { format };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function union(members: Node[], preserve = false, exclusive = false): Node {
|
|
211
|
+
if (exclusive) {
|
|
212
|
+
return { _tag: "Union", exclusive: true, members, preserve };
|
|
213
|
+
}
|
|
214
|
+
const nullable = Array.some(
|
|
215
|
+
members,
|
|
216
|
+
member => member._tag === "Null" || member._tag === "NullOr",
|
|
217
|
+
);
|
|
218
|
+
const inner = pipe(
|
|
219
|
+
members,
|
|
220
|
+
Array.map(withoutNull),
|
|
221
|
+
Array.filter(member => member._tag !== "Null"),
|
|
222
|
+
);
|
|
223
|
+
if (Array.some(inner, member => member._tag === "Unknown")) {
|
|
224
|
+
return unknown;
|
|
225
|
+
}
|
|
226
|
+
const kept = Array.filter(
|
|
227
|
+
inner,
|
|
228
|
+
member =>
|
|
229
|
+
!(
|
|
230
|
+
member._tag === "Record" &&
|
|
231
|
+
member.value._tag === "Unknown" &&
|
|
232
|
+
member.description === undefined &&
|
|
233
|
+
member.checks === undefined
|
|
234
|
+
),
|
|
235
|
+
);
|
|
236
|
+
const distinct = Array.dedupeWith(
|
|
237
|
+
Boolean.match(
|
|
238
|
+
Array.some(
|
|
239
|
+
kept,
|
|
240
|
+
member => member._tag === "Struct" || member._tag === "Record" || member._tag === "Ref",
|
|
241
|
+
),
|
|
242
|
+
{ onFalse: () => inner, onTrue: () => kept },
|
|
243
|
+
),
|
|
244
|
+
(left, right) => JSON.stringify(left) === JSON.stringify(right),
|
|
245
|
+
);
|
|
246
|
+
const core: Node = Array.match(distinct, {
|
|
247
|
+
onEmpty: (): Node => ({ _tag: "Null" }),
|
|
248
|
+
onNonEmpty: (nonEmpty): Node => {
|
|
249
|
+
if (nonEmpty.length === 1) {
|
|
250
|
+
return Array.headNonEmpty(nonEmpty);
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
_tag: "Union",
|
|
254
|
+
members: distinct,
|
|
255
|
+
...Boolean.match(preserve, { onFalse: () => ({}), onTrue: () => ({ preserve: true }) }),
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
if (nullable) {
|
|
260
|
+
return nullOr(core);
|
|
261
|
+
}
|
|
262
|
+
return core;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function componentSchemas(spec: OpenApiSpec.Spec) {
|
|
266
|
+
return OpenApiSpec.asObject(spec.document.components?.schemas) ?? {};
|
|
267
|
+
}
|
|
268
|
+
const componentPattern = /^#\/components\/schemas\/([^/]+)$/u;
|
|
269
|
+
|
|
270
|
+
function componentName(ref: string) {
|
|
271
|
+
return pipe(
|
|
272
|
+
ref,
|
|
273
|
+
String.match(componentPattern),
|
|
274
|
+
Option.flatMap(Array.get(1)),
|
|
275
|
+
Option.map(OpenApiSpec.pointerKey),
|
|
276
|
+
Option.getOrUndefined,
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function componentKey(document: number, name: string) {
|
|
281
|
+
return `${document}:${name}`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function componentOf(key: string) {
|
|
285
|
+
const match = Option.getOrThrow(pipe(key, String.match(/^(\d+):([\s\S]*)$/u)));
|
|
286
|
+
return {
|
|
287
|
+
document: Option.getOrThrow(Number.parse(Array.getUnsafe(match, 1))),
|
|
288
|
+
name: Array.getUnsafe(match, 2),
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function componentPointer(name: string) {
|
|
293
|
+
return `#/components/schemas/${OpenApiSpec.pointerToken(name)}`;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function containsFile(
|
|
297
|
+
context: Context,
|
|
298
|
+
value: unknown,
|
|
299
|
+
visited = HashSet.empty<string>(),
|
|
300
|
+
): boolean {
|
|
301
|
+
if (Array.isArray(value)) {
|
|
302
|
+
return Array.some(value, member => containsFile(context, member, visited));
|
|
303
|
+
}
|
|
304
|
+
if (!Predicate.isObject(value)) {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
if (isBinary(value)) {
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
if (Predicate.isString(value["$ref"])) {
|
|
311
|
+
if (HashSet.has(visited, value["$ref"])) {
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
const target = OpenApiSpec.target(context.spec, value["$ref"]);
|
|
315
|
+
return (
|
|
316
|
+
Result.isSuccess(target) &&
|
|
317
|
+
containsFile(context, target.success, HashSet.add(visited, value["$ref"]))
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
return Array.some(Record.values(value), member => containsFile(context, member, visited));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function reference(context: Context, ref: string, pointer: string): Node {
|
|
324
|
+
const name = componentName(ref);
|
|
325
|
+
if (
|
|
326
|
+
name !== undefined &&
|
|
327
|
+
(!context.multipart || !containsFile(context, componentSchemas(context.spec)[name]))
|
|
328
|
+
) {
|
|
329
|
+
if (Record.has(componentSchemas(context.spec), name)) {
|
|
330
|
+
return { _tag: "Ref", lazy: false, target: componentKey(context.document, name) };
|
|
331
|
+
}
|
|
332
|
+
return degrade(context, pointer, `reference ${ref} cannot be resolved`, unknown);
|
|
333
|
+
}
|
|
334
|
+
if (HashSet.has(context.visiting, ref)) {
|
|
335
|
+
return degrade(context, pointer, `reference cycle at ${ref}`, unknown);
|
|
336
|
+
}
|
|
337
|
+
const target = OpenApiSpec.target(context.spec, ref);
|
|
338
|
+
return Result.isFailure(target)
|
|
339
|
+
? degrade(context, pointer, target.failure, unknown)
|
|
340
|
+
: convert({ ...context, visiting: HashSet.add(context.visiting, ref) }, target.success, ref);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function literals(context: Context, schema: OpenApiSpec.JsonObject, pointer: string) {
|
|
344
|
+
const enumeration = schema["enum"];
|
|
345
|
+
if (!Array.isArray(enumeration) && !Record.has(schema, "const")) {
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
const values = Array.isArray(enumeration) ? enumeration : [schema["const"]];
|
|
349
|
+
if (Array.isArrayEmpty(values)) {
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
const literal = Array.filter(
|
|
353
|
+
values,
|
|
354
|
+
(value): value is string | number | boolean =>
|
|
355
|
+
Predicate.isString(value) || Predicate.isNumber(value) || Predicate.isBoolean(value),
|
|
356
|
+
);
|
|
357
|
+
const nullable = Array.some(values, Predicate.isNull);
|
|
358
|
+
const nulls = Boolean.match(nullable, { onFalse: () => 0, onTrue: () => 1 });
|
|
359
|
+
if (literal.length + nulls < Array.dedupe(values).length) {
|
|
360
|
+
degrade(
|
|
361
|
+
context,
|
|
362
|
+
pointer,
|
|
363
|
+
"its enum holds values that are not strings, numbers or booleans",
|
|
364
|
+
unknown,
|
|
365
|
+
);
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
const node = Boolean.match(Array.isArrayNonEmpty(literal), {
|
|
369
|
+
onFalse: (): Node => ({ _tag: "Null" }),
|
|
370
|
+
onTrue: (): Node => ({ _tag: "Literals", literals: Array.dedupe(literal) }),
|
|
371
|
+
});
|
|
372
|
+
if (nullable) {
|
|
373
|
+
return nullOr(node);
|
|
374
|
+
}
|
|
375
|
+
return node;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function navigation(schema: OpenApiSpec.JsonObject): Node {
|
|
379
|
+
if (Predicate.isString(schema["description"])) {
|
|
380
|
+
return { _tag: "Unknown", description: schema["description"] };
|
|
381
|
+
}
|
|
382
|
+
return unknown;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function sole(property: unknown) {
|
|
386
|
+
const declared = OpenApiSpec.asObject(property);
|
|
387
|
+
if (
|
|
388
|
+
declared !== undefined &&
|
|
389
|
+
Array.isArray(declared["enum"]) &&
|
|
390
|
+
declared["enum"].length === 1 &&
|
|
391
|
+
Array.getUnsafe(declared["enum"], 0) === declared["default"]
|
|
392
|
+
) {
|
|
393
|
+
return declared["default"];
|
|
394
|
+
}
|
|
395
|
+
return undefined;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function flagged(context: Context, property: unknown, keyword: "readOnly" | "writeOnly") {
|
|
399
|
+
return (
|
|
400
|
+
(Predicate.hasProperty(property, keyword) && property[keyword] === true) ||
|
|
401
|
+
OpenApiSpec.resolve(context.spec, property)?.[keyword] === true
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function object(context: Context, schema: OpenApiSpec.JsonObject, pointer: string): Node {
|
|
406
|
+
const checks = Array.flatMap(
|
|
407
|
+
[
|
|
408
|
+
["minProperties", "isMinProperties"],
|
|
409
|
+
["maxProperties", "isMaxProperties"],
|
|
410
|
+
] as const,
|
|
411
|
+
entry =>
|
|
412
|
+
Predicate.isNumber(schema[entry[0]]) ? [`Schema.${entry[1]}(${schema[entry[0]]})`] : [],
|
|
413
|
+
);
|
|
414
|
+
const properties = Record.toEntries(OpenApiSpec.asObject(schema.properties) ?? {});
|
|
415
|
+
const required = Array.filter(
|
|
416
|
+
Array.isArray(schema.required) ? schema.required : [],
|
|
417
|
+
Predicate.isString,
|
|
418
|
+
);
|
|
419
|
+
const additional = schema.additionalProperties;
|
|
420
|
+
if (Array.isArrayEmpty(properties)) {
|
|
421
|
+
return {
|
|
422
|
+
_tag: "Record",
|
|
423
|
+
...(Array.isReadonlyArrayNonEmpty(checks) ? { checks } : {}),
|
|
424
|
+
value: Predicate.isObject(additional)
|
|
425
|
+
? convert(context, additional, `${pointer}/additionalProperties`)
|
|
426
|
+
: additional === false
|
|
427
|
+
? { _tag: "Never" }
|
|
428
|
+
: unknown,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
_tag: "Struct",
|
|
433
|
+
...(additional === false ||
|
|
434
|
+
(Predicate.isObject(additional) && !Record.isEmptyRecord(additional))
|
|
435
|
+
? {
|
|
436
|
+
additionalProperties:
|
|
437
|
+
additional === false
|
|
438
|
+
? { _tag: "Never" }
|
|
439
|
+
: convert(context, additional, `${pointer}/additionalProperties`),
|
|
440
|
+
}
|
|
441
|
+
: {}),
|
|
442
|
+
...(Array.isReadonlyArrayNonEmpty(checks) ? { checks } : {}),
|
|
443
|
+
fields: Array.map(properties, property => {
|
|
444
|
+
const fieldDefault = sole(property[1]);
|
|
445
|
+
return {
|
|
446
|
+
...(flagged(context, property[1], "readOnly") ? { readOnly: true } : {}),
|
|
447
|
+
...(flagged(context, property[1], "writeOnly") ? { writeOnly: true } : {}),
|
|
448
|
+
name: property[0],
|
|
449
|
+
node:
|
|
450
|
+
Predicate.hasProperty(property[1], "x-ms-navigationProperty") &&
|
|
451
|
+
property[1]["x-ms-navigationProperty"] === true
|
|
452
|
+
? navigation(property[1])
|
|
453
|
+
: convert(
|
|
454
|
+
context,
|
|
455
|
+
property[1],
|
|
456
|
+
`${pointer}/properties/${OpenApiSpec.pointerToken(property[0])}`,
|
|
457
|
+
),
|
|
458
|
+
optional: !Array.contains(required, property[0]),
|
|
459
|
+
...(fieldDefault === undefined ? {} : { default: fieldDefault }),
|
|
460
|
+
};
|
|
461
|
+
}),
|
|
462
|
+
rest: additional !== false,
|
|
463
|
+
spreads: [],
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function typed(
|
|
468
|
+
context: Context,
|
|
469
|
+
schema: OpenApiSpec.JsonObject,
|
|
470
|
+
pointer: string,
|
|
471
|
+
type: string,
|
|
472
|
+
): Node {
|
|
473
|
+
if (type === "string") {
|
|
474
|
+
if (context.multipart) {
|
|
475
|
+
const resolved = OpenApiSpec.resolve(context.spec, schema);
|
|
476
|
+
if (resolved !== undefined && isBinary(resolved)) {
|
|
477
|
+
return { _tag: "File" };
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
const alphabet = alphabetOf(schema);
|
|
481
|
+
if (alphabet !== undefined) {
|
|
482
|
+
return { _tag: "Bytes", alphabet };
|
|
483
|
+
}
|
|
484
|
+
if (schema["format"] === "date-time-rfc-2822") {
|
|
485
|
+
return { _tag: "Instant", clock: "rfc-2822" };
|
|
486
|
+
}
|
|
487
|
+
const checks = [
|
|
488
|
+
...Array.flatMap(["minLength", "maxLength"] as const, key =>
|
|
489
|
+
Predicate.isNumber(schema[key])
|
|
490
|
+
? [
|
|
491
|
+
`Schema.makeFilter<string>(value => Array.fromIterable(value).length ${key === "minLength" ? ">=" : "<="} ${schema[key]}, { expected: ${JSON.stringify(`${key} ${schema[key]} Unicode code points`)}, toJsonSchema: () => ({ ${key}: ${schema[key]} }) })`,
|
|
492
|
+
]
|
|
493
|
+
: [],
|
|
494
|
+
),
|
|
495
|
+
...Option.toArray(
|
|
496
|
+
Option.map(patternOf(schema["pattern"]), pattern =>
|
|
497
|
+
pattern.flags === ""
|
|
498
|
+
? `Schema.isPattern(new RegExp(${JSON.stringify(pattern.source)}))`
|
|
499
|
+
: `Schema.isPattern(new RegExp(${JSON.stringify(pattern.source)}, ${JSON.stringify(pattern.flags)}))`,
|
|
500
|
+
),
|
|
501
|
+
),
|
|
502
|
+
];
|
|
503
|
+
return { _tag: "String", ...withChecks(checks), ...formatted(schema) };
|
|
504
|
+
}
|
|
505
|
+
if (type === "number" || type === "integer") {
|
|
506
|
+
const checks = keywordChecks(schema, [
|
|
507
|
+
[
|
|
508
|
+
"minimum",
|
|
509
|
+
Boolean.match(schema["exclusiveMinimum"] === true, {
|
|
510
|
+
onFalse: () => "isGreaterThanOrEqualTo",
|
|
511
|
+
onTrue: () => "isGreaterThan",
|
|
512
|
+
}),
|
|
513
|
+
],
|
|
514
|
+
[
|
|
515
|
+
"maximum",
|
|
516
|
+
Boolean.match(schema["exclusiveMaximum"] === true, {
|
|
517
|
+
onFalse: () => "isLessThanOrEqualTo",
|
|
518
|
+
onTrue: () => "isLessThan",
|
|
519
|
+
}),
|
|
520
|
+
],
|
|
521
|
+
["exclusiveMinimum", "isGreaterThan"],
|
|
522
|
+
["exclusiveMaximum", "isLessThan"],
|
|
523
|
+
["multipleOf", "isMultipleOf"],
|
|
524
|
+
]);
|
|
525
|
+
if (type === "integer" && schema["format"] === "unix-time") {
|
|
526
|
+
return { _tag: "Instant", clock: "unix-seconds" };
|
|
527
|
+
}
|
|
528
|
+
if (type === "integer") {
|
|
529
|
+
return { _tag: "Int", ...withChecks(checks), ...formatted(schema) };
|
|
530
|
+
}
|
|
531
|
+
return { _tag: "Number", ...withChecks(checks), ...formatted(schema) };
|
|
532
|
+
}
|
|
533
|
+
if (type === "boolean") {
|
|
534
|
+
return { _tag: "Boolean" };
|
|
535
|
+
}
|
|
536
|
+
if (type === "null") {
|
|
537
|
+
return { _tag: "Null" };
|
|
538
|
+
}
|
|
539
|
+
if (type === "array") {
|
|
540
|
+
const checks = [
|
|
541
|
+
...Array.flatMap(
|
|
542
|
+
[
|
|
543
|
+
["minItems", "isMinLength"],
|
|
544
|
+
["maxItems", "isMaxLength"],
|
|
545
|
+
] as const,
|
|
546
|
+
entry =>
|
|
547
|
+
Predicate.isNumber(schema[entry[0]]) ? [`Schema.${entry[1]}(${schema[entry[0]]})`] : [],
|
|
548
|
+
),
|
|
549
|
+
...(schema.uniqueItems === true ? ["Schema.isUnique()"] : []),
|
|
550
|
+
];
|
|
551
|
+
return {
|
|
552
|
+
_tag: "Array",
|
|
553
|
+
item: convert(context, schema.items ?? true, `${pointer}/items`),
|
|
554
|
+
...(Array.isReadonlyArrayNonEmpty(checks) ? { checks } : {}),
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
if (type === "object") {
|
|
558
|
+
return object(context, schema, pointer);
|
|
559
|
+
}
|
|
560
|
+
return degrade(context, pointer, `its type ${type} is not a JSON Schema type`, unknown);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function types(schema: OpenApiSpec.JsonObject): readonly string[] {
|
|
564
|
+
const declared = Array.filter(Array.ensure(schema.type ?? []), Predicate.isString);
|
|
565
|
+
if (Array.isArrayNonEmpty(declared)) {
|
|
566
|
+
return declared;
|
|
567
|
+
}
|
|
568
|
+
if (schema.properties !== undefined || schema.additionalProperties !== undefined) {
|
|
569
|
+
return ["object"];
|
|
570
|
+
}
|
|
571
|
+
return schema.items === undefined ? [] : ["array"];
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
interface Part {
|
|
575
|
+
readonly closed?: readonly string[];
|
|
576
|
+
readonly spreads: readonly string[];
|
|
577
|
+
readonly fields: readonly Field[];
|
|
578
|
+
readonly inherited: readonly Field[];
|
|
579
|
+
readonly rest: boolean;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function part(context: Context, member: unknown, pointer: string): Option.Option<Part> {
|
|
583
|
+
const ref = OpenApiSpec.asObject(member)?.$ref;
|
|
584
|
+
const name = Predicate.isString(ref) ? componentName(ref) : undefined;
|
|
585
|
+
if (name !== undefined && Record.has(componentSchemas(context.spec), name)) {
|
|
586
|
+
const key = componentKey(context.document, name);
|
|
587
|
+
if (HashSet.has(context.visiting, key)) {
|
|
588
|
+
degrade(context, pointer, `allOf member ${name} extends itself and is ignored`, unknown);
|
|
589
|
+
return Option.none();
|
|
590
|
+
}
|
|
591
|
+
const component = componentSchemas(context.spec)[name];
|
|
592
|
+
const visiting = { ...context, visiting: HashSet.add(context.visiting, key) };
|
|
593
|
+
const base = withoutNull(convert(visiting, component, componentPointer(name)));
|
|
594
|
+
if (base._tag === "Ref") {
|
|
595
|
+
return part(
|
|
596
|
+
visiting,
|
|
597
|
+
{ $ref: componentPointer(componentOf(base.target).name) },
|
|
598
|
+
componentPointer(name),
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
if (base._tag === "Struct" && (!context.multipart || !containsFile(context, component))) {
|
|
602
|
+
if (base.rest || base.additionalProperties?._tag === "Never") {
|
|
603
|
+
return Option.some({
|
|
604
|
+
...(base.additionalProperties?._tag === "Never"
|
|
605
|
+
? { closed: Array.map(base.fields, field => field.name) }
|
|
606
|
+
: {}),
|
|
607
|
+
fields: base.fields,
|
|
608
|
+
inherited: [],
|
|
609
|
+
rest: base.rest,
|
|
610
|
+
spreads: base.spreads,
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
return Option.some({ fields: [], inherited: base.fields, rest: false, spreads: [key] });
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
const bare = withoutNull(convert(context, member, pointer));
|
|
617
|
+
if (bare._tag === "Struct") {
|
|
618
|
+
if (bare.additionalProperties !== undefined && bare.additionalProperties._tag !== "Never") {
|
|
619
|
+
degrade(context, pointer, "typed additionalProperties inside allOf are widened", bare);
|
|
620
|
+
}
|
|
621
|
+
return Option.some({
|
|
622
|
+
...(bare.additionalProperties?._tag === "Never"
|
|
623
|
+
? { closed: Array.map(bare.fields, field => field.name) }
|
|
624
|
+
: {}),
|
|
625
|
+
fields: bare.fields,
|
|
626
|
+
inherited: [],
|
|
627
|
+
rest: bare.rest,
|
|
628
|
+
spreads: bare.spreads,
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
if (bare._tag === "Record" && bare.value._tag === "Never") {
|
|
632
|
+
return Option.some({ closed: [], fields: [], inherited: [], rest: false, spreads: [] });
|
|
633
|
+
}
|
|
634
|
+
if (
|
|
635
|
+
bare._tag === "Unknown" ||
|
|
636
|
+
(bare._tag === "Record" && bare.value._tag === "Unknown" && bare.checks === undefined)
|
|
637
|
+
) {
|
|
638
|
+
return Option.none();
|
|
639
|
+
}
|
|
640
|
+
degrade(context, pointer, "an allOf member that is not an object is ignored", unknown);
|
|
641
|
+
return Option.none();
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function isAnnotation(member: unknown) {
|
|
645
|
+
const declared = OpenApiSpec.asObject(member);
|
|
646
|
+
return (
|
|
647
|
+
declared !== undefined &&
|
|
648
|
+
Array.every(
|
|
649
|
+
Record.keys(declared),
|
|
650
|
+
key =>
|
|
651
|
+
Array.contains(
|
|
652
|
+
[
|
|
653
|
+
"default",
|
|
654
|
+
"deprecated",
|
|
655
|
+
"description",
|
|
656
|
+
"example",
|
|
657
|
+
"nullable",
|
|
658
|
+
"readOnly",
|
|
659
|
+
"title",
|
|
660
|
+
"writeOnly",
|
|
661
|
+
],
|
|
662
|
+
key,
|
|
663
|
+
) || pipe(key, String.startsWith("x-")),
|
|
664
|
+
)
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function subtypes(context: Context, alternatives: unknown[], pointer: string) {
|
|
669
|
+
return Array.some(alternatives, alternative => {
|
|
670
|
+
const extended = OpenApiSpec.resolve(context.spec, alternative)?.["allOf"];
|
|
671
|
+
return (
|
|
672
|
+
Array.isArray(extended) &&
|
|
673
|
+
Array.some(extended, member => OpenApiSpec.asObject(member)?.["$ref"] === pointer)
|
|
674
|
+
);
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
function expandAlternatives(
|
|
679
|
+
context: Context,
|
|
680
|
+
schema: OpenApiSpec.JsonObject,
|
|
681
|
+
pointer: string,
|
|
682
|
+
members: unknown[],
|
|
683
|
+
position: number,
|
|
684
|
+
): Option.Option<Node> {
|
|
685
|
+
const member = Array.getUnsafe(members, position);
|
|
686
|
+
const resolved = OpenApiSpec.resolve(context.spec, member);
|
|
687
|
+
const alternatives = resolved?.["oneOf"] ?? resolved?.["anyOf"];
|
|
688
|
+
const ref = OpenApiSpec.asObject(member)?.["$ref"];
|
|
689
|
+
if (
|
|
690
|
+
!Array.isArray(alternatives) ||
|
|
691
|
+
(Predicate.isString(ref) &&
|
|
692
|
+
(HashSet.has(context.visiting, ref) || subtypes(context, alternatives, ref)))
|
|
693
|
+
) {
|
|
694
|
+
return Option.none();
|
|
695
|
+
}
|
|
696
|
+
const shared = Record.filter(resolved ?? {}, (_, key) => key !== "oneOf" && key !== "anyOf");
|
|
697
|
+
const expanding = Predicate.isString(ref)
|
|
698
|
+
? { ...context, visiting: HashSet.add(context.visiting, ref) }
|
|
699
|
+
: context;
|
|
700
|
+
return Option.some(
|
|
701
|
+
union(
|
|
702
|
+
Array.map(alternatives, alternative => {
|
|
703
|
+
const replacement = Boolean.match(shared["properties"] === undefined, {
|
|
704
|
+
onFalse: () => ({ allOf: [alternative, shared] }),
|
|
705
|
+
onTrue: () => alternative,
|
|
706
|
+
});
|
|
707
|
+
return allOf(
|
|
708
|
+
expanding,
|
|
709
|
+
{
|
|
710
|
+
...schema,
|
|
711
|
+
allOf: Array.map(members, (other, index) =>
|
|
712
|
+
Boolean.match(index === position, {
|
|
713
|
+
onFalse: () => other,
|
|
714
|
+
onTrue: () => replacement,
|
|
715
|
+
}),
|
|
716
|
+
),
|
|
717
|
+
},
|
|
718
|
+
pointer,
|
|
719
|
+
);
|
|
720
|
+
}),
|
|
721
|
+
true,
|
|
722
|
+
Array.isArray(resolved?.["oneOf"]),
|
|
723
|
+
),
|
|
724
|
+
);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
function mergeMembers(
|
|
728
|
+
context: Context,
|
|
729
|
+
schema: OpenApiSpec.JsonObject,
|
|
730
|
+
pointer: string,
|
|
731
|
+
members: unknown[],
|
|
732
|
+
): Node {
|
|
733
|
+
const own = Record.remove(schema, "allOf");
|
|
734
|
+
const hasOwn = own["properties"] !== undefined || own["additionalProperties"] !== undefined;
|
|
735
|
+
// Specs such as Asana's mark a referenced object nullable with a typed `nullable` member beside
|
|
736
|
+
// the reference, so any nullable member makes the whole nullable.
|
|
737
|
+
const nullable = Array.some(
|
|
738
|
+
members,
|
|
739
|
+
member => OpenApiSpec.asObject(member)?.["nullable"] === true,
|
|
740
|
+
);
|
|
741
|
+
const shapes = Array.filter(
|
|
742
|
+
Array.map(members, (member, index) => ({ index, member })),
|
|
743
|
+
entry => !isAnnotation(entry.member),
|
|
744
|
+
);
|
|
745
|
+
if (Array.isArrayEmpty(shapes) && !hasOwn) {
|
|
746
|
+
return unknown;
|
|
747
|
+
}
|
|
748
|
+
if (shapes.length === 1 && !hasOwn) {
|
|
749
|
+
const only = Array.getUnsafe(shapes, 0);
|
|
750
|
+
const node = convert(context, only.member, `${pointer}/allOf/${only.index}`);
|
|
751
|
+
if (nullable) {
|
|
752
|
+
return nullOr(node);
|
|
753
|
+
}
|
|
754
|
+
return node;
|
|
755
|
+
}
|
|
756
|
+
const parts = Array.getSomes([
|
|
757
|
+
...Array.map(members, (member, index) => part(context, member, `${pointer}/allOf/${index}`)),
|
|
758
|
+
...(hasOwn ? [part(context, { ...own, type: "object" }, pointer)] : []),
|
|
759
|
+
]);
|
|
760
|
+
const required = Array.filter(
|
|
761
|
+
Array.isArray(schema.required) ? schema.required : [],
|
|
762
|
+
Predicate.isString,
|
|
763
|
+
);
|
|
764
|
+
const declared = Array.flatMap(parts, entry => entry.fields);
|
|
765
|
+
const inherited = Array.flatMap(parts, entry => entry.inherited);
|
|
766
|
+
const merged = [
|
|
767
|
+
...declared,
|
|
768
|
+
...Array.filter(
|
|
769
|
+
inherited,
|
|
770
|
+
field =>
|
|
771
|
+
field.optional &&
|
|
772
|
+
Array.contains(required, field.name) &&
|
|
773
|
+
!Array.some(declared, other => other.name === field.name),
|
|
774
|
+
),
|
|
775
|
+
];
|
|
776
|
+
const fields = Array.filter(
|
|
777
|
+
merged,
|
|
778
|
+
(field, index) =>
|
|
779
|
+
!Array.some(Array.drop(merged, index + 1), later => later.name === field.name),
|
|
780
|
+
);
|
|
781
|
+
const closed = Array.flatMap(parts, entry => (entry.closed === undefined ? [] : [entry.closed]));
|
|
782
|
+
const struct = {
|
|
783
|
+
_tag: "Struct",
|
|
784
|
+
...(Array.isReadonlyArrayNonEmpty(closed)
|
|
785
|
+
? { additionalProperties: { _tag: "Never" } as const }
|
|
786
|
+
: {}),
|
|
787
|
+
fields: Array.map(fields, field => ({
|
|
788
|
+
...field,
|
|
789
|
+
...(Array.some(closed, names => !Array.contains(names, field.name))
|
|
790
|
+
? { node: { _tag: "Never" } as const }
|
|
791
|
+
: {}),
|
|
792
|
+
...(Array.contains(required, field.name) ? { optional: false } : {}),
|
|
793
|
+
})),
|
|
794
|
+
rest: Array.isReadonlyArrayEmpty(closed) && Array.some(parts, entry => entry.rest),
|
|
795
|
+
spreads: Array.dedupe(Array.flatMap(parts, entry => entry.spreads)),
|
|
796
|
+
} satisfies Node;
|
|
797
|
+
if (nullable) {
|
|
798
|
+
return nullOr(struct);
|
|
799
|
+
}
|
|
800
|
+
return struct;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
function allOf(context: Context, schema: OpenApiSpec.JsonObject, pointer: string): Node {
|
|
804
|
+
const members = Array.isArray(schema["allOf"]) ? schema["allOf"] : [];
|
|
805
|
+
return pipe(
|
|
806
|
+
Array.findFirst(members, (_, position) =>
|
|
807
|
+
expandAlternatives(context, schema, pointer, members, position),
|
|
808
|
+
),
|
|
809
|
+
Option.getOrElse(() => mergeMembers(context, schema, pointer, members)),
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
function shape(context: Context, schema: OpenApiSpec.JsonObject, pointer: string): Node {
|
|
814
|
+
if (Predicate.isString(schema.$ref)) {
|
|
815
|
+
return reference(context, schema.$ref, pointer);
|
|
816
|
+
}
|
|
817
|
+
const literal = literals(context, schema, pointer);
|
|
818
|
+
if (literal !== undefined) {
|
|
819
|
+
return literal;
|
|
820
|
+
}
|
|
821
|
+
const keyword = Option.getOrUndefined(
|
|
822
|
+
Array.findFirst(["oneOf", "anyOf"] as const, name => Array.isArray(schema[name])),
|
|
823
|
+
);
|
|
824
|
+
if (keyword !== undefined) {
|
|
825
|
+
const members = Array.ensure(schema[keyword]);
|
|
826
|
+
const own = Record.remove(schema, keyword);
|
|
827
|
+
if (subtypes(context, members, pointer)) {
|
|
828
|
+
return shape(context, own, pointer);
|
|
829
|
+
}
|
|
830
|
+
return union(
|
|
831
|
+
Array.map(members, (member, index) =>
|
|
832
|
+
own.properties === undefined && own.allOf === undefined
|
|
833
|
+
? convert(context, member, `${pointer}/${keyword}/${index}`)
|
|
834
|
+
: allOf(context, { allOf: [member, own] }, pointer),
|
|
835
|
+
),
|
|
836
|
+
true,
|
|
837
|
+
keyword === "oneOf",
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
if (Array.isArray(schema.allOf)) {
|
|
841
|
+
return allOf(context, schema, pointer);
|
|
842
|
+
}
|
|
843
|
+
return Array.match(types(schema), {
|
|
844
|
+
onEmpty: () => unknown,
|
|
845
|
+
onNonEmpty: declared =>
|
|
846
|
+
union(Array.map(declared, type => typed(context, schema, pointer, type))),
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
function convert(context: Context, schema: unknown, pointer: string): Node {
|
|
851
|
+
if (schema === false) {
|
|
852
|
+
return { _tag: "Never" };
|
|
853
|
+
}
|
|
854
|
+
if (Predicate.isBoolean(schema)) {
|
|
855
|
+
return unknown;
|
|
856
|
+
}
|
|
857
|
+
if (!Predicate.isObject(schema)) {
|
|
858
|
+
return degrade(context, pointer, "it is not a schema object", unknown);
|
|
859
|
+
}
|
|
860
|
+
const node = shape(context, schema, pointer);
|
|
861
|
+
const nullable = schema.nullable === true ? nullOr(node) : node;
|
|
862
|
+
return Predicate.isString(schema.description) &&
|
|
863
|
+
String.isNonEmpty(String.trim(schema.description))
|
|
864
|
+
? { ...nullable, description: schema.description }
|
|
865
|
+
: nullable;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
function multipart(context: Context, schema: OpenApiOperations.SchemaAt): Node {
|
|
869
|
+
const located = OpenApiSpec.locate(context.spec, schema.value, schema.pointer);
|
|
870
|
+
if (Result.isFailure(located)) {
|
|
871
|
+
return degrade(context, schema.pointer, located.failure, unknown);
|
|
872
|
+
}
|
|
873
|
+
return convert({ ...context, multipart: true }, located.success.value, located.success.pointer);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
export function defaultContentType(bodies: readonly OpenApiOperations.Body[]) {
|
|
877
|
+
return pipe(
|
|
878
|
+
Array.findFirst(bodies, body => body.encoding === "json"),
|
|
879
|
+
Option.map(body => body.contentType),
|
|
880
|
+
Option.getOrUndefined,
|
|
881
|
+
);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
function contractOf(context: Context, operation: OpenApiOperations.Operation): Contract {
|
|
885
|
+
function read(schema: Option.Option<OpenApiOperations.SchemaAt>) {
|
|
886
|
+
return Option.match(schema, {
|
|
887
|
+
onNone: () => unknown,
|
|
888
|
+
onSome: found => convert(context, found.value, found.pointer),
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
const parameters = Array.map(
|
|
892
|
+
Array.dedupeWith(operation.parameters, (left, right) => left.name === right.name),
|
|
893
|
+
parameter => {
|
|
894
|
+
const node = Option.match(parameter.schema, {
|
|
895
|
+
onNone: (): Node => ({ _tag: "String" }),
|
|
896
|
+
onSome: found => convert(context, found.value, found.pointer),
|
|
897
|
+
});
|
|
898
|
+
const declared = Option.getOrUndefined(
|
|
899
|
+
Option.map(parameter.schema, found => OpenApiSpec.resolve(context.spec, found.value)),
|
|
900
|
+
);
|
|
901
|
+
const sent =
|
|
902
|
+
parameter.location === "header" && parameter.required
|
|
903
|
+
? declared?.["default"]
|
|
904
|
+
: sole(declared);
|
|
905
|
+
return {
|
|
906
|
+
name: parameter.name,
|
|
907
|
+
node:
|
|
908
|
+
parameter.description === undefined
|
|
909
|
+
? node
|
|
910
|
+
: { ...node, description: parameter.description },
|
|
911
|
+
optional: !parameter.required,
|
|
912
|
+
...(sent === undefined ? {} : { default: sent }),
|
|
913
|
+
};
|
|
914
|
+
},
|
|
915
|
+
);
|
|
916
|
+
const defaultBody = defaultContentType(operation.bodies);
|
|
917
|
+
const inputs = Array.map(operation.bodies, (body): Node => ({
|
|
918
|
+
_tag: "Struct",
|
|
919
|
+
fields: [
|
|
920
|
+
...parameters,
|
|
921
|
+
...Boolean.match(operation.bodies.length > 1, {
|
|
922
|
+
onFalse: () => [],
|
|
923
|
+
onTrue: () => [
|
|
924
|
+
{
|
|
925
|
+
name: "contentType",
|
|
926
|
+
node: { _tag: "Literals", literals: [body.contentType] } satisfies Node,
|
|
927
|
+
optional: body.contentType === defaultBody,
|
|
928
|
+
},
|
|
929
|
+
],
|
|
930
|
+
}),
|
|
931
|
+
{
|
|
932
|
+
name: "payload",
|
|
933
|
+
node: pipe(
|
|
934
|
+
Match.value(body.encoding),
|
|
935
|
+
Match.when("multipart", () =>
|
|
936
|
+
Option.match(body.schema, {
|
|
937
|
+
onNone: () => unknown,
|
|
938
|
+
onSome: found => multipart(context, found),
|
|
939
|
+
}),
|
|
940
|
+
),
|
|
941
|
+
Match.when("text", (): Node => ({ _tag: "String" })),
|
|
942
|
+
Match.when("binary", (): Node => ({ _tag: "File" })),
|
|
943
|
+
Match.orElse(() =>
|
|
944
|
+
operation.googleUpload === undefined
|
|
945
|
+
? read(body.schema)
|
|
946
|
+
: Option.match(body.schema, {
|
|
947
|
+
onNone: () => unknown,
|
|
948
|
+
onSome: found => multipart(context, found),
|
|
949
|
+
}),
|
|
950
|
+
),
|
|
951
|
+
),
|
|
952
|
+
optional: !body.required,
|
|
953
|
+
},
|
|
954
|
+
],
|
|
955
|
+
rest: false,
|
|
956
|
+
spreads: [],
|
|
957
|
+
}));
|
|
958
|
+
const outputs = Array.map(operation.responses, (response): Node => {
|
|
959
|
+
const node = pipe(
|
|
960
|
+
Match.value(response.kind),
|
|
961
|
+
Match.when("json", () => read(response.schema)),
|
|
962
|
+
Match.when("text", (): Node => ({ _tag: "String" })),
|
|
963
|
+
Match.when("binary", (): Node => ({ _tag: "ToolFile" })),
|
|
964
|
+
Match.when("void", (): Node => ({ _tag: "Struct", fields: [], rest: false, spreads: [] })),
|
|
965
|
+
Match.when("unknown", () => unknown),
|
|
966
|
+
Match.exhaustive,
|
|
967
|
+
);
|
|
968
|
+
return operation.responses.length === 1 && response.headers === undefined
|
|
969
|
+
? node
|
|
970
|
+
: {
|
|
971
|
+
_tag: "Struct",
|
|
972
|
+
fields: [
|
|
973
|
+
{
|
|
974
|
+
name: "status",
|
|
975
|
+
node: /^2\d\d$/u.test(response.status)
|
|
976
|
+
? { _tag: "Literals", literals: [Option.getOrThrow(Number.parse(response.status))] }
|
|
977
|
+
: { _tag: "Int" },
|
|
978
|
+
optional: false,
|
|
979
|
+
},
|
|
980
|
+
{
|
|
981
|
+
name: "contentType",
|
|
982
|
+
node: { _tag: "Literals", literals: [response.contentType] },
|
|
983
|
+
optional: false,
|
|
984
|
+
},
|
|
985
|
+
{ name: "body", node, optional: false },
|
|
986
|
+
...(response.headers === undefined
|
|
987
|
+
? []
|
|
988
|
+
: [
|
|
989
|
+
{
|
|
990
|
+
name: "headers",
|
|
991
|
+
node: {
|
|
992
|
+
_tag: "Struct",
|
|
993
|
+
fields: Array.map(response.headers, name => ({
|
|
994
|
+
name,
|
|
995
|
+
node: { _tag: "String" } satisfies Node,
|
|
996
|
+
optional: true,
|
|
997
|
+
})),
|
|
998
|
+
rest: false,
|
|
999
|
+
spreads: [],
|
|
1000
|
+
} satisfies Node,
|
|
1001
|
+
optional: false,
|
|
1002
|
+
},
|
|
1003
|
+
]),
|
|
1004
|
+
],
|
|
1005
|
+
rest: false,
|
|
1006
|
+
spreads: [],
|
|
1007
|
+
};
|
|
1008
|
+
});
|
|
1009
|
+
const delimited = pipe(
|
|
1010
|
+
operation.parameters,
|
|
1011
|
+
Array.flatMap((parameter): [string, string][] => {
|
|
1012
|
+
if (parameter.delimiter === undefined) {
|
|
1013
|
+
return [];
|
|
1014
|
+
}
|
|
1015
|
+
return [[parameter.name, parameter.delimiter]];
|
|
1016
|
+
}),
|
|
1017
|
+
Array.sort(Order.mapInput(Order.String, (entry: [string, string]) => entry[0])),
|
|
1018
|
+
);
|
|
1019
|
+
const deepObject = pipe(
|
|
1020
|
+
Array.filter(operation.parameters, parameter => parameter.deepObject),
|
|
1021
|
+
Array.map(parameter => parameter.name),
|
|
1022
|
+
);
|
|
1023
|
+
return {
|
|
1024
|
+
...(operation.pathSegments === undefined ? {} : { pathSegments: operation.pathSegments }),
|
|
1025
|
+
...(operation.preauthenticatedUpload === true ? { preauthenticatedUpload: true } : {}),
|
|
1026
|
+
...(operation.googleDownload === true ? { googleDownload: true } : {}),
|
|
1027
|
+
...(operation.nextLink === true ? { nextLink: true } : {}),
|
|
1028
|
+
...(operation.alias === undefined ? {} : { alias: operation.alias }),
|
|
1029
|
+
...(operation.googleUpload === undefined ? {} : { googleUpload: operation.googleUpload }),
|
|
1030
|
+
...(operation.graphql === undefined ? {} : { graphql: operation.graphql }),
|
|
1031
|
+
...(operation.mcp === undefined ? {} : { mcp: operation.mcp }),
|
|
1032
|
+
bodies: operation.bodies,
|
|
1033
|
+
...Boolean.match(
|
|
1034
|
+
Array.some(operation.parameters, parameter => parameter.exploded === true),
|
|
1035
|
+
{
|
|
1036
|
+
onFalse: () => ({}),
|
|
1037
|
+
onTrue: () => ({
|
|
1038
|
+
exploded: Array.map(
|
|
1039
|
+
Array.filter(operation.parameters, parameter => parameter.exploded === true),
|
|
1040
|
+
parameter => parameter.name,
|
|
1041
|
+
),
|
|
1042
|
+
}),
|
|
1043
|
+
},
|
|
1044
|
+
),
|
|
1045
|
+
commaSeparated: Array.map(
|
|
1046
|
+
Array.filter(operation.parameters, parameter => parameter.commaSeparated),
|
|
1047
|
+
parameter => parameter.name,
|
|
1048
|
+
),
|
|
1049
|
+
...Boolean.match(Array.isArrayNonEmpty(delimited), {
|
|
1050
|
+
onFalse: () => ({}),
|
|
1051
|
+
onTrue: () => ({ delimited }),
|
|
1052
|
+
}),
|
|
1053
|
+
...Boolean.match(Array.isArrayNonEmpty(deepObject), {
|
|
1054
|
+
onFalse: () => ({}),
|
|
1055
|
+
onTrue: () => ({ deepObject }),
|
|
1056
|
+
}),
|
|
1057
|
+
headers: Array.map(
|
|
1058
|
+
Array.filter(operation.parameters, parameter => parameter.location === "header"),
|
|
1059
|
+
parameter => parameter.name,
|
|
1060
|
+
),
|
|
1061
|
+
input:
|
|
1062
|
+
inputs.length > 1
|
|
1063
|
+
? { _tag: "Union", members: inputs }
|
|
1064
|
+
: Option.getOrElse(Array.head(inputs), (): Node => ({
|
|
1065
|
+
_tag: "Struct",
|
|
1066
|
+
fields: parameters,
|
|
1067
|
+
rest: false,
|
|
1068
|
+
spreads: [],
|
|
1069
|
+
})),
|
|
1070
|
+
method: String.toUpperCase(operation.method),
|
|
1071
|
+
output: outputs.length > 1 ? { _tag: "Union", members: outputs } : Array.getUnsafe(outputs, 0),
|
|
1072
|
+
query: Array.map(
|
|
1073
|
+
Array.filter(
|
|
1074
|
+
operation.parameters,
|
|
1075
|
+
parameter =>
|
|
1076
|
+
parameter.location === "query" &&
|
|
1077
|
+
!(operation.preauthenticatedUpload === true && parameter.name === "uploadUrl") &&
|
|
1078
|
+
!(operation.nextLink === true && parameter.name === "nextLink"),
|
|
1079
|
+
),
|
|
1080
|
+
parameter => parameter.name,
|
|
1081
|
+
),
|
|
1082
|
+
responses: operation.responses,
|
|
1083
|
+
url: operation.url,
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
function children(node: Node): readonly Node[] {
|
|
1088
|
+
return pipe(
|
|
1089
|
+
Match.value(node),
|
|
1090
|
+
Match.tag("Array", current => [current.item]),
|
|
1091
|
+
Match.tag("Record", current => [current.value]),
|
|
1092
|
+
Match.tag("NullOr", current => [current.node]),
|
|
1093
|
+
Match.tag("Union", current => current.members),
|
|
1094
|
+
Match.tag("Struct", current => [
|
|
1095
|
+
...Array.map(current.fields, field => field.node),
|
|
1096
|
+
...(current.additionalProperties === undefined ? [] : [current.additionalProperties]),
|
|
1097
|
+
]),
|
|
1098
|
+
Match.orElse(() => []),
|
|
1099
|
+
);
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
function mapChildren(node: Node, map: (child: Node) => Node): Node {
|
|
1103
|
+
return pipe(
|
|
1104
|
+
Match.value(node),
|
|
1105
|
+
Match.tag("Array", (current): Node => ({ ...current, item: map(current.item) })),
|
|
1106
|
+
Match.tag("Record", (current): Node => ({ ...current, value: map(current.value) })),
|
|
1107
|
+
Match.tag("NullOr", (current): Node => ({ ...current, node: map(current.node) })),
|
|
1108
|
+
Match.tag("Union", (current): Node => ({
|
|
1109
|
+
...current,
|
|
1110
|
+
members: Array.map(current.members, map),
|
|
1111
|
+
})),
|
|
1112
|
+
Match.tag("Struct", (current): Node => ({
|
|
1113
|
+
...current,
|
|
1114
|
+
...(current.additionalProperties === undefined
|
|
1115
|
+
? {}
|
|
1116
|
+
: { additionalProperties: map(current.additionalProperties) }),
|
|
1117
|
+
fields: Array.map(current.fields, field => ({ ...field, node: map(field.node) })),
|
|
1118
|
+
})),
|
|
1119
|
+
Match.orElse((current): Node => current),
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
export function targets(node: Node): string[] {
|
|
1124
|
+
return pipe(
|
|
1125
|
+
Match.value(node),
|
|
1126
|
+
Match.tag("Ref", current => [current.target]),
|
|
1127
|
+
Match.tag("Struct", current => [
|
|
1128
|
+
...current.spreads,
|
|
1129
|
+
...Array.flatMap(children(current), targets),
|
|
1130
|
+
]),
|
|
1131
|
+
Match.orElse(current => Array.flatMap(children(current), targets)),
|
|
1132
|
+
);
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
function retarget(
|
|
1136
|
+
node: Node,
|
|
1137
|
+
rename: (target: string) => string,
|
|
1138
|
+
lazy: (target: string) => boolean = () => false,
|
|
1139
|
+
): Node {
|
|
1140
|
+
const mapped = mapChildren(node, child => retarget(child, rename, lazy));
|
|
1141
|
+
if (mapped._tag === "Ref") {
|
|
1142
|
+
return { ...mapped, lazy: lazy(mapped.target), target: rename(mapped.target) };
|
|
1143
|
+
}
|
|
1144
|
+
if (mapped._tag === "Struct") {
|
|
1145
|
+
return { ...mapped, spreads: Array.map(mapped.spreads, rename) };
|
|
1146
|
+
}
|
|
1147
|
+
return mapped;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
export function hash(value: string) {
|
|
1151
|
+
return pipe(createHash("sha256").update(value).digest("hex"), String.slice(0, 16));
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
interface Hashable {
|
|
1155
|
+
readonly key: string;
|
|
1156
|
+
readonly content: string;
|
|
1157
|
+
readonly refs: readonly string[];
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
function stronglyConnected(
|
|
1161
|
+
entries: readonly { readonly key: string; readonly refs: readonly string[] }[],
|
|
1162
|
+
) {
|
|
1163
|
+
const graph = Graph.directed<string, string>(mutable => {
|
|
1164
|
+
const indices = HashMap.fromIterable(
|
|
1165
|
+
Array.map(entries, entry => [entry.key, Graph.addNode(mutable, entry.key)] as const),
|
|
1166
|
+
);
|
|
1167
|
+
for (const entry of entries) {
|
|
1168
|
+
for (const ref of entry.refs) {
|
|
1169
|
+
const to = Option.getOrUndefined(HashMap.get(indices, ref));
|
|
1170
|
+
if (to !== undefined) {
|
|
1171
|
+
Graph.addEdge(mutable, HashMap.getUnsafe(indices, entry.key), to, ref);
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
});
|
|
1176
|
+
return Array.map(Graph.stronglyConnectedComponents(graph), component =>
|
|
1177
|
+
Array.getSomes(Array.map(component, index => Graph.getNode(graph, index))),
|
|
1178
|
+
);
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
export function identities(entries: readonly Hashable[]) {
|
|
1182
|
+
const byKey = HashMap.fromIterable(Array.map(entries, entry => [entry.key, entry] as const));
|
|
1183
|
+
const components = stronglyConnected(entries);
|
|
1184
|
+
return Array.reduce(
|
|
1185
|
+
Array.reverse(components),
|
|
1186
|
+
HashMap.empty<string, string>(),
|
|
1187
|
+
(known, component) => {
|
|
1188
|
+
const inside = HashSet.fromIterable(component);
|
|
1189
|
+
function serialized(member: string, internal: (ref: string) => string) {
|
|
1190
|
+
const entry = HashMap.getUnsafe(byKey, member);
|
|
1191
|
+
const refs = Array.map(entry.refs, ref =>
|
|
1192
|
+
HashSet.has(inside, ref)
|
|
1193
|
+
? internal(ref)
|
|
1194
|
+
: Option.getOrElse(HashMap.get(known, ref), () => ref),
|
|
1195
|
+
);
|
|
1196
|
+
return `${entry.content}\u0000${Array.join(refs, "\u0000")}`;
|
|
1197
|
+
}
|
|
1198
|
+
const members = pipe(
|
|
1199
|
+
component,
|
|
1200
|
+
Array.map(member => [member, serialized(member, () => "#")] as const),
|
|
1201
|
+
Array.sort(Order.mapInput(Order.String, (entry: readonly [string, string]) => entry[1])),
|
|
1202
|
+
Array.map(entry => entry[0]),
|
|
1203
|
+
);
|
|
1204
|
+
const positions = HashMap.fromIterable(
|
|
1205
|
+
Array.map(members, (member, index) => [member, `#${index}`] as const),
|
|
1206
|
+
);
|
|
1207
|
+
const digest = hash(
|
|
1208
|
+
Array.join(
|
|
1209
|
+
Array.map(members, member =>
|
|
1210
|
+
serialized(member, ref => HashMap.getUnsafe(positions, ref)),
|
|
1211
|
+
),
|
|
1212
|
+
"\u0001",
|
|
1213
|
+
),
|
|
1214
|
+
);
|
|
1215
|
+
return Array.reduce(members, known, (identified, member, index) =>
|
|
1216
|
+
HashMap.set(identified, member, members.length === 1 ? digest : `${digest}.${index}`),
|
|
1217
|
+
);
|
|
1218
|
+
},
|
|
1219
|
+
);
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
function pascal(value: string) {
|
|
1223
|
+
const name = OpenApiSpec.identifier(value);
|
|
1224
|
+
if (String.isEmpty(name)) {
|
|
1225
|
+
return "Unnamed";
|
|
1226
|
+
}
|
|
1227
|
+
if (
|
|
1228
|
+
Array.contains(
|
|
1229
|
+
[
|
|
1230
|
+
"Action",
|
|
1231
|
+
"Array",
|
|
1232
|
+
"Auth",
|
|
1233
|
+
"Effect",
|
|
1234
|
+
"Group",
|
|
1235
|
+
"Http",
|
|
1236
|
+
"HttpClientRequest",
|
|
1237
|
+
"Record",
|
|
1238
|
+
"Schema",
|
|
1239
|
+
"ToolFile",
|
|
1240
|
+
],
|
|
1241
|
+
name,
|
|
1242
|
+
)
|
|
1243
|
+
) {
|
|
1244
|
+
return `${name}Schema`;
|
|
1245
|
+
}
|
|
1246
|
+
return name;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
function sharedWords(ids: Array.NonEmptyArray<string>) {
|
|
1250
|
+
const words = Array.map(ids, id => pipe(id, String.split("-")));
|
|
1251
|
+
const first = Array.headNonEmpty(words);
|
|
1252
|
+
function isStopWord(word: string) {
|
|
1253
|
+
return Array.contains(["a", "an", "the"], word);
|
|
1254
|
+
}
|
|
1255
|
+
return pipe(
|
|
1256
|
+
Array.flatMap(first, (_, start) =>
|
|
1257
|
+
Array.map(Array.range(start + 1, first.length), end =>
|
|
1258
|
+
pipe(
|
|
1259
|
+
Array.take(Array.drop(first, start), end - start),
|
|
1260
|
+
Array.reverse,
|
|
1261
|
+
Array.dropWhile(isStopWord),
|
|
1262
|
+
Array.reverse,
|
|
1263
|
+
Array.dropWhile(isStopWord),
|
|
1264
|
+
),
|
|
1265
|
+
),
|
|
1266
|
+
),
|
|
1267
|
+
Array.filter(run =>
|
|
1268
|
+
Array.every(Array.tailNonEmpty(words), list =>
|
|
1269
|
+
Array.some(list, (_, start) =>
|
|
1270
|
+
Array.every(run, (word, offset) =>
|
|
1271
|
+
Option.contains(Array.get(list, start + offset), word),
|
|
1272
|
+
),
|
|
1273
|
+
),
|
|
1274
|
+
),
|
|
1275
|
+
),
|
|
1276
|
+
Array.reduce(
|
|
1277
|
+
Array.empty<string>(),
|
|
1278
|
+
Order.max(Order.mapInput(Order.Number, (run: string[]) => run.length)),
|
|
1279
|
+
),
|
|
1280
|
+
Array.join(" "),
|
|
1281
|
+
);
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
function fieldsOf(node: Node) {
|
|
1285
|
+
if (node._tag === "Struct") {
|
|
1286
|
+
return node.fields;
|
|
1287
|
+
}
|
|
1288
|
+
return [];
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
function isComplex(node: Node): boolean {
|
|
1292
|
+
return (
|
|
1293
|
+
node._tag === "Struct" ||
|
|
1294
|
+
node._tag === "Union" ||
|
|
1295
|
+
(node._tag === "NullOr" && isComplex(node.node))
|
|
1296
|
+
);
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
interface Inline {
|
|
1300
|
+
readonly key: string;
|
|
1301
|
+
readonly node: Node;
|
|
1302
|
+
readonly action: number;
|
|
1303
|
+
readonly role: "Body" | "Parameters" | "Response";
|
|
1304
|
+
readonly title: Option.Option<string>;
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
/** Runs a conversion and returns what it degraded along with its result. */
|
|
1308
|
+
function tracked<A>(context: Context, run: () => A) {
|
|
1309
|
+
const before = MutableRef.get(context.degraded).length;
|
|
1310
|
+
const value = run();
|
|
1311
|
+
return { degraded: Array.drop(MutableRef.get(context.degraded), before), value };
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
interface Converted {
|
|
1315
|
+
readonly node: Node;
|
|
1316
|
+
readonly degraded: readonly Degraded[];
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
function convertComponents(context: (document: number) => Context, roots: readonly string[]) {
|
|
1320
|
+
function step(
|
|
1321
|
+
converted: HashMap.HashMap<string, Converted>,
|
|
1322
|
+
pending: readonly string[],
|
|
1323
|
+
): HashMap.HashMap<string, Converted> {
|
|
1324
|
+
const next = Array.filter(pending, key => !HashMap.has(converted, key));
|
|
1325
|
+
if (Array.isReadonlyArrayEmpty(next)) {
|
|
1326
|
+
return converted;
|
|
1327
|
+
}
|
|
1328
|
+
const added = Array.map(Array.dedupe(next), key => {
|
|
1329
|
+
const component = componentOf(key);
|
|
1330
|
+
const owner = context(component.document);
|
|
1331
|
+
const result = tracked(owner, () =>
|
|
1332
|
+
convert(
|
|
1333
|
+
{ ...owner, visiting: HashSet.make(key) },
|
|
1334
|
+
componentSchemas(owner.spec)[component.name],
|
|
1335
|
+
componentPointer(component.name),
|
|
1336
|
+
),
|
|
1337
|
+
);
|
|
1338
|
+
return [key, { degraded: result.degraded, node: result.value }] as const;
|
|
1339
|
+
});
|
|
1340
|
+
return step(
|
|
1341
|
+
Array.reduce(added, converted, (map, entry) => HashMap.set(map, entry[0], entry[1])),
|
|
1342
|
+
Array.flatMap(added, entry => targets(entry[1].node)),
|
|
1343
|
+
);
|
|
1344
|
+
}
|
|
1345
|
+
return step(HashMap.empty(), roots);
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/** Which actions reach a schema the compiler could not represent faithfully, and why. */
|
|
1349
|
+
function unsupported(
|
|
1350
|
+
own: Degraded[][],
|
|
1351
|
+
roots: string[][],
|
|
1352
|
+
components: HashMap.HashMap<string, Converted>,
|
|
1353
|
+
) {
|
|
1354
|
+
const referrers = Array.groupBy(
|
|
1355
|
+
Array.flatMap(Array.fromIterable(components), entry =>
|
|
1356
|
+
Array.map(targets(entry[1].node), target => [target, entry[0]] as const),
|
|
1357
|
+
),
|
|
1358
|
+
entry => entry[0],
|
|
1359
|
+
);
|
|
1360
|
+
function taint(
|
|
1361
|
+
found: HashSet.HashSet<string>,
|
|
1362
|
+
pending: readonly string[],
|
|
1363
|
+
): HashSet.HashSet<string> {
|
|
1364
|
+
const next = Array.filter(Array.dedupe(pending), key => !HashSet.has(found, key));
|
|
1365
|
+
if (Array.isReadonlyArrayEmpty(next)) {
|
|
1366
|
+
return found;
|
|
1367
|
+
}
|
|
1368
|
+
return taint(
|
|
1369
|
+
Array.reduce(next, found, (all, key) => HashSet.add(all, key)),
|
|
1370
|
+
Array.flatMap(next, key => Array.map(referrers[key] ?? [], entry => entry[1])),
|
|
1371
|
+
);
|
|
1372
|
+
}
|
|
1373
|
+
const tainted = taint(
|
|
1374
|
+
HashSet.empty(),
|
|
1375
|
+
Array.flatMap(Array.fromIterable(components), entry =>
|
|
1376
|
+
Array.isReadonlyArrayEmpty(entry[1].degraded) ? [] : [entry[0]],
|
|
1377
|
+
),
|
|
1378
|
+
);
|
|
1379
|
+
function reasons(pending: readonly string[], seen: HashSet.HashSet<string>): readonly Degraded[] {
|
|
1380
|
+
const next = Array.filter(
|
|
1381
|
+
Array.dedupe(pending),
|
|
1382
|
+
key => HashSet.has(tainted, key) && !HashSet.has(seen, key),
|
|
1383
|
+
);
|
|
1384
|
+
if (Array.isReadonlyArrayEmpty(next)) {
|
|
1385
|
+
return [];
|
|
1386
|
+
}
|
|
1387
|
+
const component = (key: string) => HashMap.getUnsafe(components, key);
|
|
1388
|
+
return [
|
|
1389
|
+
...Array.flatMap(next, key => component(key).degraded),
|
|
1390
|
+
...reasons(
|
|
1391
|
+
Array.flatMap(next, key => targets(component(key).node)),
|
|
1392
|
+
Array.reduce(next, seen, (all, key) => HashSet.add(all, key)),
|
|
1393
|
+
),
|
|
1394
|
+
];
|
|
1395
|
+
}
|
|
1396
|
+
return Array.map(own, (degraded, index) => [
|
|
1397
|
+
...degraded,
|
|
1398
|
+
...reasons(Array.getUnsafe(roots, index), HashSet.empty()),
|
|
1399
|
+
]);
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
interface Named {
|
|
1403
|
+
readonly key: string;
|
|
1404
|
+
readonly node: Node;
|
|
1405
|
+
readonly inline: Option.Option<Inline>;
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
function nameDeclarations(
|
|
1409
|
+
actions: readonly Action[],
|
|
1410
|
+
groups: readonly Array.NonEmptyReadonlyArray<Named>[],
|
|
1411
|
+
) {
|
|
1412
|
+
const candidates = Array.map(groups, members => {
|
|
1413
|
+
const chosen = Array.headNonEmpty(members);
|
|
1414
|
+
const inline = Option.getOrUndefined(chosen.inline);
|
|
1415
|
+
if (inline === undefined) {
|
|
1416
|
+
const name = pascal(componentOf(chosen.key).name);
|
|
1417
|
+
return { key: chosen.key, plain: name, qualified: name };
|
|
1418
|
+
}
|
|
1419
|
+
const users = Array.getSomes(Array.map(members, member => member.inline));
|
|
1420
|
+
const role = Array.every(users, user => user.role === inline.role) ? inline.role : "";
|
|
1421
|
+
const first = Array.getUnsafe(actions, inline.action).operation;
|
|
1422
|
+
const shared = Array.isReadonlyArrayNonEmpty(users)
|
|
1423
|
+
? sharedWords(
|
|
1424
|
+
Array.map(users, user => Array.getUnsafe(actions, user.action).operation.actionId),
|
|
1425
|
+
)
|
|
1426
|
+
: "";
|
|
1427
|
+
const title = Array.findFirst(users, user => user.title);
|
|
1428
|
+
if (
|
|
1429
|
+
role === "Parameters" ||
|
|
1430
|
+
(users.length > 1 && (Option.isSome(title) || String.isEmpty(shared)))
|
|
1431
|
+
) {
|
|
1432
|
+
const name = `${pascal(
|
|
1433
|
+
Option.getOrElse(title, () =>
|
|
1434
|
+
String.isEmpty(shared)
|
|
1435
|
+
? Array.match(
|
|
1436
|
+
Array.map(fieldsOf(chosen.node), field => field.name),
|
|
1437
|
+
{
|
|
1438
|
+
onEmpty: () => (chosen.node._tag === "Struct" ? "empty" : chosen.node._tag),
|
|
1439
|
+
onNonEmpty: Array.join(" "),
|
|
1440
|
+
},
|
|
1441
|
+
)
|
|
1442
|
+
: shared,
|
|
1443
|
+
),
|
|
1444
|
+
)}${role}`;
|
|
1445
|
+
return { key: chosen.key, plain: name, qualified: name };
|
|
1446
|
+
}
|
|
1447
|
+
const words = users.length === 1 ? first.actionId : shared;
|
|
1448
|
+
return {
|
|
1449
|
+
key: chosen.key,
|
|
1450
|
+
plain: `${pascal(words)}${role}`,
|
|
1451
|
+
qualified: `${pascal(`${first.groupId} ${words}`)}${role}`,
|
|
1452
|
+
};
|
|
1453
|
+
});
|
|
1454
|
+
const byPlain = Array.groupBy(candidates, candidate => candidate.plain);
|
|
1455
|
+
return pipe(
|
|
1456
|
+
candidates,
|
|
1457
|
+
Array.map(
|
|
1458
|
+
candidate =>
|
|
1459
|
+
[
|
|
1460
|
+
candidate.key,
|
|
1461
|
+
Option.getOrThrow(Record.get(byPlain, candidate.plain)).length > 1
|
|
1462
|
+
? candidate.qualified
|
|
1463
|
+
: candidate.plain,
|
|
1464
|
+
] as const,
|
|
1465
|
+
),
|
|
1466
|
+
Array.groupBy(entry => entry[1]),
|
|
1467
|
+
Record.values,
|
|
1468
|
+
grouped =>
|
|
1469
|
+
Array.reduce(
|
|
1470
|
+
grouped,
|
|
1471
|
+
{
|
|
1472
|
+
names: HashMap.empty<string, string>(),
|
|
1473
|
+
used: HashSet.fromIterable(Array.map(grouped, entries => Array.headNonEmpty(entries)[1])),
|
|
1474
|
+
},
|
|
1475
|
+
(state, entries) =>
|
|
1476
|
+
Array.reduce(entries, state, (current, entry, index) => {
|
|
1477
|
+
if (index === 0) {
|
|
1478
|
+
return { ...current, names: HashMap.set(current.names, entry[0], entry[1]) };
|
|
1479
|
+
}
|
|
1480
|
+
const name = numbered(current.used, entry[1], index + 1);
|
|
1481
|
+
return {
|
|
1482
|
+
names: HashMap.set(current.names, entry[0], name),
|
|
1483
|
+
used: HashSet.add(current.used, name),
|
|
1484
|
+
};
|
|
1485
|
+
}),
|
|
1486
|
+
).names,
|
|
1487
|
+
);
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
function numbered(used: HashSet.HashSet<string>, base: string, index: number): string {
|
|
1491
|
+
const name = `${base}${index}`;
|
|
1492
|
+
if (HashSet.has(used, name)) {
|
|
1493
|
+
return numbered(used, base, index + 1);
|
|
1494
|
+
}
|
|
1495
|
+
return name;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
function contractTargets(contract: Contract) {
|
|
1499
|
+
return [...targets(contract.input), ...targets(contract.output)];
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
export function catalog(documents: readonly OpenApiSpec.Spec[], actions: readonly Action[]) {
|
|
1503
|
+
const degraded = MutableRef.make<readonly Degraded[]>([]);
|
|
1504
|
+
function context(document: number): Context {
|
|
1505
|
+
return {
|
|
1506
|
+
degraded,
|
|
1507
|
+
document,
|
|
1508
|
+
|
|
1509
|
+
spec: Array.getUnsafe(documents, document),
|
|
1510
|
+
visiting: HashSet.empty(),
|
|
1511
|
+
};
|
|
1512
|
+
}
|
|
1513
|
+
const tracking = Array.map(actions, action =>
|
|
1514
|
+
tracked(context(action.document), () => contractOf(context(action.document), action.operation)),
|
|
1515
|
+
);
|
|
1516
|
+
const read = Array.map(tracking, entry => entry.value);
|
|
1517
|
+
|
|
1518
|
+
function isParameter(index: number) {
|
|
1519
|
+
return (entry: Field) =>
|
|
1520
|
+
Array.some(
|
|
1521
|
+
Array.getUnsafe(actions, index).operation.parameters,
|
|
1522
|
+
parameter => parameter.location !== "path" && parameter.name === entry.name,
|
|
1523
|
+
);
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
const inline = Array.flatMap(read, (contract, index): readonly Inline[] => {
|
|
1527
|
+
const action = Array.getUnsafe(actions, index);
|
|
1528
|
+
function titleOf(schema: Option.Option<OpenApiOperations.SchemaAt>) {
|
|
1529
|
+
return Option.flatMap(schema, found =>
|
|
1530
|
+
Option.liftPredicate(
|
|
1531
|
+
OpenApiSpec.resolve(context(action.document).spec, found.value)?.title,
|
|
1532
|
+
Predicate.isString,
|
|
1533
|
+
),
|
|
1534
|
+
);
|
|
1535
|
+
}
|
|
1536
|
+
const payload = Option.getOrUndefined(
|
|
1537
|
+
Array.findFirst(fieldsOf(contract.input), entry => entry.name === "payload"),
|
|
1538
|
+
);
|
|
1539
|
+
const parameters = Array.filter(fieldsOf(contract.input), isParameter(index));
|
|
1540
|
+
return [
|
|
1541
|
+
...Boolean.match(Array.isArrayNonEmpty(parameters), {
|
|
1542
|
+
onFalse: () => [],
|
|
1543
|
+
onTrue: () => [
|
|
1544
|
+
{
|
|
1545
|
+
action: index,
|
|
1546
|
+
key: `inline:${index}:parameters`,
|
|
1547
|
+
node: { _tag: "Struct", fields: parameters, rest: false, spreads: [] } satisfies Node,
|
|
1548
|
+
role: "Parameters" as const,
|
|
1549
|
+
title: Option.none(),
|
|
1550
|
+
},
|
|
1551
|
+
],
|
|
1552
|
+
}),
|
|
1553
|
+
...(payload !== undefined && isComplex(payload.node)
|
|
1554
|
+
? [
|
|
1555
|
+
{
|
|
1556
|
+
action: index,
|
|
1557
|
+
key: `inline:${index}:body`,
|
|
1558
|
+
node: payload.node,
|
|
1559
|
+
role: "Body" as const,
|
|
1560
|
+
title: titleOf(
|
|
1561
|
+
Option.flatMap(Array.head(action.operation.bodies), body => body.schema),
|
|
1562
|
+
),
|
|
1563
|
+
},
|
|
1564
|
+
]
|
|
1565
|
+
: []),
|
|
1566
|
+
...Boolean.match(isComplex(contract.output), {
|
|
1567
|
+
onFalse: () => [],
|
|
1568
|
+
onTrue: () => [
|
|
1569
|
+
{
|
|
1570
|
+
action: index,
|
|
1571
|
+
key: `inline:${index}:response`,
|
|
1572
|
+
node: contract.output,
|
|
1573
|
+
role: "Response" as const,
|
|
1574
|
+
title: titleOf(
|
|
1575
|
+
Option.flatMap(Array.head(action.operation.responses), response => response.schema),
|
|
1576
|
+
),
|
|
1577
|
+
},
|
|
1578
|
+
],
|
|
1579
|
+
}),
|
|
1580
|
+
];
|
|
1581
|
+
});
|
|
1582
|
+
|
|
1583
|
+
const converted = convertComponents(context, Array.flatMap(read, contractTargets));
|
|
1584
|
+
const components = HashMap.map(converted, entry => entry.node);
|
|
1585
|
+
const broken = unsupported(
|
|
1586
|
+
Array.map(tracking, entry => entry.degraded),
|
|
1587
|
+
Array.map(read, contractTargets),
|
|
1588
|
+
converted,
|
|
1589
|
+
);
|
|
1590
|
+
|
|
1591
|
+
function alias(key: string, seen = HashSet.empty<string>()): string {
|
|
1592
|
+
return pipe(
|
|
1593
|
+
HashMap.get(components, key),
|
|
1594
|
+
Option.flatMap(node =>
|
|
1595
|
+
node._tag === "Ref" && node.description === undefined && !HashSet.has(seen, node.target)
|
|
1596
|
+
? Option.some(node.target)
|
|
1597
|
+
: Option.none(),
|
|
1598
|
+
),
|
|
1599
|
+
Option.match({ onNone: () => key, onSome: next => alias(next, HashSet.add(seen, key)) }),
|
|
1600
|
+
);
|
|
1601
|
+
}
|
|
1602
|
+
const named = pipe(
|
|
1603
|
+
[
|
|
1604
|
+
...Array.map(
|
|
1605
|
+
Array.filter(Array.fromIterable(components), entry => alias(entry[0]) === entry[0]),
|
|
1606
|
+
(entry): Named => ({ inline: Option.none(), key: entry[0], node: entry[1] }),
|
|
1607
|
+
),
|
|
1608
|
+
...Array.map(inline, (entry): Named => ({
|
|
1609
|
+
inline: Option.some(entry),
|
|
1610
|
+
key: entry.key,
|
|
1611
|
+
node: entry.node,
|
|
1612
|
+
})),
|
|
1613
|
+
],
|
|
1614
|
+
Array.map((entry): Named => ({
|
|
1615
|
+
...entry,
|
|
1616
|
+
node: retarget(entry.node, target => alias(target)),
|
|
1617
|
+
})),
|
|
1618
|
+
Array.sort(Order.mapInput(Order.String, (entry: Named) => entry.key)),
|
|
1619
|
+
);
|
|
1620
|
+
|
|
1621
|
+
const identity = identities(
|
|
1622
|
+
Array.map(named, entry => ({
|
|
1623
|
+
content: JSON.stringify(entry.node, (field, value: unknown) => {
|
|
1624
|
+
if (field === "target") {
|
|
1625
|
+
return "";
|
|
1626
|
+
}
|
|
1627
|
+
if (field === "spreads" && Array.isArray(value)) {
|
|
1628
|
+
return value.length;
|
|
1629
|
+
}
|
|
1630
|
+
return value;
|
|
1631
|
+
}),
|
|
1632
|
+
key: entry.key,
|
|
1633
|
+
refs: targets(entry.node),
|
|
1634
|
+
})),
|
|
1635
|
+
);
|
|
1636
|
+
const groups = pipe(
|
|
1637
|
+
named,
|
|
1638
|
+
Array.groupBy(entry => HashMap.getUnsafe(identity, entry.key)),
|
|
1639
|
+
Record.values,
|
|
1640
|
+
Array.map(members =>
|
|
1641
|
+
Array.sort(
|
|
1642
|
+
members,
|
|
1643
|
+
Order.mapInput(Order.Boolean, (entry: Named) => Option.isSome(entry.inline)),
|
|
1644
|
+
),
|
|
1645
|
+
),
|
|
1646
|
+
);
|
|
1647
|
+
const shared = Array.filter(
|
|
1648
|
+
groups,
|
|
1649
|
+
members =>
|
|
1650
|
+
members.length > 1 ||
|
|
1651
|
+
!Option.exists(Array.headNonEmpty(members).inline, entry => entry.role === "Parameters"),
|
|
1652
|
+
);
|
|
1653
|
+
const declaredKeys = HashSet.fromIterable(
|
|
1654
|
+
Array.flatMap(shared, members => Array.map(members, member => member.key)),
|
|
1655
|
+
);
|
|
1656
|
+
const representative = HashMap.fromIterable(
|
|
1657
|
+
Array.flatMap(shared, members =>
|
|
1658
|
+
Array.map(members, member => [member.key, Array.headNonEmpty(members).key] as const),
|
|
1659
|
+
),
|
|
1660
|
+
);
|
|
1661
|
+
function canonical(key: string) {
|
|
1662
|
+
return Option.getOrElse(HashMap.get(representative, alias(key)), () => key);
|
|
1663
|
+
}
|
|
1664
|
+
const nodes = HashMap.fromIterable(
|
|
1665
|
+
Array.map(shared, members => {
|
|
1666
|
+
const chosen = Array.headNonEmpty(members);
|
|
1667
|
+
return [chosen.key, retarget(chosen.node, canonical)] as const;
|
|
1668
|
+
}),
|
|
1669
|
+
);
|
|
1670
|
+
|
|
1671
|
+
function flatten(node: Node, seen: HashSet.HashSet<string>): Node {
|
|
1672
|
+
const mapped = mapChildren(node, child => flatten(child, seen));
|
|
1673
|
+
if (mapped._tag !== "Struct") {
|
|
1674
|
+
return mapped;
|
|
1675
|
+
}
|
|
1676
|
+
const bases = Array.flatMap(mapped.spreads, spread => {
|
|
1677
|
+
const base = canonical(spread);
|
|
1678
|
+
const flat = Boolean.match(HashSet.has(seen, base), {
|
|
1679
|
+
onFalse: () => flatten(HashMap.getUnsafe(nodes, base), HashSet.add(seen, base)),
|
|
1680
|
+
onTrue: () => unknown,
|
|
1681
|
+
});
|
|
1682
|
+
if (flat._tag === "Struct") {
|
|
1683
|
+
return [flat];
|
|
1684
|
+
}
|
|
1685
|
+
return [];
|
|
1686
|
+
});
|
|
1687
|
+
const fields = [...Array.flatMap(bases, base => base.fields), ...mapped.fields];
|
|
1688
|
+
return {
|
|
1689
|
+
...mapped,
|
|
1690
|
+
fields: Record.values(
|
|
1691
|
+
Record.fromEntries(Array.map(fields, field => [field.name, field] as const)),
|
|
1692
|
+
),
|
|
1693
|
+
spreads: [],
|
|
1694
|
+
};
|
|
1695
|
+
}
|
|
1696
|
+
const flattened = HashMap.map(nodes, (node, key) => flatten(node, HashSet.make(key)));
|
|
1697
|
+
|
|
1698
|
+
const contracts = Array.map(read, (contract, index): Contract => {
|
|
1699
|
+
function key(role: string) {
|
|
1700
|
+
return `inline:${index}:${role}`;
|
|
1701
|
+
}
|
|
1702
|
+
const spread = HashSet.has(declaredKeys, key("parameters"));
|
|
1703
|
+
function input(node: Node): Node {
|
|
1704
|
+
if (node._tag !== "Struct") {
|
|
1705
|
+
return flatten(node, HashSet.empty());
|
|
1706
|
+
}
|
|
1707
|
+
return {
|
|
1708
|
+
_tag: "Struct",
|
|
1709
|
+
fields: Array.flatMap(fieldsOf(node), entry => {
|
|
1710
|
+
if (spread && isParameter(index)(entry)) {
|
|
1711
|
+
return [];
|
|
1712
|
+
}
|
|
1713
|
+
if (entry.name === "payload" && HashSet.has(declaredKeys, key("body"))) {
|
|
1714
|
+
return [{ ...entry, node: { _tag: "Ref", lazy: false, target: key("body") } }];
|
|
1715
|
+
}
|
|
1716
|
+
return [{ ...entry, node: flatten(entry.node, HashSet.empty()) }];
|
|
1717
|
+
}),
|
|
1718
|
+
rest: false,
|
|
1719
|
+
spreads: Boolean.match(spread, { onFalse: () => [], onTrue: () => [key("parameters")] }),
|
|
1720
|
+
};
|
|
1721
|
+
}
|
|
1722
|
+
return {
|
|
1723
|
+
...contract,
|
|
1724
|
+
input: input(contract.input),
|
|
1725
|
+
output: Boolean.match(HashSet.has(declaredKeys, key("response")), {
|
|
1726
|
+
onFalse: () => flatten(contract.output, HashSet.empty()),
|
|
1727
|
+
onTrue: (): Node => ({ _tag: "Ref", lazy: false, target: key("response") }),
|
|
1728
|
+
}),
|
|
1729
|
+
};
|
|
1730
|
+
});
|
|
1731
|
+
|
|
1732
|
+
function visit(known: HashSet.HashSet<string>, keys: readonly string[]): HashSet.HashSet<string> {
|
|
1733
|
+
return Array.reduce(keys, known, (current, key) =>
|
|
1734
|
+
HashSet.has(current, key)
|
|
1735
|
+
? current
|
|
1736
|
+
: visit(HashSet.add(current, key), targets(HashMap.getUnsafe(flattened, key))),
|
|
1737
|
+
);
|
|
1738
|
+
}
|
|
1739
|
+
const reached = visit(
|
|
1740
|
+
HashSet.empty(),
|
|
1741
|
+
Array.flatMap(contracts, contract => Array.map(contractTargets(contract), canonical)),
|
|
1742
|
+
);
|
|
1743
|
+
const reachable = Array.filter(shared, members =>
|
|
1744
|
+
HashSet.has(reached, Array.headNonEmpty(members).key),
|
|
1745
|
+
);
|
|
1746
|
+
const names = nameDeclarations(actions, reachable);
|
|
1747
|
+
const declared = Array.map(reachable, members => Array.headNonEmpty(members).key);
|
|
1748
|
+
const cycles = Array.map(
|
|
1749
|
+
stronglyConnected(
|
|
1750
|
+
Array.map(declared, key => ({ key, refs: targets(HashMap.getUnsafe(flattened, key)) })),
|
|
1751
|
+
),
|
|
1752
|
+
component => Array.sort(component, Order.String),
|
|
1753
|
+
);
|
|
1754
|
+
const cycleOf = HashMap.fromIterable(
|
|
1755
|
+
Array.flatMap(cycles, (members, index) => Array.map(members, key => [key, index] as const)),
|
|
1756
|
+
);
|
|
1757
|
+
const spreadTargets = HashSet.fromIterable(
|
|
1758
|
+
Array.flatMap(contracts, contract =>
|
|
1759
|
+
contract.input._tag === "Struct" ? Array.map(contract.input.spreads, canonical) : [],
|
|
1760
|
+
),
|
|
1761
|
+
);
|
|
1762
|
+
|
|
1763
|
+
return {
|
|
1764
|
+
...directional(
|
|
1765
|
+
Array.map(contracts, contract => ({
|
|
1766
|
+
...contract,
|
|
1767
|
+
input: retarget(contract.input, target => HashMap.getUnsafe(names, canonical(target))),
|
|
1768
|
+
output: retarget(contract.output, target => HashMap.getUnsafe(names, canonical(target))),
|
|
1769
|
+
})),
|
|
1770
|
+
Array.flatMap(Array.reverse(cycles), members =>
|
|
1771
|
+
Array.map(members, key => ({
|
|
1772
|
+
name: HashMap.getUnsafe(names, key),
|
|
1773
|
+
node: retarget(
|
|
1774
|
+
HashMap.getUnsafe(flattened, key),
|
|
1775
|
+
target => HashMap.getUnsafe(names, target),
|
|
1776
|
+
target => HashMap.getUnsafe(cycleOf, target) === HashMap.getUnsafe(cycleOf, key),
|
|
1777
|
+
),
|
|
1778
|
+
spread: HashSet.has(spreadTargets, key),
|
|
1779
|
+
})),
|
|
1780
|
+
),
|
|
1781
|
+
),
|
|
1782
|
+
unsupported: broken,
|
|
1783
|
+
};
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
export function converts(node: Node): node is Extract<Node, { _tag: "Bytes" | "Instant" }> {
|
|
1787
|
+
return node._tag === "Bytes" || node._tag === "Instant";
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
const conversionNotes = {
|
|
1791
|
+
Bytes: {
|
|
1792
|
+
input: "Pass a file: contentBase64 holds its bytes in standard Base64.",
|
|
1793
|
+
output: "Returned as a file.",
|
|
1794
|
+
},
|
|
1795
|
+
Instant: {
|
|
1796
|
+
input: "Pass an ISO 8601 date-time.",
|
|
1797
|
+
output: "Returned as an ISO 8601 date-time in UTC.",
|
|
1798
|
+
},
|
|
1799
|
+
};
|
|
1800
|
+
|
|
1801
|
+
function holdsDirectional(node: Node): boolean {
|
|
1802
|
+
return (
|
|
1803
|
+
converts(node) ||
|
|
1804
|
+
(node._tag === "Struct" &&
|
|
1805
|
+
Array.some(node.fields, entry => entry.readOnly === true || entry.writeOnly === true)) ||
|
|
1806
|
+
Array.some(children(node), holdsDirectional)
|
|
1807
|
+
);
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
function directional(contracts: Contract[], declarations: Declaration[]) {
|
|
1811
|
+
const nodes = HashMap.fromIterable(
|
|
1812
|
+
Array.map(declarations, declaration => [declaration.name, declaration.node] as const),
|
|
1813
|
+
);
|
|
1814
|
+
// Declarations that take part in a cycle; spreading one would make its inferred type circular.
|
|
1815
|
+
function lazyTargets(node: Node): string[] {
|
|
1816
|
+
return node._tag === "Ref" && node.lazy
|
|
1817
|
+
? [node.target]
|
|
1818
|
+
: Array.flatMap(children(node), lazyTargets);
|
|
1819
|
+
}
|
|
1820
|
+
const recursive = HashSet.fromIterable(
|
|
1821
|
+
Array.flatMap(
|
|
1822
|
+
[
|
|
1823
|
+
...Array.map(declarations, declaration => declaration.node),
|
|
1824
|
+
...Array.flatMap(contracts, contract => [contract.input, contract.output]),
|
|
1825
|
+
],
|
|
1826
|
+
lazyTargets,
|
|
1827
|
+
),
|
|
1828
|
+
);
|
|
1829
|
+
function cyclic(member: Node) {
|
|
1830
|
+
return member._tag === "Ref" && (member.lazy || HashSet.has(recursive, member.target));
|
|
1831
|
+
}
|
|
1832
|
+
function preservedMember(member: Node): Node {
|
|
1833
|
+
if (member._tag !== "Ref") {
|
|
1834
|
+
return member;
|
|
1835
|
+
}
|
|
1836
|
+
const target = HashMap.getUnsafe(nodes, member.target);
|
|
1837
|
+
const base = withoutNull(target);
|
|
1838
|
+
if (base._tag !== "Struct" || base.rest || base.additionalProperties?._tag === "Never") {
|
|
1839
|
+
return member;
|
|
1840
|
+
}
|
|
1841
|
+
if (target._tag === "NullOr") {
|
|
1842
|
+
return nullOr({ ...base, rest: true });
|
|
1843
|
+
}
|
|
1844
|
+
if (cyclic(member)) {
|
|
1845
|
+
return { ...base, rest: true };
|
|
1846
|
+
}
|
|
1847
|
+
return { ...base, fields: [], rest: true, spreads: [member.target] };
|
|
1848
|
+
}
|
|
1849
|
+
function preservesReferences(node: Node): boolean {
|
|
1850
|
+
return (
|
|
1851
|
+
(node._tag === "Union" &&
|
|
1852
|
+
node.preserve === true &&
|
|
1853
|
+
Array.some(node.members, member => preservedMember(member) !== member)) ||
|
|
1854
|
+
Array.some(children(node), preservesReferences)
|
|
1855
|
+
);
|
|
1856
|
+
}
|
|
1857
|
+
function spreading(known: HashSet.HashSet<string>): HashSet.HashSet<string> {
|
|
1858
|
+
const next = HashSet.fromIterable(
|
|
1859
|
+
Array.filter(
|
|
1860
|
+
Array.map(declarations, declaration => declaration.name),
|
|
1861
|
+
name =>
|
|
1862
|
+
HashSet.has(known, name) ||
|
|
1863
|
+
Array.some(targets(HashMap.getUnsafe(nodes, name)), target => HashSet.has(known, target)),
|
|
1864
|
+
),
|
|
1865
|
+
);
|
|
1866
|
+
if (HashSet.size(next) === HashSet.size(known)) {
|
|
1867
|
+
return known;
|
|
1868
|
+
}
|
|
1869
|
+
return spreading(next);
|
|
1870
|
+
}
|
|
1871
|
+
const split = spreading(
|
|
1872
|
+
HashSet.fromIterable(
|
|
1873
|
+
Array.map(
|
|
1874
|
+
Array.filter(
|
|
1875
|
+
declarations,
|
|
1876
|
+
declaration =>
|
|
1877
|
+
holdsDirectional(declaration.node) || preservesReferences(declaration.node),
|
|
1878
|
+
),
|
|
1879
|
+
declaration => declaration.name,
|
|
1880
|
+
),
|
|
1881
|
+
),
|
|
1882
|
+
);
|
|
1883
|
+
const taken = HashSet.fromIterable(Array.map(declarations, declaration => declaration.name));
|
|
1884
|
+
function free(base: string, index: number): string {
|
|
1885
|
+
const name = Boolean.match(index === 1, {
|
|
1886
|
+
onFalse: () => `${base}${index}`,
|
|
1887
|
+
onTrue: () => base,
|
|
1888
|
+
});
|
|
1889
|
+
if (HashSet.has(taken, name)) {
|
|
1890
|
+
return free(base, index + 1);
|
|
1891
|
+
}
|
|
1892
|
+
return name;
|
|
1893
|
+
}
|
|
1894
|
+
const inputNames = HashMap.fromIterable(
|
|
1895
|
+
Array.map(Array.fromIterable(split), name => [name, free(`${name}Input`, 1)] as const),
|
|
1896
|
+
);
|
|
1897
|
+
function inputName(name: string) {
|
|
1898
|
+
return Option.getOrElse(HashMap.get(inputNames, name), () => name);
|
|
1899
|
+
}
|
|
1900
|
+
// `inlined` holds the cyclic targets already inlined on this path. A type that reaches
|
|
1901
|
+
// itself again through a preserved union becomes an open object there instead of inlining forever.
|
|
1902
|
+
const openObject: Node = { _tag: "Struct", fields: [], rest: true, spreads: [] };
|
|
1903
|
+
function form(node: Node, direction: Direction, inlined = HashSet.empty<string>()): Node {
|
|
1904
|
+
const inlining =
|
|
1905
|
+
direction === "input" && node._tag === "Union" && node.preserve === true
|
|
1906
|
+
? Array.filter(
|
|
1907
|
+
node.members,
|
|
1908
|
+
member =>
|
|
1909
|
+
cyclic(member) && member._tag === "Ref" && !HashSet.has(inlined, member.target),
|
|
1910
|
+
)
|
|
1911
|
+
: [];
|
|
1912
|
+
const source =
|
|
1913
|
+
direction === "input" && node._tag === "Union" && node.preserve === true
|
|
1914
|
+
? {
|
|
1915
|
+
...node,
|
|
1916
|
+
members: Array.map(node.members, member =>
|
|
1917
|
+
cyclic(member) && member._tag === "Ref" && HashSet.has(inlined, member.target)
|
|
1918
|
+
? openObject
|
|
1919
|
+
: preservedMember(member),
|
|
1920
|
+
),
|
|
1921
|
+
}
|
|
1922
|
+
: node;
|
|
1923
|
+
const nested = Array.reduce(inlining, inlined, (known, member) =>
|
|
1924
|
+
member._tag === "Ref" ? HashSet.add(known, member.target) : known,
|
|
1925
|
+
);
|
|
1926
|
+
const mapped = mapChildren(source, child => form(child, direction, nested));
|
|
1927
|
+
if (converts(mapped)) {
|
|
1928
|
+
return {
|
|
1929
|
+
...mapped,
|
|
1930
|
+
description: Array.join(
|
|
1931
|
+
[
|
|
1932
|
+
...(mapped.description === undefined ? [] : [mapped.description]),
|
|
1933
|
+
conversionNotes[mapped._tag][direction],
|
|
1934
|
+
],
|
|
1935
|
+
" ",
|
|
1936
|
+
),
|
|
1937
|
+
direction,
|
|
1938
|
+
};
|
|
1939
|
+
}
|
|
1940
|
+
if (mapped._tag === "Ref" && direction === "input") {
|
|
1941
|
+
return { ...mapped, target: inputName(mapped.target) };
|
|
1942
|
+
}
|
|
1943
|
+
if (mapped._tag !== "Struct") {
|
|
1944
|
+
return mapped;
|
|
1945
|
+
}
|
|
1946
|
+
const excluded = Array.map(
|
|
1947
|
+
Array.filter(mapped.fields, entry =>
|
|
1948
|
+
direction === "input" ? entry.readOnly === true : entry.writeOnly === true,
|
|
1949
|
+
),
|
|
1950
|
+
entry => entry.name,
|
|
1951
|
+
);
|
|
1952
|
+
return {
|
|
1953
|
+
...mapped,
|
|
1954
|
+
...(Array.isArrayEmpty(excluded)
|
|
1955
|
+
? {}
|
|
1956
|
+
: { excluded: [...(mapped.excluded ?? []), ...excluded] }),
|
|
1957
|
+
fields: Array.filter(mapped.fields, entry =>
|
|
1958
|
+
direction === "input" ? entry.readOnly !== true : entry.writeOnly !== true,
|
|
1959
|
+
),
|
|
1960
|
+
spreads: direction === "input" ? Array.map(mapped.spreads, inputName) : mapped.spreads,
|
|
1961
|
+
};
|
|
1962
|
+
}
|
|
1963
|
+
const formed = Array.flatMap(declarations, declaration =>
|
|
1964
|
+
HashSet.has(split, declaration.name)
|
|
1965
|
+
? [
|
|
1966
|
+
{ ...declaration, node: form(declaration.node, "output") },
|
|
1967
|
+
{
|
|
1968
|
+
...declaration,
|
|
1969
|
+
name: inputName(declaration.name),
|
|
1970
|
+
node: form(declaration.node, "input"),
|
|
1971
|
+
},
|
|
1972
|
+
]
|
|
1973
|
+
: [declaration],
|
|
1974
|
+
);
|
|
1975
|
+
const formedContracts = Array.map(contracts, contract => ({
|
|
1976
|
+
...contract,
|
|
1977
|
+
input: form(contract.input, "input"),
|
|
1978
|
+
output: form(contract.output, "output"),
|
|
1979
|
+
}));
|
|
1980
|
+
const formedNodes = HashMap.fromIterable(
|
|
1981
|
+
Array.map(formed, declaration => [declaration.name, declaration.node] as const),
|
|
1982
|
+
);
|
|
1983
|
+
function reach(known: HashSet.HashSet<string>, keys: string[]): HashSet.HashSet<string> {
|
|
1984
|
+
return Array.reduce(keys, known, (current, key) => {
|
|
1985
|
+
if (HashSet.has(current, key)) {
|
|
1986
|
+
return current;
|
|
1987
|
+
}
|
|
1988
|
+
return reach(HashSet.add(current, key), targets(HashMap.getUnsafe(formedNodes, key)));
|
|
1989
|
+
});
|
|
1990
|
+
}
|
|
1991
|
+
const reached = reach(HashSet.empty(), Array.flatMap(formedContracts, contractTargets));
|
|
1992
|
+
function spreadsOf(node: Node): string[] {
|
|
1993
|
+
return [
|
|
1994
|
+
...(node._tag === "Struct" ? node.spreads : []),
|
|
1995
|
+
...Array.flatMap(children(node), spreadsOf),
|
|
1996
|
+
];
|
|
1997
|
+
}
|
|
1998
|
+
const spread = HashSet.fromIterable([
|
|
1999
|
+
...Array.flatMap(formed, declaration => spreadsOf(declaration.node)),
|
|
2000
|
+
...Array.flatMap(formedContracts, contract => [
|
|
2001
|
+
...spreadsOf(contract.input),
|
|
2002
|
+
...spreadsOf(contract.output),
|
|
2003
|
+
]),
|
|
2004
|
+
]);
|
|
2005
|
+
return {
|
|
2006
|
+
contracts: formedContracts,
|
|
2007
|
+
declarations: Array.map(
|
|
2008
|
+
Array.filter(formed, declaration => HashSet.has(reached, declaration.name)),
|
|
2009
|
+
declaration => ({
|
|
2010
|
+
...declaration,
|
|
2011
|
+
spread: declaration.spread || HashSet.has(spread, declaration.name),
|
|
2012
|
+
}),
|
|
2013
|
+
),
|
|
2014
|
+
};
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
interface Enum {
|
|
2018
|
+
readonly field: Option.Option<string>;
|
|
2019
|
+
readonly literals: readonly (string | number | boolean)[];
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
function enumsOf(node: Node, field: Option.Option<string>): readonly Enum[] {
|
|
2023
|
+
return pipe(
|
|
2024
|
+
Match.value(node),
|
|
2025
|
+
Match.tag("Literals", current =>
|
|
2026
|
+
Boolean.match(
|
|
2027
|
+
current.literals.length > 1 && Array.every(current.literals, Predicate.isString),
|
|
2028
|
+
{ onFalse: () => [], onTrue: () => [{ field, literals: current.literals }] },
|
|
2029
|
+
),
|
|
2030
|
+
),
|
|
2031
|
+
Match.tag("Struct", current => [
|
|
2032
|
+
...Array.flatMap(current.fields, entry => enumsOf(entry.node, Option.some(entry.name))),
|
|
2033
|
+
...(current.additionalProperties === undefined
|
|
2034
|
+
? []
|
|
2035
|
+
: enumsOf(current.additionalProperties, field)),
|
|
2036
|
+
]),
|
|
2037
|
+
Match.orElse(current => Array.flatMap(children(current), child => enumsOf(child, field))),
|
|
2038
|
+
);
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
function referEnums(node: Node, names: HashMap.HashMap<string, string>): Node {
|
|
2042
|
+
if (node._tag === "Literals") {
|
|
2043
|
+
return Option.match(HashMap.get(names, JSON.stringify(node.literals)), {
|
|
2044
|
+
onNone: () => node,
|
|
2045
|
+
onSome: (target): Node => ({
|
|
2046
|
+
_tag: "Ref",
|
|
2047
|
+
lazy: false,
|
|
2048
|
+
target,
|
|
2049
|
+
...Struct.pick(node, ["description"]),
|
|
2050
|
+
}),
|
|
2051
|
+
});
|
|
2052
|
+
}
|
|
2053
|
+
return mapChildren(node, child => referEnums(child, names));
|
|
2054
|
+
}
|
|
2055
|
+
|
|
2056
|
+
export function namedEnums(
|
|
2057
|
+
catalogued: Pick<ReturnType<typeof catalog>, "contracts" | "declarations">,
|
|
2058
|
+
actions: Action[],
|
|
2059
|
+
) {
|
|
2060
|
+
const found = [
|
|
2061
|
+
...Array.flatMap(catalogued.declarations, declaration =>
|
|
2062
|
+
declaration.node._tag === "Literals"
|
|
2063
|
+
? []
|
|
2064
|
+
: Array.map(enumsOf(declaration.node, Option.none()), entry => ({
|
|
2065
|
+
...entry,
|
|
2066
|
+
owner: declaration.name,
|
|
2067
|
+
})),
|
|
2068
|
+
),
|
|
2069
|
+
...Array.flatMap(catalogued.contracts, (contract, index) => {
|
|
2070
|
+
const action = Array.getUnsafe(actions, index);
|
|
2071
|
+
return Array.map(
|
|
2072
|
+
[...enumsOf(contract.input, Option.none()), ...enumsOf(contract.output, Option.none())],
|
|
2073
|
+
entry => ({ ...entry, owner: action.operation.actionId }),
|
|
2074
|
+
);
|
|
2075
|
+
}),
|
|
2076
|
+
];
|
|
2077
|
+
const candidates = pipe(
|
|
2078
|
+
found,
|
|
2079
|
+
Array.groupBy(entry => JSON.stringify(entry.literals)),
|
|
2080
|
+
Record.toEntries,
|
|
2081
|
+
Array.map(entry => {
|
|
2082
|
+
const first = Array.headNonEmpty(entry[1]);
|
|
2083
|
+
return {
|
|
2084
|
+
key: entry[0],
|
|
2085
|
+
node: { _tag: "Literals", literals: first.literals } satisfies Node,
|
|
2086
|
+
plain: pascal(Option.getOrElse(first.field, () => `${first.owner} value`)),
|
|
2087
|
+
qualified: pascal(`${first.owner} ${Option.getOrElse(first.field, () => "value")}`),
|
|
2088
|
+
};
|
|
2089
|
+
}),
|
|
2090
|
+
);
|
|
2091
|
+
const taken = HashSet.fromIterable(
|
|
2092
|
+
Array.map(catalogued.declarations, declaration => declaration.name),
|
|
2093
|
+
);
|
|
2094
|
+
function freeName(used: HashSet.HashSet<string>, base: string, index: number): string {
|
|
2095
|
+
const name = index === 1 ? base : `${base}${index}`;
|
|
2096
|
+
return HashSet.has(used, name) ? freeName(used, base, index + 1) : name;
|
|
2097
|
+
}
|
|
2098
|
+
const byPlain = Array.groupBy(candidates, candidate => candidate.plain);
|
|
2099
|
+
const names = Array.reduce(
|
|
2100
|
+
candidates,
|
|
2101
|
+
{ names: HashMap.empty<string, string>(), used: taken },
|
|
2102
|
+
(state, candidate) => {
|
|
2103
|
+
const name = freeName(
|
|
2104
|
+
state.used,
|
|
2105
|
+
HashSet.has(taken, candidate.plain) ||
|
|
2106
|
+
Option.getOrThrow(Record.get(byPlain, candidate.plain)).length > 1
|
|
2107
|
+
? candidate.qualified
|
|
2108
|
+
: candidate.plain,
|
|
2109
|
+
1,
|
|
2110
|
+
);
|
|
2111
|
+
return {
|
|
2112
|
+
names: HashMap.set(state.names, candidate.key, name),
|
|
2113
|
+
used: HashSet.add(state.used, name),
|
|
2114
|
+
};
|
|
2115
|
+
},
|
|
2116
|
+
).names;
|
|
2117
|
+
return {
|
|
2118
|
+
contracts: Array.map(catalogued.contracts, contract => ({
|
|
2119
|
+
...contract,
|
|
2120
|
+
input: referEnums(contract.input, names),
|
|
2121
|
+
output: referEnums(contract.output, names),
|
|
2122
|
+
})),
|
|
2123
|
+
declarations: [
|
|
2124
|
+
...Array.map(candidates, candidate => ({
|
|
2125
|
+
name: HashMap.getUnsafe(names, candidate.key),
|
|
2126
|
+
node: candidate.node,
|
|
2127
|
+
spread: false,
|
|
2128
|
+
})),
|
|
2129
|
+
...Array.map(catalogued.declarations, declaration =>
|
|
2130
|
+
declaration.node._tag === "Literals"
|
|
2131
|
+
? declaration
|
|
2132
|
+
: { ...declaration, node: referEnums(declaration.node, names) },
|
|
2133
|
+
),
|
|
2134
|
+
],
|
|
2135
|
+
};
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
export * as OpenApiSchema from "./schema.ts";
|