@dual-os/cli 0.0.0-stage → 0.11.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,1234 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { asObject, identifier, locate, pointerKey, pointerToken, resolve, target } from "./spec.js";
|
|
3
|
+
import "./schema.js";
|
|
4
|
+
import { Array, Boolean, Graph, HashMap, HashSet, Match, MutableRef, Number, Option, Order, Predicate, Record, Result, String, Struct, pipe } from "effect";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
// src/openapi/schema.ts
|
|
7
|
+
var schema_exports = /* @__PURE__ */ __exportAll({
|
|
8
|
+
OpenApiSchema: () => schema_exports,
|
|
9
|
+
catalog: () => catalog,
|
|
10
|
+
converts: () => converts,
|
|
11
|
+
defaultContentType: () => defaultContentType,
|
|
12
|
+
hash: () => hash,
|
|
13
|
+
identities: () => identities,
|
|
14
|
+
namedEnums: () => namedEnums,
|
|
15
|
+
targets: () => targets
|
|
16
|
+
});
|
|
17
|
+
const unknown = { _tag: "Unknown" };
|
|
18
|
+
function degrade(context, pointer, reason, node) {
|
|
19
|
+
MutableRef.update(context.degraded, Array.append({
|
|
20
|
+
pointer,
|
|
21
|
+
reason
|
|
22
|
+
}));
|
|
23
|
+
return node;
|
|
24
|
+
}
|
|
25
|
+
function nullOr(node) {
|
|
26
|
+
return node._tag === "Null" || node._tag === "NullOr" || node._tag === "Unknown" ? node : {
|
|
27
|
+
_tag: "NullOr",
|
|
28
|
+
node
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function withoutNull(node) {
|
|
32
|
+
if (node._tag === "NullOr") return node.node;
|
|
33
|
+
return node;
|
|
34
|
+
}
|
|
35
|
+
function withChecks(checks) {
|
|
36
|
+
if (Array.isArrayNonEmpty(checks)) return { checks };
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
function keywordChecks(schema, keywords) {
|
|
40
|
+
return Array.flatMap(keywords, (keyword) => {
|
|
41
|
+
const value = schema[keyword[0]];
|
|
42
|
+
if (!Predicate.isNumber(value)) return [];
|
|
43
|
+
return [`Schema.${keyword[1]}(${value})`];
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function isBinary(schema) {
|
|
47
|
+
return schema["format"] === "binary" || schema["contentEncoding"] === "binary" || Predicate.isString(schema["contentMediaType"]);
|
|
48
|
+
}
|
|
49
|
+
function alphabetOf(schema) {
|
|
50
|
+
if (schema["format"] === "data-uri") return "data-uri";
|
|
51
|
+
if (schema["format"] === "base64url" || schema["contentEncoding"] === "base64url") return "base64url";
|
|
52
|
+
if (schema["format"] === "byte" || schema["format"] === "binary" || schema["contentEncoding"] === "base64") return "base64";
|
|
53
|
+
}
|
|
54
|
+
function patternOf(pattern) {
|
|
55
|
+
if (!Predicate.isString(pattern)) return Option.none();
|
|
56
|
+
const inline = pipe(pattern, String.match(/^\(\?([ims]+)\)/u));
|
|
57
|
+
const source = Option.match(inline, {
|
|
58
|
+
onNone: () => pattern,
|
|
59
|
+
onSome: (match) => pipe(pattern, String.slice(Array.getUnsafe(match, 0).length))
|
|
60
|
+
});
|
|
61
|
+
const flags = Option.match(inline, {
|
|
62
|
+
onNone: () => "",
|
|
63
|
+
onSome: (match) => Array.getUnsafe(match, 1)
|
|
64
|
+
});
|
|
65
|
+
return Option.map(Option.liftThrowable(() => new RegExp(source, flags))(), () => ({
|
|
66
|
+
flags,
|
|
67
|
+
source
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
function formatted(schema) {
|
|
71
|
+
const format = schema["format"];
|
|
72
|
+
if (!Predicate.isString(format) || String.isEmpty(format)) return {};
|
|
73
|
+
return { format };
|
|
74
|
+
}
|
|
75
|
+
function union(members, preserve = false, exclusive = false) {
|
|
76
|
+
if (exclusive) return {
|
|
77
|
+
_tag: "Union",
|
|
78
|
+
exclusive: true,
|
|
79
|
+
members,
|
|
80
|
+
preserve
|
|
81
|
+
};
|
|
82
|
+
const nullable = Array.some(members, (member) => member._tag === "Null" || member._tag === "NullOr");
|
|
83
|
+
const inner = pipe(members, Array.map(withoutNull), Array.filter((member) => member._tag !== "Null"));
|
|
84
|
+
if (Array.some(inner, (member) => member._tag === "Unknown")) return unknown;
|
|
85
|
+
const kept = Array.filter(inner, (member) => !(member._tag === "Record" && member.value._tag === "Unknown" && member.description === void 0 && member.checks === void 0));
|
|
86
|
+
const distinct = Array.dedupeWith(Boolean.match(Array.some(kept, (member) => member._tag === "Struct" || member._tag === "Record" || member._tag === "Ref"), {
|
|
87
|
+
onFalse: () => inner,
|
|
88
|
+
onTrue: () => kept
|
|
89
|
+
}), (left, right) => JSON.stringify(left) === JSON.stringify(right));
|
|
90
|
+
const core = Array.match(distinct, {
|
|
91
|
+
onEmpty: () => ({ _tag: "Null" }),
|
|
92
|
+
onNonEmpty: (nonEmpty) => {
|
|
93
|
+
if (nonEmpty.length === 1) return Array.headNonEmpty(nonEmpty);
|
|
94
|
+
return {
|
|
95
|
+
_tag: "Union",
|
|
96
|
+
members: distinct,
|
|
97
|
+
...Boolean.match(preserve, {
|
|
98
|
+
onFalse: () => ({}),
|
|
99
|
+
onTrue: () => ({ preserve: true })
|
|
100
|
+
})
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
if (nullable) return nullOr(core);
|
|
105
|
+
return core;
|
|
106
|
+
}
|
|
107
|
+
function componentSchemas(spec) {
|
|
108
|
+
return asObject(spec.document.components?.schemas) ?? {};
|
|
109
|
+
}
|
|
110
|
+
const componentPattern = /^#\/components\/schemas\/([^/]+)$/u;
|
|
111
|
+
function componentName(ref) {
|
|
112
|
+
return pipe(ref, String.match(componentPattern), Option.flatMap(Array.get(1)), Option.map(pointerKey), Option.getOrUndefined);
|
|
113
|
+
}
|
|
114
|
+
function componentKey(document, name) {
|
|
115
|
+
return `${document}:${name}`;
|
|
116
|
+
}
|
|
117
|
+
function componentOf(key) {
|
|
118
|
+
const match = Option.getOrThrow(pipe(key, String.match(/^(\d+):([\s\S]*)$/u)));
|
|
119
|
+
return {
|
|
120
|
+
document: Option.getOrThrow(Number.parse(Array.getUnsafe(match, 1))),
|
|
121
|
+
name: Array.getUnsafe(match, 2)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function componentPointer(name) {
|
|
125
|
+
return `#/components/schemas/${pointerToken(name)}`;
|
|
126
|
+
}
|
|
127
|
+
function containsFile(context, value, visited = HashSet.empty()) {
|
|
128
|
+
if (Array.isArray(value)) return Array.some(value, (member) => containsFile(context, member, visited));
|
|
129
|
+
if (!Predicate.isObject(value)) return false;
|
|
130
|
+
if (isBinary(value)) return true;
|
|
131
|
+
if (Predicate.isString(value["$ref"])) {
|
|
132
|
+
if (HashSet.has(visited, value["$ref"])) return false;
|
|
133
|
+
const target$1 = target(context.spec, value["$ref"]);
|
|
134
|
+
return Result.isSuccess(target$1) && containsFile(context, target$1.success, HashSet.add(visited, value["$ref"]));
|
|
135
|
+
}
|
|
136
|
+
return Array.some(Record.values(value), (member) => containsFile(context, member, visited));
|
|
137
|
+
}
|
|
138
|
+
function reference(context, ref, pointer) {
|
|
139
|
+
const name = componentName(ref);
|
|
140
|
+
if (name !== void 0 && (!context.multipart || !containsFile(context, componentSchemas(context.spec)[name]))) {
|
|
141
|
+
if (Record.has(componentSchemas(context.spec), name)) return {
|
|
142
|
+
_tag: "Ref",
|
|
143
|
+
lazy: false,
|
|
144
|
+
target: componentKey(context.document, name)
|
|
145
|
+
};
|
|
146
|
+
return degrade(context, pointer, `reference ${ref} cannot be resolved`, unknown);
|
|
147
|
+
}
|
|
148
|
+
if (HashSet.has(context.visiting, ref)) return degrade(context, pointer, `reference cycle at ${ref}`, unknown);
|
|
149
|
+
const target$2 = target(context.spec, ref);
|
|
150
|
+
return Result.isFailure(target$2) ? degrade(context, pointer, target$2.failure, unknown) : convert({
|
|
151
|
+
...context,
|
|
152
|
+
visiting: HashSet.add(context.visiting, ref)
|
|
153
|
+
}, target$2.success, ref);
|
|
154
|
+
}
|
|
155
|
+
function literals(context, schema, pointer) {
|
|
156
|
+
const enumeration = schema["enum"];
|
|
157
|
+
if (!Array.isArray(enumeration) && !Record.has(schema, "const")) return;
|
|
158
|
+
const values = Array.isArray(enumeration) ? enumeration : [schema["const"]];
|
|
159
|
+
if (Array.isArrayEmpty(values)) return;
|
|
160
|
+
const literal = Array.filter(values, (value) => Predicate.isString(value) || Predicate.isNumber(value) || Predicate.isBoolean(value));
|
|
161
|
+
const nullable = Array.some(values, Predicate.isNull);
|
|
162
|
+
const nulls = Boolean.match(nullable, {
|
|
163
|
+
onFalse: () => 0,
|
|
164
|
+
onTrue: () => 1
|
|
165
|
+
});
|
|
166
|
+
if (literal.length + nulls < Array.dedupe(values).length) {
|
|
167
|
+
degrade(context, pointer, "its enum holds values that are not strings, numbers or booleans", unknown);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const node = Boolean.match(Array.isArrayNonEmpty(literal), {
|
|
171
|
+
onFalse: () => ({ _tag: "Null" }),
|
|
172
|
+
onTrue: () => ({
|
|
173
|
+
_tag: "Literals",
|
|
174
|
+
literals: Array.dedupe(literal)
|
|
175
|
+
})
|
|
176
|
+
});
|
|
177
|
+
if (nullable) return nullOr(node);
|
|
178
|
+
return node;
|
|
179
|
+
}
|
|
180
|
+
function navigation(schema) {
|
|
181
|
+
if (Predicate.isString(schema["description"])) return {
|
|
182
|
+
_tag: "Unknown",
|
|
183
|
+
description: schema["description"]
|
|
184
|
+
};
|
|
185
|
+
return unknown;
|
|
186
|
+
}
|
|
187
|
+
function sole(property) {
|
|
188
|
+
const declared = asObject(property);
|
|
189
|
+
if (declared !== void 0 && Array.isArray(declared["enum"]) && declared["enum"].length === 1 && Array.getUnsafe(declared["enum"], 0) === declared["default"]) return declared["default"];
|
|
190
|
+
}
|
|
191
|
+
function flagged(context, property, keyword) {
|
|
192
|
+
return Predicate.hasProperty(property, keyword) && property[keyword] === true || resolve(context.spec, property)?.[keyword] === true;
|
|
193
|
+
}
|
|
194
|
+
function object(context, schema, pointer) {
|
|
195
|
+
const checks = Array.flatMap([["minProperties", "isMinProperties"], ["maxProperties", "isMaxProperties"]], (entry) => Predicate.isNumber(schema[entry[0]]) ? [`Schema.${entry[1]}(${schema[entry[0]]})`] : []);
|
|
196
|
+
const properties = Record.toEntries(asObject(schema.properties) ?? {});
|
|
197
|
+
const required = Array.filter(Array.isArray(schema.required) ? schema.required : [], Predicate.isString);
|
|
198
|
+
const additional = schema.additionalProperties;
|
|
199
|
+
if (Array.isArrayEmpty(properties)) return {
|
|
200
|
+
_tag: "Record",
|
|
201
|
+
...Array.isReadonlyArrayNonEmpty(checks) ? { checks } : {},
|
|
202
|
+
value: Predicate.isObject(additional) ? convert(context, additional, `${pointer}/additionalProperties`) : additional === false ? { _tag: "Never" } : unknown
|
|
203
|
+
};
|
|
204
|
+
return {
|
|
205
|
+
_tag: "Struct",
|
|
206
|
+
...additional === false || Predicate.isObject(additional) && !Record.isEmptyRecord(additional) ? { additionalProperties: additional === false ? { _tag: "Never" } : convert(context, additional, `${pointer}/additionalProperties`) } : {},
|
|
207
|
+
...Array.isReadonlyArrayNonEmpty(checks) ? { checks } : {},
|
|
208
|
+
fields: Array.map(properties, (property) => {
|
|
209
|
+
const fieldDefault = sole(property[1]);
|
|
210
|
+
return {
|
|
211
|
+
...flagged(context, property[1], "readOnly") ? { readOnly: true } : {},
|
|
212
|
+
...flagged(context, property[1], "writeOnly") ? { writeOnly: true } : {},
|
|
213
|
+
name: property[0],
|
|
214
|
+
node: Predicate.hasProperty(property[1], "x-ms-navigationProperty") && property[1]["x-ms-navigationProperty"] === true ? navigation(property[1]) : convert(context, property[1], `${pointer}/properties/${pointerToken(property[0])}`),
|
|
215
|
+
optional: !Array.contains(required, property[0]),
|
|
216
|
+
...fieldDefault === void 0 ? {} : { default: fieldDefault }
|
|
217
|
+
};
|
|
218
|
+
}),
|
|
219
|
+
rest: additional !== false,
|
|
220
|
+
spreads: []
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function typed(context, schema, pointer, type) {
|
|
224
|
+
if (type === "string") {
|
|
225
|
+
if (context.multipart) {
|
|
226
|
+
const resolved = resolve(context.spec, schema);
|
|
227
|
+
if (resolved !== void 0 && isBinary(resolved)) return { _tag: "File" };
|
|
228
|
+
}
|
|
229
|
+
const alphabet = alphabetOf(schema);
|
|
230
|
+
if (alphabet !== void 0) return {
|
|
231
|
+
_tag: "Bytes",
|
|
232
|
+
alphabet
|
|
233
|
+
};
|
|
234
|
+
if (schema["format"] === "date-time-rfc-2822") return {
|
|
235
|
+
_tag: "Instant",
|
|
236
|
+
clock: "rfc-2822"
|
|
237
|
+
};
|
|
238
|
+
return {
|
|
239
|
+
_tag: "String",
|
|
240
|
+
...withChecks([...Array.flatMap(["minLength", "maxLength"], (key) => Predicate.isNumber(schema[key]) ? [`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]} }) })`] : []), ...Option.toArray(Option.map(patternOf(schema["pattern"]), (pattern) => pattern.flags === "" ? `Schema.isPattern(new RegExp(${JSON.stringify(pattern.source)}))` : `Schema.isPattern(new RegExp(${JSON.stringify(pattern.source)}, ${JSON.stringify(pattern.flags)}))`))]),
|
|
241
|
+
...formatted(schema)
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
if (type === "number" || type === "integer") {
|
|
245
|
+
const checks = keywordChecks(schema, [
|
|
246
|
+
["minimum", Boolean.match(schema["exclusiveMinimum"] === true, {
|
|
247
|
+
onFalse: () => "isGreaterThanOrEqualTo",
|
|
248
|
+
onTrue: () => "isGreaterThan"
|
|
249
|
+
})],
|
|
250
|
+
["maximum", Boolean.match(schema["exclusiveMaximum"] === true, {
|
|
251
|
+
onFalse: () => "isLessThanOrEqualTo",
|
|
252
|
+
onTrue: () => "isLessThan"
|
|
253
|
+
})],
|
|
254
|
+
["exclusiveMinimum", "isGreaterThan"],
|
|
255
|
+
["exclusiveMaximum", "isLessThan"],
|
|
256
|
+
["multipleOf", "isMultipleOf"]
|
|
257
|
+
]);
|
|
258
|
+
if (type === "integer" && schema["format"] === "unix-time") return {
|
|
259
|
+
_tag: "Instant",
|
|
260
|
+
clock: "unix-seconds"
|
|
261
|
+
};
|
|
262
|
+
if (type === "integer") return {
|
|
263
|
+
_tag: "Int",
|
|
264
|
+
...withChecks(checks),
|
|
265
|
+
...formatted(schema)
|
|
266
|
+
};
|
|
267
|
+
return {
|
|
268
|
+
_tag: "Number",
|
|
269
|
+
...withChecks(checks),
|
|
270
|
+
...formatted(schema)
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
if (type === "boolean") return { _tag: "Boolean" };
|
|
274
|
+
if (type === "null") return { _tag: "Null" };
|
|
275
|
+
if (type === "array") {
|
|
276
|
+
const checks = [...Array.flatMap([["minItems", "isMinLength"], ["maxItems", "isMaxLength"]], (entry) => Predicate.isNumber(schema[entry[0]]) ? [`Schema.${entry[1]}(${schema[entry[0]]})`] : []), ...schema.uniqueItems === true ? ["Schema.isUnique()"] : []];
|
|
277
|
+
return {
|
|
278
|
+
_tag: "Array",
|
|
279
|
+
item: convert(context, schema.items ?? true, `${pointer}/items`),
|
|
280
|
+
...Array.isReadonlyArrayNonEmpty(checks) ? { checks } : {}
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
if (type === "object") return object(context, schema, pointer);
|
|
284
|
+
return degrade(context, pointer, `its type ${type} is not a JSON Schema type`, unknown);
|
|
285
|
+
}
|
|
286
|
+
function types(schema) {
|
|
287
|
+
const declared = Array.filter(Array.ensure(schema.type ?? []), Predicate.isString);
|
|
288
|
+
if (Array.isArrayNonEmpty(declared)) return declared;
|
|
289
|
+
if (schema.properties !== void 0 || schema.additionalProperties !== void 0) return ["object"];
|
|
290
|
+
return schema.items === void 0 ? [] : ["array"];
|
|
291
|
+
}
|
|
292
|
+
function part(context, member, pointer) {
|
|
293
|
+
const ref = asObject(member)?.$ref;
|
|
294
|
+
const name = Predicate.isString(ref) ? componentName(ref) : void 0;
|
|
295
|
+
if (name !== void 0 && Record.has(componentSchemas(context.spec), name)) {
|
|
296
|
+
const key = componentKey(context.document, name);
|
|
297
|
+
if (HashSet.has(context.visiting, key)) {
|
|
298
|
+
degrade(context, pointer, `allOf member ${name} extends itself and is ignored`, unknown);
|
|
299
|
+
return Option.none();
|
|
300
|
+
}
|
|
301
|
+
const component = componentSchemas(context.spec)[name];
|
|
302
|
+
const visiting = {
|
|
303
|
+
...context,
|
|
304
|
+
visiting: HashSet.add(context.visiting, key)
|
|
305
|
+
};
|
|
306
|
+
const base = withoutNull(convert(visiting, component, componentPointer(name)));
|
|
307
|
+
if (base._tag === "Ref") return part(visiting, { $ref: componentPointer(componentOf(base.target).name) }, componentPointer(name));
|
|
308
|
+
if (base._tag === "Struct" && (!context.multipart || !containsFile(context, component))) {
|
|
309
|
+
if (base.rest || base.additionalProperties?._tag === "Never") return Option.some({
|
|
310
|
+
...base.additionalProperties?._tag === "Never" ? { closed: Array.map(base.fields, (field) => field.name) } : {},
|
|
311
|
+
fields: base.fields,
|
|
312
|
+
inherited: [],
|
|
313
|
+
rest: base.rest,
|
|
314
|
+
spreads: base.spreads
|
|
315
|
+
});
|
|
316
|
+
return Option.some({
|
|
317
|
+
fields: [],
|
|
318
|
+
inherited: base.fields,
|
|
319
|
+
rest: false,
|
|
320
|
+
spreads: [key]
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const bare = withoutNull(convert(context, member, pointer));
|
|
325
|
+
if (bare._tag === "Struct") {
|
|
326
|
+
if (bare.additionalProperties !== void 0 && bare.additionalProperties._tag !== "Never") degrade(context, pointer, "typed additionalProperties inside allOf are widened", bare);
|
|
327
|
+
return Option.some({
|
|
328
|
+
...bare.additionalProperties?._tag === "Never" ? { closed: Array.map(bare.fields, (field) => field.name) } : {},
|
|
329
|
+
fields: bare.fields,
|
|
330
|
+
inherited: [],
|
|
331
|
+
rest: bare.rest,
|
|
332
|
+
spreads: bare.spreads
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
if (bare._tag === "Record" && bare.value._tag === "Never") return Option.some({
|
|
336
|
+
closed: [],
|
|
337
|
+
fields: [],
|
|
338
|
+
inherited: [],
|
|
339
|
+
rest: false,
|
|
340
|
+
spreads: []
|
|
341
|
+
});
|
|
342
|
+
if (bare._tag === "Unknown" || bare._tag === "Record" && bare.value._tag === "Unknown" && bare.checks === void 0) return Option.none();
|
|
343
|
+
degrade(context, pointer, "an allOf member that is not an object is ignored", unknown);
|
|
344
|
+
return Option.none();
|
|
345
|
+
}
|
|
346
|
+
function isAnnotation(member) {
|
|
347
|
+
const declared = asObject(member);
|
|
348
|
+
return declared !== void 0 && Array.every(Record.keys(declared), (key) => Array.contains([
|
|
349
|
+
"default",
|
|
350
|
+
"deprecated",
|
|
351
|
+
"description",
|
|
352
|
+
"example",
|
|
353
|
+
"nullable",
|
|
354
|
+
"readOnly",
|
|
355
|
+
"title",
|
|
356
|
+
"writeOnly"
|
|
357
|
+
], key) || pipe(key, String.startsWith("x-")));
|
|
358
|
+
}
|
|
359
|
+
function subtypes(context, alternatives, pointer) {
|
|
360
|
+
return Array.some(alternatives, (alternative) => {
|
|
361
|
+
const extended = resolve(context.spec, alternative)?.["allOf"];
|
|
362
|
+
return Array.isArray(extended) && Array.some(extended, (member) => asObject(member)?.["$ref"] === pointer);
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
function expandAlternatives(context, schema, pointer, members, position) {
|
|
366
|
+
const member = Array.getUnsafe(members, position);
|
|
367
|
+
const resolved = resolve(context.spec, member);
|
|
368
|
+
const alternatives = resolved?.["oneOf"] ?? resolved?.["anyOf"];
|
|
369
|
+
const ref = asObject(member)?.["$ref"];
|
|
370
|
+
if (!Array.isArray(alternatives) || Predicate.isString(ref) && (HashSet.has(context.visiting, ref) || subtypes(context, alternatives, ref))) return Option.none();
|
|
371
|
+
const shared = Record.filter(resolved ?? {}, (_, key) => key !== "oneOf" && key !== "anyOf");
|
|
372
|
+
const expanding = Predicate.isString(ref) ? {
|
|
373
|
+
...context,
|
|
374
|
+
visiting: HashSet.add(context.visiting, ref)
|
|
375
|
+
} : context;
|
|
376
|
+
return Option.some(union(Array.map(alternatives, (alternative) => {
|
|
377
|
+
const replacement = Boolean.match(shared["properties"] === void 0, {
|
|
378
|
+
onFalse: () => ({ allOf: [alternative, shared] }),
|
|
379
|
+
onTrue: () => alternative
|
|
380
|
+
});
|
|
381
|
+
return allOf(expanding, {
|
|
382
|
+
...schema,
|
|
383
|
+
allOf: Array.map(members, (other, index) => Boolean.match(index === position, {
|
|
384
|
+
onFalse: () => other,
|
|
385
|
+
onTrue: () => replacement
|
|
386
|
+
}))
|
|
387
|
+
}, pointer);
|
|
388
|
+
}), true, Array.isArray(resolved?.["oneOf"])));
|
|
389
|
+
}
|
|
390
|
+
function mergeMembers(context, schema, pointer, members) {
|
|
391
|
+
const own = Record.remove(schema, "allOf");
|
|
392
|
+
const hasOwn = own["properties"] !== void 0 || own["additionalProperties"] !== void 0;
|
|
393
|
+
const nullable = Array.some(members, (member) => asObject(member)?.["nullable"] === true);
|
|
394
|
+
const shapes = Array.filter(Array.map(members, (member, index) => ({
|
|
395
|
+
index,
|
|
396
|
+
member
|
|
397
|
+
})), (entry) => !isAnnotation(entry.member));
|
|
398
|
+
if (Array.isArrayEmpty(shapes) && !hasOwn) return unknown;
|
|
399
|
+
if (shapes.length === 1 && !hasOwn) {
|
|
400
|
+
const only = Array.getUnsafe(shapes, 0);
|
|
401
|
+
const node = convert(context, only.member, `${pointer}/allOf/${only.index}`);
|
|
402
|
+
if (nullable) return nullOr(node);
|
|
403
|
+
return node;
|
|
404
|
+
}
|
|
405
|
+
const parts = Array.getSomes([...Array.map(members, (member, index) => part(context, member, `${pointer}/allOf/${index}`)), ...hasOwn ? [part(context, {
|
|
406
|
+
...own,
|
|
407
|
+
type: "object"
|
|
408
|
+
}, pointer)] : []]);
|
|
409
|
+
const required = Array.filter(Array.isArray(schema.required) ? schema.required : [], Predicate.isString);
|
|
410
|
+
const declared = Array.flatMap(parts, (entry) => entry.fields);
|
|
411
|
+
const inherited = Array.flatMap(parts, (entry) => entry.inherited);
|
|
412
|
+
const merged = [...declared, ...Array.filter(inherited, (field) => field.optional && Array.contains(required, field.name) && !Array.some(declared, (other) => other.name === field.name))];
|
|
413
|
+
const fields = Array.filter(merged, (field, index) => !Array.some(Array.drop(merged, index + 1), (later) => later.name === field.name));
|
|
414
|
+
const closed = Array.flatMap(parts, (entry) => entry.closed === void 0 ? [] : [entry.closed]);
|
|
415
|
+
const struct = {
|
|
416
|
+
_tag: "Struct",
|
|
417
|
+
...Array.isReadonlyArrayNonEmpty(closed) ? { additionalProperties: { _tag: "Never" } } : {},
|
|
418
|
+
fields: Array.map(fields, (field) => ({
|
|
419
|
+
...field,
|
|
420
|
+
...Array.some(closed, (names) => !Array.contains(names, field.name)) ? { node: { _tag: "Never" } } : {},
|
|
421
|
+
...Array.contains(required, field.name) ? { optional: false } : {}
|
|
422
|
+
})),
|
|
423
|
+
rest: Array.isReadonlyArrayEmpty(closed) && Array.some(parts, (entry) => entry.rest),
|
|
424
|
+
spreads: Array.dedupe(Array.flatMap(parts, (entry) => entry.spreads))
|
|
425
|
+
};
|
|
426
|
+
if (nullable) return nullOr(struct);
|
|
427
|
+
return struct;
|
|
428
|
+
}
|
|
429
|
+
function allOf(context, schema, pointer) {
|
|
430
|
+
const members = Array.isArray(schema["allOf"]) ? schema["allOf"] : [];
|
|
431
|
+
return pipe(Array.findFirst(members, (_, position) => expandAlternatives(context, schema, pointer, members, position)), Option.getOrElse(() => mergeMembers(context, schema, pointer, members)));
|
|
432
|
+
}
|
|
433
|
+
function shape(context, schema, pointer) {
|
|
434
|
+
if (Predicate.isString(schema.$ref)) return reference(context, schema.$ref, pointer);
|
|
435
|
+
const literal = literals(context, schema, pointer);
|
|
436
|
+
if (literal !== void 0) return literal;
|
|
437
|
+
const keyword = Option.getOrUndefined(Array.findFirst(["oneOf", "anyOf"], (name) => Array.isArray(schema[name])));
|
|
438
|
+
if (keyword !== void 0) {
|
|
439
|
+
const members = Array.ensure(schema[keyword]);
|
|
440
|
+
const own = Record.remove(schema, keyword);
|
|
441
|
+
if (subtypes(context, members, pointer)) return shape(context, own, pointer);
|
|
442
|
+
return union(Array.map(members, (member, index) => own.properties === void 0 && own.allOf === void 0 ? convert(context, member, `${pointer}/${keyword}/${index}`) : allOf(context, { allOf: [member, own] }, pointer)), true, keyword === "oneOf");
|
|
443
|
+
}
|
|
444
|
+
if (Array.isArray(schema.allOf)) return allOf(context, schema, pointer);
|
|
445
|
+
return Array.match(types(schema), {
|
|
446
|
+
onEmpty: () => unknown,
|
|
447
|
+
onNonEmpty: (declared) => union(Array.map(declared, (type) => typed(context, schema, pointer, type)))
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
function convert(context, schema, pointer) {
|
|
451
|
+
if (schema === false) return { _tag: "Never" };
|
|
452
|
+
if (Predicate.isBoolean(schema)) return unknown;
|
|
453
|
+
if (!Predicate.isObject(schema)) return degrade(context, pointer, "it is not a schema object", unknown);
|
|
454
|
+
const node = shape(context, schema, pointer);
|
|
455
|
+
const nullable = schema.nullable === true ? nullOr(node) : node;
|
|
456
|
+
return Predicate.isString(schema.description) && String.isNonEmpty(String.trim(schema.description)) ? {
|
|
457
|
+
...nullable,
|
|
458
|
+
description: schema.description
|
|
459
|
+
} : nullable;
|
|
460
|
+
}
|
|
461
|
+
function multipart(context, schema) {
|
|
462
|
+
const located = locate(context.spec, schema.value, schema.pointer);
|
|
463
|
+
if (Result.isFailure(located)) return degrade(context, schema.pointer, located.failure, unknown);
|
|
464
|
+
return convert({
|
|
465
|
+
...context,
|
|
466
|
+
multipart: true
|
|
467
|
+
}, located.success.value, located.success.pointer);
|
|
468
|
+
}
|
|
469
|
+
function defaultContentType(bodies) {
|
|
470
|
+
return pipe(Array.findFirst(bodies, (body) => body.encoding === "json"), Option.map((body) => body.contentType), Option.getOrUndefined);
|
|
471
|
+
}
|
|
472
|
+
function contractOf(context, operation) {
|
|
473
|
+
function read(schema) {
|
|
474
|
+
return Option.match(schema, {
|
|
475
|
+
onNone: () => unknown,
|
|
476
|
+
onSome: (found) => convert(context, found.value, found.pointer)
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
const parameters = Array.map(Array.dedupeWith(operation.parameters, (left, right) => left.name === right.name), (parameter) => {
|
|
480
|
+
const node = Option.match(parameter.schema, {
|
|
481
|
+
onNone: () => ({ _tag: "String" }),
|
|
482
|
+
onSome: (found) => convert(context, found.value, found.pointer)
|
|
483
|
+
});
|
|
484
|
+
const declared = Option.getOrUndefined(Option.map(parameter.schema, (found) => resolve(context.spec, found.value)));
|
|
485
|
+
const sent = parameter.location === "header" && parameter.required ? declared?.["default"] : sole(declared);
|
|
486
|
+
return {
|
|
487
|
+
name: parameter.name,
|
|
488
|
+
node: parameter.description === void 0 ? node : {
|
|
489
|
+
...node,
|
|
490
|
+
description: parameter.description
|
|
491
|
+
},
|
|
492
|
+
optional: !parameter.required,
|
|
493
|
+
...sent === void 0 ? {} : { default: sent }
|
|
494
|
+
};
|
|
495
|
+
});
|
|
496
|
+
const defaultBody = defaultContentType(operation.bodies);
|
|
497
|
+
const inputs = Array.map(operation.bodies, (body) => ({
|
|
498
|
+
_tag: "Struct",
|
|
499
|
+
fields: [
|
|
500
|
+
...parameters,
|
|
501
|
+
...Boolean.match(operation.bodies.length > 1, {
|
|
502
|
+
onFalse: () => [],
|
|
503
|
+
onTrue: () => [{
|
|
504
|
+
name: "contentType",
|
|
505
|
+
node: {
|
|
506
|
+
_tag: "Literals",
|
|
507
|
+
literals: [body.contentType]
|
|
508
|
+
},
|
|
509
|
+
optional: body.contentType === defaultBody
|
|
510
|
+
}]
|
|
511
|
+
}),
|
|
512
|
+
{
|
|
513
|
+
name: "payload",
|
|
514
|
+
node: pipe(Match.value(body.encoding), Match.when("multipart", () => Option.match(body.schema, {
|
|
515
|
+
onNone: () => unknown,
|
|
516
|
+
onSome: (found) => multipart(context, found)
|
|
517
|
+
})), Match.when("text", () => ({ _tag: "String" })), Match.when("binary", () => ({ _tag: "File" })), Match.orElse(() => operation.googleUpload === void 0 ? read(body.schema) : Option.match(body.schema, {
|
|
518
|
+
onNone: () => unknown,
|
|
519
|
+
onSome: (found) => multipart(context, found)
|
|
520
|
+
}))),
|
|
521
|
+
optional: !body.required
|
|
522
|
+
}
|
|
523
|
+
],
|
|
524
|
+
rest: false,
|
|
525
|
+
spreads: []
|
|
526
|
+
}));
|
|
527
|
+
const outputs = Array.map(operation.responses, (response) => {
|
|
528
|
+
const node = pipe(Match.value(response.kind), Match.when("json", () => read(response.schema)), Match.when("text", () => ({ _tag: "String" })), Match.when("binary", () => ({ _tag: "ToolFile" })), Match.when("void", () => ({
|
|
529
|
+
_tag: "Struct",
|
|
530
|
+
fields: [],
|
|
531
|
+
rest: false,
|
|
532
|
+
spreads: []
|
|
533
|
+
})), Match.when("unknown", () => unknown), Match.exhaustive);
|
|
534
|
+
return operation.responses.length === 1 && response.headers === void 0 ? node : {
|
|
535
|
+
_tag: "Struct",
|
|
536
|
+
fields: [
|
|
537
|
+
{
|
|
538
|
+
name: "status",
|
|
539
|
+
node: /^2\d\d$/u.test(response.status) ? {
|
|
540
|
+
_tag: "Literals",
|
|
541
|
+
literals: [Option.getOrThrow(Number.parse(response.status))]
|
|
542
|
+
} : { _tag: "Int" },
|
|
543
|
+
optional: false
|
|
544
|
+
},
|
|
545
|
+
{
|
|
546
|
+
name: "contentType",
|
|
547
|
+
node: {
|
|
548
|
+
_tag: "Literals",
|
|
549
|
+
literals: [response.contentType]
|
|
550
|
+
},
|
|
551
|
+
optional: false
|
|
552
|
+
},
|
|
553
|
+
{
|
|
554
|
+
name: "body",
|
|
555
|
+
node,
|
|
556
|
+
optional: false
|
|
557
|
+
},
|
|
558
|
+
...response.headers === void 0 ? [] : [{
|
|
559
|
+
name: "headers",
|
|
560
|
+
node: {
|
|
561
|
+
_tag: "Struct",
|
|
562
|
+
fields: Array.map(response.headers, (name) => ({
|
|
563
|
+
name,
|
|
564
|
+
node: { _tag: "String" },
|
|
565
|
+
optional: true
|
|
566
|
+
})),
|
|
567
|
+
rest: false,
|
|
568
|
+
spreads: []
|
|
569
|
+
},
|
|
570
|
+
optional: false
|
|
571
|
+
}]
|
|
572
|
+
],
|
|
573
|
+
rest: false,
|
|
574
|
+
spreads: []
|
|
575
|
+
};
|
|
576
|
+
});
|
|
577
|
+
const delimited = pipe(operation.parameters, Array.flatMap((parameter) => {
|
|
578
|
+
if (parameter.delimiter === void 0) return [];
|
|
579
|
+
return [[parameter.name, parameter.delimiter]];
|
|
580
|
+
}), Array.sort(Order.mapInput(Order.String, (entry) => entry[0])));
|
|
581
|
+
const deepObject = pipe(Array.filter(operation.parameters, (parameter) => parameter.deepObject), Array.map((parameter) => parameter.name));
|
|
582
|
+
return {
|
|
583
|
+
...operation.pathSegments === void 0 ? {} : { pathSegments: operation.pathSegments },
|
|
584
|
+
...operation.preauthenticatedUpload === true ? { preauthenticatedUpload: true } : {},
|
|
585
|
+
...operation.googleDownload === true ? { googleDownload: true } : {},
|
|
586
|
+
...operation.nextLink === true ? { nextLink: true } : {},
|
|
587
|
+
...operation.alias === void 0 ? {} : { alias: operation.alias },
|
|
588
|
+
...operation.googleUpload === void 0 ? {} : { googleUpload: operation.googleUpload },
|
|
589
|
+
...operation.graphql === void 0 ? {} : { graphql: operation.graphql },
|
|
590
|
+
...operation.mcp === void 0 ? {} : { mcp: operation.mcp },
|
|
591
|
+
bodies: operation.bodies,
|
|
592
|
+
...Boolean.match(Array.some(operation.parameters, (parameter) => parameter.exploded === true), {
|
|
593
|
+
onFalse: () => ({}),
|
|
594
|
+
onTrue: () => ({ exploded: Array.map(Array.filter(operation.parameters, (parameter) => parameter.exploded === true), (parameter) => parameter.name) })
|
|
595
|
+
}),
|
|
596
|
+
commaSeparated: Array.map(Array.filter(operation.parameters, (parameter) => parameter.commaSeparated), (parameter) => parameter.name),
|
|
597
|
+
...Boolean.match(Array.isArrayNonEmpty(delimited), {
|
|
598
|
+
onFalse: () => ({}),
|
|
599
|
+
onTrue: () => ({ delimited })
|
|
600
|
+
}),
|
|
601
|
+
...Boolean.match(Array.isArrayNonEmpty(deepObject), {
|
|
602
|
+
onFalse: () => ({}),
|
|
603
|
+
onTrue: () => ({ deepObject })
|
|
604
|
+
}),
|
|
605
|
+
headers: Array.map(Array.filter(operation.parameters, (parameter) => parameter.location === "header"), (parameter) => parameter.name),
|
|
606
|
+
input: inputs.length > 1 ? {
|
|
607
|
+
_tag: "Union",
|
|
608
|
+
members: inputs
|
|
609
|
+
} : Option.getOrElse(Array.head(inputs), () => ({
|
|
610
|
+
_tag: "Struct",
|
|
611
|
+
fields: parameters,
|
|
612
|
+
rest: false,
|
|
613
|
+
spreads: []
|
|
614
|
+
})),
|
|
615
|
+
method: String.toUpperCase(operation.method),
|
|
616
|
+
output: outputs.length > 1 ? {
|
|
617
|
+
_tag: "Union",
|
|
618
|
+
members: outputs
|
|
619
|
+
} : Array.getUnsafe(outputs, 0),
|
|
620
|
+
query: Array.map(Array.filter(operation.parameters, (parameter) => parameter.location === "query" && !(operation.preauthenticatedUpload === true && parameter.name === "uploadUrl") && !(operation.nextLink === true && parameter.name === "nextLink")), (parameter) => parameter.name),
|
|
621
|
+
responses: operation.responses,
|
|
622
|
+
url: operation.url
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
function children(node) {
|
|
626
|
+
return pipe(Match.value(node), Match.tag("Array", (current) => [current.item]), Match.tag("Record", (current) => [current.value]), Match.tag("NullOr", (current) => [current.node]), Match.tag("Union", (current) => current.members), Match.tag("Struct", (current) => [...Array.map(current.fields, (field) => field.node), ...current.additionalProperties === void 0 ? [] : [current.additionalProperties]]), Match.orElse(() => []));
|
|
627
|
+
}
|
|
628
|
+
function mapChildren(node, map) {
|
|
629
|
+
return pipe(Match.value(node), Match.tag("Array", (current) => ({
|
|
630
|
+
...current,
|
|
631
|
+
item: map(current.item)
|
|
632
|
+
})), Match.tag("Record", (current) => ({
|
|
633
|
+
...current,
|
|
634
|
+
value: map(current.value)
|
|
635
|
+
})), Match.tag("NullOr", (current) => ({
|
|
636
|
+
...current,
|
|
637
|
+
node: map(current.node)
|
|
638
|
+
})), Match.tag("Union", (current) => ({
|
|
639
|
+
...current,
|
|
640
|
+
members: Array.map(current.members, map)
|
|
641
|
+
})), Match.tag("Struct", (current) => ({
|
|
642
|
+
...current,
|
|
643
|
+
...current.additionalProperties === void 0 ? {} : { additionalProperties: map(current.additionalProperties) },
|
|
644
|
+
fields: Array.map(current.fields, (field) => ({
|
|
645
|
+
...field,
|
|
646
|
+
node: map(field.node)
|
|
647
|
+
}))
|
|
648
|
+
})), Match.orElse((current) => current));
|
|
649
|
+
}
|
|
650
|
+
function targets(node) {
|
|
651
|
+
return pipe(Match.value(node), Match.tag("Ref", (current) => [current.target]), Match.tag("Struct", (current) => [...current.spreads, ...Array.flatMap(children(current), targets)]), Match.orElse((current) => Array.flatMap(children(current), targets)));
|
|
652
|
+
}
|
|
653
|
+
function retarget(node, rename, lazy = () => false) {
|
|
654
|
+
const mapped = mapChildren(node, (child) => retarget(child, rename, lazy));
|
|
655
|
+
if (mapped._tag === "Ref") return {
|
|
656
|
+
...mapped,
|
|
657
|
+
lazy: lazy(mapped.target),
|
|
658
|
+
target: rename(mapped.target)
|
|
659
|
+
};
|
|
660
|
+
if (mapped._tag === "Struct") return {
|
|
661
|
+
...mapped,
|
|
662
|
+
spreads: Array.map(mapped.spreads, rename)
|
|
663
|
+
};
|
|
664
|
+
return mapped;
|
|
665
|
+
}
|
|
666
|
+
function hash(value) {
|
|
667
|
+
return pipe(createHash("sha256").update(value).digest("hex"), String.slice(0, 16));
|
|
668
|
+
}
|
|
669
|
+
function stronglyConnected(entries) {
|
|
670
|
+
const graph = Graph.directed((mutable) => {
|
|
671
|
+
const indices = HashMap.fromIterable(Array.map(entries, (entry) => [entry.key, Graph.addNode(mutable, entry.key)]));
|
|
672
|
+
for (const entry of entries) for (const ref of entry.refs) {
|
|
673
|
+
const to = Option.getOrUndefined(HashMap.get(indices, ref));
|
|
674
|
+
if (to !== void 0) Graph.addEdge(mutable, HashMap.getUnsafe(indices, entry.key), to, ref);
|
|
675
|
+
}
|
|
676
|
+
});
|
|
677
|
+
return Array.map(Graph.stronglyConnectedComponents(graph), (component) => Array.getSomes(Array.map(component, (index) => Graph.getNode(graph, index))));
|
|
678
|
+
}
|
|
679
|
+
function identities(entries) {
|
|
680
|
+
const byKey = HashMap.fromIterable(Array.map(entries, (entry) => [entry.key, entry]));
|
|
681
|
+
const components = stronglyConnected(entries);
|
|
682
|
+
return Array.reduce(Array.reverse(components), HashMap.empty(), (known, component) => {
|
|
683
|
+
const inside = HashSet.fromIterable(component);
|
|
684
|
+
function serialized(member, internal) {
|
|
685
|
+
const entry = HashMap.getUnsafe(byKey, member);
|
|
686
|
+
const refs = Array.map(entry.refs, (ref) => HashSet.has(inside, ref) ? internal(ref) : Option.getOrElse(HashMap.get(known, ref), () => ref));
|
|
687
|
+
return `${entry.content}\u0000${Array.join(refs, "\0")}`;
|
|
688
|
+
}
|
|
689
|
+
const members = pipe(component, Array.map((member) => [member, serialized(member, () => "#")]), Array.sort(Order.mapInput(Order.String, (entry) => entry[1])), Array.map((entry) => entry[0]));
|
|
690
|
+
const positions = HashMap.fromIterable(Array.map(members, (member, index) => [member, `#${index}`]));
|
|
691
|
+
const digest = hash(Array.join(Array.map(members, (member) => serialized(member, (ref) => HashMap.getUnsafe(positions, ref))), ""));
|
|
692
|
+
return Array.reduce(members, known, (identified, member, index) => HashMap.set(identified, member, members.length === 1 ? digest : `${digest}.${index}`));
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
function pascal(value) {
|
|
696
|
+
const name = identifier(value);
|
|
697
|
+
if (String.isEmpty(name)) return "Unnamed";
|
|
698
|
+
if (Array.contains([
|
|
699
|
+
"Action",
|
|
700
|
+
"Array",
|
|
701
|
+
"Auth",
|
|
702
|
+
"Effect",
|
|
703
|
+
"Group",
|
|
704
|
+
"Http",
|
|
705
|
+
"HttpClientRequest",
|
|
706
|
+
"Record",
|
|
707
|
+
"Schema",
|
|
708
|
+
"ToolFile"
|
|
709
|
+
], name)) return `${name}Schema`;
|
|
710
|
+
return name;
|
|
711
|
+
}
|
|
712
|
+
function sharedWords(ids) {
|
|
713
|
+
const words = Array.map(ids, (id) => pipe(id, String.split("-")));
|
|
714
|
+
const first = Array.headNonEmpty(words);
|
|
715
|
+
function isStopWord(word) {
|
|
716
|
+
return Array.contains([
|
|
717
|
+
"a",
|
|
718
|
+
"an",
|
|
719
|
+
"the"
|
|
720
|
+
], word);
|
|
721
|
+
}
|
|
722
|
+
return pipe(Array.flatMap(first, (_, start) => Array.map(Array.range(start + 1, first.length), (end) => pipe(Array.take(Array.drop(first, start), end - start), Array.reverse, Array.dropWhile(isStopWord), Array.reverse, Array.dropWhile(isStopWord)))), Array.filter((run) => Array.every(Array.tailNonEmpty(words), (list) => Array.some(list, (_, start) => Array.every(run, (word, offset) => Option.contains(Array.get(list, start + offset), word))))), Array.reduce(Array.empty(), Order.max(Order.mapInput(Order.Number, (run) => run.length))), Array.join(" "));
|
|
723
|
+
}
|
|
724
|
+
function fieldsOf(node) {
|
|
725
|
+
if (node._tag === "Struct") return node.fields;
|
|
726
|
+
return [];
|
|
727
|
+
}
|
|
728
|
+
function isComplex(node) {
|
|
729
|
+
return node._tag === "Struct" || node._tag === "Union" || node._tag === "NullOr" && isComplex(node.node);
|
|
730
|
+
}
|
|
731
|
+
/** Runs a conversion and returns what it degraded along with its result. */
|
|
732
|
+
function tracked(context, run) {
|
|
733
|
+
const before = MutableRef.get(context.degraded).length;
|
|
734
|
+
const value = run();
|
|
735
|
+
return {
|
|
736
|
+
degraded: Array.drop(MutableRef.get(context.degraded), before),
|
|
737
|
+
value
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
function convertComponents(context, roots) {
|
|
741
|
+
function step(converted, pending) {
|
|
742
|
+
const next = Array.filter(pending, (key) => !HashMap.has(converted, key));
|
|
743
|
+
if (Array.isReadonlyArrayEmpty(next)) return converted;
|
|
744
|
+
const added = Array.map(Array.dedupe(next), (key) => {
|
|
745
|
+
const component = componentOf(key);
|
|
746
|
+
const owner = context(component.document);
|
|
747
|
+
const result = tracked(owner, () => convert({
|
|
748
|
+
...owner,
|
|
749
|
+
visiting: HashSet.make(key)
|
|
750
|
+
}, componentSchemas(owner.spec)[component.name], componentPointer(component.name)));
|
|
751
|
+
return [key, {
|
|
752
|
+
degraded: result.degraded,
|
|
753
|
+
node: result.value
|
|
754
|
+
}];
|
|
755
|
+
});
|
|
756
|
+
return step(Array.reduce(added, converted, (map, entry) => HashMap.set(map, entry[0], entry[1])), Array.flatMap(added, (entry) => targets(entry[1].node)));
|
|
757
|
+
}
|
|
758
|
+
return step(HashMap.empty(), roots);
|
|
759
|
+
}
|
|
760
|
+
/** Which actions reach a schema the compiler could not represent faithfully, and why. */
|
|
761
|
+
function unsupported(own, roots, components) {
|
|
762
|
+
const referrers = Array.groupBy(Array.flatMap(Array.fromIterable(components), (entry) => Array.map(targets(entry[1].node), (target) => [target, entry[0]])), (entry) => entry[0]);
|
|
763
|
+
function taint(found, pending) {
|
|
764
|
+
const next = Array.filter(Array.dedupe(pending), (key) => !HashSet.has(found, key));
|
|
765
|
+
if (Array.isReadonlyArrayEmpty(next)) return found;
|
|
766
|
+
return taint(Array.reduce(next, found, (all, key) => HashSet.add(all, key)), Array.flatMap(next, (key) => Array.map(referrers[key] ?? [], (entry) => entry[1])));
|
|
767
|
+
}
|
|
768
|
+
const tainted = taint(HashSet.empty(), Array.flatMap(Array.fromIterable(components), (entry) => Array.isReadonlyArrayEmpty(entry[1].degraded) ? [] : [entry[0]]));
|
|
769
|
+
function reasons(pending, seen) {
|
|
770
|
+
const next = Array.filter(Array.dedupe(pending), (key) => HashSet.has(tainted, key) && !HashSet.has(seen, key));
|
|
771
|
+
if (Array.isReadonlyArrayEmpty(next)) return [];
|
|
772
|
+
const component = (key) => HashMap.getUnsafe(components, key);
|
|
773
|
+
return [...Array.flatMap(next, (key) => component(key).degraded), ...reasons(Array.flatMap(next, (key) => targets(component(key).node)), Array.reduce(next, seen, (all, key) => HashSet.add(all, key)))];
|
|
774
|
+
}
|
|
775
|
+
return Array.map(own, (degraded, index) => [...degraded, ...reasons(Array.getUnsafe(roots, index), HashSet.empty())]);
|
|
776
|
+
}
|
|
777
|
+
function nameDeclarations(actions, groups) {
|
|
778
|
+
const candidates = Array.map(groups, (members) => {
|
|
779
|
+
const chosen = Array.headNonEmpty(members);
|
|
780
|
+
const inline = Option.getOrUndefined(chosen.inline);
|
|
781
|
+
if (inline === void 0) {
|
|
782
|
+
const name = pascal(componentOf(chosen.key).name);
|
|
783
|
+
return {
|
|
784
|
+
key: chosen.key,
|
|
785
|
+
plain: name,
|
|
786
|
+
qualified: name
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
const users = Array.getSomes(Array.map(members, (member) => member.inline));
|
|
790
|
+
const role = Array.every(users, (user) => user.role === inline.role) ? inline.role : "";
|
|
791
|
+
const first = Array.getUnsafe(actions, inline.action).operation;
|
|
792
|
+
const shared = Array.isReadonlyArrayNonEmpty(users) ? sharedWords(Array.map(users, (user) => Array.getUnsafe(actions, user.action).operation.actionId)) : "";
|
|
793
|
+
const title = Array.findFirst(users, (user) => user.title);
|
|
794
|
+
if (role === "Parameters" || users.length > 1 && (Option.isSome(title) || String.isEmpty(shared))) {
|
|
795
|
+
const name = `${pascal(Option.getOrElse(title, () => String.isEmpty(shared) ? Array.match(Array.map(fieldsOf(chosen.node), (field) => field.name), {
|
|
796
|
+
onEmpty: () => chosen.node._tag === "Struct" ? "empty" : chosen.node._tag,
|
|
797
|
+
onNonEmpty: Array.join(" ")
|
|
798
|
+
}) : shared))}${role}`;
|
|
799
|
+
return {
|
|
800
|
+
key: chosen.key,
|
|
801
|
+
plain: name,
|
|
802
|
+
qualified: name
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
const words = users.length === 1 ? first.actionId : shared;
|
|
806
|
+
return {
|
|
807
|
+
key: chosen.key,
|
|
808
|
+
plain: `${pascal(words)}${role}`,
|
|
809
|
+
qualified: `${pascal(`${first.groupId} ${words}`)}${role}`
|
|
810
|
+
};
|
|
811
|
+
});
|
|
812
|
+
const byPlain = Array.groupBy(candidates, (candidate) => candidate.plain);
|
|
813
|
+
return pipe(candidates, Array.map((candidate) => [candidate.key, Option.getOrThrow(Record.get(byPlain, candidate.plain)).length > 1 ? candidate.qualified : candidate.plain]), Array.groupBy((entry) => entry[1]), Record.values, (grouped) => Array.reduce(grouped, {
|
|
814
|
+
names: HashMap.empty(),
|
|
815
|
+
used: HashSet.fromIterable(Array.map(grouped, (entries) => Array.headNonEmpty(entries)[1]))
|
|
816
|
+
}, (state, entries) => Array.reduce(entries, state, (current, entry, index) => {
|
|
817
|
+
if (index === 0) return {
|
|
818
|
+
...current,
|
|
819
|
+
names: HashMap.set(current.names, entry[0], entry[1])
|
|
820
|
+
};
|
|
821
|
+
const name = numbered(current.used, entry[1], index + 1);
|
|
822
|
+
return {
|
|
823
|
+
names: HashMap.set(current.names, entry[0], name),
|
|
824
|
+
used: HashSet.add(current.used, name)
|
|
825
|
+
};
|
|
826
|
+
})).names);
|
|
827
|
+
}
|
|
828
|
+
function numbered(used, base, index) {
|
|
829
|
+
const name = `${base}${index}`;
|
|
830
|
+
if (HashSet.has(used, name)) return numbered(used, base, index + 1);
|
|
831
|
+
return name;
|
|
832
|
+
}
|
|
833
|
+
function contractTargets(contract) {
|
|
834
|
+
return [...targets(contract.input), ...targets(contract.output)];
|
|
835
|
+
}
|
|
836
|
+
function catalog(documents, actions) {
|
|
837
|
+
const degraded = MutableRef.make([]);
|
|
838
|
+
function context(document) {
|
|
839
|
+
return {
|
|
840
|
+
degraded,
|
|
841
|
+
document,
|
|
842
|
+
spec: Array.getUnsafe(documents, document),
|
|
843
|
+
visiting: HashSet.empty()
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
const tracking = Array.map(actions, (action) => tracked(context(action.document), () => contractOf(context(action.document), action.operation)));
|
|
847
|
+
const read = Array.map(tracking, (entry) => entry.value);
|
|
848
|
+
function isParameter(index) {
|
|
849
|
+
return (entry) => Array.some(Array.getUnsafe(actions, index).operation.parameters, (parameter) => parameter.location !== "path" && parameter.name === entry.name);
|
|
850
|
+
}
|
|
851
|
+
const inline = Array.flatMap(read, (contract, index) => {
|
|
852
|
+
const action = Array.getUnsafe(actions, index);
|
|
853
|
+
function titleOf(schema) {
|
|
854
|
+
return Option.flatMap(schema, (found) => Option.liftPredicate(resolve(context(action.document).spec, found.value)?.title, Predicate.isString));
|
|
855
|
+
}
|
|
856
|
+
const payload = Option.getOrUndefined(Array.findFirst(fieldsOf(contract.input), (entry) => entry.name === "payload"));
|
|
857
|
+
const parameters = Array.filter(fieldsOf(contract.input), isParameter(index));
|
|
858
|
+
return [
|
|
859
|
+
...Boolean.match(Array.isArrayNonEmpty(parameters), {
|
|
860
|
+
onFalse: () => [],
|
|
861
|
+
onTrue: () => [{
|
|
862
|
+
action: index,
|
|
863
|
+
key: `inline:${index}:parameters`,
|
|
864
|
+
node: {
|
|
865
|
+
_tag: "Struct",
|
|
866
|
+
fields: parameters,
|
|
867
|
+
rest: false,
|
|
868
|
+
spreads: []
|
|
869
|
+
},
|
|
870
|
+
role: "Parameters",
|
|
871
|
+
title: Option.none()
|
|
872
|
+
}]
|
|
873
|
+
}),
|
|
874
|
+
...payload !== void 0 && isComplex(payload.node) ? [{
|
|
875
|
+
action: index,
|
|
876
|
+
key: `inline:${index}:body`,
|
|
877
|
+
node: payload.node,
|
|
878
|
+
role: "Body",
|
|
879
|
+
title: titleOf(Option.flatMap(Array.head(action.operation.bodies), (body) => body.schema))
|
|
880
|
+
}] : [],
|
|
881
|
+
...Boolean.match(isComplex(contract.output), {
|
|
882
|
+
onFalse: () => [],
|
|
883
|
+
onTrue: () => [{
|
|
884
|
+
action: index,
|
|
885
|
+
key: `inline:${index}:response`,
|
|
886
|
+
node: contract.output,
|
|
887
|
+
role: "Response",
|
|
888
|
+
title: titleOf(Option.flatMap(Array.head(action.operation.responses), (response) => response.schema))
|
|
889
|
+
}]
|
|
890
|
+
})
|
|
891
|
+
];
|
|
892
|
+
});
|
|
893
|
+
const converted = convertComponents(context, Array.flatMap(read, contractTargets));
|
|
894
|
+
const components = HashMap.map(converted, (entry) => entry.node);
|
|
895
|
+
const broken = unsupported(Array.map(tracking, (entry) => entry.degraded), Array.map(read, contractTargets), converted);
|
|
896
|
+
function alias(key, seen = HashSet.empty()) {
|
|
897
|
+
return pipe(HashMap.get(components, key), Option.flatMap((node) => node._tag === "Ref" && node.description === void 0 && !HashSet.has(seen, node.target) ? Option.some(node.target) : Option.none()), Option.match({
|
|
898
|
+
onNone: () => key,
|
|
899
|
+
onSome: (next) => alias(next, HashSet.add(seen, key))
|
|
900
|
+
}));
|
|
901
|
+
}
|
|
902
|
+
const named = pipe([...Array.map(Array.filter(Array.fromIterable(components), (entry) => alias(entry[0]) === entry[0]), (entry) => ({
|
|
903
|
+
inline: Option.none(),
|
|
904
|
+
key: entry[0],
|
|
905
|
+
node: entry[1]
|
|
906
|
+
})), ...Array.map(inline, (entry) => ({
|
|
907
|
+
inline: Option.some(entry),
|
|
908
|
+
key: entry.key,
|
|
909
|
+
node: entry.node
|
|
910
|
+
}))], Array.map((entry) => ({
|
|
911
|
+
...entry,
|
|
912
|
+
node: retarget(entry.node, (target) => alias(target))
|
|
913
|
+
})), Array.sort(Order.mapInput(Order.String, (entry) => entry.key)));
|
|
914
|
+
const identity = identities(Array.map(named, (entry) => ({
|
|
915
|
+
content: JSON.stringify(entry.node, (field, value) => {
|
|
916
|
+
if (field === "target") return "";
|
|
917
|
+
if (field === "spreads" && Array.isArray(value)) return value.length;
|
|
918
|
+
return value;
|
|
919
|
+
}),
|
|
920
|
+
key: entry.key,
|
|
921
|
+
refs: targets(entry.node)
|
|
922
|
+
})));
|
|
923
|
+
const groups = pipe(named, Array.groupBy((entry) => HashMap.getUnsafe(identity, entry.key)), Record.values, Array.map((members) => Array.sort(members, Order.mapInput(Order.Boolean, (entry) => Option.isSome(entry.inline)))));
|
|
924
|
+
const shared = Array.filter(groups, (members) => members.length > 1 || !Option.exists(Array.headNonEmpty(members).inline, (entry) => entry.role === "Parameters"));
|
|
925
|
+
const declaredKeys = HashSet.fromIterable(Array.flatMap(shared, (members) => Array.map(members, (member) => member.key)));
|
|
926
|
+
const representative = HashMap.fromIterable(Array.flatMap(shared, (members) => Array.map(members, (member) => [member.key, Array.headNonEmpty(members).key])));
|
|
927
|
+
function canonical(key) {
|
|
928
|
+
return Option.getOrElse(HashMap.get(representative, alias(key)), () => key);
|
|
929
|
+
}
|
|
930
|
+
const nodes = HashMap.fromIterable(Array.map(shared, (members) => {
|
|
931
|
+
const chosen = Array.headNonEmpty(members);
|
|
932
|
+
return [chosen.key, retarget(chosen.node, canonical)];
|
|
933
|
+
}));
|
|
934
|
+
function flatten(node, seen) {
|
|
935
|
+
const mapped = mapChildren(node, (child) => flatten(child, seen));
|
|
936
|
+
if (mapped._tag !== "Struct") return mapped;
|
|
937
|
+
const bases = Array.flatMap(mapped.spreads, (spread) => {
|
|
938
|
+
const base = canonical(spread);
|
|
939
|
+
const flat = Boolean.match(HashSet.has(seen, base), {
|
|
940
|
+
onFalse: () => flatten(HashMap.getUnsafe(nodes, base), HashSet.add(seen, base)),
|
|
941
|
+
onTrue: () => unknown
|
|
942
|
+
});
|
|
943
|
+
if (flat._tag === "Struct") return [flat];
|
|
944
|
+
return [];
|
|
945
|
+
});
|
|
946
|
+
const fields = [...Array.flatMap(bases, (base) => base.fields), ...mapped.fields];
|
|
947
|
+
return {
|
|
948
|
+
...mapped,
|
|
949
|
+
fields: Record.values(Record.fromEntries(Array.map(fields, (field) => [field.name, field]))),
|
|
950
|
+
spreads: []
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
const flattened = HashMap.map(nodes, (node, key) => flatten(node, HashSet.make(key)));
|
|
954
|
+
const contracts = Array.map(read, (contract, index) => {
|
|
955
|
+
function key(role) {
|
|
956
|
+
return `inline:${index}:${role}`;
|
|
957
|
+
}
|
|
958
|
+
const spread = HashSet.has(declaredKeys, key("parameters"));
|
|
959
|
+
function input(node) {
|
|
960
|
+
if (node._tag !== "Struct") return flatten(node, HashSet.empty());
|
|
961
|
+
return {
|
|
962
|
+
_tag: "Struct",
|
|
963
|
+
fields: Array.flatMap(fieldsOf(node), (entry) => {
|
|
964
|
+
if (spread && isParameter(index)(entry)) return [];
|
|
965
|
+
if (entry.name === "payload" && HashSet.has(declaredKeys, key("body"))) return [{
|
|
966
|
+
...entry,
|
|
967
|
+
node: {
|
|
968
|
+
_tag: "Ref",
|
|
969
|
+
lazy: false,
|
|
970
|
+
target: key("body")
|
|
971
|
+
}
|
|
972
|
+
}];
|
|
973
|
+
return [{
|
|
974
|
+
...entry,
|
|
975
|
+
node: flatten(entry.node, HashSet.empty())
|
|
976
|
+
}];
|
|
977
|
+
}),
|
|
978
|
+
rest: false,
|
|
979
|
+
spreads: Boolean.match(spread, {
|
|
980
|
+
onFalse: () => [],
|
|
981
|
+
onTrue: () => [key("parameters")]
|
|
982
|
+
})
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
return {
|
|
986
|
+
...contract,
|
|
987
|
+
input: input(contract.input),
|
|
988
|
+
output: Boolean.match(HashSet.has(declaredKeys, key("response")), {
|
|
989
|
+
onFalse: () => flatten(contract.output, HashSet.empty()),
|
|
990
|
+
onTrue: () => ({
|
|
991
|
+
_tag: "Ref",
|
|
992
|
+
lazy: false,
|
|
993
|
+
target: key("response")
|
|
994
|
+
})
|
|
995
|
+
})
|
|
996
|
+
};
|
|
997
|
+
});
|
|
998
|
+
function visit(known, keys) {
|
|
999
|
+
return Array.reduce(keys, known, (current, key) => HashSet.has(current, key) ? current : visit(HashSet.add(current, key), targets(HashMap.getUnsafe(flattened, key))));
|
|
1000
|
+
}
|
|
1001
|
+
const reached = visit(HashSet.empty(), Array.flatMap(contracts, (contract) => Array.map(contractTargets(contract), canonical)));
|
|
1002
|
+
const reachable = Array.filter(shared, (members) => HashSet.has(reached, Array.headNonEmpty(members).key));
|
|
1003
|
+
const names = nameDeclarations(actions, reachable);
|
|
1004
|
+
const declared = Array.map(reachable, (members) => Array.headNonEmpty(members).key);
|
|
1005
|
+
const cycles = Array.map(stronglyConnected(Array.map(declared, (key) => ({
|
|
1006
|
+
key,
|
|
1007
|
+
refs: targets(HashMap.getUnsafe(flattened, key))
|
|
1008
|
+
}))), (component) => Array.sort(component, Order.String));
|
|
1009
|
+
const cycleOf = HashMap.fromIterable(Array.flatMap(cycles, (members, index) => Array.map(members, (key) => [key, index])));
|
|
1010
|
+
const spreadTargets = HashSet.fromIterable(Array.flatMap(contracts, (contract) => contract.input._tag === "Struct" ? Array.map(contract.input.spreads, canonical) : []));
|
|
1011
|
+
return {
|
|
1012
|
+
...directional(Array.map(contracts, (contract) => ({
|
|
1013
|
+
...contract,
|
|
1014
|
+
input: retarget(contract.input, (target) => HashMap.getUnsafe(names, canonical(target))),
|
|
1015
|
+
output: retarget(contract.output, (target) => HashMap.getUnsafe(names, canonical(target)))
|
|
1016
|
+
})), Array.flatMap(Array.reverse(cycles), (members) => Array.map(members, (key) => ({
|
|
1017
|
+
name: HashMap.getUnsafe(names, key),
|
|
1018
|
+
node: retarget(HashMap.getUnsafe(flattened, key), (target) => HashMap.getUnsafe(names, target), (target) => HashMap.getUnsafe(cycleOf, target) === HashMap.getUnsafe(cycleOf, key)),
|
|
1019
|
+
spread: HashSet.has(spreadTargets, key)
|
|
1020
|
+
})))),
|
|
1021
|
+
unsupported: broken
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
function converts(node) {
|
|
1025
|
+
return node._tag === "Bytes" || node._tag === "Instant";
|
|
1026
|
+
}
|
|
1027
|
+
const conversionNotes = {
|
|
1028
|
+
Bytes: {
|
|
1029
|
+
input: "Pass a file: contentBase64 holds its bytes in standard Base64.",
|
|
1030
|
+
output: "Returned as a file."
|
|
1031
|
+
},
|
|
1032
|
+
Instant: {
|
|
1033
|
+
input: "Pass an ISO 8601 date-time.",
|
|
1034
|
+
output: "Returned as an ISO 8601 date-time in UTC."
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
function holdsDirectional(node) {
|
|
1038
|
+
return converts(node) || node._tag === "Struct" && Array.some(node.fields, (entry) => entry.readOnly === true || entry.writeOnly === true) || Array.some(children(node), holdsDirectional);
|
|
1039
|
+
}
|
|
1040
|
+
function directional(contracts, declarations) {
|
|
1041
|
+
const nodes = HashMap.fromIterable(Array.map(declarations, (declaration) => [declaration.name, declaration.node]));
|
|
1042
|
+
function lazyTargets(node) {
|
|
1043
|
+
return node._tag === "Ref" && node.lazy ? [node.target] : Array.flatMap(children(node), lazyTargets);
|
|
1044
|
+
}
|
|
1045
|
+
const recursive = HashSet.fromIterable(Array.flatMap([...Array.map(declarations, (declaration) => declaration.node), ...Array.flatMap(contracts, (contract) => [contract.input, contract.output])], lazyTargets));
|
|
1046
|
+
function cyclic(member) {
|
|
1047
|
+
return member._tag === "Ref" && (member.lazy || HashSet.has(recursive, member.target));
|
|
1048
|
+
}
|
|
1049
|
+
function preservedMember(member) {
|
|
1050
|
+
if (member._tag !== "Ref") return member;
|
|
1051
|
+
const target = HashMap.getUnsafe(nodes, member.target);
|
|
1052
|
+
const base = withoutNull(target);
|
|
1053
|
+
if (base._tag !== "Struct" || base.rest || base.additionalProperties?._tag === "Never") return member;
|
|
1054
|
+
if (target._tag === "NullOr") return nullOr({
|
|
1055
|
+
...base,
|
|
1056
|
+
rest: true
|
|
1057
|
+
});
|
|
1058
|
+
if (cyclic(member)) return {
|
|
1059
|
+
...base,
|
|
1060
|
+
rest: true
|
|
1061
|
+
};
|
|
1062
|
+
return {
|
|
1063
|
+
...base,
|
|
1064
|
+
fields: [],
|
|
1065
|
+
rest: true,
|
|
1066
|
+
spreads: [member.target]
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
function preservesReferences(node) {
|
|
1070
|
+
return node._tag === "Union" && node.preserve === true && Array.some(node.members, (member) => preservedMember(member) !== member) || Array.some(children(node), preservesReferences);
|
|
1071
|
+
}
|
|
1072
|
+
function spreading(known) {
|
|
1073
|
+
const next = HashSet.fromIterable(Array.filter(Array.map(declarations, (declaration) => declaration.name), (name) => HashSet.has(known, name) || Array.some(targets(HashMap.getUnsafe(nodes, name)), (target) => HashSet.has(known, target))));
|
|
1074
|
+
if (HashSet.size(next) === HashSet.size(known)) return known;
|
|
1075
|
+
return spreading(next);
|
|
1076
|
+
}
|
|
1077
|
+
const split = spreading(HashSet.fromIterable(Array.map(Array.filter(declarations, (declaration) => holdsDirectional(declaration.node) || preservesReferences(declaration.node)), (declaration) => declaration.name)));
|
|
1078
|
+
const taken = HashSet.fromIterable(Array.map(declarations, (declaration) => declaration.name));
|
|
1079
|
+
function free(base, index) {
|
|
1080
|
+
const name = Boolean.match(index === 1, {
|
|
1081
|
+
onFalse: () => `${base}${index}`,
|
|
1082
|
+
onTrue: () => base
|
|
1083
|
+
});
|
|
1084
|
+
if (HashSet.has(taken, name)) return free(base, index + 1);
|
|
1085
|
+
return name;
|
|
1086
|
+
}
|
|
1087
|
+
const inputNames = HashMap.fromIterable(Array.map(Array.fromIterable(split), (name) => [name, free(`${name}Input`, 1)]));
|
|
1088
|
+
function inputName(name) {
|
|
1089
|
+
return Option.getOrElse(HashMap.get(inputNames, name), () => name);
|
|
1090
|
+
}
|
|
1091
|
+
const openObject = {
|
|
1092
|
+
_tag: "Struct",
|
|
1093
|
+
fields: [],
|
|
1094
|
+
rest: true,
|
|
1095
|
+
spreads: []
|
|
1096
|
+
};
|
|
1097
|
+
function form(node, direction, inlined = HashSet.empty()) {
|
|
1098
|
+
const inlining = direction === "input" && node._tag === "Union" && node.preserve === true ? Array.filter(node.members, (member) => cyclic(member) && member._tag === "Ref" && !HashSet.has(inlined, member.target)) : [];
|
|
1099
|
+
const source = direction === "input" && node._tag === "Union" && node.preserve === true ? {
|
|
1100
|
+
...node,
|
|
1101
|
+
members: Array.map(node.members, (member) => cyclic(member) && member._tag === "Ref" && HashSet.has(inlined, member.target) ? openObject : preservedMember(member))
|
|
1102
|
+
} : node;
|
|
1103
|
+
const nested = Array.reduce(inlining, inlined, (known, member) => member._tag === "Ref" ? HashSet.add(known, member.target) : known);
|
|
1104
|
+
const mapped = mapChildren(source, (child) => form(child, direction, nested));
|
|
1105
|
+
if (converts(mapped)) return {
|
|
1106
|
+
...mapped,
|
|
1107
|
+
description: Array.join([...mapped.description === void 0 ? [] : [mapped.description], conversionNotes[mapped._tag][direction]], " "),
|
|
1108
|
+
direction
|
|
1109
|
+
};
|
|
1110
|
+
if (mapped._tag === "Ref" && direction === "input") return {
|
|
1111
|
+
...mapped,
|
|
1112
|
+
target: inputName(mapped.target)
|
|
1113
|
+
};
|
|
1114
|
+
if (mapped._tag !== "Struct") return mapped;
|
|
1115
|
+
const excluded = Array.map(Array.filter(mapped.fields, (entry) => direction === "input" ? entry.readOnly === true : entry.writeOnly === true), (entry) => entry.name);
|
|
1116
|
+
return {
|
|
1117
|
+
...mapped,
|
|
1118
|
+
...Array.isArrayEmpty(excluded) ? {} : { excluded: [...mapped.excluded ?? [], ...excluded] },
|
|
1119
|
+
fields: Array.filter(mapped.fields, (entry) => direction === "input" ? entry.readOnly !== true : entry.writeOnly !== true),
|
|
1120
|
+
spreads: direction === "input" ? Array.map(mapped.spreads, inputName) : mapped.spreads
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
const formed = Array.flatMap(declarations, (declaration) => HashSet.has(split, declaration.name) ? [{
|
|
1124
|
+
...declaration,
|
|
1125
|
+
node: form(declaration.node, "output")
|
|
1126
|
+
}, {
|
|
1127
|
+
...declaration,
|
|
1128
|
+
name: inputName(declaration.name),
|
|
1129
|
+
node: form(declaration.node, "input")
|
|
1130
|
+
}] : [declaration]);
|
|
1131
|
+
const formedContracts = Array.map(contracts, (contract) => ({
|
|
1132
|
+
...contract,
|
|
1133
|
+
input: form(contract.input, "input"),
|
|
1134
|
+
output: form(contract.output, "output")
|
|
1135
|
+
}));
|
|
1136
|
+
const formedNodes = HashMap.fromIterable(Array.map(formed, (declaration) => [declaration.name, declaration.node]));
|
|
1137
|
+
function reach(known, keys) {
|
|
1138
|
+
return Array.reduce(keys, known, (current, key) => {
|
|
1139
|
+
if (HashSet.has(current, key)) return current;
|
|
1140
|
+
return reach(HashSet.add(current, key), targets(HashMap.getUnsafe(formedNodes, key)));
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
const reached = reach(HashSet.empty(), Array.flatMap(formedContracts, contractTargets));
|
|
1144
|
+
function spreadsOf(node) {
|
|
1145
|
+
return [...node._tag === "Struct" ? node.spreads : [], ...Array.flatMap(children(node), spreadsOf)];
|
|
1146
|
+
}
|
|
1147
|
+
const spread = HashSet.fromIterable([...Array.flatMap(formed, (declaration) => spreadsOf(declaration.node)), ...Array.flatMap(formedContracts, (contract) => [...spreadsOf(contract.input), ...spreadsOf(contract.output)])]);
|
|
1148
|
+
return {
|
|
1149
|
+
contracts: formedContracts,
|
|
1150
|
+
declarations: Array.map(Array.filter(formed, (declaration) => HashSet.has(reached, declaration.name)), (declaration) => ({
|
|
1151
|
+
...declaration,
|
|
1152
|
+
spread: declaration.spread || HashSet.has(spread, declaration.name)
|
|
1153
|
+
}))
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
function enumsOf(node, field) {
|
|
1157
|
+
return pipe(Match.value(node), Match.tag("Literals", (current) => Boolean.match(current.literals.length > 1 && Array.every(current.literals, Predicate.isString), {
|
|
1158
|
+
onFalse: () => [],
|
|
1159
|
+
onTrue: () => [{
|
|
1160
|
+
field,
|
|
1161
|
+
literals: current.literals
|
|
1162
|
+
}]
|
|
1163
|
+
})), Match.tag("Struct", (current) => [...Array.flatMap(current.fields, (entry) => enumsOf(entry.node, Option.some(entry.name))), ...current.additionalProperties === void 0 ? [] : enumsOf(current.additionalProperties, field)]), Match.orElse((current) => Array.flatMap(children(current), (child) => enumsOf(child, field))));
|
|
1164
|
+
}
|
|
1165
|
+
function referEnums(node, names) {
|
|
1166
|
+
if (node._tag === "Literals") return Option.match(HashMap.get(names, JSON.stringify(node.literals)), {
|
|
1167
|
+
onNone: () => node,
|
|
1168
|
+
onSome: (target) => ({
|
|
1169
|
+
_tag: "Ref",
|
|
1170
|
+
lazy: false,
|
|
1171
|
+
target,
|
|
1172
|
+
...Struct.pick(node, ["description"])
|
|
1173
|
+
})
|
|
1174
|
+
});
|
|
1175
|
+
return mapChildren(node, (child) => referEnums(child, names));
|
|
1176
|
+
}
|
|
1177
|
+
function namedEnums(catalogued, actions) {
|
|
1178
|
+
const found = [...Array.flatMap(catalogued.declarations, (declaration) => declaration.node._tag === "Literals" ? [] : Array.map(enumsOf(declaration.node, Option.none()), (entry) => ({
|
|
1179
|
+
...entry,
|
|
1180
|
+
owner: declaration.name
|
|
1181
|
+
}))), ...Array.flatMap(catalogued.contracts, (contract, index) => {
|
|
1182
|
+
const action = Array.getUnsafe(actions, index);
|
|
1183
|
+
return Array.map([...enumsOf(contract.input, Option.none()), ...enumsOf(contract.output, Option.none())], (entry) => ({
|
|
1184
|
+
...entry,
|
|
1185
|
+
owner: action.operation.actionId
|
|
1186
|
+
}));
|
|
1187
|
+
})];
|
|
1188
|
+
const candidates = pipe(found, Array.groupBy((entry) => JSON.stringify(entry.literals)), Record.toEntries, Array.map((entry) => {
|
|
1189
|
+
const first = Array.headNonEmpty(entry[1]);
|
|
1190
|
+
return {
|
|
1191
|
+
key: entry[0],
|
|
1192
|
+
node: {
|
|
1193
|
+
_tag: "Literals",
|
|
1194
|
+
literals: first.literals
|
|
1195
|
+
},
|
|
1196
|
+
plain: pascal(Option.getOrElse(first.field, () => `${first.owner} value`)),
|
|
1197
|
+
qualified: pascal(`${first.owner} ${Option.getOrElse(first.field, () => "value")}`)
|
|
1198
|
+
};
|
|
1199
|
+
}));
|
|
1200
|
+
const taken = HashSet.fromIterable(Array.map(catalogued.declarations, (declaration) => declaration.name));
|
|
1201
|
+
function freeName(used, base, index) {
|
|
1202
|
+
const name = index === 1 ? base : `${base}${index}`;
|
|
1203
|
+
return HashSet.has(used, name) ? freeName(used, base, index + 1) : name;
|
|
1204
|
+
}
|
|
1205
|
+
const byPlain = Array.groupBy(candidates, (candidate) => candidate.plain);
|
|
1206
|
+
const names = Array.reduce(candidates, {
|
|
1207
|
+
names: HashMap.empty(),
|
|
1208
|
+
used: taken
|
|
1209
|
+
}, (state, candidate) => {
|
|
1210
|
+
const name = freeName(state.used, HashSet.has(taken, candidate.plain) || Option.getOrThrow(Record.get(byPlain, candidate.plain)).length > 1 ? candidate.qualified : candidate.plain, 1);
|
|
1211
|
+
return {
|
|
1212
|
+
names: HashMap.set(state.names, candidate.key, name),
|
|
1213
|
+
used: HashSet.add(state.used, name)
|
|
1214
|
+
};
|
|
1215
|
+
}).names;
|
|
1216
|
+
return {
|
|
1217
|
+
contracts: Array.map(catalogued.contracts, (contract) => ({
|
|
1218
|
+
...contract,
|
|
1219
|
+
input: referEnums(contract.input, names),
|
|
1220
|
+
output: referEnums(contract.output, names)
|
|
1221
|
+
})),
|
|
1222
|
+
declarations: [...Array.map(candidates, (candidate) => ({
|
|
1223
|
+
name: HashMap.getUnsafe(names, candidate.key),
|
|
1224
|
+
node: candidate.node,
|
|
1225
|
+
spread: false
|
|
1226
|
+
})), ...Array.map(catalogued.declarations, (declaration) => declaration.node._tag === "Literals" ? declaration : {
|
|
1227
|
+
...declaration,
|
|
1228
|
+
node: referEnums(declaration.node, names)
|
|
1229
|
+
})]
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
export { schema_exports as OpenApiSchema, catalog, converts, defaultContentType, hash, identities, namedEnums, targets };
|
|
1233
|
+
|
|
1234
|
+
//# sourceMappingURL=schema.js.map
|