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,104 @@
|
|
|
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.createCommandFormatter = createCommandFormatter;
|
|
46
|
+
exports.splitCommand = splitCommand;
|
|
47
|
+
const child_process_1 = require("child_process");
|
|
48
|
+
const fs = __importStar(require("fs"));
|
|
49
|
+
const os = __importStar(require("os"));
|
|
50
|
+
const path = __importStar(require("path"));
|
|
51
|
+
function createCommandFormatter(format, targetOptions, targetName) {
|
|
52
|
+
if (!(format === null || format === void 0 ? void 0 : format.command))
|
|
53
|
+
return undefined;
|
|
54
|
+
const override = targetOptions.formatter;
|
|
55
|
+
const enabled = override === undefined ? format.enabled !== false : Boolean(override);
|
|
56
|
+
if (!enabled)
|
|
57
|
+
return undefined;
|
|
58
|
+
const argv = splitCommand(format.command);
|
|
59
|
+
if (!argv.length)
|
|
60
|
+
throw new Error(`${targetName}: format.command is empty`);
|
|
61
|
+
const program = argv[0];
|
|
62
|
+
return {
|
|
63
|
+
name: path.basename(program),
|
|
64
|
+
format(filePath, content) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
if (format.stdin !== false)
|
|
67
|
+
return run(program, argv.slice(1).map((a) => a.split('{file}').join(filePath)), content);
|
|
68
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sdkgen-format-'));
|
|
69
|
+
const tmp = path.join(dir, path.basename(filePath));
|
|
70
|
+
try {
|
|
71
|
+
fs.writeFileSync(tmp, content, 'utf8');
|
|
72
|
+
run(program, argv.slice(1).map((a) => a.split('{file}').join(tmp)));
|
|
73
|
+
return fs.readFileSync(tmp, 'utf8');
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function run(program, args, input) {
|
|
83
|
+
var _a;
|
|
84
|
+
const result = (0, child_process_1.spawnSync)(program, args, { input, encoding: 'utf8' });
|
|
85
|
+
if (result.error) {
|
|
86
|
+
if (result.error.code === 'ENOENT') {
|
|
87
|
+
throw new Error(`formatter "${program}" was not found on PATH`);
|
|
88
|
+
}
|
|
89
|
+
throw result.error;
|
|
90
|
+
}
|
|
91
|
+
if (result.status !== 0) {
|
|
92
|
+
const stderr = ((_a = result.stderr) !== null && _a !== void 0 ? _a : '').trim();
|
|
93
|
+
throw new Error(stderr || `${program} exited with status ${result.status}`);
|
|
94
|
+
}
|
|
95
|
+
return result.stdout;
|
|
96
|
+
}
|
|
97
|
+
function splitCommand(command) {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
const out = [];
|
|
100
|
+
const re = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
101
|
+
for (const m of command.matchAll(re))
|
|
102
|
+
out.push((_b = (_a = m[1]) !== null && _a !== void 0 ? _a : m[2]) !== null && _b !== void 0 ? _b : m[3]);
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Target } from '../../core/plugin';
|
|
2
|
+
import { type LoadedTargetDefinition } from './config';
|
|
3
|
+
export { isDeclarativeTargetDir, loadTargetDefinition, targetConfigJsonSchema } from './config';
|
|
4
|
+
export type { TargetConfig } from './config';
|
|
5
|
+
export type { TargetExtensions, ExtensionContext } from './extensions';
|
|
6
|
+
export type { FileView, ModelView, PropertyView, MemberView, OperationView, ResourceView, ParamView } from './views';
|
|
7
|
+
export declare function createDeclarativeTarget(dir: string, targetOptions?: Record<string, unknown>): Promise<Target>;
|
|
8
|
+
export declare function buildTarget(def: LoadedTargetDefinition, targetOptions?: Record<string, unknown>): Target;
|
|
9
|
+
export declare function configTargetDir(target: string): string | undefined;
|
|
@@ -0,0 +1,170 @@
|
|
|
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.targetConfigJsonSchema = exports.loadTargetDefinition = exports.isDeclarativeTargetDir = void 0;
|
|
46
|
+
exports.createDeclarativeTarget = createDeclarativeTarget;
|
|
47
|
+
exports.buildTarget = buildTarget;
|
|
48
|
+
exports.configTargetDir = configTargetDir;
|
|
49
|
+
const path = __importStar(require("path"));
|
|
50
|
+
const zod_1 = require("zod");
|
|
51
|
+
const config_1 = require("./config");
|
|
52
|
+
const files_1 = require("./files");
|
|
53
|
+
const format_1 = require("./format");
|
|
54
|
+
const liquid_1 = require("./liquid");
|
|
55
|
+
const views_1 = require("./views");
|
|
56
|
+
var config_2 = require("./config");
|
|
57
|
+
Object.defineProperty(exports, "isDeclarativeTargetDir", { enumerable: true, get: function () { return config_2.isDeclarativeTargetDir; } });
|
|
58
|
+
Object.defineProperty(exports, "loadTargetDefinition", { enumerable: true, get: function () { return config_2.loadTargetDefinition; } });
|
|
59
|
+
Object.defineProperty(exports, "targetConfigJsonSchema", { enumerable: true, get: function () { return config_2.targetConfigJsonSchema; } });
|
|
60
|
+
function createDeclarativeTarget(dir_1) {
|
|
61
|
+
return __awaiter(this, arguments, void 0, function* (dir, targetOptions = {}) {
|
|
62
|
+
const def = yield (0, config_1.loadTargetDefinition)(dir);
|
|
63
|
+
return buildTarget(def, targetOptions);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function buildTarget(def, targetOptions = {}) {
|
|
67
|
+
var _a;
|
|
68
|
+
const { config, extensions } = def;
|
|
69
|
+
const templates = (0, liquid_1.createStringTemplates)();
|
|
70
|
+
const state = {};
|
|
71
|
+
const currentEnv = (filter) => {
|
|
72
|
+
if (!state.env)
|
|
73
|
+
throw new Error(`filter "${filter}" used outside a ${config.name} output`);
|
|
74
|
+
return state.env;
|
|
75
|
+
};
|
|
76
|
+
const filters = Object.assign(Object.assign(Object.assign({}, liquid_1.caseFilters), { type: (node, position = 'property') => {
|
|
77
|
+
const env = currentEnv('type');
|
|
78
|
+
return env.speller.spell(node, position, (0, views_1.createScope)(env).imports).text;
|
|
79
|
+
}, name: (raw, kind = 'type') => currentEnv('name').namer.name(kind, String(raw !== null && raw !== void 0 ? raw : '')), escape: (name, kind = 'property') => currentEnv('escape').namer.escape(kind, String(name !== null && name !== void 0 ? name : '')), namespace: (segments) => currentEnv('namespace').namespace(Array.isArray(segments) ? segments.map(String) : String(segments !== null && segments !== void 0 ? segments : '').split('.').filter(Boolean)), one_line: (text) => String(text !== null && text !== void 0 ? text : '').replace(/\s*\n\s*/g, ' ').trim(), literal: (value) => currentEnv('literal').speller.literal(value), indent: (text, n = 2) => {
|
|
80
|
+
const pad = ' '.repeat(Number(n));
|
|
81
|
+
return String(text !== null && text !== void 0 ? text : '')
|
|
82
|
+
.split('\n')
|
|
83
|
+
.map((line) => (line ? pad + line : line))
|
|
84
|
+
.join('\n');
|
|
85
|
+
}, doc_lines: (text) => (text === null || text === undefined || text === '' ? [] : String(text).split(/\r?\n/)) }), extensions.filters);
|
|
86
|
+
for (const [name, impl] of Object.entries(filters))
|
|
87
|
+
templates.registerFilter(name, impl);
|
|
88
|
+
const hooks = [];
|
|
89
|
+
if (extensions.hooks)
|
|
90
|
+
hooks.push(extensions.hooks);
|
|
91
|
+
const maxBlank = (_a = config.format) === null || _a === void 0 ? void 0 : _a.maxBlankLines;
|
|
92
|
+
const collapse = maxBlank === undefined ? (s) => s : (s) => s.replace(new RegExp(`\\n{${maxBlank + 2},}`, 'g'), '\n'.repeat(maxBlank + 1));
|
|
93
|
+
const outputs = Object.entries(config.outputs).map(([name, output]) => {
|
|
94
|
+
var _a;
|
|
95
|
+
const rules = (0, files_1.outputRules)(config, name);
|
|
96
|
+
const { defaults, schema } = optionsFor(name, Object.assign(Object.assign({}, (usesResources(rules) ? resourceOptions(config) : {})), output.options));
|
|
97
|
+
const plugin = {
|
|
98
|
+
name,
|
|
99
|
+
description: (_a = output.description) !== null && _a !== void 0 ? _a : `${config.name} ${name}`,
|
|
100
|
+
defaults,
|
|
101
|
+
optionsSchema: schema,
|
|
102
|
+
generate(ctx) {
|
|
103
|
+
const env = (0, views_1.createEnv)(ctx.api, ctx.options, config, extensions, templates, ctx.log);
|
|
104
|
+
state.env = env;
|
|
105
|
+
try {
|
|
106
|
+
for (const entry of (0, files_1.planFiles)(env, rules)) {
|
|
107
|
+
const view = (0, files_1.buildFileView)(env, entry);
|
|
108
|
+
const content = collapse(ctx.templates.render(entry.rule.template, view));
|
|
109
|
+
ctx.emit(Object.assign({ path: entry.path, content }, (entry.rule.format !== undefined ? { format: entry.rule.format } : {})));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
state.env = undefined;
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
return plugin;
|
|
118
|
+
});
|
|
119
|
+
const [templatesDir, ...templateFallbackDirs] = def.templateDirs;
|
|
120
|
+
return Object.assign(Object.assign(Object.assign(Object.assign({ name: config.name, templatesDir }, (templateFallbackDirs.length ? { templateFallbackDirs } : {})), { outputs, formatter: (0, format_1.createCommandFormatter)(config.format, targetOptions, config.name), filters }), (hooks.length ? { hooks } : {})), (config.transforms ? { transforms: config.transforms } : {}));
|
|
121
|
+
}
|
|
122
|
+
const usesResources = (rules) => rules.some((r) => { var _a; return r.each === 'resource' || ((_a = r.contents) === null || _a === void 0 ? void 0 : _a.includes('resources')); });
|
|
123
|
+
function resourceOptions(config) {
|
|
124
|
+
var _a, _b;
|
|
125
|
+
var _c, _d;
|
|
126
|
+
return {
|
|
127
|
+
groupBy: {
|
|
128
|
+
type: 'string',
|
|
129
|
+
enum: [...config_1.RESOURCE_GROUPINGS],
|
|
130
|
+
default: (_c = (_a = config.resources) === null || _a === void 0 ? void 0 : _a.by) !== null && _c !== void 0 ? _c : 'path',
|
|
131
|
+
description: 'How operations are grouped into resources: path segment, first tag, or operationId prefix.',
|
|
132
|
+
},
|
|
133
|
+
stripPrefixes: {
|
|
134
|
+
type: 'array',
|
|
135
|
+
default: (_d = (_b = config.resources) === null || _b === void 0 ? void 0 : _b.stripPrefixes) !== null && _d !== void 0 ? _d : [],
|
|
136
|
+
description: 'Leading path prefixes ignored when grouping by path.',
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function optionsFor(output, specs) {
|
|
141
|
+
var _a;
|
|
142
|
+
const defaults = { dir: `./${output}` };
|
|
143
|
+
const shape = {
|
|
144
|
+
dir: zod_1.z.string().describe('Output directory (resolved against cwd by the core).'),
|
|
145
|
+
format: zod_1.z.boolean().optional().describe("Run the target formatter over this output's files (default true)."),
|
|
146
|
+
};
|
|
147
|
+
for (const [key, spec] of Object.entries(specs)) {
|
|
148
|
+
let field;
|
|
149
|
+
if ((_a = spec.enum) === null || _a === void 0 ? void 0 : _a.length) {
|
|
150
|
+
field = spec.enum.every((v) => typeof v === 'string')
|
|
151
|
+
? zod_1.z.enum(spec.enum)
|
|
152
|
+
: zod_1.z.union(spec.enum.map((v) => zod_1.z.literal(v)));
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
field = { string: zod_1.z.string(), number: zod_1.z.number(), boolean: zod_1.z.boolean(), array: zod_1.z.array(zod_1.z.unknown()), object: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()) }[spec.type];
|
|
156
|
+
}
|
|
157
|
+
if (spec.description)
|
|
158
|
+
field = field.describe(spec.description);
|
|
159
|
+
if (spec.default !== undefined)
|
|
160
|
+
defaults[key] = spec.default;
|
|
161
|
+
else if (key !== 'dir')
|
|
162
|
+
field = field.optional();
|
|
163
|
+
shape[key] = field;
|
|
164
|
+
}
|
|
165
|
+
return { defaults, schema: zod_1.z.object(shape).strict() };
|
|
166
|
+
}
|
|
167
|
+
function configTargetDir(target) {
|
|
168
|
+
const dir = /(^|[\\/])(target\.json|target\.config\.js)$/.test(target) ? path.dirname(target) : target;
|
|
169
|
+
return (0, config_1.isDeclarativeTargetDir)(dir) ? dir : undefined;
|
|
170
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type FilterImplOptions } from 'liquidjs';
|
|
2
|
+
export interface StringTemplates {
|
|
3
|
+
render(template: string, data: Record<string, unknown>): string;
|
|
4
|
+
references(template: string, name: string): boolean;
|
|
5
|
+
registerFilter(name: string, impl: FilterImplOptions): void;
|
|
6
|
+
}
|
|
7
|
+
export declare function createStringTemplates(): StringTemplates;
|
|
8
|
+
export declare const caseFilters: Record<string, FilterImplOptions>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.caseFilters = void 0;
|
|
4
|
+
exports.createStringTemplates = createStringTemplates;
|
|
5
|
+
const liquidjs_1 = require("liquidjs");
|
|
6
|
+
const naming_case_1 = require("./naming-case");
|
|
7
|
+
const PLAIN = /^(?:[^{]|\{(?![{%]))*(?:\{\{\s*[A-Za-z_][A-Za-z0-9_]*\s*\}\}(?:[^{]|\{(?![{%]))*)*$/;
|
|
8
|
+
function createStringTemplates() {
|
|
9
|
+
const liquid = new liquidjs_1.Liquid({ strictVariables: true, strictFilters: true, lenientIf: true, greedy: false });
|
|
10
|
+
for (const [name, impl] of Object.entries(exports.caseFilters))
|
|
11
|
+
liquid.registerFilter(name, impl);
|
|
12
|
+
liquid.registerFilter('quote', (v) => JSON.stringify(v));
|
|
13
|
+
const parsed = new Map();
|
|
14
|
+
const refs = new Map();
|
|
15
|
+
return {
|
|
16
|
+
registerFilter: (name, impl) => liquid.registerFilter(name, impl),
|
|
17
|
+
references(template, name) {
|
|
18
|
+
const key = `${name}\u0000${template}`;
|
|
19
|
+
let hit = refs.get(key);
|
|
20
|
+
if (hit === undefined) {
|
|
21
|
+
hit = new RegExp(`(?:\\{\\{|\\{%)[^}]*\\b${name}\\b`).test(template);
|
|
22
|
+
refs.set(key, hit);
|
|
23
|
+
}
|
|
24
|
+
return hit;
|
|
25
|
+
},
|
|
26
|
+
render(template, data) {
|
|
27
|
+
if (!template.includes('{'))
|
|
28
|
+
return template;
|
|
29
|
+
if (PLAIN.test(template)) {
|
|
30
|
+
return template.replace(/\{\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}\}/g, (_, name) => {
|
|
31
|
+
if (!(name in data) || data[name] === undefined)
|
|
32
|
+
throw new Error(`undefined variable: ${name} (in "${template}")`);
|
|
33
|
+
const value = data[name];
|
|
34
|
+
return value === null ? '' : Array.isArray(value) ? value.join('') : String(value);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
let tpl = parsed.get(template);
|
|
38
|
+
if (!tpl) {
|
|
39
|
+
tpl = liquid.parse(template);
|
|
40
|
+
parsed.set(template, tpl);
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
return liquid.renderSync(tpl, data);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new Error(`${error.message} (in "${template}")`);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
exports.caseFilters = {
|
|
52
|
+
pascal: (v) => (0, naming_case_1.applyCase)('pascal', String(v !== null && v !== void 0 ? v : '')),
|
|
53
|
+
camel: (v) => (0, naming_case_1.applyCase)('camel', String(v !== null && v !== void 0 ? v : '')),
|
|
54
|
+
snake: (v) => (0, naming_case_1.applyCase)('snake', String(v !== null && v !== void 0 ? v : '')),
|
|
55
|
+
upper_snake: (v) => (0, naming_case_1.applyCase)('upper_snake', String(v !== null && v !== void 0 ? v : '')),
|
|
56
|
+
kebab: (v) => (0, naming_case_1.applyCase)('kebab', String(v !== null && v !== void 0 ? v : '')),
|
|
57
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.words = words;
|
|
4
|
+
exports.applyCase = applyCase;
|
|
5
|
+
function words(raw) {
|
|
6
|
+
return String(raw)
|
|
7
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
8
|
+
.split(/[^A-Za-z0-9]+/)
|
|
9
|
+
.filter(Boolean)
|
|
10
|
+
.map((w) => w.toLowerCase());
|
|
11
|
+
}
|
|
12
|
+
function applyCase(style, raw) {
|
|
13
|
+
if (style === 'preserve')
|
|
14
|
+
return raw;
|
|
15
|
+
const ws = words(raw);
|
|
16
|
+
switch (style) {
|
|
17
|
+
case 'pascal':
|
|
18
|
+
return ws.map(upperFirst).join('');
|
|
19
|
+
case 'camel':
|
|
20
|
+
return ws.map((w, i) => (i === 0 ? w : upperFirst(w))).join('');
|
|
21
|
+
case 'snake':
|
|
22
|
+
return ws.join('_');
|
|
23
|
+
case 'upper_snake':
|
|
24
|
+
return ws.join('_').toUpperCase();
|
|
25
|
+
case 'kebab':
|
|
26
|
+
return ws.join('-');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function upperFirst(w) {
|
|
30
|
+
return w.charAt(0).toUpperCase() + w.slice(1);
|
|
31
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { EnumValue, Model, ModelId } from '../../ir/types';
|
|
2
|
+
import { type CaseName, type NamingKind, type NamingRuleInput, type TargetConfig } from './config';
|
|
3
|
+
import type { ExtensionContext, TargetExtensions } from './extensions';
|
|
4
|
+
import type { StringTemplates } from './liquid';
|
|
5
|
+
type RuleObject = Exclude<NamingRuleInput, string>;
|
|
6
|
+
export interface ResolvedRule {
|
|
7
|
+
case: RuleObject['case'];
|
|
8
|
+
fallbackCase?: CaseName;
|
|
9
|
+
empty?: string;
|
|
10
|
+
identifier: RegExp | null;
|
|
11
|
+
invalid: string;
|
|
12
|
+
reserved: Set<string>;
|
|
13
|
+
escape: string;
|
|
14
|
+
after?: string;
|
|
15
|
+
avoid: string[];
|
|
16
|
+
unique: boolean;
|
|
17
|
+
qualify?: 'namespace';
|
|
18
|
+
numeric?: string;
|
|
19
|
+
null?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface NamingEngine {
|
|
22
|
+
rule(kind: NamingKind): ResolvedRule;
|
|
23
|
+
base(kind: NamingKind, raw: string, override?: RuleObject): string;
|
|
24
|
+
name(kind: NamingKind, raw: string, extra?: {
|
|
25
|
+
value?: EnumValue;
|
|
26
|
+
}): string;
|
|
27
|
+
unique(kind: NamingKind, name: string, used: Set<string>): string;
|
|
28
|
+
typeNames(models: Model[]): Map<ModelId, string>;
|
|
29
|
+
escape(kind: NamingKind, name: string): string;
|
|
30
|
+
enumKey(value: EnumValue): string;
|
|
31
|
+
namespace(segments: string[]): string[];
|
|
32
|
+
}
|
|
33
|
+
export interface NamingDeps {
|
|
34
|
+
naming: TargetConfig['naming'];
|
|
35
|
+
templates: StringTemplates;
|
|
36
|
+
options: Record<string, unknown>;
|
|
37
|
+
extensions?: TargetExtensions['naming'];
|
|
38
|
+
context: () => ExtensionContext;
|
|
39
|
+
avoidSets: Record<string, () => Set<string>>;
|
|
40
|
+
}
|
|
41
|
+
export declare function createNamingEngine(deps: NamingDeps): NamingEngine;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createNamingEngine = createNamingEngine;
|
|
4
|
+
const config_1 = require("./config");
|
|
5
|
+
const naming_case_1 = require("./naming-case");
|
|
6
|
+
const DEFAULT_RULES = {
|
|
7
|
+
type: { case: 'pascal' },
|
|
8
|
+
property: { case: 'preserve' },
|
|
9
|
+
enumKey: { case: 'upper_snake' },
|
|
10
|
+
file: { case: 'preserve', identifier: false },
|
|
11
|
+
operation: { case: 'camel' },
|
|
12
|
+
param: { case: 'preserve' },
|
|
13
|
+
resource: { case: 'pascal' },
|
|
14
|
+
namespace: { case: 'preserve', identifier: false },
|
|
15
|
+
};
|
|
16
|
+
const INHERITS = { param: 'property', resource: 'type' };
|
|
17
|
+
const DEFAULT_IDENTIFIER = '^[_A-Za-z][_A-Za-z0-9]*$';
|
|
18
|
+
const MAX_ESCAPES = 100;
|
|
19
|
+
function createNamingEngine(deps) {
|
|
20
|
+
var _a;
|
|
21
|
+
const config = (_a = deps.naming) !== null && _a !== void 0 ? _a : {};
|
|
22
|
+
const cache = new Map();
|
|
23
|
+
const toObject = (input) => input === undefined ? undefined : typeof input === 'string' ? { case: input } : input;
|
|
24
|
+
const resolve = (kind, override) => {
|
|
25
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
26
|
+
const configured = (_a = toObject(config[kind])) !== null && _a !== void 0 ? _a : (INHERITS[kind] ? toObject(config[INHERITS[kind]]) : undefined);
|
|
27
|
+
const merged = Object.assign(Object.assign(Object.assign({}, DEFAULT_RULES[kind]), configured), override);
|
|
28
|
+
const identifier = merged.identifier === false ? null : new RegExp(typeof merged.identifier === 'string' ? merged.identifier : (_b = config.identifier) !== null && _b !== void 0 ? _b : DEFAULT_IDENTIFIER);
|
|
29
|
+
return {
|
|
30
|
+
case: merged.case,
|
|
31
|
+
fallbackCase: merged.fallbackCase,
|
|
32
|
+
empty: merged.empty,
|
|
33
|
+
identifier,
|
|
34
|
+
invalid: (_d = (_c = merged.invalid) !== null && _c !== void 0 ? _c : config.invalid) !== null && _d !== void 0 ? _d : '_{{ name }}',
|
|
35
|
+
reserved: new Set([...((_e = config.reserved) !== null && _e !== void 0 ? _e : []), ...((_f = merged.reserved) !== null && _f !== void 0 ? _f : [])]),
|
|
36
|
+
escape: (_h = (_g = merged.escape) !== null && _g !== void 0 ? _g : config.escape) !== null && _h !== void 0 ? _h : '{{ name }}_',
|
|
37
|
+
after: merged.after,
|
|
38
|
+
avoid: (_j = merged.avoid) !== null && _j !== void 0 ? _j : [],
|
|
39
|
+
unique: (_k = merged.unique) !== null && _k !== void 0 ? _k : false,
|
|
40
|
+
qualify: merged.qualify,
|
|
41
|
+
numeric: merged.numeric,
|
|
42
|
+
null: merged.null,
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
const rule = (kind) => {
|
|
46
|
+
let r = cache.get(kind);
|
|
47
|
+
if (!r)
|
|
48
|
+
cache.set(kind, (r = resolve(kind)));
|
|
49
|
+
return r;
|
|
50
|
+
};
|
|
51
|
+
const render = (tpl, name) => deps.templates.render(tpl, { name, options: deps.options });
|
|
52
|
+
const caseOf = (r) => {
|
|
53
|
+
var _a, _b;
|
|
54
|
+
const spec = (_a = r.case) !== null && _a !== void 0 ? _a : 'preserve';
|
|
55
|
+
if (typeof spec === 'object') {
|
|
56
|
+
const value = (_b = deps.options[spec.option]) !== null && _b !== void 0 ? _b : spec.default;
|
|
57
|
+
if (!config_1.CASE_NAMES.includes(value)) {
|
|
58
|
+
throw new Error(`naming: option "${spec.option}" must be one of ${config_1.CASE_NAMES.join(', ')} (got ${JSON.stringify(value)})`);
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
if (config_1.CASE_NAMES.includes(spec))
|
|
63
|
+
return spec;
|
|
64
|
+
if (spec.includes('{{') || spec.includes('{%'))
|
|
65
|
+
return { template: spec };
|
|
66
|
+
throw new Error(`naming: unknown case "${spec}" (use ${config_1.CASE_NAMES.join(' | ')} or a liquid template)`);
|
|
67
|
+
};
|
|
68
|
+
const shape = (r, raw) => {
|
|
69
|
+
const style = caseOf(r);
|
|
70
|
+
let name;
|
|
71
|
+
if (typeof style === 'object')
|
|
72
|
+
name = render(style.template, raw);
|
|
73
|
+
else if (style === 'preserve' && r.fallbackCase && ((r.identifier && !r.identifier.test(raw)) || r.reserved.has(raw))) {
|
|
74
|
+
name = (0, naming_case_1.applyCase)(r.fallbackCase, raw);
|
|
75
|
+
}
|
|
76
|
+
else
|
|
77
|
+
name = (0, naming_case_1.applyCase)(style, raw);
|
|
78
|
+
if (name === '' && r.empty !== undefined)
|
|
79
|
+
name = r.empty;
|
|
80
|
+
if (r.identifier && !r.identifier.test(name))
|
|
81
|
+
name = render(r.invalid, name);
|
|
82
|
+
return name;
|
|
83
|
+
};
|
|
84
|
+
const finish = (r, shaped) => {
|
|
85
|
+
let name = shaped;
|
|
86
|
+
if (r.reserved.has(name))
|
|
87
|
+
name = render(r.escape, name);
|
|
88
|
+
if (r.after)
|
|
89
|
+
name = render(r.after, name);
|
|
90
|
+
if (r.avoid.length) {
|
|
91
|
+
const avoid = new Set();
|
|
92
|
+
for (const entry of r.avoid) {
|
|
93
|
+
const named = deps.avoidSets[entry];
|
|
94
|
+
if (named)
|
|
95
|
+
named().forEach((n) => avoid.add(n));
|
|
96
|
+
else
|
|
97
|
+
avoid.add(entry);
|
|
98
|
+
}
|
|
99
|
+
for (let i = 0; avoid.has(name) && i < MAX_ESCAPES; i++)
|
|
100
|
+
name = render(r.escape, name);
|
|
101
|
+
}
|
|
102
|
+
return name;
|
|
103
|
+
};
|
|
104
|
+
const base = (kind, raw, override) => {
|
|
105
|
+
const r = override ? resolve(kind, override) : rule(kind);
|
|
106
|
+
return finish(r, shape(r, raw));
|
|
107
|
+
};
|
|
108
|
+
const name = (kind, raw, extra = {}) => {
|
|
109
|
+
var _a;
|
|
110
|
+
const fallback = base(kind, raw);
|
|
111
|
+
const fn = (_a = deps.extensions) === null || _a === void 0 ? void 0 : _a[kind];
|
|
112
|
+
if (!fn)
|
|
113
|
+
return fallback;
|
|
114
|
+
const ctx = Object.assign(Object.assign(Object.assign(Object.assign({}, deps.context()), { kind }), ('value' in extra ? { value: extra.value } : {})), { apply: (other, override) => base(kind, other, override) });
|
|
115
|
+
return fn(raw, fallback, ctx);
|
|
116
|
+
};
|
|
117
|
+
const unique = (kind, candidate, used) => {
|
|
118
|
+
if (!rule(kind).unique || !used.has(candidate)) {
|
|
119
|
+
used.add(candidate);
|
|
120
|
+
return candidate;
|
|
121
|
+
}
|
|
122
|
+
for (let i = 2;; i++) {
|
|
123
|
+
const next = `${candidate}${i}`;
|
|
124
|
+
if (!used.has(next)) {
|
|
125
|
+
used.add(next);
|
|
126
|
+
return next;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
return {
|
|
131
|
+
rule,
|
|
132
|
+
base,
|
|
133
|
+
name,
|
|
134
|
+
unique,
|
|
135
|
+
enumKey(value) {
|
|
136
|
+
const r = rule('enumKey');
|
|
137
|
+
let raw = value === null ? 'null' : String(value);
|
|
138
|
+
if (value === null && r.null !== undefined)
|
|
139
|
+
raw = render(r.null, raw);
|
|
140
|
+
else if (typeof value === 'number' && r.numeric !== undefined) {
|
|
141
|
+
raw = deps.templates.render(r.numeric, { value, abs: String(value).replace(/^-/, ''), negative: value < 0, name: raw, options: deps.options });
|
|
142
|
+
}
|
|
143
|
+
return name('enumKey', raw, { value });
|
|
144
|
+
},
|
|
145
|
+
namespace(segments) {
|
|
146
|
+
const r = rule('namespace');
|
|
147
|
+
return segments.filter((seg) => r.empty !== undefined || (typeof r.case === 'string' && r.case === 'preserve' ? seg !== '' : (0, naming_case_1.words)(seg).length > 0)).map((seg) => name('namespace', seg));
|
|
148
|
+
},
|
|
149
|
+
escape: (kind, n) => {
|
|
150
|
+
const r = rule(kind);
|
|
151
|
+
const shaped = r.identifier && !r.identifier.test(n) ? render(r.invalid, n) : n;
|
|
152
|
+
return r.reserved.has(shaped) ? render(r.escape, shaped) : shaped;
|
|
153
|
+
},
|
|
154
|
+
typeNames(models) {
|
|
155
|
+
var _a, _b;
|
|
156
|
+
const r = rule('type');
|
|
157
|
+
const counts = new Map();
|
|
158
|
+
if (r.qualify) {
|
|
159
|
+
for (const m of models) {
|
|
160
|
+
const key = shape(r, m.name);
|
|
161
|
+
counts.set(key, ((_a = counts.get(key)) !== null && _a !== void 0 ? _a : 0) + 1);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const used = new Set();
|
|
165
|
+
const out = new Map();
|
|
166
|
+
for (const m of models) {
|
|
167
|
+
const clash = r.qualify && ((_b = counts.get(shape(r, m.name))) !== null && _b !== void 0 ? _b : 0) > 1;
|
|
168
|
+
const raw = clash ? [...m.namespace, m.name].join(' ') : m.name;
|
|
169
|
+
out.set(m.id, unique('type', name('type', raw), used));
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|