swagger-sdk-generator 0.0.0-stage → 3.0.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/README.md +58 -2
- package/dist/cli/commands/generate.d.ts +2 -0
- package/dist/cli/commands/generate.js +196 -0
- package/dist/cli/commands/init.d.ts +2 -0
- package/dist/cli/commands/init.js +130 -0
- package/dist/cli/commands/inspect.d.ts +2 -0
- package/dist/cli/commands/inspect.js +164 -0
- package/dist/cli/commands/templates.d.ts +2 -0
- package/dist/cli/commands/templates.js +126 -0
- package/dist/cli/context.d.ts +16 -0
- package/dist/cli/context.js +85 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +25 -0
- package/dist/config/dotenv.d.ts +2 -0
- package/dist/config/dotenv.js +82 -0
- package/dist/config/interpolate.d.ts +8 -0
- package/dist/config/interpolate.js +85 -0
- package/dist/config/load.d.ts +11 -0
- package/dist/config/load.js +182 -0
- package/dist/config/migrate-v3.d.ts +5 -0
- package/dist/config/migrate-v3.js +6 -0
- package/dist/config/mode-compatibility.d.ts +9 -0
- package/dist/config/mode-compatibility.js +17 -0
- package/dist/config/modes.d.ts +13 -0
- package/dist/config/modes.js +27 -0
- package/dist/config/preset-definitions.d.ts +2 -0
- package/dist/config/preset-definitions.js +4 -0
- package/dist/config/presets.d.ts +25 -0
- package/dist/config/presets.js +36 -0
- package/dist/config/resolve.d.ts +32 -0
- package/dist/config/resolve.js +288 -0
- package/dist/config/schema.d.ts +93 -0
- package/dist/config/schema.js +173 -0
- package/dist/config/types.d.ts +42 -0
- package/dist/config/types.js +2 -0
- package/dist/config/workspace.d.ts +30 -0
- package/dist/config/workspace.js +95 -0
- package/dist/core/case-align.d.ts +5 -0
- package/dist/core/case-align.js +90 -0
- package/dist/core/deep-merge.d.ts +2 -0
- package/dist/core/deep-merge.js +21 -0
- package/dist/core/file-set.d.ts +10 -0
- package/dist/core/file-set.js +31 -0
- package/dist/core/import-module.d.ts +1 -0
- package/dist/core/import-module.js +61 -0
- package/dist/core/log.d.ts +9 -0
- package/dist/core/log.js +24 -0
- package/dist/core/manifest.d.ts +17 -0
- package/dist/core/manifest.js +195 -0
- package/dist/core/overlaps.d.ts +6 -0
- package/dist/core/overlaps.js +67 -0
- package/dist/core/package-version.d.ts +1 -0
- package/dist/core/package-version.js +44 -0
- package/dist/core/pipeline.d.ts +30 -0
- package/dist/core/pipeline.js +335 -0
- package/dist/core/plugin.d.ts +72 -0
- package/dist/core/plugin.js +2 -0
- package/dist/core/targets.d.ts +14 -0
- package/dist/core/targets.js +120 -0
- package/dist/core/unmanaged.d.ts +3 -0
- package/dist/core/unmanaged.js +101 -0
- package/dist/core/writer.d.ts +28 -0
- package/dist/core/writer.js +148 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +37 -0
- package/dist/ir/casing.d.ts +5 -0
- package/dist/ir/casing.js +22 -0
- package/dist/ir/graph.d.ts +8 -0
- package/dist/ir/graph.js +95 -0
- package/dist/ir/operations/grouping.d.ts +17 -0
- package/dist/ir/operations/grouping.js +39 -0
- package/dist/ir/operations/legacy-naming.d.ts +2 -0
- package/dist/ir/operations/legacy-naming.js +8 -0
- package/dist/ir/operations/naming.d.ts +6 -0
- package/dist/ir/operations/naming.js +54 -0
- package/dist/ir/option-errors.d.ts +3 -0
- package/dist/ir/option-errors.js +68 -0
- package/dist/ir/transforms/builtins.d.ts +2 -0
- package/dist/ir/transforms/builtins.js +4 -0
- package/dist/ir/transforms/index.d.ts +10 -0
- package/dist/ir/transforms/index.js +39 -0
- package/dist/ir/transforms/types.d.ts +9 -0
- package/dist/ir/transforms/types.js +2 -0
- package/dist/ir/types.d.ts +140 -0
- package/dist/ir/types.js +2 -0
- package/dist/runtime/axios.d.ts +28 -0
- package/dist/runtime/axios.js +132 -0
- package/dist/runtime/fetch.d.ts +6 -0
- package/dist/runtime/fetch.js +38 -0
- package/dist/runtime/index.d.ts +54 -0
- package/dist/runtime/index.js +167 -0
- package/dist/runtime/rtk.d.ts +35 -0
- package/dist/runtime/rtk.js +36 -0
- package/dist/runtime/transport.d.ts +38 -0
- package/dist/runtime/transport.js +30 -0
- package/dist/runtime/wire.d.ts +27 -0
- package/dist/runtime/wire.js +148 -0
- package/dist/spec/html-entities.d.ts +1 -0
- package/dist/spec/html-entities.js +25 -0
- package/dist/spec/load.d.ts +26 -0
- package/dist/spec/load.js +130 -0
- package/dist/spec/openapi/all-of.d.ts +3 -0
- package/dist/spec/openapi/all-of.js +9 -0
- package/dist/spec/openapi/normalize.d.ts +4 -0
- package/dist/spec/openapi/normalize.js +31 -0
- package/dist/spec/openapi/operations.d.ts +4 -0
- package/dist/spec/openapi/operations.js +173 -0
- package/dist/spec/openapi/schema.d.ts +51 -0
- package/dist/spec/openapi/schema.js +307 -0
- package/dist/targets/declarative/config.d.ts +533 -0
- package/dist/targets/declarative/config.js +280 -0
- package/dist/targets/declarative/extensions.d.ts +45 -0
- package/dist/targets/declarative/extensions.js +123 -0
- package/dist/targets/declarative/files.d.ts +13 -0
- package/dist/targets/declarative/files.js +127 -0
- package/dist/targets/declarative/format.d.ts +4 -0
- package/dist/targets/declarative/format.js +104 -0
- package/dist/targets/declarative/index.d.ts +9 -0
- package/dist/targets/declarative/index.js +170 -0
- package/dist/targets/declarative/liquid.d.ts +8 -0
- package/dist/targets/declarative/liquid.js +57 -0
- package/dist/targets/declarative/naming-case.d.ts +3 -0
- package/dist/targets/declarative/naming-case.js +31 -0
- package/dist/targets/declarative/naming.d.ts +42 -0
- package/dist/targets/declarative/naming.js +174 -0
- package/dist/targets/declarative/scaffold.d.ts +5 -0
- package/dist/targets/declarative/scaffold.js +146 -0
- package/dist/targets/declarative/types.d.ts +52 -0
- package/dist/targets/declarative/types.js +287 -0
- package/dist/targets/declarative/views.d.ts +180 -0
- package/dist/targets/declarative/views.js +447 -0
- package/dist/targets/python/extensions.js +152 -0
- package/dist/targets/python/target.json +549 -0
- package/dist/targets/python/templates/client/client.liquid +114 -0
- package/dist/targets/python/templates/client/httpx_transport.liquid +172 -0
- package/dist/targets/python/templates/client/init.liquid +20 -0
- package/dist/targets/python/templates/client/partials/operation_method.liquid +41 -0
- package/dist/targets/python/templates/client/resource.liquid +28 -0
- package/dist/targets/python/templates/client/runtime.liquid +133 -0
- package/dist/targets/python/templates/client/transport.liquid +62 -0
- package/dist/targets/python/templates/models/module.liquid +48 -0
- package/dist/targets/python/templates/models/partials/docstring.liquid +5 -0
- package/dist/targets/python/templates/models/partials/enum_class.liquid +11 -0
- package/dist/targets/python/templates/models/partials/field_line.liquid +5 -0
- package/dist/targets/python/templates/models/partials/model_class.liquid +17 -0
- package/dist/targets/typescript/compatibility-naming.d.ts +6 -0
- package/dist/targets/typescript/compatibility-naming.js +28 -0
- package/dist/targets/typescript/compatibility-options.d.ts +5 -0
- package/dist/targets/typescript/compatibility-options.js +8 -0
- package/dist/targets/typescript/compatibility-target.d.ts +5 -0
- package/dist/targets/typescript/compatibility-target.js +10 -0
- package/dist/targets/typescript/http-operations.d.ts +34 -0
- package/dist/targets/typescript/http-operations.js +155 -0
- package/dist/targets/typescript/imports.d.ts +43 -0
- package/dist/targets/typescript/imports.js +154 -0
- package/dist/targets/typescript/index.d.ts +7 -0
- package/dist/targets/typescript/index.js +52 -0
- package/dist/targets/typescript/mock-compatibility.d.ts +4 -0
- package/dist/targets/typescript/mock-compatibility.js +6 -0
- package/dist/targets/typescript/mock-hints.d.ts +13 -0
- package/dist/targets/typescript/mock-hints.js +75 -0
- package/dist/targets/typescript/mock-reachability.d.ts +12 -0
- package/dist/targets/typescript/mock-reachability.js +87 -0
- package/dist/targets/typescript/model-compatibility.d.ts +14 -0
- package/dist/targets/typescript/model-compatibility.js +16 -0
- package/dist/targets/typescript/naming.d.ts +13 -0
- package/dist/targets/typescript/naming.js +50 -0
- package/dist/targets/typescript/options.d.ts +124 -0
- package/dist/targets/typescript/options.js +90 -0
- package/dist/targets/typescript/outputs/client.d.ts +67 -0
- package/dist/targets/typescript/outputs/client.js +187 -0
- package/dist/targets/typescript/outputs/mocks.d.ts +32 -0
- package/dist/targets/typescript/outputs/mocks.js +371 -0
- package/dist/targets/typescript/outputs/models.d.ts +85 -0
- package/dist/targets/typescript/outputs/models.js +246 -0
- package/dist/targets/typescript/outputs/rtk-query.d.ts +69 -0
- package/dist/targets/typescript/outputs/rtk-query.js +266 -0
- package/dist/targets/typescript/prettier.d.ts +7 -0
- package/dist/targets/typescript/prettier.js +44 -0
- package/dist/targets/typescript/templates/client/client.liquid +30 -0
- package/dist/targets/typescript/templates/client/partials/api-member.liquid +13 -0
- package/dist/targets/typescript/templates/client/partials/jsdoc.liquid +10 -0
- package/dist/targets/typescript/templates/client/partials/method.liquid +26 -0
- package/dist/targets/typescript/templates/client/partials/query-type.liquid +10 -0
- package/dist/targets/typescript/templates/client/resource.liquid +34 -0
- package/dist/targets/typescript/templates/mocks/mock.liquid +15 -0
- package/dist/targets/typescript/templates/mocks/partials/factory.liquid +12 -0
- package/dist/targets/typescript/templates/mocks/partials/imports.liquid +7 -0
- package/dist/targets/typescript/templates/models/alias.liquid +16 -0
- package/dist/targets/typescript/templates/models/enum.liquid +27 -0
- package/dist/targets/typescript/templates/models/index.liquid +14 -0
- package/dist/targets/typescript/templates/models/model.liquid +25 -0
- package/dist/targets/typescript/templates/models/partials/const.liquid +10 -0
- package/dist/targets/typescript/templates/models/partials/imports.liquid +9 -0
- package/dist/targets/typescript/templates/models/partials/jsdoc.liquid +9 -0
- package/dist/targets/typescript/templates/models/partials/property.liquid +7 -0
- package/dist/targets/typescript/templates/rtk-query/api.liquid +16 -0
- package/dist/targets/typescript/templates/rtk-query/partials/arg-types.liquid +6 -0
- package/dist/targets/typescript/templates/rtk-query/partials/endpoint-mutation.liquid +13 -0
- package/dist/targets/typescript/templates/rtk-query/partials/endpoint-query.liquid +13 -0
- package/dist/targets/typescript/templates/rtk-query/partials/file-warning.liquid +7 -0
- package/dist/targets/typescript/templates/rtk-query/partials/hooks.liquid +9 -0
- package/dist/targets/typescript/templates/rtk-query/partials/imports.liquid +7 -0
- package/dist/targets/typescript/templates/rtk-query/partials/request-config.liquid +9 -0
- package/dist/targets/typescript/templates/rtk-query/resource.liquid +40 -0
- package/dist/targets/typescript/ts-type.d.ts +70 -0
- package/dist/targets/typescript/ts-type.js +62 -0
- package/dist/targets/typescript/type-printer.d.ts +21 -0
- package/dist/targets/typescript/type-printer.js +115 -0
- package/dist/templates/engine.d.ts +20 -0
- package/dist/templates/engine.js +161 -0
- package/dist/templates/inspect.d.ts +27 -0
- package/dist/templates/inspect.js +331 -0
- package/package.json +65 -4
- package/schema/sdk-gen.local.schema.json +75 -0
- package/schema/sdk-gen.schema.json +980 -0
- package/schema/sdk-gen.target.schema.json +1384 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { EmittedFile, Formatter, SourceInfo } from './plugin';
|
|
2
|
+
import type { Logger } from './log';
|
|
3
|
+
export interface OutputRun {
|
|
4
|
+
source: SourceInfo;
|
|
5
|
+
output: string;
|
|
6
|
+
dir: string;
|
|
7
|
+
files: EmittedFile[];
|
|
8
|
+
format: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface WriteOptions {
|
|
11
|
+
formatter?: Formatter;
|
|
12
|
+
prune: boolean;
|
|
13
|
+
log: Logger;
|
|
14
|
+
}
|
|
15
|
+
export interface WriteReport {
|
|
16
|
+
source: string;
|
|
17
|
+
output: string;
|
|
18
|
+
dir: string;
|
|
19
|
+
written: string[];
|
|
20
|
+
unchanged: string[];
|
|
21
|
+
deleted: string[];
|
|
22
|
+
}
|
|
23
|
+
export declare function commitSource(runs: OutputRun[], options: WriteOptions): Promise<WriteReport[]>;
|
|
24
|
+
export declare function manifestKey(run: Pick<OutputRun, 'source' | 'output'>): string;
|
|
25
|
+
export declare function finalizeRun(run: OutputRun, options: Pick<WriteOptions, 'formatter' | 'log'>): Promise<{
|
|
26
|
+
run: OutputRun;
|
|
27
|
+
files: EmittedFile[];
|
|
28
|
+
}>;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.commitSource = commitSource;
|
|
46
|
+
exports.manifestKey = manifestKey;
|
|
47
|
+
exports.finalizeRun = finalizeRun;
|
|
48
|
+
const fs = __importStar(require("fs-extra"));
|
|
49
|
+
const path = __importStar(require("path"));
|
|
50
|
+
const case_align_1 = require("./case-align");
|
|
51
|
+
const manifest_1 = require("./manifest");
|
|
52
|
+
const FORMAT_CONCURRENCY = 16;
|
|
53
|
+
function commitSource(runs, options) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
const finalized = yield Promise.all(runs.map((run) => finalizeRun(run, options)));
|
|
56
|
+
const reports = [];
|
|
57
|
+
const casing = new case_align_1.CaseAligner();
|
|
58
|
+
for (const { run, files } of finalized) {
|
|
59
|
+
const report = {
|
|
60
|
+
source: run.source.name,
|
|
61
|
+
output: run.output,
|
|
62
|
+
dir: run.dir,
|
|
63
|
+
written: [],
|
|
64
|
+
unchanged: [],
|
|
65
|
+
deleted: [],
|
|
66
|
+
};
|
|
67
|
+
for (const file of files) {
|
|
68
|
+
const abs = path.join(run.dir, ...file.path.split('/'));
|
|
69
|
+
yield casing.align(run.dir, file.path);
|
|
70
|
+
const existing = yield readIfExists(abs);
|
|
71
|
+
if (existing === file.content) {
|
|
72
|
+
report.unchanged.push(file.path);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
yield writeAtomic(abs, file.content);
|
|
76
|
+
report.written.push(file.path);
|
|
77
|
+
}
|
|
78
|
+
reports.push(report);
|
|
79
|
+
}
|
|
80
|
+
for (const [i, { run, files }] of finalized.entries()) {
|
|
81
|
+
const result = yield (0, manifest_1.pruneOrphans)(run.dir, files.map((f) => f.path), { noPrune: !options.prune, sourceKey: manifestKey(run), isAliasKey: legacyManifestKey(run) });
|
|
82
|
+
reports[i].deleted = result.deletedFiles;
|
|
83
|
+
}
|
|
84
|
+
return reports;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function manifestKey(run) {
|
|
88
|
+
return `${run.source.name}#${run.output}`;
|
|
89
|
+
}
|
|
90
|
+
function legacyManifestKey(run) {
|
|
91
|
+
const prefix = `${run.source.name}#`;
|
|
92
|
+
const suffix = `#${run.output}`;
|
|
93
|
+
return (key) => key.startsWith(prefix) && key.endsWith(suffix) && key.length > prefix.length + suffix.length - 1;
|
|
94
|
+
}
|
|
95
|
+
function writeAtomic(abs, content) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
yield fs.ensureDir(path.dirname(abs));
|
|
98
|
+
const tmp = `${abs}.${process.pid}.${Date.now()}.tmp`;
|
|
99
|
+
try {
|
|
100
|
+
yield fs.writeFile(tmp, content, 'utf8');
|
|
101
|
+
yield fs.rename(tmp, abs);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
yield fs.remove(tmp).catch(() => undefined);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function finalizeRun(run, options) {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
const out = new Array(run.files.length);
|
|
112
|
+
for (let start = 0; start < run.files.length; start += FORMAT_CONCURRENCY) {
|
|
113
|
+
const batch = run.files.slice(start, start + FORMAT_CONCURRENCY);
|
|
114
|
+
yield Promise.all(batch.map((file, offset) => __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
var _a;
|
|
116
|
+
const shouldFormat = ((_a = file.format) !== null && _a !== void 0 ? _a : run.format) && options.formatter !== undefined;
|
|
117
|
+
let content = file.content;
|
|
118
|
+
if (shouldFormat) {
|
|
119
|
+
const abs = path.join(run.dir, ...file.path.split('/'));
|
|
120
|
+
try {
|
|
121
|
+
content = yield options.formatter.format(abs, content);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
125
|
+
throw new Error(`[${run.source.name}/${run.output}] ${options.formatter.name} failed on ${file.path}: ${message}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
out[start + offset] = Object.assign(Object.assign({}, file), { content: ensureTrailingNewline(content) });
|
|
129
|
+
})));
|
|
130
|
+
}
|
|
131
|
+
return { run, files: out };
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
function ensureTrailingNewline(content) {
|
|
135
|
+
return content.trimEnd() + '\n';
|
|
136
|
+
}
|
|
137
|
+
function readIfExists(file) {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
try {
|
|
140
|
+
return yield fs.readFile(file, 'utf8');
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (error.code === 'ENOENT')
|
|
144
|
+
return undefined;
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type AsyncResolveOptions } from './config/resolve';
|
|
2
|
+
import { type UserConfig } from './config/schema';
|
|
3
|
+
import { type Logger } from './core/log';
|
|
4
|
+
import { type SourceResult } from './core/pipeline';
|
|
5
|
+
export interface GenerateOptions extends Partial<Omit<AsyncResolveOptions, 'cwd' | 'promptable' | 'promptedWorkspace'>> {
|
|
6
|
+
cwd?: string;
|
|
7
|
+
log?: Logger;
|
|
8
|
+
dryRun?: boolean;
|
|
9
|
+
only?: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare function generate(config?: UserConfig, options?: GenerateOptions): Promise<SourceResult[]>;
|
|
12
|
+
export { defineConfig, type UserConfig } from './config/schema';
|
|
13
|
+
export type { ResolvedConfig, ResolvedSource } from './config/types';
|
|
14
|
+
export type * from './ir/types';
|
|
15
|
+
export type { IrTransform } from './ir/transforms/types';
|
|
16
|
+
export type { Target, OutputPlugin, OutputContext, OutputBaseOptions, EmittedFile, Formatter, TemplateRenderer, RuntimeFiles, } from './core/plugin';
|
|
17
|
+
export type { TargetFactory, PluginModule } from './core/targets';
|
|
18
|
+
export type { SourceResult } from './core/pipeline';
|
|
19
|
+
export { createTypeScriptTarget } from './targets/typescript';
|
|
20
|
+
export type { GenerationMode } from './config/modes';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.createTypeScriptTarget = exports.defineConfig = void 0;
|
|
13
|
+
exports.generate = generate;
|
|
14
|
+
const load_1 = require("./config/load");
|
|
15
|
+
const resolve_1 = require("./config/resolve");
|
|
16
|
+
const schema_1 = require("./config/schema");
|
|
17
|
+
const log_1 = require("./core/log");
|
|
18
|
+
const pipeline_1 = require("./core/pipeline");
|
|
19
|
+
function generate(config_1) {
|
|
20
|
+
return __awaiter(this, arguments, void 0, function* (config, options = {}) {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
const cwd = (_a = options.cwd) !== null && _a !== void 0 ? _a : process.cwd();
|
|
23
|
+
let user = config ? (0, schema_1.parseUserConfig)(config) : undefined;
|
|
24
|
+
if (!user) {
|
|
25
|
+
const loaded = yield (0, load_1.loadConfigFile)(cwd);
|
|
26
|
+
if (!loaded)
|
|
27
|
+
throw new Error(`No sdk-gen config found in ${cwd}`);
|
|
28
|
+
user = loaded.config;
|
|
29
|
+
}
|
|
30
|
+
const resolved = yield (0, resolve_1.resolveConfigAsync)(user, Object.assign(Object.assign({}, options), { cwd }));
|
|
31
|
+
return (0, pipeline_1.runPipeline)(resolved, { log: (_b = options.log) !== null && _b !== void 0 ? _b : (0, log_1.createLogger)(), dryRun: options.dryRun, only: options.only });
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
var schema_2 = require("./config/schema");
|
|
35
|
+
Object.defineProperty(exports, "defineConfig", { enumerable: true, get: function () { return schema_2.defineConfig; } });
|
|
36
|
+
var typescript_1 = require("./targets/typescript");
|
|
37
|
+
Object.defineProperty(exports, "createTypeScriptTarget", { enumerable: true, get: function () { return typescript_1.createTypeScriptTarget; } });
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function camelizeSeparators(s: string): string;
|
|
2
|
+
export declare function upperFirst(s: string): string;
|
|
3
|
+
export declare function lowerFirst(s: string): string;
|
|
4
|
+
export declare function pascalCase(s: string): string;
|
|
5
|
+
export declare function camelCase(s: string): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.camelizeSeparators = camelizeSeparators;
|
|
4
|
+
exports.upperFirst = upperFirst;
|
|
5
|
+
exports.lowerFirst = lowerFirst;
|
|
6
|
+
exports.pascalCase = pascalCase;
|
|
7
|
+
exports.camelCase = camelCase;
|
|
8
|
+
function camelizeSeparators(s) {
|
|
9
|
+
return s.replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ''));
|
|
10
|
+
}
|
|
11
|
+
function upperFirst(s) {
|
|
12
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
13
|
+
}
|
|
14
|
+
function lowerFirst(s) {
|
|
15
|
+
return s.charAt(0).toLowerCase() + s.slice(1);
|
|
16
|
+
}
|
|
17
|
+
function pascalCase(s) {
|
|
18
|
+
return upperFirst(camelizeSeparators(s));
|
|
19
|
+
}
|
|
20
|
+
function camelCase(s) {
|
|
21
|
+
return lowerFirst(camelizeSeparators(s));
|
|
22
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ApiSpec, ModelId, TypeNode } from './types';
|
|
2
|
+
export declare function childTypes(node: TypeNode): TypeNode[];
|
|
3
|
+
export declare function walkType(node: TypeNode, visit: (node: TypeNode) => boolean | void): void;
|
|
4
|
+
export declare function referencedIds(node: TypeNode): ModelId[];
|
|
5
|
+
export declare function refGraph(api: ApiSpec): Map<ModelId, ModelId[]>;
|
|
6
|
+
export declare function backEdges(graph: Map<ModelId, ModelId[]>): Set<string>;
|
|
7
|
+
export declare function edgeKey(from: ModelId, to: ModelId): string;
|
|
8
|
+
export declare function topologicalModelIds(api: ApiSpec): ModelId[];
|
package/dist/ir/graph.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.childTypes = childTypes;
|
|
4
|
+
exports.walkType = walkType;
|
|
5
|
+
exports.referencedIds = referencedIds;
|
|
6
|
+
exports.refGraph = refGraph;
|
|
7
|
+
exports.backEdges = backEdges;
|
|
8
|
+
exports.edgeKey = edgeKey;
|
|
9
|
+
exports.topologicalModelIds = topologicalModelIds;
|
|
10
|
+
function childTypes(node) {
|
|
11
|
+
switch (node.kind) {
|
|
12
|
+
case 'array':
|
|
13
|
+
return [node.items];
|
|
14
|
+
case 'map':
|
|
15
|
+
return [node.values];
|
|
16
|
+
case 'object':
|
|
17
|
+
return [...node.properties.map((p) => p.type), ...(node.additionalProperties ? [node.additionalProperties] : [])];
|
|
18
|
+
case 'union':
|
|
19
|
+
return node.variants;
|
|
20
|
+
case 'intersection':
|
|
21
|
+
return node.members;
|
|
22
|
+
default:
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function walkType(node, visit) {
|
|
27
|
+
if (visit(node) === false)
|
|
28
|
+
return;
|
|
29
|
+
for (const child of childTypes(node))
|
|
30
|
+
walkType(child, visit);
|
|
31
|
+
}
|
|
32
|
+
function referencedIds(node) {
|
|
33
|
+
const out = [];
|
|
34
|
+
walkType(node, (n) => {
|
|
35
|
+
if (n.kind === 'ref' && !n.unresolved && !out.includes(n.id))
|
|
36
|
+
out.push(n.id);
|
|
37
|
+
});
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
function refGraph(api) {
|
|
41
|
+
const known = new Set(api.models.map((m) => m.id));
|
|
42
|
+
const graph = new Map();
|
|
43
|
+
for (const model of api.models) {
|
|
44
|
+
graph.set(model.id, referencedIds(model.type).filter((id) => known.has(id)).sort());
|
|
45
|
+
}
|
|
46
|
+
return graph;
|
|
47
|
+
}
|
|
48
|
+
function backEdges(graph) {
|
|
49
|
+
var _a;
|
|
50
|
+
const WHITE = 0;
|
|
51
|
+
const GRAY = 1;
|
|
52
|
+
const BLACK = 2;
|
|
53
|
+
const color = new Map();
|
|
54
|
+
const cut = new Set();
|
|
55
|
+
const visit = (node) => {
|
|
56
|
+
var _a, _b;
|
|
57
|
+
color.set(node, GRAY);
|
|
58
|
+
for (const target of (_a = graph.get(node)) !== null && _a !== void 0 ? _a : []) {
|
|
59
|
+
const c = (_b = color.get(target)) !== null && _b !== void 0 ? _b : WHITE;
|
|
60
|
+
if (c === GRAY)
|
|
61
|
+
cut.add(edgeKey(node, target));
|
|
62
|
+
else if (c === WHITE)
|
|
63
|
+
visit(target);
|
|
64
|
+
}
|
|
65
|
+
color.set(node, BLACK);
|
|
66
|
+
};
|
|
67
|
+
for (const node of Array.from(graph.keys()).sort()) {
|
|
68
|
+
if (((_a = color.get(node)) !== null && _a !== void 0 ? _a : WHITE) === WHITE)
|
|
69
|
+
visit(node);
|
|
70
|
+
}
|
|
71
|
+
return cut;
|
|
72
|
+
}
|
|
73
|
+
function edgeKey(from, to) {
|
|
74
|
+
return `${from}->${to}`;
|
|
75
|
+
}
|
|
76
|
+
function topologicalModelIds(api) {
|
|
77
|
+
const graph = refGraph(api);
|
|
78
|
+
const cut = backEdges(graph);
|
|
79
|
+
const order = [];
|
|
80
|
+
const done = new Set();
|
|
81
|
+
const place = (id) => {
|
|
82
|
+
var _a;
|
|
83
|
+
if (done.has(id))
|
|
84
|
+
return;
|
|
85
|
+
done.add(id);
|
|
86
|
+
for (const dep of (_a = graph.get(id)) !== null && _a !== void 0 ? _a : []) {
|
|
87
|
+
if (!cut.has(edgeKey(id, dep)))
|
|
88
|
+
place(dep);
|
|
89
|
+
}
|
|
90
|
+
order.push(id);
|
|
91
|
+
};
|
|
92
|
+
for (const model of api.models)
|
|
93
|
+
place(model.id);
|
|
94
|
+
return order;
|
|
95
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Operation } from '../types';
|
|
2
|
+
export interface GroupingOptions {
|
|
3
|
+
stripPrefixes: string[];
|
|
4
|
+
folders: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface ResourceGroup {
|
|
7
|
+
key: string;
|
|
8
|
+
folder: string;
|
|
9
|
+
resource: string;
|
|
10
|
+
operations: Operation[];
|
|
11
|
+
}
|
|
12
|
+
export declare function groupOperations(operations: Operation[], options: GroupingOptions): ResourceGroup[];
|
|
13
|
+
export declare function locateResource(pathString: string, options: GroupingOptions): {
|
|
14
|
+
folder: string;
|
|
15
|
+
resource: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function stripPrefix(pathString: string, prefixes: string[]): string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.groupOperations = groupOperations;
|
|
4
|
+
exports.locateResource = locateResource;
|
|
5
|
+
exports.stripPrefix = stripPrefix;
|
|
6
|
+
const casing_1 = require("../casing");
|
|
7
|
+
function groupOperations(operations, options) {
|
|
8
|
+
const groups = new Map();
|
|
9
|
+
for (const op of operations) {
|
|
10
|
+
const { folder, resource } = locateResource(op.path, options);
|
|
11
|
+
const key = folder ? `${folder}/${(0, casing_1.upperFirst)(resource)}` : (0, casing_1.upperFirst)(resource);
|
|
12
|
+
let group = groups.get(key);
|
|
13
|
+
if (!group) {
|
|
14
|
+
group = { key, folder, resource, operations: [] };
|
|
15
|
+
groups.set(key, group);
|
|
16
|
+
}
|
|
17
|
+
group.operations.push(op);
|
|
18
|
+
}
|
|
19
|
+
return Array.from(groups.values());
|
|
20
|
+
}
|
|
21
|
+
function locateResource(pathString, options) {
|
|
22
|
+
var _a, _b, _c;
|
|
23
|
+
const segments = stripPrefix(pathString, options.stripPrefixes).split('/');
|
|
24
|
+
let folder = '';
|
|
25
|
+
let resource = (_a = segments[1]) !== null && _a !== void 0 ? _a : '';
|
|
26
|
+
if (options.folders) {
|
|
27
|
+
folder = (_b = segments[1]) !== null && _b !== void 0 ? _b : '';
|
|
28
|
+
resource = (_c = segments[2]) !== null && _c !== void 0 ? _c : '';
|
|
29
|
+
if (!resource) {
|
|
30
|
+
resource = folder;
|
|
31
|
+
folder = '';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { folder, resource: resource || 'api' };
|
|
35
|
+
}
|
|
36
|
+
function stripPrefix(pathString, prefixes) {
|
|
37
|
+
const prefix = prefixes.find((p) => p && (pathString === p || pathString.startsWith(p.endsWith('/') ? p : `${p}/`)));
|
|
38
|
+
return prefix ? pathString.slice(prefix.length) : pathString;
|
|
39
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.COMPATIBILITY_NAMING = void 0;
|
|
4
|
+
exports.legacyName = legacyName;
|
|
5
|
+
exports.COMPATIBILITY_NAMING = [];
|
|
6
|
+
function legacyName(_operation) {
|
|
7
|
+
throw new Error('Unsupported operation naming strategy');
|
|
8
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Operation } from '../types';
|
|
2
|
+
export declare const OPERATION_NAMING: readonly ["path", "path-last-param", "operation-id"];
|
|
3
|
+
export type OperationNamingStrategy = typeof OPERATION_NAMING[number];
|
|
4
|
+
export declare function operationName(op: Operation, strategy: OperationNamingStrategy): string;
|
|
5
|
+
export declare function uniqueOperationNames(operations: Operation[], strategy: OperationNamingStrategy, reserved?: Iterable<string>): Map<Operation, string>;
|
|
6
|
+
export declare function identifierSafe(name: string): string;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OPERATION_NAMING = void 0;
|
|
4
|
+
exports.operationName = operationName;
|
|
5
|
+
exports.uniqueOperationNames = uniqueOperationNames;
|
|
6
|
+
exports.identifierSafe = identifierSafe;
|
|
7
|
+
const casing_1 = require("../casing");
|
|
8
|
+
const legacy_naming_1 = require("./legacy-naming");
|
|
9
|
+
exports.OPERATION_NAMING = [...legacy_naming_1.COMPATIBILITY_NAMING, 'path', 'path-last-param', 'operation-id'];
|
|
10
|
+
function operationName(op, strategy) {
|
|
11
|
+
switch (strategy) {
|
|
12
|
+
case 'path':
|
|
13
|
+
return pathName(op, true);
|
|
14
|
+
case 'path-last-param':
|
|
15
|
+
return pathName(op, false);
|
|
16
|
+
case 'operation-id':
|
|
17
|
+
return op.operationId ? identifierSafe((0, casing_1.camelCase)(op.operationId.replace(/[^A-Za-z0-9_\-\s]/g, '_'))) : pathName(op, true);
|
|
18
|
+
default:
|
|
19
|
+
return (0, legacy_naming_1.legacyName)(op);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function uniqueOperationNames(operations, strategy, reserved = []) {
|
|
23
|
+
const used = new Set(reserved);
|
|
24
|
+
const names = new Map();
|
|
25
|
+
for (const op of operations) {
|
|
26
|
+
const base = operationName(op, strategy);
|
|
27
|
+
let name = base;
|
|
28
|
+
for (let counter = 1; used.has(name); counter++)
|
|
29
|
+
name = `${base}${counter}`;
|
|
30
|
+
used.add(name);
|
|
31
|
+
names.set(op, name);
|
|
32
|
+
}
|
|
33
|
+
return names;
|
|
34
|
+
}
|
|
35
|
+
function identifierSafe(name) {
|
|
36
|
+
return /^[0-9]/.test(name) ? `_${name}` : name || '_';
|
|
37
|
+
}
|
|
38
|
+
function pathName(op, allParams) {
|
|
39
|
+
const parts = op.path.split('/').filter((p) => p.length > 0 && p !== 'app' && p !== 'api');
|
|
40
|
+
const words = [];
|
|
41
|
+
const params = [];
|
|
42
|
+
for (const part of parts) {
|
|
43
|
+
if (/^\{[^}]+\}$/.test(part))
|
|
44
|
+
params.push(part.slice(1, -1));
|
|
45
|
+
else
|
|
46
|
+
words.push(identifierWord(part));
|
|
47
|
+
}
|
|
48
|
+
const used = allParams ? params : params.slice(-1);
|
|
49
|
+
const suffix = used.length ? 'By' + used.map(identifierWord).join('By') : '';
|
|
50
|
+
return `${op.method}${words.join('')}${suffix}`;
|
|
51
|
+
}
|
|
52
|
+
function identifierWord(segment) {
|
|
53
|
+
return (0, casing_1.pascalCase)(segment.replace(/[^A-Za-z0-9_\-\s]+/g, '-').replace(/^-+|-+$/g, ''));
|
|
54
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatOptionsError = formatOptionsError;
|
|
4
|
+
exports.closest = closest;
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
function formatOptionsError(schema, error, input) {
|
|
7
|
+
const hints = [];
|
|
8
|
+
for (const issue of error.issues) {
|
|
9
|
+
if (issue.code === 'invalid_value' && input !== undefined) {
|
|
10
|
+
const given = valueAt(input, issue.path);
|
|
11
|
+
const options = issue.values.filter((v) => typeof v === 'string');
|
|
12
|
+
const guess = typeof given === 'string' ? closest(given, options) : undefined;
|
|
13
|
+
if (guess)
|
|
14
|
+
hints.push(`${issue.path.join('.')}: "${given}" → did you mean "${guess}"?`);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (issue.code !== 'unrecognized_keys')
|
|
18
|
+
continue;
|
|
19
|
+
const known = objectKeysAt(schema, issue.path);
|
|
20
|
+
for (const key of issue.keys) {
|
|
21
|
+
const guess = closest(key, known);
|
|
22
|
+
if (guess)
|
|
23
|
+
hints.push(`"${key}" → did you mean "${guess}"?`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const pretty = zod_1.z.prettifyError(error);
|
|
27
|
+
return hints.length ? `${pretty}\n${hints.map((h) => ` hint: ${h}`).join('\n')}` : pretty;
|
|
28
|
+
}
|
|
29
|
+
function valueAt(input, at) {
|
|
30
|
+
return at.reduce((value, key) => (value && typeof value === 'object' ? value[key] : undefined), input);
|
|
31
|
+
}
|
|
32
|
+
function objectKeysAt(schema, at) {
|
|
33
|
+
var _a;
|
|
34
|
+
let current = schema;
|
|
35
|
+
for (const segment of at) {
|
|
36
|
+
const shape = shapeOf(current);
|
|
37
|
+
current = shape === null || shape === void 0 ? void 0 : shape[String(segment)];
|
|
38
|
+
}
|
|
39
|
+
return Object.keys((_a = shapeOf(current)) !== null && _a !== void 0 ? _a : {});
|
|
40
|
+
}
|
|
41
|
+
function shapeOf(schema) {
|
|
42
|
+
let s = schema;
|
|
43
|
+
for (let i = 0; i < 4 && s && !s.shape && typeof s.unwrap === 'function'; i++)
|
|
44
|
+
s = s.unwrap();
|
|
45
|
+
return s === null || s === void 0 ? void 0 : s.shape;
|
|
46
|
+
}
|
|
47
|
+
function closest(input, candidates) {
|
|
48
|
+
let best;
|
|
49
|
+
for (const key of candidates) {
|
|
50
|
+
const distance = levenshtein(input.toLowerCase(), key.toLowerCase());
|
|
51
|
+
if (!best || distance < best.distance)
|
|
52
|
+
best = { key, distance };
|
|
53
|
+
}
|
|
54
|
+
return best && best.distance <= Math.max(2, Math.floor(input.length / 3)) ? best.key : undefined;
|
|
55
|
+
}
|
|
56
|
+
function levenshtein(a, b) {
|
|
57
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
58
|
+
for (let i = 1; i <= a.length; i++) {
|
|
59
|
+
let previous = row[0];
|
|
60
|
+
row[0] = i;
|
|
61
|
+
for (let j = 1; j <= b.length; j++) {
|
|
62
|
+
const current = row[j];
|
|
63
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, previous + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
64
|
+
previous = current;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return row[b.length];
|
|
68
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ApiSpec } from '../types';
|
|
2
|
+
import type { IrTransform } from './types';
|
|
3
|
+
export type { IrTransform } from './types';
|
|
4
|
+
export declare function registerTransform(transform: IrTransform<any>): void;
|
|
5
|
+
export declare function getTransform(name: string): IrTransform<any> | undefined;
|
|
6
|
+
export declare function listTransforms(): IrTransform<any>[];
|
|
7
|
+
export declare function applyTransforms(api: ApiSpec, enabled: Array<{
|
|
8
|
+
name: string;
|
|
9
|
+
options: Record<string, unknown>;
|
|
10
|
+
}>): ApiSpec;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerTransform = registerTransform;
|
|
4
|
+
exports.getTransform = getTransform;
|
|
5
|
+
exports.listTransforms = listTransforms;
|
|
6
|
+
exports.applyTransforms = applyTransforms;
|
|
7
|
+
const builtins_1 = require("./builtins");
|
|
8
|
+
const option_errors_1 = require("../option-errors");
|
|
9
|
+
const registry = new Map(builtins_1.BUILTIN_TRANSFORMS.map((t) => [t.name, t]));
|
|
10
|
+
function registerTransform(transform) {
|
|
11
|
+
registry.set(transform.name, transform);
|
|
12
|
+
}
|
|
13
|
+
function getTransform(name) {
|
|
14
|
+
return registry.get(name);
|
|
15
|
+
}
|
|
16
|
+
function listTransforms() {
|
|
17
|
+
return Array.from(registry.values());
|
|
18
|
+
}
|
|
19
|
+
function applyTransforms(api, enabled) {
|
|
20
|
+
let current = api;
|
|
21
|
+
for (const { name, options } of enabled) {
|
|
22
|
+
const transform = registry.get(name);
|
|
23
|
+
if (!transform) {
|
|
24
|
+
throw new Error(`Unknown IR transform "${name}". Available: ${Array.from(registry.keys()).join(', ') || '(none)'}`);
|
|
25
|
+
}
|
|
26
|
+
const merged = Object.assign(Object.assign({}, transform.defaults), options);
|
|
27
|
+
if (transform.optionsSchema) {
|
|
28
|
+
const result = transform.optionsSchema.safeParse(merged);
|
|
29
|
+
if (!result.success) {
|
|
30
|
+
throw new Error(`Invalid options for transform ${name}:\n${(0, option_errors_1.formatOptionsError)(transform.optionsSchema, result.error, merged)}`);
|
|
31
|
+
}
|
|
32
|
+
current = transform.apply(current, result.data);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
current = transform.apply(current, merged);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return current;
|
|
39
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ApiSpec } from '../types';
|
|
2
|
+
import type { ZodType } from 'zod';
|
|
3
|
+
export interface IrTransform<O extends object = Record<string, unknown>> {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
defaults: O;
|
|
7
|
+
optionsSchema?: ZodType<O>;
|
|
8
|
+
apply(api: ApiSpec, options: O): ApiSpec;
|
|
9
|
+
}
|