@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,207 @@
|
|
|
1
|
+
import { Array, Option, Predicate, Record, Result, String, pipe } from "effect";
|
|
2
|
+
|
|
3
|
+
import { OpenApiSpec } from "./spec.ts";
|
|
4
|
+
|
|
5
|
+
export function expand(raw: OpenApiSpec.JsonObject) {
|
|
6
|
+
const components = OpenApiSpec.asObject(raw.components) ?? {};
|
|
7
|
+
const schemas = OpenApiSpec.asObject(components.schemas) ?? {};
|
|
8
|
+
function mappingOf(discriminator: OpenApiSpec.JsonObject) {
|
|
9
|
+
return Record.map(OpenApiSpec.asObject(discriminator["mapping"]) ?? {}, target =>
|
|
10
|
+
Predicate.isString(target) && Record.has(schemas, target)
|
|
11
|
+
? `#/components/schemas/${OpenApiSpec.pointerToken(target)}`
|
|
12
|
+
: target,
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
const bases = Record.getSomes(
|
|
16
|
+
Record.map(schemas, (schema, name) => {
|
|
17
|
+
const node = OpenApiSpec.asObject(schema) ?? {};
|
|
18
|
+
const declarations = [
|
|
19
|
+
node,
|
|
20
|
+
...Array.filter(Array.isArray(node.allOf) ? node.allOf : [], Predicate.isObject),
|
|
21
|
+
];
|
|
22
|
+
return Array.findFirst(declarations, declaration => {
|
|
23
|
+
const discriminator = OpenApiSpec.asObject(declaration["discriminator"]);
|
|
24
|
+
const propertyName = discriminator?.["propertyName"];
|
|
25
|
+
if (
|
|
26
|
+
discriminator === undefined ||
|
|
27
|
+
!Predicate.isString(propertyName) ||
|
|
28
|
+
!(
|
|
29
|
+
Predicate.isObject(discriminator["mapping"]) ||
|
|
30
|
+
Array.isArray(node["oneOf"]) ||
|
|
31
|
+
Array.isArray(node["anyOf"])
|
|
32
|
+
)
|
|
33
|
+
) {
|
|
34
|
+
return Option.none();
|
|
35
|
+
}
|
|
36
|
+
const own = `#/components/schemas/${OpenApiSpec.pointerToken(name)}`;
|
|
37
|
+
return Option.some({
|
|
38
|
+
base: `${name}DualBase`,
|
|
39
|
+
defaultValue:
|
|
40
|
+
OpenApiSpec.asObject(OpenApiSpec.asObject(declaration["properties"])?.[propertyName])?.[
|
|
41
|
+
"default"
|
|
42
|
+
] ??
|
|
43
|
+
Option.getOrUndefined(
|
|
44
|
+
Option.map(
|
|
45
|
+
Array.findFirst(
|
|
46
|
+
Record.toEntries(mappingOf(discriminator)),
|
|
47
|
+
entry => entry[1] === own,
|
|
48
|
+
),
|
|
49
|
+
entry => entry[0],
|
|
50
|
+
),
|
|
51
|
+
),
|
|
52
|
+
discriminator,
|
|
53
|
+
node,
|
|
54
|
+
propertyName,
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
const collision = Option.getOrUndefined(
|
|
60
|
+
Array.findFirst(Record.values(bases), entry => Record.has(schemas, entry.base)),
|
|
61
|
+
);
|
|
62
|
+
if (collision !== undefined) {
|
|
63
|
+
return Result.fail(
|
|
64
|
+
`The source schema ${collision.base} conflicts with a generated discriminator base`,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
function inheritance(node: unknown): unknown {
|
|
68
|
+
if (!Predicate.isObject(node)) {
|
|
69
|
+
return node;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
...node,
|
|
73
|
+
...(Array.isArray(node.allOf)
|
|
74
|
+
? {
|
|
75
|
+
allOf: Array.map(node.allOf, member => {
|
|
76
|
+
const ref = OpenApiSpec.asObject(member)?.$ref;
|
|
77
|
+
const base =
|
|
78
|
+
Predicate.isString(ref) && pipe(ref, String.startsWith("#/components/schemas/"))
|
|
79
|
+
? bases[
|
|
80
|
+
OpenApiSpec.pointerKey(
|
|
81
|
+
pipe(ref, String.slice("#/components/schemas/".length)),
|
|
82
|
+
)
|
|
83
|
+
]
|
|
84
|
+
: undefined;
|
|
85
|
+
return base === undefined
|
|
86
|
+
? inheritance(member)
|
|
87
|
+
: {
|
|
88
|
+
...OpenApiSpec.asObject(member),
|
|
89
|
+
$ref: `#/components/schemas/${OpenApiSpec.pointerToken(base.base)}`,
|
|
90
|
+
};
|
|
91
|
+
}),
|
|
92
|
+
}
|
|
93
|
+
: {}),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const expanded = Result.all(
|
|
97
|
+
Record.map(bases, (declared, name) => {
|
|
98
|
+
const mapping = {
|
|
99
|
+
...mappingOf(declared.discriminator),
|
|
100
|
+
...(Predicate.isString(declared.defaultValue)
|
|
101
|
+
? { [declared.defaultValue]: `#/components/schemas/${OpenApiSpec.pointerToken(name)}` }
|
|
102
|
+
: {}),
|
|
103
|
+
};
|
|
104
|
+
const explicit = pipe(
|
|
105
|
+
[declared.node["oneOf"], declared.node["anyOf"]],
|
|
106
|
+
Array.findFirst(value => Array.isArray(value)),
|
|
107
|
+
Option.getOrElse(() => []),
|
|
108
|
+
);
|
|
109
|
+
const implicit = Array.getSomes(
|
|
110
|
+
Array.map(explicit, member => {
|
|
111
|
+
const ref = OpenApiSpec.asObject(member)?.["$ref"];
|
|
112
|
+
return Predicate.isString(ref) && !Array.contains(Record.values(mapping), ref)
|
|
113
|
+
? Option.some([
|
|
114
|
+
OpenApiSpec.pointerKey(pipe(ref, String.split("/"), Array.lastNonEmpty)),
|
|
115
|
+
ref,
|
|
116
|
+
] as const)
|
|
117
|
+
: Option.none();
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
return Result.map(
|
|
121
|
+
Result.all(
|
|
122
|
+
Array.map([...Record.toEntries(mapping), ...implicit], variant => {
|
|
123
|
+
const ref = variant[1];
|
|
124
|
+
if (
|
|
125
|
+
!Predicate.isString(ref) ||
|
|
126
|
+
!pipe(ref, String.startsWith("#/components/schemas/"))
|
|
127
|
+
) {
|
|
128
|
+
return Result.fail(`Discriminator ${name} has a non-local mapping for ${variant[0]}`);
|
|
129
|
+
}
|
|
130
|
+
const target = OpenApiSpec.pointerKey(
|
|
131
|
+
pipe(ref, String.slice("#/components/schemas/".length)),
|
|
132
|
+
);
|
|
133
|
+
if (!Record.has(schemas, target)) {
|
|
134
|
+
return Result.fail(`Discriminator ${name} refers to missing schema ${target}`);
|
|
135
|
+
}
|
|
136
|
+
const base = bases[target];
|
|
137
|
+
return Result.succeed({
|
|
138
|
+
allOf: [
|
|
139
|
+
{
|
|
140
|
+
$ref:
|
|
141
|
+
base === undefined
|
|
142
|
+
? ref
|
|
143
|
+
: `#/components/schemas/${OpenApiSpec.pointerToken(base.base)}`,
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
properties: {
|
|
147
|
+
[declared.propertyName]: {
|
|
148
|
+
enum: [variant[0]],
|
|
149
|
+
type: "string",
|
|
150
|
+
...(target === name && declared.defaultValue === variant[0]
|
|
151
|
+
? { default: variant[0] }
|
|
152
|
+
: {}),
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
required: [declared.propertyName],
|
|
156
|
+
type: "object",
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
});
|
|
160
|
+
}),
|
|
161
|
+
),
|
|
162
|
+
variants => {
|
|
163
|
+
const own = `#/components/schemas/${OpenApiSpec.pointerToken(name)}`;
|
|
164
|
+
const alternatives = [
|
|
165
|
+
...variants,
|
|
166
|
+
...Array.filter(
|
|
167
|
+
explicit,
|
|
168
|
+
member => !Predicate.isString(OpenApiSpec.asObject(member)?.["$ref"]),
|
|
169
|
+
),
|
|
170
|
+
];
|
|
171
|
+
if (Array.isArray(declared.node["oneOf"])) {
|
|
172
|
+
return { oneOf: alternatives };
|
|
173
|
+
}
|
|
174
|
+
if (Array.isArray(declared.node["anyOf"])) {
|
|
175
|
+
return { anyOf: alternatives };
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
anyOf: Array.some(
|
|
179
|
+
[...Record.values(mapping), ...Array.map(implicit, entry => entry[1])],
|
|
180
|
+
ref => ref === own,
|
|
181
|
+
)
|
|
182
|
+
? variants
|
|
183
|
+
: [
|
|
184
|
+
...variants,
|
|
185
|
+
{ $ref: `#/components/schemas/${OpenApiSpec.pointerToken(declared.base)}` },
|
|
186
|
+
],
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
}),
|
|
191
|
+
);
|
|
192
|
+
return Result.map(expanded, variants => ({
|
|
193
|
+
...raw,
|
|
194
|
+
components: {
|
|
195
|
+
...components,
|
|
196
|
+
schemas: {
|
|
197
|
+
...Record.map(schemas, inheritance),
|
|
198
|
+
...Record.fromEntries(
|
|
199
|
+
Record.collect(bases, (_, entry) => [entry.base, inheritance(entry.node)] as const),
|
|
200
|
+
),
|
|
201
|
+
...variants,
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
}));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export * as OpenApiPolymorphism from "./polymorphism.ts";
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { Array, Effect, Option, Predicate, Record, Schema, String, pipe } from "effect";
|
|
2
|
+
|
|
3
|
+
import { OpenApiSpec } from "./spec.ts";
|
|
4
|
+
|
|
5
|
+
type JsonObject = Record.ReadonlyRecord<string, unknown>;
|
|
6
|
+
|
|
7
|
+
type Description = typeof Description.Type;
|
|
8
|
+
const Description = Schema.Union([
|
|
9
|
+
Schema.String,
|
|
10
|
+
Schema.Struct({ content: Schema.optionalKey(Schema.String) }),
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
type Pair = typeof Pair.Type;
|
|
14
|
+
const Pair = Schema.Struct({
|
|
15
|
+
disabled: Schema.optionalKey(Schema.Boolean),
|
|
16
|
+
key: Schema.String,
|
|
17
|
+
type: Schema.optionalKey(Schema.String),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
type Request = typeof Request.Type;
|
|
21
|
+
const Request = Schema.Struct({
|
|
22
|
+
body: Schema.optionalKey(
|
|
23
|
+
Schema.Struct({
|
|
24
|
+
formdata: Schema.optionalKey(Schema.mutable(Schema.Array(Pair))),
|
|
25
|
+
mode: Schema.optionalKey(Schema.String),
|
|
26
|
+
options: Schema.optionalKey(
|
|
27
|
+
Schema.Struct({
|
|
28
|
+
raw: Schema.optionalKey(Schema.Struct({ language: Schema.optionalKey(Schema.String) })),
|
|
29
|
+
}),
|
|
30
|
+
),
|
|
31
|
+
raw: Schema.optionalKey(Schema.String),
|
|
32
|
+
urlencoded: Schema.optionalKey(Schema.mutable(Schema.Array(Pair))),
|
|
33
|
+
}),
|
|
34
|
+
),
|
|
35
|
+
description: Schema.optionalKey(Description),
|
|
36
|
+
header: Schema.optionalKey(Schema.mutable(Schema.Array(Pair))),
|
|
37
|
+
method: Schema.String,
|
|
38
|
+
url: Schema.Union([Schema.String, Schema.Struct({ raw: Schema.String })]),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
type Example = typeof Example.Type;
|
|
42
|
+
const Example = Schema.Struct({
|
|
43
|
+
body: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
|
44
|
+
code: Schema.Finite,
|
|
45
|
+
name: Schema.optionalKey(Schema.String),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
type Collection = typeof Collection.Type;
|
|
49
|
+
const Collection = Schema.Struct({
|
|
50
|
+
auth: Schema.optionalKey(
|
|
51
|
+
Schema.Struct({
|
|
52
|
+
apikey: Schema.optionalKey(
|
|
53
|
+
Schema.Array(Schema.Struct({ key: Schema.String, value: Schema.Unknown })),
|
|
54
|
+
),
|
|
55
|
+
type: Schema.String,
|
|
56
|
+
}),
|
|
57
|
+
),
|
|
58
|
+
info: Schema.Struct({
|
|
59
|
+
description: Schema.optionalKey(Description),
|
|
60
|
+
name: Schema.String,
|
|
61
|
+
schema: Schema.String,
|
|
62
|
+
}),
|
|
63
|
+
item: Schema.mutable(Schema.Array(Schema.Unknown)),
|
|
64
|
+
variable: Schema.optionalKey(
|
|
65
|
+
Schema.Array(Schema.Struct({ key: Schema.String, value: Schema.optionalKey(Schema.Unknown) })),
|
|
66
|
+
),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
type Found = { examples: Example[]; folders: string[]; name: string; request: Request };
|
|
70
|
+
|
|
71
|
+
export type Signature = typeof Signature.Type;
|
|
72
|
+
export const Signature = Schema.Struct({
|
|
73
|
+
info: Schema.Struct({
|
|
74
|
+
schema: Schema.String.check(Schema.isPattern(/schema\.getpostman\.com/u)),
|
|
75
|
+
}),
|
|
76
|
+
item: Schema.Array(Schema.Unknown),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
function text(description?: Description) {
|
|
80
|
+
return Predicate.isString(description) ? description : description?.content;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function requests(items: unknown[], folders: string[]): Found[] {
|
|
84
|
+
return Array.flatMap(items, item => {
|
|
85
|
+
const node = OpenApiSpec.asObject(item) ?? {};
|
|
86
|
+
const name = Predicate.isString(node["name"]) ? node["name"] : "";
|
|
87
|
+
if (Array.isArray(node["item"])) return requests(node["item"], [...folders, name]);
|
|
88
|
+
return Option.match(Schema.decodeUnknownOption(Request)(node["request"]), {
|
|
89
|
+
onNone: () => [],
|
|
90
|
+
onSome: request => [
|
|
91
|
+
{
|
|
92
|
+
examples: Array.getSomes(
|
|
93
|
+
Array.map(Array.isArray(node["response"]) ? node["response"] : [], example =>
|
|
94
|
+
Schema.decodeUnknownOption(Example)(example),
|
|
95
|
+
),
|
|
96
|
+
),
|
|
97
|
+
folders,
|
|
98
|
+
name,
|
|
99
|
+
request,
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function infer(value: unknown, nullable: boolean): JsonObject {
|
|
107
|
+
const nulls = nullable ? { nullable: true } : {};
|
|
108
|
+
if (Array.isArray(value)) {
|
|
109
|
+
return {
|
|
110
|
+
items: Array.match(value, {
|
|
111
|
+
onEmpty: () => ({}),
|
|
112
|
+
onNonEmpty: items => infer(Array.headNonEmpty(items), nullable),
|
|
113
|
+
}),
|
|
114
|
+
type: "array",
|
|
115
|
+
...nulls,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
if (Predicate.isObject(value)) {
|
|
119
|
+
return {
|
|
120
|
+
additionalProperties: true,
|
|
121
|
+
properties: Record.map(value, member => infer(member, nullable)),
|
|
122
|
+
type: "object",
|
|
123
|
+
...nulls,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (Predicate.isString(value)) return { type: "string", ...nulls };
|
|
127
|
+
if (Predicate.isNumber(value)) return { type: "number", ...nulls };
|
|
128
|
+
if (Predicate.isBoolean(value)) return { type: "boolean", ...nulls };
|
|
129
|
+
return {};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function mediaOf(body: string, json: boolean, nullable: boolean) {
|
|
133
|
+
return Option.match(Schema.decodeOption(Schema.fromJsonString(Schema.Json))(body), {
|
|
134
|
+
onNone: () =>
|
|
135
|
+
json
|
|
136
|
+
? { "application/json": { schema: {} } }
|
|
137
|
+
: { "text/plain": { schema: { type: "string" } } },
|
|
138
|
+
onSome: parsed => ({ "application/json": { schema: infer(parsed, nullable) } }),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function fields(pairs?: Pair[]) {
|
|
143
|
+
return Array.filter(pairs ?? [], pair => pair.disabled !== true);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function requestBody(request: Request) {
|
|
147
|
+
const body = request.body;
|
|
148
|
+
if (body?.mode === "raw" && Predicate.isString(body.raw)) {
|
|
149
|
+
return { content: mediaOf(body.raw, body.options?.raw?.language === "json", false) };
|
|
150
|
+
}
|
|
151
|
+
const multipart = body?.mode === "formdata";
|
|
152
|
+
const pairs = fields(multipart ? body.formdata : body?.urlencoded);
|
|
153
|
+
if (!Array.isReadonlyArrayNonEmpty(pairs)) return undefined;
|
|
154
|
+
return {
|
|
155
|
+
content: {
|
|
156
|
+
[multipart ? "multipart/form-data" : "application/x-www-form-urlencoded"]: {
|
|
157
|
+
schema: {
|
|
158
|
+
properties: Record.fromIterableWith(pairs, pair => [
|
|
159
|
+
pair.key,
|
|
160
|
+
pair.type === "file" ? { format: "binary", type: "string" } : { type: "string" },
|
|
161
|
+
]),
|
|
162
|
+
type: "object",
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function braces(template: string) {
|
|
170
|
+
return pipe(template, String.replaceAll("{{", "{"), String.replaceAll("}}", "}"));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function split(target: string) {
|
|
174
|
+
const scheme = pipe(
|
|
175
|
+
target,
|
|
176
|
+
String.match(/^[a-z][a-z0-9+.-]*:\/\//iu),
|
|
177
|
+
Option.map(match => match[0]),
|
|
178
|
+
Option.getOrElse(() => ""),
|
|
179
|
+
);
|
|
180
|
+
const rest = pipe(target, String.slice(scheme.length));
|
|
181
|
+
const slash = pipe(
|
|
182
|
+
rest,
|
|
183
|
+
String.indexOf("/"),
|
|
184
|
+
Option.getOrElse(() => rest.length),
|
|
185
|
+
);
|
|
186
|
+
return {
|
|
187
|
+
origin: braces(`${scheme}${pipe(rest, String.slice(0, slash))}`),
|
|
188
|
+
path: pipe(rest, String.slice(slash)),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function pathOf(path: string) {
|
|
193
|
+
const walked = Array.reduce(
|
|
194
|
+
pipe(path, String.split("/")),
|
|
195
|
+
{ names: Array.empty<string>(), previous: "", segments: Array.empty<string>() },
|
|
196
|
+
(found, segment) => {
|
|
197
|
+
const base = `${pipe(found.previous, OpenApiSpec.camelize, OpenApiSpec.singular)}Id`;
|
|
198
|
+
const inferred =
|
|
199
|
+
String.isNonEmpty(found.previous) && /^(?:\d+|[0-9a-f]{8}-[0-9a-f-]{27})$/iu.test(segment);
|
|
200
|
+
const written = inferred
|
|
201
|
+
? `{${Array.contains(found.names, base) ? `${base}${found.names.length + 1}` : base}}`
|
|
202
|
+
: pipe(segment, braces, String.replace(/^:(.+)$/u, "{$1}"));
|
|
203
|
+
const names = OpenApiSpec.templateNames(written);
|
|
204
|
+
return {
|
|
205
|
+
names: [...found.names, ...names],
|
|
206
|
+
previous: Array.isReadonlyArrayEmpty(names) ? segment : found.previous,
|
|
207
|
+
segments: [...found.segments, written],
|
|
208
|
+
};
|
|
209
|
+
},
|
|
210
|
+
);
|
|
211
|
+
return { names: walked.names, template: Array.join(walked.segments, "/") };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function apiKeyValue(collection: Collection, key: string) {
|
|
215
|
+
return pipe(
|
|
216
|
+
collection.auth?.apikey ?? [],
|
|
217
|
+
Array.findFirst(entry => entry.key === key),
|
|
218
|
+
Option.map(entry => entry.value),
|
|
219
|
+
Option.filter(Predicate.isString),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function securityScheme(collection: Collection) {
|
|
224
|
+
const type = collection.auth?.type;
|
|
225
|
+
if (type === "bearer") return { name: "bearerAuth", scheme: { scheme: "bearer", type: "http" } };
|
|
226
|
+
if (type === "basic") return { name: "basicAuth", scheme: { scheme: "basic", type: "http" } };
|
|
227
|
+
if (type !== "apikey") return undefined;
|
|
228
|
+
return {
|
|
229
|
+
name: "apiKeyAuth",
|
|
230
|
+
scheme: {
|
|
231
|
+
in: Option.getOrElse(apiKeyValue(collection, "in"), () => "header"),
|
|
232
|
+
name: Option.getOrElse(apiKeyValue(collection, "key"), () => "Authorization"),
|
|
233
|
+
type: "apiKey",
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Two requests share a method and path but target different hosts, which one operation cannot express. */
|
|
239
|
+
export class PostmanHostConflict extends Schema.TaggedError<PostmanHostConflict>()(
|
|
240
|
+
"PostmanHostConflict",
|
|
241
|
+
{ message: Schema.String },
|
|
242
|
+
) {}
|
|
243
|
+
|
|
244
|
+
export const toOpenApi = Effect.fn("OpenApiPostman.toOpenApi")(function* (
|
|
245
|
+
input: unknown,
|
|
246
|
+
): Effect.fn.Return<JsonObject, Schema.SchemaError | PostmanHostConflict> {
|
|
247
|
+
const collection = yield* Schema.decodeUnknownEffect(Collection)(input);
|
|
248
|
+
const variables = Record.fromIterableWith(collection.variable ?? [], variable => [
|
|
249
|
+
variable.key,
|
|
250
|
+
Predicate.isString(variable.value) ? variable.value : variable.key,
|
|
251
|
+
]);
|
|
252
|
+
function server(origin: string) {
|
|
253
|
+
const names = OpenApiSpec.templateNames(origin);
|
|
254
|
+
const resolved = Array.reduce(names, origin, (url, name) =>
|
|
255
|
+
pipe(url, String.replaceAll(`{${name}}`, variables[name] ?? name)),
|
|
256
|
+
);
|
|
257
|
+
return {
|
|
258
|
+
url: /^[a-z][a-z0-9+.-]*:\/\//iu.test(resolved) ? origin : `https://${origin}`,
|
|
259
|
+
variables: Record.fromIterableWith(names, name => [
|
|
260
|
+
name,
|
|
261
|
+
{ default: variables[name] ?? name },
|
|
262
|
+
]),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
const parsed = Array.map(requests(collection.item, []), entry => {
|
|
266
|
+
const raw = Predicate.isString(entry.request.url) ? entry.request.url : entry.request.url.raw;
|
|
267
|
+
const parts = pipe(raw, String.split("#"), Array.headNonEmpty, String.split("?"));
|
|
268
|
+
const target = split(Array.headNonEmpty(parts));
|
|
269
|
+
const path = pathOf(target.path);
|
|
270
|
+
return {
|
|
271
|
+
entry,
|
|
272
|
+
method: pipe(entry.request.method, String.toLowerCase),
|
|
273
|
+
names: path.names,
|
|
274
|
+
origin: target.origin,
|
|
275
|
+
query: pipe(
|
|
276
|
+
parts,
|
|
277
|
+
Array.drop(1),
|
|
278
|
+
Array.join("?"),
|
|
279
|
+
String.split("&"),
|
|
280
|
+
Array.map(pair => pipe(pair, String.split("="), Array.headNonEmpty)),
|
|
281
|
+
Array.filter(String.isNonEmpty),
|
|
282
|
+
Array.map(name =>
|
|
283
|
+
Option.getOrElse(Option.liftThrowable(decodeURIComponent)(name), () => name),
|
|
284
|
+
),
|
|
285
|
+
),
|
|
286
|
+
template: path.template,
|
|
287
|
+
};
|
|
288
|
+
});
|
|
289
|
+
const groups = Record.values(
|
|
290
|
+
Array.groupBy(parsed, request => `${request.method} ${request.template}`),
|
|
291
|
+
);
|
|
292
|
+
const mixed = Option.getOrUndefined(
|
|
293
|
+
Array.findFirst(groups, group =>
|
|
294
|
+
Array.some(group, member => member.origin !== Array.headNonEmpty(group).origin),
|
|
295
|
+
),
|
|
296
|
+
);
|
|
297
|
+
if (mixed !== undefined) {
|
|
298
|
+
const first = Array.headNonEmpty(mixed);
|
|
299
|
+
return yield* PostmanHostConflict.make({
|
|
300
|
+
message: `${Array.join(
|
|
301
|
+
Array.map(mixed, member => member.entry.name),
|
|
302
|
+
", ",
|
|
303
|
+
)} send ${String.toUpperCase(first.method)} ${first.template} to different hosts`,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
const operations = pipe(
|
|
307
|
+
groups,
|
|
308
|
+
Array.map(group => {
|
|
309
|
+
const first = Array.headNonEmpty(group);
|
|
310
|
+
const request = first.entry.request;
|
|
311
|
+
const body = pipe(
|
|
312
|
+
group,
|
|
313
|
+
Array.findFirst(member => Option.fromNullishOr(requestBody(member.entry.request))),
|
|
314
|
+
Option.getOrUndefined,
|
|
315
|
+
);
|
|
316
|
+
const examples = Array.filter(
|
|
317
|
+
Array.flatMap(group, member => member.entry.examples),
|
|
318
|
+
example => example.code >= 200 && example.code < 300,
|
|
319
|
+
);
|
|
320
|
+
const responses = Array.dedupeWith(examples, (left, right) => left.code === right.code);
|
|
321
|
+
const headers = Array.dedupeWith(
|
|
322
|
+
Array.flatMap(group, member => fields(member.entry.request.header)),
|
|
323
|
+
(left: Pair, right: Pair) => String.toLowerCase(left.key) === String.toLowerCase(right.key),
|
|
324
|
+
);
|
|
325
|
+
const description = text(request.description);
|
|
326
|
+
return {
|
|
327
|
+
method: first.method,
|
|
328
|
+
operation: {
|
|
329
|
+
...(description === undefined ? {} : { description }),
|
|
330
|
+
operationId: Array.join([...first.entry.folders, first.entry.name], "."),
|
|
331
|
+
parameters: [
|
|
332
|
+
...Array.map(first.names, name => ({
|
|
333
|
+
in: "path",
|
|
334
|
+
name,
|
|
335
|
+
required: true,
|
|
336
|
+
schema: { type: "string" },
|
|
337
|
+
})),
|
|
338
|
+
...Array.map(Array.dedupe(Array.flatMap(group, member => member.query)), name => ({
|
|
339
|
+
in: "query",
|
|
340
|
+
name,
|
|
341
|
+
schema: { type: "string" },
|
|
342
|
+
})),
|
|
343
|
+
...Array.map(headers, header => ({
|
|
344
|
+
in: "header",
|
|
345
|
+
name: header.key,
|
|
346
|
+
schema: { type: "string" },
|
|
347
|
+
})),
|
|
348
|
+
],
|
|
349
|
+
...(body === undefined ? {} : { requestBody: body }),
|
|
350
|
+
responses: Array.isReadonlyArrayNonEmpty(responses)
|
|
351
|
+
? Record.fromIterableWith(responses, example => [
|
|
352
|
+
`${example.code}`,
|
|
353
|
+
{
|
|
354
|
+
description: example.name ?? "Example response",
|
|
355
|
+
...(String.isNonEmpty(example.body ?? "")
|
|
356
|
+
? { content: mediaOf(example.body ?? "", false, true) }
|
|
357
|
+
: {}),
|
|
358
|
+
},
|
|
359
|
+
])
|
|
360
|
+
: { "200": { description: "The collection records no example response" } },
|
|
361
|
+
...(Option.contains(
|
|
362
|
+
Option.map(Array.head(parsed), found => found.origin),
|
|
363
|
+
first.origin,
|
|
364
|
+
)
|
|
365
|
+
? {}
|
|
366
|
+
: { servers: [server(first.origin)] }),
|
|
367
|
+
summary: first.entry.name,
|
|
368
|
+
...(Array.isReadonlyArrayNonEmpty(first.entry.folders)
|
|
369
|
+
? { tags: [Array.headNonEmpty(first.entry.folders)] }
|
|
370
|
+
: {}),
|
|
371
|
+
},
|
|
372
|
+
template: first.template,
|
|
373
|
+
};
|
|
374
|
+
}),
|
|
375
|
+
);
|
|
376
|
+
const security = securityScheme(collection);
|
|
377
|
+
return {
|
|
378
|
+
...(security === undefined
|
|
379
|
+
? {}
|
|
380
|
+
: {
|
|
381
|
+
components: { securitySchemes: { [security.name]: security.scheme } },
|
|
382
|
+
security: [{ [security.name]: [] }],
|
|
383
|
+
}),
|
|
384
|
+
info: {
|
|
385
|
+
...(text(collection.info.description) === undefined
|
|
386
|
+
? {}
|
|
387
|
+
: { description: text(collection.info.description) }),
|
|
388
|
+
title: collection.info.name,
|
|
389
|
+
version: "1",
|
|
390
|
+
},
|
|
391
|
+
openapi: "3.0.3",
|
|
392
|
+
paths: pipe(
|
|
393
|
+
operations,
|
|
394
|
+
Array.groupBy(entry => entry.template),
|
|
395
|
+
Record.map(entries =>
|
|
396
|
+
Record.fromIterableWith(entries, entry => [entry.method, entry.operation]),
|
|
397
|
+
),
|
|
398
|
+
),
|
|
399
|
+
servers: Array.map(Array.take(parsed, 1), first => server(first.origin)),
|
|
400
|
+
};
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
export * as OpenApiPostman from "./postman.ts";
|