@amartus/oas-utils 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +107 -0
  3. package/dist/cli.d.ts +3 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +78 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/index.d.ts +4 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +4 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/lib/cliActions.d.ts +22 -0
  12. package/dist/lib/cliActions.d.ts.map +1 -0
  13. package/dist/lib/cliActions.js +120 -0
  14. package/dist/lib/cliActions.js.map +1 -0
  15. package/dist/lib/oasUtils.d.ts +5 -0
  16. package/dist/lib/oasUtils.d.ts.map +1 -0
  17. package/dist/lib/oasUtils.js +9 -0
  18. package/dist/lib/oasUtils.js.map +1 -0
  19. package/dist/lib/optimizeAllOfComposition.d.ts +13 -0
  20. package/dist/lib/optimizeAllOfComposition.d.ts.map +1 -0
  21. package/dist/lib/optimizeAllOfComposition.js +99 -0
  22. package/dist/lib/optimizeAllOfComposition.js.map +1 -0
  23. package/dist/lib/removeFromOneOfByName.d.ts +15 -0
  24. package/dist/lib/removeFromOneOfByName.d.ts.map +1 -0
  25. package/dist/lib/removeFromOneOfByName.js +70 -0
  26. package/dist/lib/removeFromOneOfByName.js.map +1 -0
  27. package/dist/lib/removeUnusedSchemas.d.ts +16 -0
  28. package/dist/lib/removeUnusedSchemas.d.ts.map +1 -0
  29. package/dist/lib/removeUnusedSchemas.js +190 -0
  30. package/dist/lib/removeUnusedSchemas.js.map +1 -0
  31. package/dist/redocly/optimize-allof-decorator.d.ts +6 -0
  32. package/dist/redocly/optimize-allof-decorator.d.ts.map +1 -0
  33. package/dist/redocly/optimize-allof-decorator.js +11 -0
  34. package/dist/redocly/optimize-allof-decorator.js.map +1 -0
  35. package/dist/redocly/plugin.d.ts +23 -0
  36. package/dist/redocly/plugin.d.ts.map +1 -0
  37. package/dist/redocly/plugin.js +29 -0
  38. package/dist/redocly/plugin.js.map +1 -0
  39. package/dist/redocly/remove-from-oneof-decorator.d.ts +15 -0
  40. package/dist/redocly/remove-from-oneof-decorator.d.ts.map +1 -0
  41. package/dist/redocly/remove-from-oneof-decorator.js +35 -0
  42. package/dist/redocly/remove-from-oneof-decorator.js.map +1 -0
  43. package/dist/redocly/remove-unused-decorator.d.ts +9 -0
  44. package/dist/redocly/remove-unused-decorator.d.ts.map +1 -0
  45. package/dist/redocly/remove-unused-decorator.js +17 -0
  46. package/dist/redocly/remove-unused-decorator.js.map +1 -0
  47. package/package.json +64 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Amartus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # oas-utils
2
+
3
+ Utilities for working with OpenAPI (OAS) documents. Includes tools to remove unused schemas and to remove entries from oneOf. Use them as a CLI or as Redocly decorators.
4
+
5
+ ## Definition of "unused schema"
6
+
7
+ "Unused" means a schema under `components.schemas` that is not referenced (directly or transitively) from anything reachable under the `paths` section. Exception: if a schema has an `allOf` that references a schema already considered used, that schema is also considered used. Traversal follows `$ref` chains found within schemas and follows `$ref` from `paths` into other components (e.g. `requestBodies`) to discover nested schema usage.
8
+
9
+ ## Install
10
+
11
+ ```
12
+ npm i -D oas-utils
13
+ ```
14
+
15
+ Or clone this repo and build locally.
16
+
17
+ ### Local global install
18
+
19
+ To install globally from a local clone:
20
+
21
+ ```
22
+ npm install -g
23
+ ```
24
+
25
+ This makes the `oas-utils` CLI available system-wide.
26
+
27
+ ## CLI usage
28
+
29
+ ```
30
+ oas-utils remove-unused <input.yaml> -o output.yaml [--keep Name1 Name2] [--aggressive] [--ignore-parents NameX]
31
+ # Read from stdin and write to stdout
32
+ cat openapi.yaml | oas-utils remove-unused --keep CommonError > pruned.yaml
33
+ ```
34
+
35
+ Options:
36
+ - --keep: schema names to keep regardless of usage; can be passed multiple times or as a variadic list (e.g., `--keep CommonError Pagination`).
37
+ - --aggressive: also prune other unused components referenced from paths (responses, headers, requestBodies, parameters, examples, links, callbacks, securitySchemes). Non-referenced entries in these sections are removed.
38
+ - --ignore-parents: schema names that shouldn't promote children via allOf (can be repeated). Useful to avoid allOf upward inclusion when the parent acts as an abstract/base.
39
+
40
+ Remove entries from oneOf and update discriminators:
41
+
42
+ ```
43
+ oas-utils remove-oneof <input.yaml> --parent Pet --remove Cat --remove Dog -o output.yaml
44
+ # Or remove globally from all oneOf occurrences
45
+ oas-utils remove-oneof <input.yaml> --remove Cat
46
+ # Guess variant names: Cat, Cat_* will be included
47
+ oas-utils remove-oneof <input.yaml> --parent Pet --remove Cat --guess
48
+ ```
49
+
50
+ Options (remove-oneof):
51
+ - --parent: parent schema name containing oneOf; if omitted, removal is global across the document.
52
+ - --remove: schema name(s) to remove; can be repeated.
53
+ - --guess: expand each name to include variants starting with `<name>_`.
54
+
55
+ ## As Redocly decorators
56
+
57
+ 1) Add the plugin to `plugins` in your `redocly.yaml` (path relative to the config):
58
+
59
+ ```
60
+ plugins:
61
+ - ./node_modules/oas-utils/dist/redocly/plugin.js
62
+ ```
63
+
64
+ 2) Enable the decorator:
65
+
66
+ ```
67
+ decorators:
68
+ # Remove unused schemas
69
+ oas-utils/remove-unused:
70
+ keep: [CommonError, Pagination]
71
+ aggressive: true
72
+
73
+ # Remove entries from oneOf (and update discriminator mappings)
74
+ oas-utils/remove-oneof:
75
+ parent: Pet # optional; if omitted, removal is global
76
+ remove: [Cat, Cat_variant1]
77
+ guess: false
78
+ ```
79
+
80
+ 3) Run bundling with Redocly CLI and the decorators will apply the transformations. With `aggressive: true`, unused non-schema components (responses, headers, requestBodies, etc.) are removed as well.
81
+
82
+ Notes:
83
+ - Preferred plugin id is `oas-utils`. Old aliases `oas-remove-unused/remove-unused-schemas` and `oas-remove-unused/remove-from-oneof` still work.
84
+
85
+ ## Programmatic usage
86
+
87
+ ```
88
+ import { removeUnusedSchemas } from 'oas-utils';
89
+ const pruned = removeUnusedSchemas(doc, { keep: ['CommonError'], aggressive: true });
90
+ ```
91
+
92
+ ## Notes
93
+ - This tool resolves direct and transitive `$ref` usages of `components.schemas` and deletes unreferenced definitions. Starting points are only what’s reachable from `paths`.
94
+ - Special rule: any schema with `allOf` that references a used schema is also considered used.
95
+ - With `--aggressive`, unused non-schema components (responses, headers, requestBodies, parameters, examples, links, callbacks, securitySchemes) are removed as well.
96
+ - YAML and JSON are supported; output format follows `-o` extension, otherwise YAML.
97
+
98
+ ## Development and tests
99
+
100
+ Run builds and unit tests (fixture-based):
101
+
102
+ ```
103
+ npm run build
104
+ npm run test
105
+ ```
106
+
107
+ Fixture files live under `test/resources` as `<name>.input.yaml`, `<name>.expected.yaml`, with optional `<name>.options.json` to pass core options like `{ "keep": ["Foo"], "aggressive": true }`.
package/dist/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
package/dist/cli.js ADDED
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env node
2
+ import fs from "node:fs/promises";
3
+ import { Command } from "commander";
4
+ import { runRemoveUnused, runRemoveOneOf, optimizeAllOf } from "./lib/cliActions.js";
5
+ import YAML from "yaml";
6
+ const program = new Command();
7
+ program
8
+ .command("remove-unused")
9
+ .showHelpAfterError()
10
+ .description("Remove unused schemas from an OpenAPI (OAS) file")
11
+ .argument("[input]", "Path to input OpenAPI file (YAML or JSON). If omitted, reads from stdin")
12
+ .option("-o, --output <file>", "Write result to this file (defaults to stdout)")
13
+ .option("--keep <names...>", "Schema names to keep regardless of usage (can be repeated)", [])
14
+ .option("--aggressive", "Also prune empty component sections", false)
15
+ .option("--ignore-parents <names...>", "Schema names to ignore as allOf parents (can be repeated)", [])
16
+ .action(async (input, opts) => {
17
+ try {
18
+ await runRemoveUnused(opts, format, () => reader(input));
19
+ }
20
+ catch (err) {
21
+ console.error(`Error: ${err?.message || String(err)}`);
22
+ process.exitCode = 1;
23
+ }
24
+ });
25
+ program
26
+ .command("remove-oneof")
27
+ .showHelpAfterError()
28
+ .description("Remove one or more schemas from oneOf and update discriminator mappings")
29
+ .argument("<input>", "Path to input OpenAPI file (YAML or JSON)")
30
+ .option("--parent <name>", "Parent schema name containing oneOf")
31
+ .requiredOption("--remove <name...>", "Schema name(s) to remove from oneOf (can be repeated)")
32
+ .option("-o, --output <file>", "Write result to this file (defaults to stdout)")
33
+ .option("--guess", "Guess schema names for each --remove <name>", false)
34
+ .action(async (input, opts) => {
35
+ try {
36
+ await runRemoveOneOf(opts, format, () => reader(input));
37
+ }
38
+ catch (err) {
39
+ console.error(`Error: ${err?.message || String(err)}`);
40
+ process.exitCode = 1;
41
+ }
42
+ });
43
+ program
44
+ .command("optimize-allof")
45
+ .showHelpAfterError()
46
+ .description("Optimize allOf composition in an OpenAPI (OAS) file")
47
+ .argument("[input]", "Path to input OpenAPI file (YAML or JSON). If omitted, reads from stdin")
48
+ .option("-o, --output <file>", "Write result to this file (defaults to stdout)")
49
+ .action(async (input, opts) => {
50
+ await optimizeAllOf(opts, format, () => reader(input));
51
+ });
52
+ if (process.argv.length <= 2) {
53
+ program.help();
54
+ }
55
+ else {
56
+ program.parse();
57
+ }
58
+ async function readStdin() {
59
+ const chunks = [];
60
+ for await (const chunk of process.stdin)
61
+ chunks.push(Buffer.from(chunk));
62
+ return Buffer.concat(chunks).toString("utf8");
63
+ }
64
+ async function loadDoc(input) {
65
+ const raw = await fs.readFile(input, "utf8");
66
+ return raw;
67
+ }
68
+ async function reader(input) {
69
+ if (input) {
70
+ return loadDoc(input);
71
+ }
72
+ return readStdin();
73
+ }
74
+ function format(doc, target) {
75
+ const isJson = target?.endsWith(".json");
76
+ return isJson ? JSON.stringify(doc, null, 2) + "\n" : YAML.stringify(doc);
77
+ }
78
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,IAAI,MAAM,MAAM,CAAC;AAGxB,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAC9B,OAAO;KACJ,OAAO,CAAC,eAAe,CAAC;KACxB,kBAAkB,EAAE;KACpB,WAAW,CAAC,kDAAkD,CAAC;KAC/D,QAAQ,CACP,SAAS,EACT,yEAAyE,CAC1E;KACA,MAAM,CACL,qBAAqB,EACrB,gDAAgD,CACjD;KACA,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;AAGD,SAAS,MAAM,CAAC,GAAQ,EAAE,MAAe;IACvC,MAAM,MAAM,GAAG,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;AAC5E,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from './lib/removeUnusedSchemas.js';
2
+ export * from './lib/removeFromOneOfByName.js';
3
+ export * from './lib/optimizeAllOfComposition.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './lib/removeUnusedSchemas.js';
2
+ export * from './lib/removeFromOneOfByName.js';
3
+ export * from './lib/optimizeAllOfComposition.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC"}
@@ -0,0 +1,22 @@
1
+ export declare function runRemoveUnused(opts: {
2
+ output?: string;
3
+ keep?: string;
4
+ aggressive?: boolean;
5
+ ignoreParents?: string;
6
+ }, format: (doc: any, target?: string) => string, reader: () => Promise<string>): Promise<void>;
7
+ export declare function runRemoveOneOf(opts: {
8
+ parent: string | undefined;
9
+ remove: string[];
10
+ guess: boolean;
11
+ output?: string;
12
+ }, format: (doc: any, target?: string) => string, reader: () => Promise<string>): Promise<void>;
13
+ /**
14
+ * Optimizes allOf composition in the provided OpenAPI document.
15
+ *
16
+ * @param input - Path to the input OpenAPI document.
17
+ * @param output - Path to the output OpenAPI document.
18
+ */
19
+ export declare function optimizeAllOf(opts: {
20
+ output?: string;
21
+ }, format: (doc: any, target?: string) => string, reader: () => Promise<string>): Promise<void>;
22
+ //# sourceMappingURL=cliActions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cliActions.d.ts","sourceRoot":"","sources":["../../src/lib/cliActions.ts"],"names":[],"mappings":"AAkDA,wBAAsB,eAAe,CACnC,IAAI,EAAE;IACJ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,EACD,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBAoB9B;AAQD,wBAAsB,cAAc,CAClC,IAAI,EAAE;IAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,EACvF,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBAyC9B;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,CAAG,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,EAC7D,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBAO9B"}
@@ -0,0 +1,120 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import YAML from "yaml";
4
+ import { optimizeAllOfComposition } from "./optimizeAllOfComposition.js";
5
+ import { removeUnusedSchemas } from "./removeUnusedSchemas.js";
6
+ import { removeFromOneOfByName, removeFromOneOfGlobally, } from "./removeFromOneOfByName.js";
7
+ function parseYamlOrJson(data) {
8
+ // Accept pre-parsed objects (useful in tests)
9
+ if (data && typeof data === "object")
10
+ return data;
11
+ if (typeof data !== "string")
12
+ return {};
13
+ try {
14
+ return YAML.parse(data);
15
+ }
16
+ catch (yamlError) {
17
+ try {
18
+ return JSON.parse(data);
19
+ }
20
+ catch (jsonError) {
21
+ console.error("Failed to parse input as YAML or JSON:", yamlError, jsonError);
22
+ return {};
23
+ }
24
+ }
25
+ }
26
+ async function writeOutput(doc, output, format) {
27
+ const out = format(doc, output);
28
+ if (output) {
29
+ await fs.mkdir(path.dirname(output), { recursive: true });
30
+ await fs.writeFile(output, out, "utf8");
31
+ }
32
+ else {
33
+ process.stdout.write(out);
34
+ }
35
+ }
36
+ function logSchemaChanges(before, after) {
37
+ for (const name of before) {
38
+ if (after.includes(name)) {
39
+ console.error(`[KEEP] ${name}`);
40
+ }
41
+ else {
42
+ console.error(`[REMOVE] ${name}`);
43
+ }
44
+ }
45
+ }
46
+ export async function runRemoveUnused(opts, format, reader) {
47
+ // Commander now passes keep and ignoreParents as arrays (variadic)
48
+ const keep = Array.isArray(opts.keep) ? opts.keep : (opts.keep ? String(opts.keep).split(",").map(s => s.trim()).filter(Boolean) : []);
49
+ const ignoreParents = Array.isArray(opts.ignoreParents) ? opts.ignoreParents : (opts.ignoreParents ? String(opts.ignoreParents).split(",").map(s => s.trim()).filter(Boolean) : []);
50
+ const ropts = {
51
+ keep,
52
+ aggressive: Boolean(opts.aggressive),
53
+ ignoreParents,
54
+ };
55
+ let doc, ext;
56
+ doc = parseYamlOrJson(await reader());
57
+ const beforeSchemas = Object.keys(doc?.components?.schemas ?? {});
58
+ removeUnusedSchemas(doc, ropts);
59
+ const afterSchemas = Object.keys(doc?.components?.schemas ?? {});
60
+ logSchemaChanges(beforeSchemas, afterSchemas);
61
+ await writeOutput(doc, opts.output, format);
62
+ }
63
+ function guess(name, doc) {
64
+ const keys = doc?.components?.schemas ? Object.keys(doc.components.schemas) : [];
65
+ return [name, ...keys.filter((key) => key.startsWith(`${name}_`))];
66
+ }
67
+ export async function runRemoveOneOf(opts, format, reader) {
68
+ const doc = parseYamlOrJson(await reader());
69
+ if (!doc.components || !doc.components.schemas) {
70
+ console.error("[ERROR] The input document does not contain valid components.schemas.");
71
+ return;
72
+ }
73
+ const toRemove = opts.guess ? opts.remove.flatMap((name) => guess(name, doc)) : opts.remove;
74
+ if (opts.parent) {
75
+ let anyChanged = false;
76
+ for (const name of toRemove) {
77
+ const changed = removeFromOneOfByName(doc, opts.parent, name);
78
+ if (changed) {
79
+ console.error(`[REMOVE-ONEOF] Removed '${name}' from oneOf of '${opts.parent}'.`);
80
+ anyChanged = true;
81
+ }
82
+ else {
83
+ console.error(`[WARN] No change: schema '${name}' not found in oneOf of '${opts.parent}'.`);
84
+ }
85
+ }
86
+ if (!anyChanged) {
87
+ console.error(`[WARN] No schemas removed from oneOf of '${opts.parent}'.`);
88
+ }
89
+ }
90
+ else {
91
+ let total = 0;
92
+ for (const name of toRemove) {
93
+ const count = removeFromOneOfGlobally(doc, name);
94
+ if (count === 0) {
95
+ console.error(`[WARN] No change: schema '${name}' not found in any oneOf.`);
96
+ }
97
+ else {
98
+ console.error(`[REMOVE-ONEOF-GLOBAL] Removed '${name}' from ${count} oneOf(s) globally.`);
99
+ total += count;
100
+ }
101
+ }
102
+ if (total === 0) {
103
+ console.error(`[WARN] No schemas removed globally.`);
104
+ }
105
+ }
106
+ await writeOutput(doc, opts.output, format);
107
+ }
108
+ /**
109
+ * Optimizes allOf composition in the provided OpenAPI document.
110
+ *
111
+ * @param input - Path to the input OpenAPI document.
112
+ * @param output - Path to the output OpenAPI document.
113
+ */
114
+ export async function optimizeAllOf(opts, format, reader) {
115
+ const data = await reader();
116
+ const doc = parseYamlOrJson(data);
117
+ optimizeAllOfComposition(doc);
118
+ await writeOutput(doc, opts.output, format);
119
+ }
120
+ //# sourceMappingURL=cliActions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cliActions.js","sourceRoot":"","sources":["../../src/lib/cliActions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAiB,MAAM,0BAA0B,CAAC;AAC9E,OAAO,EACL,qBAAqB,EACrB,uBAAuB,GACxB,MAAM,4BAA4B,CAAC;AAEpC,SAAS,eAAe,CAAC,IAAS;IAChC,8CAA8C;IAC9C,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAClD,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,EAAE,CAAC;IACxC,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAAC,OAAO,SAAS,EAAE,CAAC;QACnB,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC;QAAC,OAAO,SAAS,EAAE,CAAC;YACnB,OAAO,CAAC,KAAK,CAAC,wCAAwC,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;YAC9E,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC;AACH,CAAC;AAED,KAAK,UAAU,WAAW,CACxB,GAAQ,EACR,MAA0B,EAC1B,MAA6C;IAE7C,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAChC,IAAI,MAAM,EAAE,CAAC;QACX,MAA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QAAQ,EAAE,CAAC;YAC5B,MAAM,KAAK,GAAG,uBAAuB,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACjD,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;gBAChB,OAAO,CAAC,KAAK,CAAC,6BAA6B,IAAI,2BAA2B,CAAC,CAAC;YAC9E,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,KAAK,CAAC,kCAAkC,IAAI,UAAU,KAAK,qBAAqB,CAAC,CAAC;gBAC1F,KAAK,IAAI,KAAK,CAAC;YACjB,CAAC;QACH,CAAC;QACD,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YAChB,OAAO,CAAC,KAAK,CAAC,qCAAqC,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IACD,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAG,IAAyB,EAC7D,MAA6C,EAC7C,MAA6B;IAC7B,MAAM,IAAI,GAAG,MAAM,MAAM,EAAE,CAAC;IAC5B,MAAM,GAAG,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IAElC,wBAAwB,CAAC,GAAG,CAAC,CAAC;IAE9B,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Helper to extract a components/schemas ref name from a $ref string.
3
+ */
4
+ export declare function refToName(ref: string): string | undefined;
5
+ //# sourceMappingURL=oasUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oasUtils.d.ts","sourceRoot":"","sources":["../../src/lib/oasUtils.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGzD"}
@@ -0,0 +1,9 @@
1
+ // Common OpenAPI schema utilities
2
+ /**
3
+ * Helper to extract a components/schemas ref name from a $ref string.
4
+ */
5
+ export function refToName(ref) {
6
+ const m = ref.match(/^#\/(?:components\/)?schemas\/([^#/]+)$/);
7
+ return m ? decodeURIComponent(m[1]) : undefined;
8
+ }
9
+ //# sourceMappingURL=oasUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oasUtils.js","sourceRoot":"","sources":["../../src/lib/oasUtils.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAElC;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC/D,OAAO,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAClD,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Optimize allOf composition globally across an OpenAPI document.
3
+ *
4
+ * Rule: within any schema S that has an allOf array containing $ref items, if one referenced
5
+ * schema (e.g., A) is already included transitively via another referenced schema (e.g., B
6
+ * where B.allOf includes A, possibly via deeper chains), then the direct reference to A in S.allOf
7
+ * is redundant and can be removed.
8
+ *
9
+ * Only $ref entries inside top-level allOf arrays of components.schemas are considered for removal.
10
+ * Inline constraints in allOf are preserved.
11
+ */
12
+ export declare function optimizeAllOfComposition(doc: any): any;
13
+ //# sourceMappingURL=optimizeAllOfComposition.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optimizeAllOfComposition.d.ts","sourceRoot":"","sources":["../../src/lib/optimizeAllOfComposition.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAiFtD"}
@@ -0,0 +1,99 @@
1
+ import { refToName } from "./oasUtils.js";
2
+ /**
3
+ * Optimize allOf composition globally across an OpenAPI document.
4
+ *
5
+ * Rule: within any schema S that has an allOf array containing $ref items, if one referenced
6
+ * schema (e.g., A) is already included transitively via another referenced schema (e.g., B
7
+ * where B.allOf includes A, possibly via deeper chains), then the direct reference to A in S.allOf
8
+ * is redundant and can be removed.
9
+ *
10
+ * Only $ref entries inside top-level allOf arrays of components.schemas are considered for removal.
11
+ * Inline constraints in allOf are preserved.
12
+ */
13
+ export function optimizeAllOfComposition(doc) {
14
+ if (!doc || typeof doc !== "object")
15
+ return doc;
16
+ const schemas = doc.components?.schemas;
17
+ if (!schemas || typeof schemas !== "object")
18
+ return doc;
19
+ // Build direct allOf graph: name -> Set<direct base names>
20
+ const directAllOf = new Map();
21
+ const getDirectAllOf = (name) => {
22
+ if (directAllOf.has(name))
23
+ return directAllOf.get(name);
24
+ const bases = new Set();
25
+ const schema = schemas[name];
26
+ const allOf = schema?.allOf;
27
+ if (Array.isArray(allOf)) {
28
+ for (const part of allOf) {
29
+ if (part && typeof part === "object" && typeof part.$ref === "string") {
30
+ const base = refToName(part.$ref);
31
+ if (base)
32
+ bases.add(base);
33
+ }
34
+ }
35
+ }
36
+ directAllOf.set(name, bases);
37
+ return bases;
38
+ };
39
+ // Compute transitive closure for a schema name using DFS with memoization.
40
+ const transitiveMemo = new Map();
41
+ const visiting = new Set();
42
+ const getTransitiveAllOf = (name) => {
43
+ if (transitiveMemo.has(name))
44
+ return transitiveMemo.get(name);
45
+ if (visiting.has(name))
46
+ return new Set(); // break cycles
47
+ visiting.add(name);
48
+ const acc = new Set();
49
+ for (const base of getDirectAllOf(name)) {
50
+ if (!acc.has(base))
51
+ acc.add(base);
52
+ // Merge base's transitive
53
+ const t = getTransitiveAllOf(base);
54
+ for (const x of t)
55
+ acc.add(x);
56
+ }
57
+ visiting.delete(name);
58
+ transitiveMemo.set(name, acc);
59
+ return acc;
60
+ };
61
+ // For each schema with allOf, drop redundant $ref items
62
+ for (const [name, schema] of Object.entries(schemas)) {
63
+ const allOf = schema?.allOf;
64
+ if (!Array.isArray(allOf))
65
+ continue;
66
+ // Collect indices of $ref parts and their names
67
+ const refItems = [];
68
+ for (let i = 0; i < allOf.length; i++) {
69
+ const part = allOf[i];
70
+ if (part && typeof part === "object" && typeof part.$ref === "string") {
71
+ const base = refToName(part.$ref);
72
+ if (base)
73
+ refItems.push({ idx: i, name: base });
74
+ }
75
+ }
76
+ if (refItems.length < 2)
77
+ continue; // nothing to optimize
78
+ const toDrop = new Set();
79
+ // For each pair (A,B) among refs in this allOf, if B transitively includes A, drop A.
80
+ for (let i = 0; i < refItems.length; i++) {
81
+ const a = refItems[i];
82
+ for (let j = 0; j < refItems.length; j++) {
83
+ if (i === j)
84
+ continue;
85
+ const b = refItems[j];
86
+ // Check if b includes a transitively
87
+ const trans = getTransitiveAllOf(b.name);
88
+ if (trans.has(a.name)) {
89
+ toDrop.add(a.idx);
90
+ }
91
+ }
92
+ }
93
+ if (toDrop.size > 0) {
94
+ schema.allOf = allOf.filter((_, idx) => !toDrop.has(idx));
95
+ }
96
+ }
97
+ return doc;
98
+ }
99
+ //# sourceMappingURL=optimizeAllOfComposition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optimizeAllOfComposition.js","sourceRoot":"","sources":["../../src/lib/optimizeAllOfComposition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE1C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,wBAAwB,CAAC,GAAQ;IAC/C,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,GAAG,CAAC;IAChD,MAAM,OAAO,GAAoC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC;IACzE,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,GAAG,CAAC;IAExD,2DAA2D;IAC3D,MAAM,WAAW,GAAG,IAAI,GAAG,EAAuB,CAAC;IAEnD,MAAM,cAAc,GAAG,CAAC,IAAY,EAAe,EAAE;QACnD,IAAI,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,CAAC,MAAM,GAAG,CAAC;YAAE,SAAS,CAAC,sBAAsB;QAEzD,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;QACjC,sFAAsF;QACtF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACzC,MAAM,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;YACtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;gBACzC,IAAI,CAAC,KAAK,CAAC;oBAAE,SAAS;gBACtB,MAAM,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;gBACtB,qCAAqC;gBACrC,MAAM,KAAK,GAAG,kBAAkB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;gBACzC,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;oBACtB,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;gBACpB,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,MAAM,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;YACnB,MAAc,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QACrE,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Remove a schema from all oneOfs in the OAS and update discriminator mappings globally.
3
+ * @param doc OpenAPI document
4
+ * @param removeName Name of the schema to remove from all oneOfs
5
+ * @returns Number of schemas modified
6
+ */
7
+ export declare function removeFromOneOfGlobally(doc: any, removeName: string): number;
8
+ /**
9
+ * Remove a schema from oneOf by name and update discriminator mappings.
10
+ * @param doc OpenAPI document
11
+ * @param parentSchemaName Name of the parent schema containing oneOf
12
+ * @param removeName Name of the schema to remove from oneOf
13
+ */
14
+ export declare function removeFromOneOfByName(doc: any, parentSchemaName: string, removeName: string): boolean;
15
+ //# sourceMappingURL=removeFromOneOfByName.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeFromOneOfByName.d.ts","sourceRoot":"","sources":["../../src/lib/removeFromOneOfByName.ts"],"names":[],"mappings":"AA+BA;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAqB5E;AAGD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,GAAG,EAAE,gBAAgB,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAQrG"}
@@ -0,0 +1,70 @@
1
+ function updateDiscriminator(node, removeName) {
2
+ if (node.discriminator && node.discriminator.mapping) {
3
+ for (const [key, ref] of Object.entries(node.discriminator.mapping)) {
4
+ const name = refToName(ref);
5
+ if (name === removeName) {
6
+ delete node.discriminator.mapping[key];
7
+ return true;
8
+ }
9
+ }
10
+ }
11
+ return false;
12
+ }
13
+ function updateOneOf(node, removeName) {
14
+ if (!node || !Array.isArray(node.oneOf)) {
15
+ return false;
16
+ }
17
+ const beforeLength = node.oneOf.length;
18
+ node.oneOf = node.oneOf.filter((item) => {
19
+ if (typeof item?.$ref === "string") {
20
+ const name = refToName(item.$ref);
21
+ return name !== removeName;
22
+ }
23
+ return true;
24
+ });
25
+ return beforeLength !== node.oneOf.length;
26
+ }
27
+ /**
28
+ * Remove a schema from all oneOfs in the OAS and update discriminator mappings globally.
29
+ * @param doc OpenAPI document
30
+ * @param removeName Name of the schema to remove from all oneOfs
31
+ * @returns Number of schemas modified
32
+ */
33
+ export function removeFromOneOfGlobally(doc, removeName) {
34
+ let modified = 0;
35
+ function traverse(node) {
36
+ if (!node || typeof node !== 'object')
37
+ return;
38
+ if (Array.isArray(node)) {
39
+ node.forEach(traverse);
40
+ return;
41
+ }
42
+ if (updateOneOf(node, removeName)) {
43
+ updateDiscriminator(node, removeName);
44
+ modified++;
45
+ }
46
+ for (const k of Object.keys(node)) {
47
+ traverse(node[k]);
48
+ }
49
+ }
50
+ traverse(doc);
51
+ return modified;
52
+ }
53
+ import { refToName } from './oasUtils.js';
54
+ /**
55
+ * Remove a schema from oneOf by name and update discriminator mappings.
56
+ * @param doc OpenAPI document
57
+ * @param parentSchemaName Name of the parent schema containing oneOf
58
+ * @param removeName Name of the schema to remove from oneOf
59
+ */
60
+ export function removeFromOneOfByName(doc, parentSchemaName, removeName) {
61
+ if (!doc?.components?.schemas)
62
+ return false;
63
+ const parentSchema = doc.components.schemas[parentSchemaName];
64
+ const removed = updateOneOf(parentSchema, removeName);
65
+ if (removed) {
66
+ return updateDiscriminator(parentSchema, removeName);
67
+ }
68
+ return false;
69
+ }
70
+ //# sourceMappingURL=removeFromOneOfByName.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeFromOneOfByName.js","sourceRoot":"","sources":["../../src/lib/removeFromOneOfByName.ts"],"names":[],"mappings":"AAEA,SAAS,mBAAmB,CAAC,IAAS,EAAE,UAAkB;IACpD,IAAI,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC;QACrD,KAAK,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;YACpE,MAAM,IAAI,GAAG,SAAS,CAAC,GAAa,CAAC,CAAC;YACtC,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;gBACxB,OAAO,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;gBACvC,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACnB,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AAC,CAAC;QACpB,CAAC;IACH,CAAC;IACD,QAAQ,CAAC,GAAG,CAAC,CAAC;IACd,OAAO,QAAQ,CAAC;AAClB,CAAC;AACD,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE1C;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,GAAQ,EAAE,gBAAwB,EAAE,UAAkB;IACxF,IAAI,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO;QAAE,OAAO,KAAK,CAAC;IAC5C,MAAM,YAAY,GAAG,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC9D,MAAM,OAAO,GAAG,WAAW,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;IACtD,IAAI,OAAO,EAAE,CAAC;QACZ,OAAO,mBAAmB,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;IACvD,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { OpenAPI } from "openapi-types";
2
+ export type OASDoc = OpenAPI.Document;
3
+ export interface RemoveOptions {
4
+ /** If true, also remove unused parameters, responses, headers, requestBodies, etc. */
5
+ aggressive?: boolean;
6
+ /** Keep schemas by name regardless of usage */
7
+ keep?: string[];
8
+ /** Ignore these schemas as allOf parents (do not promote children via these) */
9
+ ignoreParents?: string[];
10
+ }
11
+ /**
12
+ * Remove unused components.schemas from an OpenAPI document (for CLI command).
13
+ * Mutates and returns the same object for convenience.
14
+ */
15
+ export declare function removeUnusedSchemas(doc: any, opts?: RemoveOptions): any;
16
+ //# sourceMappingURL=removeUnusedSchemas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeUnusedSchemas.d.ts","sourceRoot":"","sources":["../../src/lib/removeUnusedSchemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC,MAAM,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;AAEtC,MAAM,WAAW,aAAa;IAC5B,sFAAsF;IACtF,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,+CAA+C;IAC/C,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,gFAAgF;IAChF,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B;AAKD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,GAAE,aAAkB,GAAG,GAAG,CAmJ3E"}
@@ -0,0 +1,190 @@
1
+ import { refToName } from './oasUtils.js';
2
+ /**
3
+ * Remove unused components.schemas from an OpenAPI document (for CLI command).
4
+ * Mutates and returns the same object for convenience.
5
+ */
6
+ export function removeUnusedSchemas(doc, opts = {}) {
7
+ if (!doc || typeof doc !== "object")
8
+ return doc;
9
+ const keepSet = new Set((opts.keep ?? []).map((s) => String(s)));
10
+ const schemas = doc.components?.schemas;
11
+ if (!schemas || typeof schemas !== "object")
12
+ return doc;
13
+ // 1) Collect $ref targets starting ONLY from the paths section
14
+ // Follow $ref to non-schema components to discover schemas nested within them.
15
+ const used = new Set();
16
+ const queue = [];
17
+ const visitedComponents = new Set(); // key: section:name
18
+ const usedComponents = new Map(); // section -> names
19
+ const markUsedComponent = (section, name) => {
20
+ if (!usedComponents.has(section))
21
+ usedComponents.set(section, new Set());
22
+ usedComponents.get(section).add(name);
23
+ };
24
+ const visitCollectFromPaths = (node) => {
25
+ if (!node || typeof node !== "object")
26
+ return;
27
+ if (Array.isArray(node))
28
+ return node.forEach(visitCollectFromPaths);
29
+ if (typeof node.$ref === "string") {
30
+ const refStr = node.$ref;
31
+ const n = refToName(refStr);
32
+ if (n && !used.has(n)) {
33
+ used.add(n);
34
+ queue.push(n);
35
+ }
36
+ else {
37
+ // Resolve other components references like #/components/requestBodies/Name
38
+ const m = refStr.match(/^#\/components\/([^\/]+)\/([^#\/]+)$/);
39
+ if (m) {
40
+ const section = m[1];
41
+ const name = decodeURIComponent(m[2]);
42
+ const key = section + ":" + name;
43
+ if (!visitedComponents.has(key)) {
44
+ visitedComponents.add(key);
45
+ markUsedComponent(section, name);
46
+ const target = doc?.components?.[section]?.[name];
47
+ if (target)
48
+ visitCollectFromPaths(target);
49
+ }
50
+ }
51
+ }
52
+ }
53
+ for (const k of Object.keys(node))
54
+ visitCollectFromPaths(node[k]);
55
+ };
56
+ if (doc.paths)
57
+ visitCollectFromPaths(doc.paths);
58
+ // 2) Follow transitive references among schemas (downward closure via $ref in schema bodies)
59
+ const collectRefsInSchema = (schema, add) => {
60
+ if (!schema || typeof schema !== "object")
61
+ return;
62
+ if (Array.isArray(schema))
63
+ return schema.forEach((x) => collectRefsInSchema(x, add));
64
+ if (typeof schema.$ref === "string") {
65
+ const maybe = refToName(schema.$ref);
66
+ if (maybe)
67
+ add(maybe);
68
+ }
69
+ for (const k of Object.keys(schema))
70
+ collectRefsInSchema(schema[k], add);
71
+ };
72
+ while (queue.length) {
73
+ const name = queue.pop();
74
+ const schema = schemas[name];
75
+ if (!schema)
76
+ continue;
77
+ collectRefsInSchema(schema, (n) => {
78
+ if (!used.has(n)) {
79
+ used.add(n);
80
+ queue.push(n);
81
+ }
82
+ });
83
+ }
84
+ // 3) Special rule: if a schema has an allOf that references any used schema, it is also considered used.
85
+ // Implement as an upward closure using a reverse map from base->composers. Find allOf anywhere inside the schema.
86
+ const reverseAllOf = new Map();
87
+ const collectAllOfRefs = (node, add) => {
88
+ if (!node || typeof node !== "object")
89
+ return;
90
+ if (Array.isArray(node))
91
+ return node.forEach((x) => collectAllOfRefs(x, add));
92
+ if (Array.isArray(node.allOf)) {
93
+ for (const part of node.allOf) {
94
+ if (part &&
95
+ typeof part === "object" &&
96
+ typeof part.$ref === "string") {
97
+ const base = refToName(part.$ref);
98
+ if (base)
99
+ add(base);
100
+ }
101
+ }
102
+ }
103
+ for (const k of Object.keys(node))
104
+ collectAllOfRefs(node[k], add);
105
+ };
106
+ for (const [name, schema] of Object.entries(schemas)) {
107
+ collectAllOfRefs(schema, (base) => {
108
+ if (!reverseAllOf.has(base))
109
+ reverseAllOf.set(base, new Set());
110
+ reverseAllOf.get(base).add(name);
111
+ });
112
+ }
113
+ // Only promote via allOf if the "parent" is already referenced (in used set)
114
+ const ignoreParentsSet = new Set((opts.ignoreParents ?? []).map(String));
115
+ const upQueue = Array.from(used);
116
+ const promoted = new Set();
117
+ while (upQueue.length) {
118
+ const base = upQueue.pop();
119
+ if (!used.has(base))
120
+ continue; // Only promote if parent is referenced
121
+ if (ignoreParentsSet.has(base))
122
+ continue; // Do not promote children via ignored parents
123
+ const parents = reverseAllOf.get(base);
124
+ if (!parents)
125
+ continue;
126
+ for (const parent of parents) {
127
+ if (!used.has(parent)) {
128
+ used.add(parent);
129
+ promoted.add(parent);
130
+ upQueue.push(parent);
131
+ }
132
+ }
133
+ }
134
+ // After promoting via allOf, include their transitive refs as well.
135
+ const queue2 = Array.from(promoted);
136
+ while (queue2.length) {
137
+ const name = queue2.pop();
138
+ const schema = schemas[name];
139
+ if (!schema)
140
+ continue;
141
+ collectRefsInSchema(schema, (n) => {
142
+ if (!used.has(n)) {
143
+ used.add(n);
144
+ queue2.push(n);
145
+ }
146
+ });
147
+ }
148
+ // 4) Remove unused schemas unless explicitly kept
149
+ for (const name of Object.keys(schemas)) {
150
+ if (keepSet.has(name))
151
+ continue;
152
+ if (!used.has(name))
153
+ delete schemas[name];
154
+ }
155
+ if (opts.aggressive && doc.components) {
156
+ pruneUnusedComponents(doc, usedComponents);
157
+ }
158
+ return doc;
159
+ }
160
+ function pruneUnusedComponents(doc, usedComponents) {
161
+ const c = doc.components ?? {};
162
+ const sections = [
163
+ "parameters",
164
+ "responses",
165
+ "headers",
166
+ "requestBodies",
167
+ "examples",
168
+ "links",
169
+ "callbacks",
170
+ "securitySchemes",
171
+ ];
172
+ if (usedComponents) {
173
+ for (const section of sections) {
174
+ const map = c[section];
175
+ if (!map || typeof map !== "object")
176
+ continue;
177
+ const usedSet = usedComponents.get(section) ?? new Set();
178
+ for (const name of Object.keys(map)) {
179
+ if (!usedSet.has(name))
180
+ delete map[name];
181
+ }
182
+ if (Object.keys(map).length === 0)
183
+ delete c[section];
184
+ }
185
+ }
186
+ // Finally, delete empty components object
187
+ if (Object.keys(c).length === 0)
188
+ delete doc.components;
189
+ }
190
+ //# sourceMappingURL=removeUnusedSchemas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeUnusedSchemas.js","sourceRoot":"","sources":["../../src/lib/removeUnusedSchemas.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE1C;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAQ,EAAE,OAAsB,EAAE;IACpE,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,GAAG,CAAC;IAChD,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAEjE,MAAM,OAAO,GAAoC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC;IACzE,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,GAAG,CAAC;IAGxD,+DAA+D;IAC/D,+EAA+E;IAC/E,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAU,CAAC,CAAC,oBAAoB;IACjE,MAAM,cAAc,GAAG,IAAI,GAAG,EAAuB,CAAC,CAAC,mBAAmB;IAE1E,MAAM,iBAAiB,GAAG,CAAC,OAAe,EAAE,IAAY,EAAE,EAAE;QAC1D,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;YAAE,cAAc,CAAC,GAAG,CAAC,OAAO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;QACP,WAAW;QACX,iBAAiB;KAClB,CAAC;IAEF,IAAI,cAAc,EAAE,CAAC;QACnB,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC/B,MAAM,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC;YACvB,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;gBAAE,SAAS;YAC9C,MAAM,OAAO,GAAG,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,GAAG,EAAU,CAAC;YACjE,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;gBACpC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;oBAAE,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC;YAC3C,CAAC;YACD,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IAED,0CAA0C;IAC1C,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,GAAG,CAAC,UAAU,CAAC;AACzD,CAAC"}
@@ -0,0 +1,6 @@
1
+ export default function OptimizeAllOfDecorator(): {
2
+ Root: {
3
+ leave(target: any): void;
4
+ };
5
+ };
6
+ //# sourceMappingURL=optimize-allof-decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optimize-allof-decorator.d.ts","sourceRoot":"","sources":["../../src/redocly/optimize-allof-decorator.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,UAAU,sBAAsB;;sBAG1B,GAAG;;EAKtB"}
@@ -0,0 +1,11 @@
1
+ import { optimizeAllOfComposition } from "../lib/optimizeAllOfComposition.js";
2
+ export default function OptimizeAllOfDecorator() {
3
+ return {
4
+ Root: {
5
+ leave(target) {
6
+ optimizeAllOfComposition(target);
7
+ },
8
+ },
9
+ };
10
+ }
11
+ //# sourceMappingURL=optimize-allof-decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optimize-allof-decorator.js","sourceRoot":"","sources":["../../src/redocly/optimize-allof-decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAE9E,MAAM,CAAC,OAAO,UAAU,sBAAsB;IAC5C,OAAO;QACL,IAAI,EAAE;YACJ,KAAK,CAAC,MAAW;gBACf,wBAAwB,CAAC,MAAM,CAAC,CAAC;YACnC,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,23 @@
1
+ import RemoveUnusedDecorator from "./remove-unused-decorator.js";
2
+ import RemoveFromOneOfDecorator from "./remove-from-oneof-decorator.js";
3
+ import OptimizeAllOfDecorator from "./optimize-allof-decorator.js";
4
+ export default function oasRemoveUnusedPlugin(): {
5
+ id: string;
6
+ decorators: {
7
+ oas3: {
8
+ "remove-unused": typeof RemoveUnusedDecorator;
9
+ "remove-oneof": typeof RemoveFromOneOfDecorator;
10
+ "optimize-allof": typeof OptimizeAllOfDecorator;
11
+ "remove-unused-schemas": typeof RemoveUnusedDecorator;
12
+ "remove-from-oneof": typeof RemoveFromOneOfDecorator;
13
+ };
14
+ oas2: {
15
+ "remove-unused": typeof RemoveUnusedDecorator;
16
+ "remove-oneof": typeof RemoveFromOneOfDecorator;
17
+ "optimize-allof": typeof OptimizeAllOfDecorator;
18
+ "remove-unused-schemas": typeof RemoveUnusedDecorator;
19
+ "remove-from-oneof": typeof RemoveFromOneOfDecorator;
20
+ };
21
+ };
22
+ };
23
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/redocly/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,8BAA8B,CAAC;AACjE,OAAO,wBAAwB,MAAM,kCAAkC,CAAC;AACxE,OAAO,sBAAsB,MAAM,+BAA+B,CAAC;AAEnE,MAAM,CAAC,OAAO,UAAU,qBAAqB;;;;;;;;;;;;;;;;;;EAwB5C"}
@@ -0,0 +1,29 @@
1
+ import RemoveUnusedDecorator from "./remove-unused-decorator.js";
2
+ import RemoveFromOneOfDecorator from "./remove-from-oneof-decorator.js";
3
+ import OptimizeAllOfDecorator from "./optimize-allof-decorator.js";
4
+ export default function oasRemoveUnusedPlugin() {
5
+ return {
6
+ id: "oas-utils",
7
+ decorators: {
8
+ oas3: {
9
+ // Preferred names (aligned with CLI subcommands)
10
+ "remove-unused": RemoveUnusedDecorator,
11
+ "remove-oneof": RemoveFromOneOfDecorator,
12
+ "optimize-allof": OptimizeAllOfDecorator,
13
+ // Back-compat aliases
14
+ "remove-unused-schemas": RemoveUnusedDecorator,
15
+ "remove-from-oneof": RemoveFromOneOfDecorator,
16
+ },
17
+ oas2: {
18
+ // Preferred names
19
+ "remove-unused": RemoveUnusedDecorator,
20
+ "remove-oneof": RemoveFromOneOfDecorator,
21
+ "optimize-allof": OptimizeAllOfDecorator,
22
+ // Back-compat aliases
23
+ "remove-unused-schemas": RemoveUnusedDecorator,
24
+ "remove-from-oneof": RemoveFromOneOfDecorator,
25
+ },
26
+ },
27
+ };
28
+ }
29
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/redocly/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,8BAA8B,CAAC;AACjE,OAAO,wBAAwB,MAAM,kCAAkC,CAAC;AACxE,OAAO,sBAAsB,MAAM,+BAA+B,CAAC;AAEnE,MAAM,CAAC,OAAO,UAAU,qBAAqB;IAC3C,OAAO;QACP,EAAE,EAAE,WAAW;QACb,UAAU,EAAE;YACV,IAAI,EAAE;gBACR,iDAAiD;gBACjD,eAAe,EAAE,qBAAqB;gBACtC,cAAc,EAAE,wBAAwB;gBACxC,gBAAgB,EAAE,sBAAsB;gBACxC,sBAAsB;gBACtB,uBAAuB,EAAE,qBAAqB;gBAC9C,mBAAmB,EAAE,wBAAwB;aAC1C;YACD,IAAI,EAAE;gBACR,kBAAkB;gBAClB,eAAe,EAAE,qBAAqB;gBACtC,cAAc,EAAE,wBAAwB;gBACxC,gBAAgB,EAAE,sBAAsB;gBACxC,sBAAsB;gBACtB,uBAAuB,EAAE,qBAAqB;gBAC9C,mBAAmB,EAAE,wBAAwB;aAC1C;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,15 @@
1
+ type Config = {
2
+ /** Optional parent schema name containing oneOf; if omitted, remove globally */
3
+ parent?: string;
4
+ /** Schema names to remove (at least one required) */
5
+ remove: string[];
6
+ /** If true, expand each provided name to name + all schemas starting with name_ */
7
+ guess?: boolean;
8
+ };
9
+ export default function RemoveFromOneOfDecorator(config: Config): {
10
+ readonly Root: {
11
+ readonly leave: (target: any) => void;
12
+ };
13
+ };
14
+ export {};
15
+ //# sourceMappingURL=remove-from-oneof-decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remove-from-oneof-decorator.d.ts","sourceRoot":"","sources":["../../src/redocly/remove-from-oneof-decorator.ts"],"names":[],"mappings":"AAEA,KAAK,MAAM,GAAG;IACZ,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qDAAqD;IACrD,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,mFAAmF;IACnF,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAOF,MAAM,CAAC,OAAO,UAAU,wBAAwB,CAAC,MAAM,EAAE,MAAM;;iCAW3C,GAAG;;EAkBtB"}
@@ -0,0 +1,35 @@
1
+ import { removeFromOneOfByName, removeFromOneOfGlobally } from '../lib/removeFromOneOfByName.js';
2
+ function guessNames(base, doc) {
3
+ const keys = doc?.components?.schemas ? Object.keys(doc.components.schemas) : [];
4
+ return [base, ...keys.filter((k) => k.startsWith(`${base}_`))];
5
+ }
6
+ export default function RemoveFromOneOfDecorator(config) {
7
+ const remove = Array.isArray(config?.remove) ? config.remove : [];
8
+ const parent = config?.parent;
9
+ const guess = Boolean(config?.guess);
10
+ if (!remove.length) {
11
+ // Redocly will call this during bundling; be tolerant and no-op when misconfigured
12
+ return { Root: { leave() { } } };
13
+ }
14
+ return {
15
+ Root: {
16
+ leave(target) {
17
+ const doc = target;
18
+ const toRemove = guess
19
+ ? remove.flatMap((n) => guessNames(n, doc))
20
+ : remove;
21
+ if (parent) {
22
+ for (const name of toRemove) {
23
+ removeFromOneOfByName(doc, parent, name);
24
+ }
25
+ }
26
+ else {
27
+ for (const name of toRemove) {
28
+ removeFromOneOfGlobally(doc, name);
29
+ }
30
+ }
31
+ },
32
+ },
33
+ };
34
+ }
35
+ //# sourceMappingURL=remove-from-oneof-decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remove-from-oneof-decorator.js","sourceRoot":"","sources":["../../src/redocly/remove-from-oneof-decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAWjG,SAAS,UAAU,CAAC,IAAY,EAAE,GAAQ;IACxC,MAAM,IAAI,GAAG,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACjF,OAAO,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC;AACjE,CAAC;AAED,MAAM,CAAC,OAAO,UAAU,wBAAwB,CAAC,MAAc;IAC7D,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;IAClE,MAAM,MAAM,GAAG,MAAM,EAAE,MAAM,CAAC;IAC9B,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACrC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QACnB,mFAAmF;QACnF,OAAO,EAAE,IAAI,EAAE,EAAE,KAAK,KAAI,CAAC,EAAE,EAAW,CAAC;IAC3C,CAAC;IAED,OAAO;QACL,IAAI,EAAE;YACJ,KAAK,CAAC,MAAW;gBACf,MAAM,GAAG,GAAG,MAAM,CAAC;gBACnB,MAAM,QAAQ,GAAG,KAAK;oBACpB,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;oBAC3C,CAAC,CAAC,MAAM,CAAC;gBAEX,IAAI,MAAM,EAAE,CAAC;oBACX,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;wBAC5B,qBAAqB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;oBAC3C,CAAC;gBACH,CAAC;qBAAM,CAAC;oBACN,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;wBAC5B,uBAAuB,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;oBACrC,CAAC;gBACH,CAAC;YACH,CAAC;SACF;KACO,CAAC;AACb,CAAC"}
@@ -0,0 +1,9 @@
1
+ export default function RemoveUnusedDecorator(config?: {
2
+ keep?: string[];
3
+ aggressive?: boolean;
4
+ }): {
5
+ Root: {
6
+ leave(target: any): void;
7
+ };
8
+ };
9
+ //# sourceMappingURL=remove-unused-decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remove-unused-decorator.d.ts","sourceRoot":"","sources":["../../src/redocly/remove-unused-decorator.ts"],"names":[],"mappings":"AAIA,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,MAAM,GAAE;IAAE,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAO;;sBAOhF,GAAG;;EAKtB"}
@@ -0,0 +1,17 @@
1
+ import { removeUnusedSchemas } from '../lib/removeUnusedSchemas.js';
2
+ // Redocly decorator factory.
3
+ // See: https://redocly.com/docs/cli/custom-plugins/custom-decorators
4
+ export default function RemoveUnusedDecorator(config = {}) {
5
+ const options = {
6
+ keep: config.keep ?? [],
7
+ aggressive: config.aggressive ?? false,
8
+ };
9
+ return {
10
+ Root: {
11
+ leave(target) {
12
+ removeUnusedSchemas(target, options);
13
+ },
14
+ },
15
+ };
16
+ }
17
+ //# sourceMappingURL=remove-unused-decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remove-unused-decorator.js","sourceRoot":"","sources":["../../src/redocly/remove-unused-decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAiB,MAAM,+BAA+B,CAAC;AAEnF,6BAA6B;AAC7B,qEAAqE;AACrE,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,SAAoD,EAAE;IAClG,MAAM,OAAO,GAAkB;QAC7B,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,EAAE;QACvB,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,KAAK;KACvC,CAAC;IACF,OAAO;QACL,IAAI,EAAE;YACJ,KAAK,CAAC,MAAW;gBACf,mBAAmB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YACvC,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@amartus/oas-utils",
3
+ "version": "0.1.0",
4
+ "description": "Utilities for working with OpenAPI (OAS) documents.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "oas-utils": "dist/cli.js"
10
+ },
11
+ "exports": {
12
+ ".": "./dist/index.js",
13
+ "./redocly/plugin": "./dist/redocly/plugin.js"
14
+ },
15
+ "keywords": [
16
+ "openapi",
17
+ "oas",
18
+ "schemas",
19
+ "redocly",
20
+ "decorator",
21
+ "cli"
22
+ ],
23
+ "author": "bartosz.michalik@amartus.com",
24
+ "license": "MIT",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/Amartus/oas-utils.git"
28
+ },
29
+ "homepage": "https://github.com/Amartus/oas-utils#readme",
30
+ "bugs": {
31
+ "url": "https://github.com/Amartus/oas-utils/issues"
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "engines": {
39
+ "node": ">=18"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "scripts": {
45
+ "build": "tsc -p tsconfig.json",
46
+ "clean": "rm -rf dist",
47
+ "dev": "tsx watch src/cli.ts",
48
+ "typecheck": "tsc --noEmit -p tsconfig.json",
49
+ "test": "vitest run",
50
+ "test:watch": "vitest",
51
+ "prepare": "npm run build"
52
+ },
53
+ "dependencies": {
54
+ "commander": "^12.1.0",
55
+ "openapi-types": "^12.1.3",
56
+ "yaml": "^2.5.1"
57
+ },
58
+ "devDependencies": {
59
+ "@types/node": "^22.5.4",
60
+ "tsx": "^4.19.1",
61
+ "typescript": "^5.5.4",
62
+ "vitest": "^2.1.9"
63
+ }
64
+ }