@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,113 @@
|
|
|
1
|
+
import { Array, Option, Predicate, Record, Result, String, pipe } from "effect";
|
|
2
|
+
|
|
3
|
+
import { OpenApiSpec } from "./spec.ts";
|
|
4
|
+
|
|
5
|
+
const methods = ["delete", "get", "head", "options", "patch", "post", "put", "trace"];
|
|
6
|
+
|
|
7
|
+
type Selection = {
|
|
8
|
+
readonly include?: readonly string[];
|
|
9
|
+
readonly exclude?: readonly string[];
|
|
10
|
+
readonly aliases?: Record.ReadonlyRecord<string, string>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `[METHOD ]path` patterns: `*` matches within one path segment, `**` across segments, and a
|
|
15
|
+
* trailing `/**` also matches the path itself.
|
|
16
|
+
*/
|
|
17
|
+
function matcher(patterns: readonly string[]) {
|
|
18
|
+
const compiled = Array.map(patterns, pattern => {
|
|
19
|
+
const match = /^(?:([A-Za-z]+)\s+)?(\/.*)$/u.exec(String.trim(pattern));
|
|
20
|
+
const expression = pipe(
|
|
21
|
+
match?.[2] ?? pattern,
|
|
22
|
+
String.replaceAll(/[.+?^${}()|[\]\\]/gu, "\\$&"),
|
|
23
|
+
String.replaceAll("/**", "\u0000"),
|
|
24
|
+
String.replaceAll("**", "\u0001"),
|
|
25
|
+
String.replaceAll("*", "[^/]*"),
|
|
26
|
+
String.replaceAll("\u0000", "(?:/.*)?"),
|
|
27
|
+
String.replaceAll("\u0001", ".*"),
|
|
28
|
+
);
|
|
29
|
+
return {
|
|
30
|
+
method: Option.map(Option.fromNullishOr(match?.[1]), String.toLowerCase),
|
|
31
|
+
path: new RegExp(`^${expression}$`, "u"),
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
return (path: string, method: string) =>
|
|
35
|
+
Array.some(
|
|
36
|
+
compiled,
|
|
37
|
+
entry =>
|
|
38
|
+
Option.getOrElse(
|
|
39
|
+
Option.map(entry.method, found => found === method),
|
|
40
|
+
() => true,
|
|
41
|
+
) && entry.path.test(path),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Keeps the operations a package selects. An alias such as `/users/{user-id}` → `/me` folds each
|
|
47
|
+
* `/me/…` operation into its `/users/{user-id}/…` twin, whose parameter then becomes optional.
|
|
48
|
+
*/
|
|
49
|
+
export function select(document: OpenApiSpec.JsonObject, selection: Selection) {
|
|
50
|
+
const paths = OpenApiSpec.asObject(document.paths) ?? {};
|
|
51
|
+
const aliases = Record.toEntries(selection.aliases ?? {});
|
|
52
|
+
const parameters = Record.map(selection.aliases ?? {}, (_, prefix) =>
|
|
53
|
+
Array.getSomes(
|
|
54
|
+
Array.map(Array.fromIterable(pipe(prefix, String.matchAll(/\{([^}]+)\}/gu))), match =>
|
|
55
|
+
Array.get(match, 1),
|
|
56
|
+
),
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
for (const entry of Record.toEntries(parameters)) {
|
|
60
|
+
if (entry[1].length !== 1) {
|
|
61
|
+
return Result.fail(`alias ${entry[0]} must contain one path parameter`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const included = matcher(selection.include ?? ["/**"]);
|
|
65
|
+
const excluded = matcher(selection.exclude ?? []);
|
|
66
|
+
function mirrors(path: string, method: string, from: string, to: string) {
|
|
67
|
+
if (!pipe(path, String.startsWith(`${from}/`)) && path !== from) return false;
|
|
68
|
+
const other = `${to}${pipe(path, String.slice(from.length))}`;
|
|
69
|
+
return Predicate.isObject(OpenApiSpec.asObject(paths[other])?.[method]);
|
|
70
|
+
}
|
|
71
|
+
return Result.succeed({
|
|
72
|
+
...document,
|
|
73
|
+
paths: Record.filterMap(paths, (item, path) => {
|
|
74
|
+
const value = OpenApiSpec.asObject(item) ?? {};
|
|
75
|
+
const operations = Array.filter(
|
|
76
|
+
methods,
|
|
77
|
+
method =>
|
|
78
|
+
Predicate.isObject(value[method]) &&
|
|
79
|
+
included(path, method) &&
|
|
80
|
+
!excluded(path, method) &&
|
|
81
|
+
!Array.some(aliases, entry => mirrors(path, method, entry[1], entry[0])),
|
|
82
|
+
);
|
|
83
|
+
if (Array.isReadonlyArrayEmpty(operations)) return Result.failVoid;
|
|
84
|
+
return Result.succeed({
|
|
85
|
+
...Record.filter(value, (_, key) => !Array.contains(methods, key)),
|
|
86
|
+
...Record.fromEntries(
|
|
87
|
+
Array.map(operations, method => {
|
|
88
|
+
const operation = OpenApiSpec.asObject(value[method]) ?? {};
|
|
89
|
+
return [
|
|
90
|
+
method,
|
|
91
|
+
Option.match(
|
|
92
|
+
Array.findFirst(aliases, entry => mirrors(path, method, entry[0], entry[1])),
|
|
93
|
+
{
|
|
94
|
+
onNone: () => operation,
|
|
95
|
+
onSome: entry => ({
|
|
96
|
+
...operation,
|
|
97
|
+
"x-dual-alias": {
|
|
98
|
+
parameter: Array.getUnsafe(parameters[entry[0]] ?? [], 0),
|
|
99
|
+
prefix: entry[0],
|
|
100
|
+
replacement: entry[1],
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
|
+
},
|
|
104
|
+
),
|
|
105
|
+
] as const;
|
|
106
|
+
}),
|
|
107
|
+
),
|
|
108
|
+
});
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export * as OpenApiSelect from "./select.ts";
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Array,
|
|
3
|
+
Effect,
|
|
4
|
+
HashSet,
|
|
5
|
+
Number,
|
|
6
|
+
Option,
|
|
7
|
+
Predicate,
|
|
8
|
+
Record,
|
|
9
|
+
Result,
|
|
10
|
+
Schema,
|
|
11
|
+
String,
|
|
12
|
+
pipe,
|
|
13
|
+
} from "effect";
|
|
14
|
+
|
|
15
|
+
// oxlint-disable-next-line no-restricted-imports -- Effect's Yaml rejects or misreads the Zendesk, GitLab, Facebook, Snowflake SQL and Xero specs the plugins import.
|
|
16
|
+
import { CORE_SCHEMA, load } from "js-yaml";
|
|
17
|
+
|
|
18
|
+
import type { Mcp } from "@dual-os/sdk/Mcp";
|
|
19
|
+
|
|
20
|
+
import { OpenApiDiscovery } from "./discovery.ts";
|
|
21
|
+
import { OpenApiGraphql } from "./graphql.ts";
|
|
22
|
+
import { OpenApiMcp } from "./mcp.ts";
|
|
23
|
+
import { OpenApiPolymorphism } from "./polymorphism.ts";
|
|
24
|
+
import { OpenApiPostman } from "./postman.ts";
|
|
25
|
+
import { OpenApiSwagger } from "./swagger.ts";
|
|
26
|
+
|
|
27
|
+
export type JsonObject = typeof JsonObject.Type;
|
|
28
|
+
export const JsonObject = Schema.Record(Schema.String, Schema.Unknown);
|
|
29
|
+
|
|
30
|
+
type Document = typeof Document.Type;
|
|
31
|
+
const Document = Schema.Struct({
|
|
32
|
+
components: Schema.optional(JsonObject),
|
|
33
|
+
info: Schema.Struct({
|
|
34
|
+
description: Schema.optional(Schema.String),
|
|
35
|
+
title: Schema.String,
|
|
36
|
+
version: Schema.String,
|
|
37
|
+
}),
|
|
38
|
+
openapi: Schema.String.check(Schema.isPattern(/^3\.\d/u)),
|
|
39
|
+
paths: Schema.Record(Schema.String, JsonObject),
|
|
40
|
+
servers: Schema.optional(Schema.Array(JsonObject)),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export type Spec = { readonly raw: JsonObject; readonly document: Document };
|
|
44
|
+
|
|
45
|
+
type RawSecurityScheme = typeof RawSecurityScheme.Type;
|
|
46
|
+
const RawSecurityScheme = Schema.Struct({
|
|
47
|
+
description: Schema.optional(Schema.String),
|
|
48
|
+
flows: Schema.optional(
|
|
49
|
+
Schema.Struct({
|
|
50
|
+
authorizationCode: Schema.optionalKey(
|
|
51
|
+
Schema.Struct({
|
|
52
|
+
authorizationUrl: Schema.optionalKey(Schema.String),
|
|
53
|
+
refreshUrl: Schema.optionalKey(Schema.String),
|
|
54
|
+
scopes: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
|
|
55
|
+
tokenUrl: Schema.optionalKey(Schema.String),
|
|
56
|
+
}),
|
|
57
|
+
),
|
|
58
|
+
}),
|
|
59
|
+
),
|
|
60
|
+
in: Schema.optional(Schema.String),
|
|
61
|
+
name: Schema.optional(Schema.String),
|
|
62
|
+
scheme: Schema.optional(Schema.String),
|
|
63
|
+
type: Schema.String,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
export function asObject(value: unknown) {
|
|
67
|
+
return Predicate.isObject(value) ? value : undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function kebab(value: string) {
|
|
71
|
+
return pipe(
|
|
72
|
+
value,
|
|
73
|
+
String.replace(/([a-z0-9])([A-Z])/gu, "$1-$2"),
|
|
74
|
+
String.replace(/([A-Z])([A-Z][a-z])/gu, "$1-$2"),
|
|
75
|
+
String.replace(/[^A-Za-z0-9]+/gu, "-"),
|
|
76
|
+
String.replace(/^-+|-+$/gu, ""),
|
|
77
|
+
String.toLowerCase,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function titleCase(value: string) {
|
|
82
|
+
return pipe(
|
|
83
|
+
value,
|
|
84
|
+
String.replace(/([a-z0-9])([A-Z])/gu, "$1 $2"),
|
|
85
|
+
String.replace(/([A-Z])([A-Z][a-z])/gu, "$1 $2"),
|
|
86
|
+
String.replace(/[^A-Za-z0-9]+/gu, " "),
|
|
87
|
+
String.trim,
|
|
88
|
+
String.split(" "),
|
|
89
|
+
Array.filter(String.isNonEmpty),
|
|
90
|
+
Array.map(String.capitalize),
|
|
91
|
+
Array.join(" "),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function camelize(value: string) {
|
|
96
|
+
const name = pipe(
|
|
97
|
+
Array.fromIterable(pipe(value, String.matchAll(/[A-Za-z]+|\d+/gu))),
|
|
98
|
+
Array.map(match => String.capitalize(match[0])),
|
|
99
|
+
Array.join(""),
|
|
100
|
+
String.uncapitalize,
|
|
101
|
+
);
|
|
102
|
+
return /^\d/u.test(name) ? `_${name}` : name;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function singular(word: string) {
|
|
106
|
+
return pipe(word, String.replace(/ies$/u, "y"), String.replace(/s$/u, ""));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function identifier(value: string) {
|
|
110
|
+
return String.capitalize(camelize(value));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function templateNames(template: string) {
|
|
114
|
+
return Array.getSomes(
|
|
115
|
+
Array.map(Array.fromIterable(pipe(template, String.matchAll(/\{([^}]+)\}/gu))), match =>
|
|
116
|
+
Array.get(match, 1),
|
|
117
|
+
),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function firstServer(...declarations: unknown[]) {
|
|
122
|
+
return pipe(
|
|
123
|
+
declarations,
|
|
124
|
+
Array.filterMap(declaration => {
|
|
125
|
+
const first = asObject(
|
|
126
|
+
Array.isArray(declaration) ? Option.getOrUndefined(Array.head(declaration)) : undefined,
|
|
127
|
+
);
|
|
128
|
+
const url = first?.["url"];
|
|
129
|
+
return Predicate.isString(url)
|
|
130
|
+
? Result.succeed({ url, variables: asObject(first?.["variables"]) ?? {} })
|
|
131
|
+
: Result.failVoid;
|
|
132
|
+
}),
|
|
133
|
+
Array.head,
|
|
134
|
+
Option.getOrElse((): { url: string; variables: JsonObject } => ({ url: "/", variables: {} })),
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function serverUrl(server: ReturnType<typeof firstServer>, specUrl?: URL) {
|
|
139
|
+
const defaults = Result.all(
|
|
140
|
+
Array.map(templateNames(server.url), name => {
|
|
141
|
+
const value = asObject(server.variables[name])?.["default"];
|
|
142
|
+
return Predicate.isString(value)
|
|
143
|
+
? Result.succeed([name, value] as const)
|
|
144
|
+
: Result.fail(`its server URL ${server.url} has no default for the variable ${name}`);
|
|
145
|
+
}),
|
|
146
|
+
);
|
|
147
|
+
if (Result.isFailure(defaults)) return Result.fail(defaults.failure);
|
|
148
|
+
const declared = Array.reduce(defaults.success, server.url, (url, variable) =>
|
|
149
|
+
pipe(url, String.replaceAll(`{${variable[0]}}`, variable[1])),
|
|
150
|
+
);
|
|
151
|
+
const absolute = URL.canParse(declared) ? new URL(declared) : undefined;
|
|
152
|
+
if (absolute === undefined && specUrl === undefined) {
|
|
153
|
+
return Result.fail(
|
|
154
|
+
`its server URL ${declared} is relative and the document has no URL to resolve it against; set the spec's sourceUrl`,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
if (absolute === undefined && !URL.canParse(declared, specUrl)) {
|
|
158
|
+
return Result.fail(`its server URL ${declared} is not a valid URL`);
|
|
159
|
+
}
|
|
160
|
+
const resolved = absolute ?? new URL(declared, specUrl);
|
|
161
|
+
if (resolved.protocol !== "http:" && resolved.protocol !== "https:") {
|
|
162
|
+
return Result.fail(`its server URL ${resolved.href} does not use HTTP(S)`);
|
|
163
|
+
}
|
|
164
|
+
return Result.succeed(pipe(resolved.href, String.replace(/\/$/u, "")));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export type Located = { readonly value: JsonObject; readonly pointer: string };
|
|
168
|
+
|
|
169
|
+
export function pointerToken(token: string) {
|
|
170
|
+
return pipe(token, String.replaceAll("~", "~0"), String.replaceAll("/", "~1"));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function pointerKey(token: string) {
|
|
174
|
+
return pipe(
|
|
175
|
+
token,
|
|
176
|
+
String.replaceAll("~1", "/"),
|
|
177
|
+
String.replaceAll("~0", "~"),
|
|
178
|
+
decodeURIComponent,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function child(value: unknown, token: string) {
|
|
183
|
+
if (Array.isArray(value)) {
|
|
184
|
+
return Option.flatMap(Number.parse(token), index => Array.get(value, index));
|
|
185
|
+
}
|
|
186
|
+
if (!Predicate.isObject(value)) return Option.none();
|
|
187
|
+
return Option.fromUndefinedOr(value[pointerKey(token)]);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function pointerTarget(raw: unknown, pointer: string): Result.Result<unknown, string> {
|
|
191
|
+
return Result.fromOption(
|
|
192
|
+
Array.reduce(
|
|
193
|
+
pipe(pointer, String.split("/"), Array.drop(1)),
|
|
194
|
+
Option.some(raw),
|
|
195
|
+
(current, token) => Option.flatMap(current, value => child(value, token)),
|
|
196
|
+
),
|
|
197
|
+
() => `reference #${pointer} could not be resolved`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function target(spec: Spec, reference: string): Result.Result<unknown, string> {
|
|
202
|
+
if (reference !== "#" && !pipe(reference, String.startsWith("#/"))) {
|
|
203
|
+
return Result.fail(`reference ${reference} is not local; bundle the document first`);
|
|
204
|
+
}
|
|
205
|
+
return pointerTarget(spec.raw, pipe(reference, String.slice(1)));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function locate(spec: Spec, value: unknown, pointer: string) {
|
|
209
|
+
function step(
|
|
210
|
+
current: unknown,
|
|
211
|
+
at: string,
|
|
212
|
+
visited: HashSet.HashSet<string>,
|
|
213
|
+
): Result.Result<Located, string> {
|
|
214
|
+
if (!Predicate.isObject(current)) return Result.fail(`${at} is not an object`);
|
|
215
|
+
const reference = current.$ref;
|
|
216
|
+
if (!Predicate.isString(reference)) return Result.succeed({ pointer: at, value: current });
|
|
217
|
+
if (HashSet.has(visited, reference)) return Result.fail(`reference cycle at ${reference}`);
|
|
218
|
+
return Result.andThen(target(spec, reference), next =>
|
|
219
|
+
step(next, reference, HashSet.add(visited, reference)),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
return step(value, pointer, HashSet.empty());
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function resolve(spec: Spec, value: unknown) {
|
|
226
|
+
return Result.getOrUndefined(locate(spec, value, "#"))?.value;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function sortKeys(value: unknown): unknown {
|
|
230
|
+
if (Array.isArray(value)) return Array.map(value, sortKeys);
|
|
231
|
+
if (!Predicate.isObject(value)) return value;
|
|
232
|
+
return Record.fromEntries(
|
|
233
|
+
Array.map(Array.sort(Record.keys(value), String.Order), key => [key, sortKeys(value[key])]),
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export const parseYaml = Effect.fn("OpenApiSpec.parseYaml")(function* (text: string) {
|
|
238
|
+
return yield* Effect.try(() => load(text, { schema: CORE_SCHEMA }));
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
export const decode = Effect.fn("OpenApiSpec.decode")(function* (source: string) {
|
|
242
|
+
return pipe(source, String.trimStart, String.startsWith("{"))
|
|
243
|
+
? yield* Schema.decodeEffect(Schema.fromJsonString(JsonObject))(source)
|
|
244
|
+
: yield* Schema.decodeUnknownEffect(JsonObject)(yield* parseYaml(source));
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
export const parse = Effect.fn("OpenApiSpec.parse")(function* (
|
|
248
|
+
source: string,
|
|
249
|
+
bundle: (document: JsonObject) => Effect.Effect<JsonObject, { message: string }>,
|
|
250
|
+
) {
|
|
251
|
+
const decoded = yield* decode(source);
|
|
252
|
+
if (Schema.is(Schema.Struct({ kind: Schema.Literal("discovery#restDescription") }))(decoded)) {
|
|
253
|
+
return yield* OpenApiDiscovery.toOpenApi(Record.map(decoded, sortKeys));
|
|
254
|
+
}
|
|
255
|
+
if (Schema.is(OpenApiPostman.Signature)(decoded)) {
|
|
256
|
+
return Record.map(yield* OpenApiPostman.toOpenApi(decoded), sortKeys);
|
|
257
|
+
}
|
|
258
|
+
const parsed = Record.map(yield* bundle(decoded), sortKeys);
|
|
259
|
+
return Schema.is(Schema.Struct({ swagger: Schema.Literal("2.0") }))(parsed)
|
|
260
|
+
? Record.map(yield* OpenApiSwagger.toOpenApi(parsed), sortKeys)
|
|
261
|
+
: parsed;
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
export const parseGraphql = Effect.fn("OpenApiSpec.parseGraphql")(function* (
|
|
265
|
+
source: string,
|
|
266
|
+
endpoint: string,
|
|
267
|
+
) {
|
|
268
|
+
return Record.map(yield* OpenApiGraphql.toOpenApi(source, endpoint), sortKeys);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
export function parseMcp(server: Mcp.Implementation, tools: Mcp.Tool[], endpoint: string) {
|
|
272
|
+
return Record.map(OpenApiMcp.toOpenApi(server, tools, endpoint), sortKeys);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Adds a `nextLink` input to operations paged with `x-ms-pageable`, which follows the returned URL. */
|
|
276
|
+
function pageable(raw: JsonObject) {
|
|
277
|
+
return {
|
|
278
|
+
...raw,
|
|
279
|
+
paths: Record.map(asObject(raw.paths) ?? {}, item =>
|
|
280
|
+
Record.map(asObject(item) ?? {}, (entry, method) => {
|
|
281
|
+
const operation = asObject(entry);
|
|
282
|
+
const name = asObject(operation?.["x-ms-pageable"])?.nextLinkName;
|
|
283
|
+
if (method !== "get" || operation === undefined || !Predicate.isString(name)) return entry;
|
|
284
|
+
return {
|
|
285
|
+
...operation,
|
|
286
|
+
parameters: [
|
|
287
|
+
...(Array.isArray(operation.parameters) ? operation.parameters : []),
|
|
288
|
+
{
|
|
289
|
+
description: `Follow the complete ${name} URL from a previous page; other query inputs are ignored for this request`,
|
|
290
|
+
in: "query",
|
|
291
|
+
name: "nextLink",
|
|
292
|
+
schema: { type: "string" },
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
"x-dual-next-link": true,
|
|
296
|
+
};
|
|
297
|
+
}),
|
|
298
|
+
),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Adds paging inputs and expands discriminated schemas of a parsed, overlaid document. */
|
|
303
|
+
export function expand(raw: JsonObject) {
|
|
304
|
+
return OpenApiPolymorphism.expand(pageable(raw));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** The compiler's view of an expanded document with its selected operations. */
|
|
308
|
+
export const document = Effect.fn("OpenApiSpec.document")(function* (raw: JsonObject) {
|
|
309
|
+
return { document: yield* Schema.decodeUnknownEffect(Document)(raw), raw } satisfies Spec;
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
function unsupported(reason: string) {
|
|
313
|
+
return { _tag: "Unsupported", reason } as const;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export type OAuth2Kind = {
|
|
317
|
+
_tag: "OAuth2";
|
|
318
|
+
authentication?: "basic" | "none" | "post";
|
|
319
|
+
authorizationUrl?: string;
|
|
320
|
+
pkce?: boolean;
|
|
321
|
+
resource?: string;
|
|
322
|
+
scopes: string[];
|
|
323
|
+
tokenUrl?: string;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
function classify(scheme: RawSecurityScheme, base?: string) {
|
|
327
|
+
function absolute(url: string) {
|
|
328
|
+
return base !== undefined && !URL.canParse(url) && URL.canParse(url, base)
|
|
329
|
+
? new URL(url, base).href
|
|
330
|
+
: url;
|
|
331
|
+
}
|
|
332
|
+
const type = String.toLowerCase(scheme.type);
|
|
333
|
+
const httpScheme = String.toLowerCase(scheme.scheme ?? "");
|
|
334
|
+
if (type === "http" && httpScheme === "bearer") return { _tag: "HttpBearer" } as const;
|
|
335
|
+
if (type === "http" && httpScheme === "basic") return { _tag: "HttpBasic" } as const;
|
|
336
|
+
if (type === "http") return unsupported(`the HTTP authentication scheme "${httpScheme}"`);
|
|
337
|
+
if (type === "apikey") {
|
|
338
|
+
const location = String.toLowerCase(scheme.in ?? "");
|
|
339
|
+
if ((location === "header" || location === "query") && scheme.name !== undefined) {
|
|
340
|
+
return { _tag: "ApiKey", in: location, key: scheme.name } as const;
|
|
341
|
+
}
|
|
342
|
+
return unsupported(`an API key in "${scheme.in ?? "an unknown location"}"`);
|
|
343
|
+
}
|
|
344
|
+
if (type === "oauth2") {
|
|
345
|
+
const flow = scheme.flows?.authorizationCode;
|
|
346
|
+
if (flow === undefined) {
|
|
347
|
+
return unsupported(
|
|
348
|
+
"an OAuth2 scheme without an authorizationCode flow, the only one Dual connects",
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
const kind: OAuth2Kind = {
|
|
352
|
+
_tag: "OAuth2",
|
|
353
|
+
...(flow.authorizationUrl === undefined
|
|
354
|
+
? {}
|
|
355
|
+
: { authorizationUrl: absolute(flow.authorizationUrl) }),
|
|
356
|
+
...(flow.tokenUrl === undefined ? {} : { tokenUrl: absolute(flow.tokenUrl) }),
|
|
357
|
+
scopes: Array.sort(Record.keys(flow.scopes ?? {}), String.Order),
|
|
358
|
+
};
|
|
359
|
+
return kind;
|
|
360
|
+
}
|
|
361
|
+
return unsupported(`the "${scheme.type}" security scheme type`);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export function securitySchemes(spec: Spec, specUrl?: URL) {
|
|
365
|
+
const base = Result.getOrUndefined(serverUrl(firstServer(spec.document.servers), specUrl));
|
|
366
|
+
return Record.collect(
|
|
367
|
+
asObject(spec.document.components?.securitySchemes) ?? {},
|
|
368
|
+
(name, value) => {
|
|
369
|
+
const methodId = kebab(name);
|
|
370
|
+
const scheme = Schema.decodeUnknownOption(RawSecurityScheme)(resolve(spec, value));
|
|
371
|
+
return {
|
|
372
|
+
description: Option.getOrUndefined(scheme)?.description,
|
|
373
|
+
displayName: titleCase(name),
|
|
374
|
+
kind: String.isEmpty(methodId)
|
|
375
|
+
? unsupported("a name without letters or digits")
|
|
376
|
+
: Option.match(scheme, {
|
|
377
|
+
onNone: () => unsupported("a malformed security scheme"),
|
|
378
|
+
onSome: found => classify(found, base),
|
|
379
|
+
}),
|
|
380
|
+
methodId,
|
|
381
|
+
name,
|
|
382
|
+
};
|
|
383
|
+
},
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export type SecurityScheme = ReturnType<typeof securitySchemes>[number];
|
|
388
|
+
|
|
389
|
+
/** Whether the document declares any OAuth2 scheme, whatever its flows. */
|
|
390
|
+
export function declaresOAuth(spec: Spec) {
|
|
391
|
+
return Array.some(
|
|
392
|
+
Record.values(asObject(spec.document.components?.securitySchemes) ?? {}),
|
|
393
|
+
value =>
|
|
394
|
+
Option.exists(
|
|
395
|
+
Schema.decodeUnknownOption(RawSecurityScheme)(resolve(spec, value)),
|
|
396
|
+
scheme => String.toLowerCase(scheme.type) === "oauth2",
|
|
397
|
+
),
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export * as OpenApiSpec from "./spec.ts";
|