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
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# swagger-sdk-generator
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Generate TypeScript and Python SDKs from OpenAPI. Configure the source documents and the outputs you want in `sdk-gen.config.json`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install swagger-sdk-generator
|
|
7
|
+
npx sdk-gen init --input ./openapi.json
|
|
8
|
+
npx sdk-gen generate
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## TypeScript modes
|
|
12
|
+
|
|
13
|
+
| Mode | Default outputs |
|
|
14
|
+
| --- | --- |
|
|
15
|
+
| `default` | Models and thin Redux Toolkit Query endpoints and hooks |
|
|
16
|
+
| `future` | Models and a typed promise client |
|
|
17
|
+
|
|
18
|
+
Set `mode` in the config or pass `--mode` to `init` or `generate`. Supply a file or URL for each source. Explicit outputs select models, clients, RTK Query modules, or seeded mocks. OpenAPI `allOf` composes schemas as intersections. Use the bundled JSON schema for the available options.
|
|
19
|
+
|
|
20
|
+
This command generates a promise client directly from a local spec.
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npx sdk-gen generate --input ./openapi.json --mode future
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"$schema": "./node_modules/swagger-sdk-generator/schema/sdk-gen.schema.json",
|
|
29
|
+
"version": 4,
|
|
30
|
+
"mode": "future",
|
|
31
|
+
"sources": [{ "name": "api", "file": "./openapi.json" }]
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Runtime
|
|
36
|
+
|
|
37
|
+
The generated SDK contains API-specific files. Shared code is available through package runtime imports. The promise client uses Fetch by default. To configure Axios, pass an instance the application owns.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import axios from "axios"
|
|
41
|
+
import { createAxiosTransport } from "swagger-sdk-generator/runtime"
|
|
42
|
+
import { createApi } from "./src/api/client/client"
|
|
43
|
+
|
|
44
|
+
export const api = createApi({
|
|
45
|
+
transport: createAxiosTransport(axios.create({ baseURL: "/api" })),
|
|
46
|
+
hooks: {
|
|
47
|
+
beforeRequest: (request) => ({
|
|
48
|
+
...request,
|
|
49
|
+
headers: { ...request.headers, "X-App": "web" },
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
})
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Custom transports implement `Transport` from `swagger-sdk-generator/runtime`. They resolve with the HTTP status, headers and decoded body for all HTTP responses. The runtime normalizes HTTP failures, cancellation and network errors. Promise clients also accept typed operation hooks. The RTK Query wrapper supports importing an application-owned base API.
|
|
56
|
+
|
|
57
|
+
## Requirements
|
|
58
|
+
|
|
59
|
+
Use Node.js 20 or later for the CLI. RTK Query hooks use Redux Toolkit, React and React Redux. Install Faker when using generated TypeScript mocks.
|
|
@@ -0,0 +1,196 @@
|
|
|
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.registerGenerate = registerGenerate;
|
|
46
|
+
const commander_1 = require("commander");
|
|
47
|
+
const fs = __importStar(require("fs"));
|
|
48
|
+
const path = __importStar(require("path"));
|
|
49
|
+
const presets_1 = require("../../config/presets");
|
|
50
|
+
const mode_compatibility_1 = require("../../config/mode-compatibility");
|
|
51
|
+
const modes_1 = require("../../config/modes");
|
|
52
|
+
const resolve_1 = require("../../config/resolve");
|
|
53
|
+
const log_1 = require("../../core/log");
|
|
54
|
+
const pipeline_1 = require("../../core/pipeline");
|
|
55
|
+
const context_1 = require("../context");
|
|
56
|
+
const OUTPUT_FLAGS = [
|
|
57
|
+
{ flag: 'models', output: 'models', legacyDirFlag: 'output', help: 'models (TypeScript types)' },
|
|
58
|
+
{ flag: 'client', output: 'client', help: 'promise API client over the Transport contract' },
|
|
59
|
+
...(mode_compatibility_1.HAS_COMPATIBILITY_OUTPUTS ? [{ flag: 'stores', output: 'stores', legacyDirFlag: 'storeOutput', help: 'Redux stores and thunks' }] : []),
|
|
60
|
+
{ flag: 'mocks', output: 'mocks', legacyDirFlag: 'mockOutput', help: 'deterministic faker mocks' },
|
|
61
|
+
{ flag: 'rtkQuery', output: 'rtkQuery', legacyDirFlag: 'rtkQueryOutput', help: 'RTK Query api + hooks' },
|
|
62
|
+
];
|
|
63
|
+
function registerGenerate(program) {
|
|
64
|
+
const cmd = program
|
|
65
|
+
.command('generate')
|
|
66
|
+
.alias('gen')
|
|
67
|
+
.description('Generate code from the configured OpenAPI sources')
|
|
68
|
+
.option('-c, --config <file>', 'config file (default: discovered in cwd)')
|
|
69
|
+
.option('-i, --input <url|file>', 'run without a config file against this spec')
|
|
70
|
+
.option('-u, --url <url>', 'alias of --input for a URL (v3 CLI compat)')
|
|
71
|
+
.option('-f, --file <path>', 'alias of --input for a file (v3 CLI compat)')
|
|
72
|
+
.addOption(new commander_1.Option('--mode <mode>', 'TypeScript SDK mode').choices(modes_1.GENERATION_MODES))
|
|
73
|
+
.option('-w, --workspace <name>', 'value for {workspace} placeholders (else config.workspace / SDK_WORKSPACE); with "hosts": the production token or "prod" = production')
|
|
74
|
+
.option('-s, --source <names...>', 'only run these sources')
|
|
75
|
+
.option('-O, --outputs <names...>', 'only run these outputs of each source (e.g. mocks); the others keep their files')
|
|
76
|
+
.option('-t, --templates <dirs...>', 'template override dirs (searched before config ones)')
|
|
77
|
+
.option('--dry-run', 'generate + format but write nothing; list what would change')
|
|
78
|
+
.option('--no-prune', 'keep files a previous run generated but this run did not')
|
|
79
|
+
.option('--prune-unmanaged', 'also delete generated-banner files no run ever recorded (pre-manifest leftovers); pair with --dry-run to list first')
|
|
80
|
+
.option('--no-format', 'skip the formatter (prettier / biome)')
|
|
81
|
+
.option('--silent', 'only print errors');
|
|
82
|
+
if ((0, presets_1.listPresets)().length)
|
|
83
|
+
cmd.option('-p, --preset <name>', 'preset for --input, or a preset that provides its own sources');
|
|
84
|
+
for (const { flag, help } of OUTPUT_FLAGS) {
|
|
85
|
+
cmd.option(`--${kebab(flag)} [dir]`, `enable ${help} (optionally into <dir>)`);
|
|
86
|
+
}
|
|
87
|
+
if (mode_compatibility_1.HAS_COMPATIBILITY_OUTPUTS)
|
|
88
|
+
cmd.option('--store-output <dir>', 'stores dir (v3 CLI compat)');
|
|
89
|
+
cmd
|
|
90
|
+
.option('-o, --output <dir>', 'models dir (v3 CLI compat)')
|
|
91
|
+
.option('--mock-output <dir>', 'mocks dir (v3 CLI compat)')
|
|
92
|
+
.option('--rtk-query-output <dir>', 'rtk-query dir (v3 CLI compat)')
|
|
93
|
+
.action((flags) => __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
const log = (0, log_1.createLogger)(flags.silent ? 'error' : 'info');
|
|
95
|
+
const results = yield generate(flags, log);
|
|
96
|
+
const failed = results.filter((r) => !r.ok);
|
|
97
|
+
if (failed.length) {
|
|
98
|
+
log.error(`\n${failed.length}/${results.length} source(s) failed: ${failed.map((r) => r.source).join(', ')}`);
|
|
99
|
+
process.exitCode = 1;
|
|
100
|
+
}
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
function generate(flags, log) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
var _a, _b, _c;
|
|
106
|
+
var _d, _e, _f;
|
|
107
|
+
const input = (_e = (_d = flags.input) !== null && _d !== void 0 ? _d : flags.url) !== null && _e !== void 0 ? _e : flags.file;
|
|
108
|
+
const enableOutputs = [];
|
|
109
|
+
const outputDirs = {};
|
|
110
|
+
for (const { flag, output, legacyDirFlag } of OUTPUT_FLAGS) {
|
|
111
|
+
const value = flags[flag];
|
|
112
|
+
const legacyDir = legacyDirFlag ? flags[legacyDirFlag] : undefined;
|
|
113
|
+
if (value !== undefined || typeof legacyDir === 'string')
|
|
114
|
+
enableOutputs.push(output);
|
|
115
|
+
if (typeof value === 'string')
|
|
116
|
+
outputDirs[output] = path.resolve(value);
|
|
117
|
+
else if (typeof legacyDir === 'string')
|
|
118
|
+
outputDirs[output] = path.resolve(legacyDir);
|
|
119
|
+
}
|
|
120
|
+
let user;
|
|
121
|
+
let baseDir = process.cwd();
|
|
122
|
+
const preset = flags.preset !== undefined ? (0, presets_1.getPreset)(flags.preset) : undefined;
|
|
123
|
+
if (flags.preset !== undefined && !preset)
|
|
124
|
+
(0, context_1.fail)(`Unknown preset "${flags.preset}".`);
|
|
125
|
+
const presetHasSources = Boolean((_b = (_a = preset === null || preset === void 0 ? void 0 : preset.config) === null || _a === void 0 ? void 0 : _a.sources) === null || _b === void 0 ? void 0 : _b.length);
|
|
126
|
+
if (input) {
|
|
127
|
+
if (presetHasSources) {
|
|
128
|
+
(0, context_1.fail)(`Preset "${flags.preset}" brings its own sources; run \`sdk-gen generate --preset ${flags.preset}\` without --input (or use a preset without sources).`);
|
|
129
|
+
}
|
|
130
|
+
if ((enableOutputs.length || flags.preset) && !enableOutputs.includes('models'))
|
|
131
|
+
enableOutputs.unshift('models');
|
|
132
|
+
const isUrl = /^https?:\/\//i.test(input);
|
|
133
|
+
user = (0, resolve_1.adHocConfig)(Object.assign(Object.assign({}, (isUrl ? { url: input } : { file: input })), { preset: flags.preset, mode: flags.mode, outputs: enableOutputs.length ? Object.fromEntries(enableOutputs.map((o) => [o, outputDirs[o] ? { dir: outputDirs[o] } : {}])) : undefined }));
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
const project = yield (0, context_1.loadProject)(log, flags.config);
|
|
137
|
+
if (!project && presetHasSources) {
|
|
138
|
+
user = { version: 4, preset: flags.preset, mode: flags.mode };
|
|
139
|
+
log.info(`⚙️ preset ${flags.preset} (no config file)`);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
if (!project) {
|
|
143
|
+
(0, context_1.fail)('No config file found (sdk-gen.config.json / .sdk-gen.json …). Run `sdk-gen init`, or pass --input <spec>.');
|
|
144
|
+
}
|
|
145
|
+
if (flags.preset) {
|
|
146
|
+
(0, context_1.fail)(presetHasSources
|
|
147
|
+
? `--preset with a config file: set "preset": "${flags.preset}" in ${path.basename((_f = project.file) !== null && _f !== void 0 ? _f : 'the config file')} instead.`
|
|
148
|
+
: '--preset only applies to --input runs; set "preset" in the config file instead.');
|
|
149
|
+
}
|
|
150
|
+
user = flags.mode ? Object.assign(Object.assign({}, project.config), { mode: flags.mode }) : project.config;
|
|
151
|
+
baseDir = project.baseDir;
|
|
152
|
+
if (project.file) {
|
|
153
|
+
const shown = [project.file, project.localFile].filter((f) => Boolean(f)).map((f) => path.relative(process.cwd(), f) || f);
|
|
154
|
+
log.info(`⚙️ ${shown.join(' + ')}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const resolved = yield (0, resolve_1.resolveConfigAsync)(user, Object.assign({ cwd: baseDir, workspace: flags.workspace, enableOutputs,
|
|
159
|
+
outputDirs, prune: flags.prune, format: flags.format, templates: (_c = flags.templates) === null || _c === void 0 ? void 0 : _c.map((t) => path.resolve(t)), only: flags.source }, (0, context_1.terminalPrompt)()));
|
|
160
|
+
const results = yield (0, pipeline_1.runPipeline)(resolved, {
|
|
161
|
+
log,
|
|
162
|
+
dryRun: flags.dryRun,
|
|
163
|
+
only: flags.source,
|
|
164
|
+
outputs: flags.outputs,
|
|
165
|
+
pruneUnmanaged: flags.pruneUnmanaged,
|
|
166
|
+
});
|
|
167
|
+
if (flags.dryRun)
|
|
168
|
+
printPlan(results, baseDir, log);
|
|
169
|
+
return results;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
function printPlan(results, baseDir, log) {
|
|
173
|
+
var _a;
|
|
174
|
+
for (const result of results) {
|
|
175
|
+
for (const run of (_a = result.planned) !== null && _a !== void 0 ? _a : []) {
|
|
176
|
+
let created = 0;
|
|
177
|
+
let changed = 0;
|
|
178
|
+
let same = 0;
|
|
179
|
+
for (const file of run.files) {
|
|
180
|
+
const abs = path.join(run.dir, ...file.path.split('/'));
|
|
181
|
+
const existing = fs.existsSync(abs) ? fs.readFileSync(abs, 'utf8') : undefined;
|
|
182
|
+
if (existing === undefined)
|
|
183
|
+
created++;
|
|
184
|
+
else if (existing === file.content)
|
|
185
|
+
same++;
|
|
186
|
+
else
|
|
187
|
+
changed++;
|
|
188
|
+
}
|
|
189
|
+
log.info(` [dry-run] ${result.source}/${run.output} → ${path.relative(baseDir, run.dir) || '.'}: ` +
|
|
190
|
+
`${run.files.length} files (${created} new, ${changed} changed, ${same} unchanged)`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function kebab(s) {
|
|
195
|
+
return s.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
196
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
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.registerInit = registerInit;
|
|
46
|
+
const commander_1 = require("commander");
|
|
47
|
+
const fs = __importStar(require("fs"));
|
|
48
|
+
const path = __importStar(require("path"));
|
|
49
|
+
const load_1 = require("../../config/load");
|
|
50
|
+
const presets_1 = require("../../config/presets");
|
|
51
|
+
const modes_1 = require("../../config/modes");
|
|
52
|
+
const scaffold_1 = require("../../targets/declarative/scaffold");
|
|
53
|
+
const context_1 = require("../context");
|
|
54
|
+
function registerInit(program) {
|
|
55
|
+
const command = program
|
|
56
|
+
.command('init')
|
|
57
|
+
.description('Write a starter sdk-gen.config.json')
|
|
58
|
+
.addOption(new commander_1.Option('--mode <mode>', 'TypeScript SDK mode').choices(modes_1.GENERATION_MODES))
|
|
59
|
+
.option('-i, --input <url|file>', 'OpenAPI spec URL or file')
|
|
60
|
+
.option('--force', 'overwrite an existing config')
|
|
61
|
+
.option('--local', 'write your gitignored <config>.local.json (workspace, secrets, local spec) next to the project config')
|
|
62
|
+
.option('-w, --workspace <name>', 'with --local: your workspace')
|
|
63
|
+
.option('--target <name>', 'scaffold a new config target folder (target.json + templates) instead of a config')
|
|
64
|
+
.option('--dir <dir>', 'with --target: where to scaffold (default ./sdk-targets/<name>)');
|
|
65
|
+
const presets = (0, presets_1.listPresets)();
|
|
66
|
+
if (presets.length)
|
|
67
|
+
command.option('-p, --preset <name>', `preset (${presets.map((p) => p.name).join(', ')})`);
|
|
68
|
+
command.action((flags) => __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
var _a, _b, _c;
|
|
70
|
+
var _d;
|
|
71
|
+
if (flags.local)
|
|
72
|
+
return initLocal(flags);
|
|
73
|
+
if (flags.target)
|
|
74
|
+
return initTarget(flags.target, flags);
|
|
75
|
+
const file = path.resolve('sdk-gen.config.json');
|
|
76
|
+
if (fs.existsSync(file) && !flags.force)
|
|
77
|
+
(0, context_1.fail)(`${path.basename(file)} already exists (use --force).`);
|
|
78
|
+
if (flags.preset && !(0, presets_1.getPreset)(flags.preset))
|
|
79
|
+
(0, context_1.fail)(`Unknown preset "${flags.preset}".`);
|
|
80
|
+
const input = (_d = flags.input) !== null && _d !== void 0 ? _d : './openapi.json';
|
|
81
|
+
const presetSources = flags.preset !== undefined && Boolean((_c = (_b = (_a = (0, presets_1.getPreset)(flags.preset)) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.sources) === null || _c === void 0 ? void 0 : _c.length) && !flags.input;
|
|
82
|
+
const sources = [
|
|
83
|
+
Object.assign(Object.assign({ name: 'api' }, (/^https?:\/\//i.test(input) ? { url: input } : { file: input })), (flags.preset && !flags.mode && {
|
|
84
|
+
outputs: { models: { dir: './src/api/models' }, stores: { dir: './src/api/stores' } },
|
|
85
|
+
})),
|
|
86
|
+
];
|
|
87
|
+
const config = Object.assign(Object.assign(Object.assign({ $schema: './node_modules/swagger-sdk-generator/schema/sdk-gen.schema.json', version: 4 }, (flags.preset ? { preset: flags.preset } : {})), (flags.mode ? { mode: flags.mode } : {})), (presetSources ? {} : { sources }));
|
|
88
|
+
fs.writeFileSync(file, JSON.stringify(config, null, 2) + '\n');
|
|
89
|
+
console.log(`Wrote ${path.basename(file)}. Next: sdk-gen generate`);
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
function initTarget(name, flags) {
|
|
93
|
+
var _a;
|
|
94
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(name))
|
|
95
|
+
(0, context_1.fail)(`Invalid target name "${name}" (letters, digits, . _ -).`);
|
|
96
|
+
const dir = path.resolve((_a = flags.dir) !== null && _a !== void 0 ? _a : path.join('sdk-targets', name));
|
|
97
|
+
try {
|
|
98
|
+
const written = (0, scaffold_1.scaffoldConfigTarget)(dir, name, { force: flags.force });
|
|
99
|
+
for (const file of written)
|
|
100
|
+
console.log(`wrote ${path.relative(process.cwd(), file)}`);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
(0, context_1.fail)(error.message);
|
|
104
|
+
}
|
|
105
|
+
const rel = `./${path.relative(process.cwd(), dir).split(path.sep).join('/')}`;
|
|
106
|
+
console.log(`\nConfig target "${name}" ready. Use it: "target": "${rel}" in sdk-gen.config.json, then sdk-gen generate.`);
|
|
107
|
+
}
|
|
108
|
+
function initLocal(flags) {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
const projectFile = yield (0, load_1.findConfigFile)(process.cwd());
|
|
111
|
+
if (!projectFile)
|
|
112
|
+
(0, context_1.fail)('No project config found here; run `sdk-gen init` first (the local file sits next to it).');
|
|
113
|
+
const file = (0, load_1.localConfigPath)(projectFile);
|
|
114
|
+
if (fs.existsSync(file) && !flags.force)
|
|
115
|
+
(0, context_1.fail)(`${path.basename(file)} already exists (use --force).`);
|
|
116
|
+
const local = Object.assign(Object.assign({ $schema: './node_modules/swagger-sdk-generator/schema/sdk-gen.local.schema.json' }, (flags.workspace ? { workspace: flags.workspace } : { workspace: '' })), { env: {} });
|
|
117
|
+
fs.writeFileSync(file, JSON.stringify(local, null, 2) + '\n');
|
|
118
|
+
console.log(`Wrote ${path.relative(process.cwd(), file) || file} — personal settings, never commit it.`);
|
|
119
|
+
ensureGitignored(path.dirname(projectFile), path.basename(file));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function ensureGitignored(dir, name) {
|
|
123
|
+
const gitignore = path.join(dir, '.gitignore');
|
|
124
|
+
const content = fs.existsSync(gitignore) ? fs.readFileSync(gitignore, 'utf8') : '';
|
|
125
|
+
const lines = content.split(/\r?\n/).map((l) => l.trim());
|
|
126
|
+
if (lines.includes(name) || lines.includes(`/${name}`) || lines.includes('*.local.json'))
|
|
127
|
+
return;
|
|
128
|
+
fs.writeFileSync(gitignore, `${content}${content && !content.endsWith('\n') ? '\n' : ''}/${name}\n`);
|
|
129
|
+
console.log(`Added /${name} to ${path.relative(process.cwd(), gitignore) || '.gitignore'}.`);
|
|
130
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
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.registerInspect = registerInspect;
|
|
46
|
+
const path = __importStar(require("path"));
|
|
47
|
+
const resolve_1 = require("../../config/resolve");
|
|
48
|
+
const log_1 = require("../../core/log");
|
|
49
|
+
const pipeline_1 = require("../../core/pipeline");
|
|
50
|
+
const targets_1 = require("../../core/targets");
|
|
51
|
+
const load_1 = require("../../spec/load");
|
|
52
|
+
const context_1 = require("../context");
|
|
53
|
+
function registerInspect(program) {
|
|
54
|
+
program
|
|
55
|
+
.command('inspect')
|
|
56
|
+
.description('Show the IR a source normalizes to (what outputs and templates receive)')
|
|
57
|
+
.option('-c, --config <file>', 'config file')
|
|
58
|
+
.option('-s, --source <name>', 'source to inspect (default: first)')
|
|
59
|
+
.option('-w, --workspace <name>', 'value for {workspace} placeholders')
|
|
60
|
+
.option('--model <id>', 'print one model as JSON')
|
|
61
|
+
.option('--operation <id>', 'print one operation as JSON ("get /path")')
|
|
62
|
+
.option('--json', 'print the whole IR as JSON (raw spec objects omitted)')
|
|
63
|
+
.option('--diagnostics', 'list every spec problem found while normalizing (warnings + infos)')
|
|
64
|
+
.option('--template <name>', 'print the view-model(s) a template receives (e.g. models/model)')
|
|
65
|
+
.option('--grep <text>', 'with --template: only renders whose data JSON contains <text>')
|
|
66
|
+
.option('--limit <n>', 'with --template: max renders to print', '1')
|
|
67
|
+
.action((flags) => __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
var _a, _b, _c;
|
|
69
|
+
var _d, _e, _f;
|
|
70
|
+
const project = yield (0, context_1.loadProject)((0, log_1.createLogger)('error'), flags.config);
|
|
71
|
+
if (!project)
|
|
72
|
+
(0, context_1.fail)('No config file found.');
|
|
73
|
+
const resolved = yield (0, resolve_1.resolveConfigAsync)(project.config, Object.assign({ cwd: project.baseDir, workspace: flags.workspace, only: [(_e = (_d = flags.source) !== null && _d !== void 0 ? _d : (_b = (_a = project.config.sources) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.name) !== null && _e !== void 0 ? _e : ''] }, (0, context_1.terminalPrompt)()));
|
|
74
|
+
const source = resolved.sources[0];
|
|
75
|
+
if (!source) {
|
|
76
|
+
const skipped = (_c = resolved.skipped) === null || _c === void 0 ? void 0 : _c[0];
|
|
77
|
+
(0, context_1.fail)(skipped ? `Source "${skipped.name}" is skipped this run (skip: ${skipped.reason}).` : `Unknown source "${flags.source}".`);
|
|
78
|
+
}
|
|
79
|
+
const api = (0, pipeline_1.buildApi)(yield (0, load_1.loadSpec)(source.input), source);
|
|
80
|
+
if (flags.model) {
|
|
81
|
+
const model = api.models.find((m) => m.id === flags.model || m.name === flags.model);
|
|
82
|
+
if (!model)
|
|
83
|
+
(0, context_1.fail)(`No model "${flags.model}" in ${source.name}.`);
|
|
84
|
+
return print(model);
|
|
85
|
+
}
|
|
86
|
+
if (flags.operation) {
|
|
87
|
+
const op = api.operations.find((o) => o.id === flags.operation || o.operationId === flags.operation);
|
|
88
|
+
if (!op)
|
|
89
|
+
(0, context_1.fail)(`No operation "${flags.operation}" in ${source.name}.`);
|
|
90
|
+
return print(op);
|
|
91
|
+
}
|
|
92
|
+
if (flags.json)
|
|
93
|
+
return print(api);
|
|
94
|
+
if (flags.diagnostics)
|
|
95
|
+
return printDiagnostics(api);
|
|
96
|
+
if (flags.template)
|
|
97
|
+
return printTemplateData(resolved, source, api, flags);
|
|
98
|
+
summarize(api, source.name, path.relative(process.cwd(), (_f = project.file) !== null && _f !== void 0 ? _f : '') || undefined);
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
function printTemplateData(resolved, source, api, flags) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const target = yield (0, targets_1.loadTarget)(resolved);
|
|
104
|
+
const limit = Number(flags.limit) || 1;
|
|
105
|
+
const hits = [];
|
|
106
|
+
const location = source.input.kind === 'url' ? source.input.url : source.input.path;
|
|
107
|
+
yield (0, pipeline_1.generateOutputs)(api, source, { name: source.name, location }, target, resolved, (0, log_1.createLogger)('error'), {
|
|
108
|
+
onRender: (output, name, data) => {
|
|
109
|
+
if (name !== flags.template || hits.length >= limit)
|
|
110
|
+
return;
|
|
111
|
+
if (flags.grep && !JSON.stringify(data, null, 2).includes(flags.grep))
|
|
112
|
+
return;
|
|
113
|
+
hits.push({ output, data });
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
if (!hits.length)
|
|
117
|
+
(0, context_1.fail)(`Template "${flags.template}" was not rendered${flags.grep ? ` with data matching "${flags.grep}"` : ''} by any enabled output.`);
|
|
118
|
+
for (const hit of hits) {
|
|
119
|
+
console.log(`── ${hit.output} → ${flags.template}`);
|
|
120
|
+
print(hit.data);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function printDiagnostics(api) {
|
|
125
|
+
var _a;
|
|
126
|
+
const items = (_a = api.diagnostics) !== null && _a !== void 0 ? _a : [];
|
|
127
|
+
if (!items.length)
|
|
128
|
+
return console.log('No spec diagnostics.');
|
|
129
|
+
for (const d of items)
|
|
130
|
+
console.log(`${d.level.padEnd(4)} ${d.code.padEnd(20)} ${d.at}\n ${d.message}`);
|
|
131
|
+
console.log(`\n${items.filter((d) => d.level === 'warn').length} warnings, ${items.filter((d) => d.level === 'info').length} infos`);
|
|
132
|
+
}
|
|
133
|
+
function print(value) {
|
|
134
|
+
console.log(JSON.stringify(value, (key, v) => (key === 'raw' ? undefined : v), 2));
|
|
135
|
+
}
|
|
136
|
+
function summarize(api, source, file) {
|
|
137
|
+
var _a;
|
|
138
|
+
console.log(`${source}${file ? ` (${file})` : ''}: ${api.info.title} ${api.info.version} — OpenAPI ${api.info.openapi}`);
|
|
139
|
+
console.log(`\n${api.models.length} models`);
|
|
140
|
+
for (const m of api.models)
|
|
141
|
+
console.log(` ${m.id.padEnd(60)} ${describe(m.type)}`);
|
|
142
|
+
console.log(`\n${api.operations.length} operations`);
|
|
143
|
+
const warnings = ((_a = api.diagnostics) !== null && _a !== void 0 ? _a : []).filter((d) => d.level === 'warn').length;
|
|
144
|
+
if (warnings)
|
|
145
|
+
console.log(`\n${warnings} spec warnings — see --diagnostics`);
|
|
146
|
+
for (const o of api.operations)
|
|
147
|
+
console.log(` ${o.method.toUpperCase().padEnd(7)} ${o.path}`);
|
|
148
|
+
}
|
|
149
|
+
function describe(node) {
|
|
150
|
+
switch (node.kind) {
|
|
151
|
+
case 'object':
|
|
152
|
+
return `object(${node.properties.length})`;
|
|
153
|
+
case 'enum':
|
|
154
|
+
return `enum(${node.values.length})`;
|
|
155
|
+
case 'ref':
|
|
156
|
+
return `→ ${node.id}`;
|
|
157
|
+
case 'array':
|
|
158
|
+
return `${describe(node.items)}[]`;
|
|
159
|
+
case 'primitive':
|
|
160
|
+
return node.type;
|
|
161
|
+
default:
|
|
162
|
+
return node.kind;
|
|
163
|
+
}
|
|
164
|
+
}
|