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,51 @@
|
|
|
1
|
+
import type { NormalizeOptions } from '../../config/types';
|
|
2
|
+
import type { Diagnostic, Model, ModelId, TypeNode } from '../../ir/types';
|
|
3
|
+
export type RawRecord = Record<string, unknown>;
|
|
4
|
+
export interface RawSchema extends RawRecord {
|
|
5
|
+
$ref?: unknown;
|
|
6
|
+
type?: unknown;
|
|
7
|
+
format?: unknown;
|
|
8
|
+
enum?: unknown;
|
|
9
|
+
const?: unknown;
|
|
10
|
+
items?: unknown;
|
|
11
|
+
properties?: unknown;
|
|
12
|
+
required?: unknown;
|
|
13
|
+
additionalProperties?: unknown;
|
|
14
|
+
allOf?: unknown;
|
|
15
|
+
oneOf?: unknown;
|
|
16
|
+
anyOf?: unknown;
|
|
17
|
+
discriminator?: unknown;
|
|
18
|
+
nullable?: unknown;
|
|
19
|
+
description?: unknown;
|
|
20
|
+
deprecated?: unknown;
|
|
21
|
+
readOnly?: unknown;
|
|
22
|
+
writeOnly?: unknown;
|
|
23
|
+
default?: unknown;
|
|
24
|
+
example?: unknown;
|
|
25
|
+
}
|
|
26
|
+
export declare function isRecord(value: unknown): value is RawRecord;
|
|
27
|
+
export declare function asRecord(value: unknown): RawRecord | undefined;
|
|
28
|
+
export declare function asArray(value: unknown): unknown[] | undefined;
|
|
29
|
+
export declare function asString(value: unknown): string | undefined;
|
|
30
|
+
export declare function asBoolean(value: unknown): boolean | undefined;
|
|
31
|
+
export declare function stringList(value: unknown): string[];
|
|
32
|
+
export interface SchemaContext {
|
|
33
|
+
schemas: RawRecord;
|
|
34
|
+
allOf: NormalizeOptions['allOf'];
|
|
35
|
+
diagnostics: DiagnosticSink;
|
|
36
|
+
}
|
|
37
|
+
export declare class DiagnosticSink {
|
|
38
|
+
readonly items: Diagnostic[];
|
|
39
|
+
private stack;
|
|
40
|
+
get at(): string;
|
|
41
|
+
within<T>(segment: string, fn: () => T): T;
|
|
42
|
+
atLocation<T>(location: string, fn: () => T): T;
|
|
43
|
+
report(level: Diagnostic['level'], code: string, message: string): void;
|
|
44
|
+
}
|
|
45
|
+
export interface ResolvedRef {
|
|
46
|
+
id: ModelId;
|
|
47
|
+
unresolved?: boolean;
|
|
48
|
+
}
|
|
49
|
+
export declare function resolveSchemaRef(ref: string, schemas: RawRecord): ResolvedRef;
|
|
50
|
+
export declare function convertSchema(value: unknown, ctx: SchemaContext): TypeNode;
|
|
51
|
+
export declare function convertModel(id: ModelId, schema: unknown, ctx: SchemaContext): Model;
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DiagnosticSink = void 0;
|
|
4
|
+
exports.isRecord = isRecord;
|
|
5
|
+
exports.asRecord = asRecord;
|
|
6
|
+
exports.asArray = asArray;
|
|
7
|
+
exports.asString = asString;
|
|
8
|
+
exports.asBoolean = asBoolean;
|
|
9
|
+
exports.stringList = stringList;
|
|
10
|
+
exports.resolveSchemaRef = resolveSchemaRef;
|
|
11
|
+
exports.convertSchema = convertSchema;
|
|
12
|
+
exports.convertModel = convertModel;
|
|
13
|
+
const all_of_1 = require("./all-of");
|
|
14
|
+
function isRecord(value) {
|
|
15
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
16
|
+
}
|
|
17
|
+
function asRecord(value) {
|
|
18
|
+
return isRecord(value) ? value : undefined;
|
|
19
|
+
}
|
|
20
|
+
function asArray(value) {
|
|
21
|
+
return Array.isArray(value) ? value : undefined;
|
|
22
|
+
}
|
|
23
|
+
function asString(value) {
|
|
24
|
+
return typeof value === 'string' ? value : undefined;
|
|
25
|
+
}
|
|
26
|
+
function asBoolean(value) {
|
|
27
|
+
return typeof value === 'boolean' ? value : undefined;
|
|
28
|
+
}
|
|
29
|
+
function stringList(value) {
|
|
30
|
+
var _a;
|
|
31
|
+
return ((_a = asArray(value)) !== null && _a !== void 0 ? _a : []).filter((v) => typeof v === 'string');
|
|
32
|
+
}
|
|
33
|
+
function hasOwn(record, key) {
|
|
34
|
+
return Object.prototype.hasOwnProperty.call(record, key);
|
|
35
|
+
}
|
|
36
|
+
class DiagnosticSink {
|
|
37
|
+
constructor() {
|
|
38
|
+
this.items = [];
|
|
39
|
+
this.stack = ['#'];
|
|
40
|
+
}
|
|
41
|
+
get at() {
|
|
42
|
+
return this.stack[this.stack.length - 1];
|
|
43
|
+
}
|
|
44
|
+
within(segment, fn) {
|
|
45
|
+
this.stack.push(`${this.at}/${segment.replace(/~/g, '~0').replace(/\//g, '~1')}`);
|
|
46
|
+
try {
|
|
47
|
+
return fn();
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
this.stack.pop();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
atLocation(location, fn) {
|
|
54
|
+
this.stack.push(location);
|
|
55
|
+
try {
|
|
56
|
+
return fn();
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
this.stack.pop();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
report(level, code, message) {
|
|
63
|
+
this.items.push({ level, code, message, at: this.at });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.DiagnosticSink = DiagnosticSink;
|
|
67
|
+
const SCHEMA_REF_PREFIX = '#/components/schemas/';
|
|
68
|
+
function resolveSchemaRef(ref, schemas) {
|
|
69
|
+
if (ref.startsWith(SCHEMA_REF_PREFIX)) {
|
|
70
|
+
const key = ref.slice(SCHEMA_REF_PREFIX.length).replace(/^\/+/, '');
|
|
71
|
+
return hasOwn(schemas, key) ? { id: key } : { id: key, unresolved: true };
|
|
72
|
+
}
|
|
73
|
+
const last = ref.slice(ref.lastIndexOf('/') + 1);
|
|
74
|
+
if (last && hasOwn(schemas, last))
|
|
75
|
+
return { id: last };
|
|
76
|
+
return { id: ref, unresolved: true };
|
|
77
|
+
}
|
|
78
|
+
function refNode(ref, ctx) {
|
|
79
|
+
const resolved = resolveSchemaRef(ref, ctx.schemas);
|
|
80
|
+
if (resolved.unresolved) {
|
|
81
|
+
ctx.diagnostics.report('warn', 'unresolved-ref', `$ref "${ref}" does not resolve to a component schema`);
|
|
82
|
+
return { kind: 'ref', id: resolved.id, unresolved: true };
|
|
83
|
+
}
|
|
84
|
+
if (!ref.startsWith(SCHEMA_REF_PREFIX)) {
|
|
85
|
+
ctx.diagnostics.report('info', 'nonstandard-ref', `non-standard $ref "${ref}" resolved by name to "${resolved.id}"`);
|
|
86
|
+
}
|
|
87
|
+
return { kind: 'ref', id: resolved.id };
|
|
88
|
+
}
|
|
89
|
+
const PRIMITIVE_NAMES = ['string', 'integer', 'number', 'boolean', 'null'];
|
|
90
|
+
function isPrimitiveName(value) {
|
|
91
|
+
return typeof value === 'string' && PRIMITIVE_NAMES.includes(value);
|
|
92
|
+
}
|
|
93
|
+
function isEnumValue(value) {
|
|
94
|
+
return value === null || ['string', 'number', 'boolean'].includes(typeof value);
|
|
95
|
+
}
|
|
96
|
+
function convertSchema(value, ctx) {
|
|
97
|
+
if (!isRecord(value))
|
|
98
|
+
return { kind: 'any' };
|
|
99
|
+
return withCommonFields(convertShape(value, ctx), value);
|
|
100
|
+
}
|
|
101
|
+
function convertShape(schema, ctx) {
|
|
102
|
+
if (typeof schema.$ref === 'string')
|
|
103
|
+
return refNode(schema.$ref, ctx);
|
|
104
|
+
const enumValues = enumList(schema);
|
|
105
|
+
if (enumValues && schema.type !== 'array')
|
|
106
|
+
return enumNode(schema, enumValues);
|
|
107
|
+
if (enumValues)
|
|
108
|
+
ctx.diagnostics.report('info', 'array-enum-ignored', '`enum` on an array schema is ignored (put it on `items`)');
|
|
109
|
+
if (hasOwn(schema, 'const'))
|
|
110
|
+
return literalNode(schema.const);
|
|
111
|
+
if (asArray(schema.allOf))
|
|
112
|
+
return allOfNode(schema, ctx);
|
|
113
|
+
if (asArray(schema.oneOf))
|
|
114
|
+
return unionNode(schema, 'oneOf', ctx);
|
|
115
|
+
if (asArray(schema.anyOf))
|
|
116
|
+
return unionNode(schema, 'anyOf', ctx);
|
|
117
|
+
const typeList = typeNames(schema.type);
|
|
118
|
+
if (typeList)
|
|
119
|
+
return typeListNode(schema, typeList, ctx);
|
|
120
|
+
if (schema.type === 'array')
|
|
121
|
+
return arrayNode(schema, ctx);
|
|
122
|
+
if (isObjectSchema(schema))
|
|
123
|
+
return objectOrMapNode(schema, ctx);
|
|
124
|
+
if (schema.type === undefined && hasOwn(schema, 'items'))
|
|
125
|
+
return arrayNode(schema, ctx);
|
|
126
|
+
if (isPrimitiveName(schema.type))
|
|
127
|
+
return primitiveNode(schema.type, schema.format);
|
|
128
|
+
if (schema.type !== undefined)
|
|
129
|
+
ctx.diagnostics.report('warn', 'unknown-type', `unknown type ${JSON.stringify(schema.type)} — treated as untyped`);
|
|
130
|
+
return { kind: 'any' };
|
|
131
|
+
}
|
|
132
|
+
function withCommonFields(node, schema) {
|
|
133
|
+
if (schema.nullable === true)
|
|
134
|
+
node.nullable = true;
|
|
135
|
+
const description = asString(schema.description);
|
|
136
|
+
if (description !== undefined)
|
|
137
|
+
node.description = description;
|
|
138
|
+
const deprecated = asBoolean(schema.deprecated);
|
|
139
|
+
if (deprecated !== undefined)
|
|
140
|
+
node.deprecated = deprecated;
|
|
141
|
+
const readOnly = asBoolean(schema.readOnly);
|
|
142
|
+
if (readOnly !== undefined)
|
|
143
|
+
node.readOnly = readOnly;
|
|
144
|
+
const writeOnly = asBoolean(schema.writeOnly);
|
|
145
|
+
if (writeOnly !== undefined)
|
|
146
|
+
node.writeOnly = writeOnly;
|
|
147
|
+
if (hasOwn(schema, 'default'))
|
|
148
|
+
node.default = schema.default;
|
|
149
|
+
if (hasOwn(schema, 'example'))
|
|
150
|
+
node.example = schema.example;
|
|
151
|
+
node.raw = schema;
|
|
152
|
+
return node;
|
|
153
|
+
}
|
|
154
|
+
function primitiveNode(type, format) {
|
|
155
|
+
const fmt = asString(format);
|
|
156
|
+
return fmt !== undefined && type !== 'null' ? { kind: 'primitive', type, format: fmt } : { kind: 'primitive', type };
|
|
157
|
+
}
|
|
158
|
+
function enumList(schema) {
|
|
159
|
+
var _a;
|
|
160
|
+
let values = (_a = asArray(schema.enum)) !== null && _a !== void 0 ? _a : (isRecord(schema.enum) ? Object.values(schema.enum) : undefined);
|
|
161
|
+
if (!values || values.length === 0)
|
|
162
|
+
return undefined;
|
|
163
|
+
if (Array.isArray(values[0]))
|
|
164
|
+
values = values[0];
|
|
165
|
+
const out = [];
|
|
166
|
+
for (const value of values) {
|
|
167
|
+
if (isEnumValue(value) && !out.includes(value))
|
|
168
|
+
out.push(value);
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
function enumNode(schema, values) {
|
|
173
|
+
var _a;
|
|
174
|
+
const names = (_a = typeNames(schema.type)) !== null && _a !== void 0 ? _a : (typeof schema.type === 'string' ? [schema.type] : []);
|
|
175
|
+
const nonNull = names.filter((n) => n !== 'null');
|
|
176
|
+
const node = { kind: 'enum', values, origin: 'schema' };
|
|
177
|
+
const base = nonNull.length === 1 ? nonNull[0] : undefined;
|
|
178
|
+
if (base === 'string' || base === 'integer' || base === 'number' || base === 'boolean')
|
|
179
|
+
node.base = base;
|
|
180
|
+
if (names.length > 1 && names.includes('null'))
|
|
181
|
+
node.nullable = true;
|
|
182
|
+
return node;
|
|
183
|
+
}
|
|
184
|
+
function literalNode(value) {
|
|
185
|
+
return isEnumValue(value) ? { kind: 'literal', value } : { kind: 'any' };
|
|
186
|
+
}
|
|
187
|
+
function typeNames(type) {
|
|
188
|
+
if (Array.isArray(type))
|
|
189
|
+
return type.filter((t) => typeof t === 'string').map((t) => t.trim());
|
|
190
|
+
if (typeof type === 'string' && type.includes('|'))
|
|
191
|
+
return type.split('|').map((t) => t.trim());
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
function typeListNode(schema, names, ctx) {
|
|
195
|
+
const variants = names.map((name) => {
|
|
196
|
+
if (isPrimitiveName(name))
|
|
197
|
+
return primitiveNode(name, schema.format);
|
|
198
|
+
if (name === 'array')
|
|
199
|
+
return arrayNode(schema, ctx);
|
|
200
|
+
if (name === 'object')
|
|
201
|
+
return objectOrMapNode(schema, ctx);
|
|
202
|
+
ctx.diagnostics.report('warn', 'unknown-type', `unknown type "${name}" in type list — treated as untyped`);
|
|
203
|
+
return { kind: 'any' };
|
|
204
|
+
});
|
|
205
|
+
return { kind: 'union', variants, origin: 'type-list' };
|
|
206
|
+
}
|
|
207
|
+
function arrayNode(schema, ctx) {
|
|
208
|
+
return { kind: 'array', items: convertSchema(schema.items, ctx) };
|
|
209
|
+
}
|
|
210
|
+
function isObjectSchema(schema) {
|
|
211
|
+
return schema.type === 'object' || (schema.type === undefined && isRecord(schema.properties));
|
|
212
|
+
}
|
|
213
|
+
function additionalPropertiesNode(schema, ctx) {
|
|
214
|
+
if (schema.additionalProperties === true)
|
|
215
|
+
return { kind: 'any' };
|
|
216
|
+
if (isRecord(schema.additionalProperties))
|
|
217
|
+
return convertSchema(schema.additionalProperties, ctx);
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
function objectNode(schema, ctx) {
|
|
221
|
+
var _a;
|
|
222
|
+
const required = stringList(schema.required);
|
|
223
|
+
const properties = Object.entries((_a = asRecord(schema.properties)) !== null && _a !== void 0 ? _a : {}).map(([name, propSchema]) => propertyFrom(name, propSchema, required, ctx));
|
|
224
|
+
const node = { kind: 'object', properties };
|
|
225
|
+
const additional = additionalPropertiesNode(schema, ctx);
|
|
226
|
+
if (additional)
|
|
227
|
+
node.additionalProperties = additional;
|
|
228
|
+
return node;
|
|
229
|
+
}
|
|
230
|
+
function objectOrMapNode(schema, ctx) {
|
|
231
|
+
const properties = asRecord(schema.properties);
|
|
232
|
+
if (properties && Object.keys(properties).length > 0)
|
|
233
|
+
return objectNode(schema, ctx);
|
|
234
|
+
const additional = additionalPropertiesNode(schema, ctx);
|
|
235
|
+
if (additional)
|
|
236
|
+
return { kind: 'map', values: additional };
|
|
237
|
+
return { kind: 'object', properties: [] };
|
|
238
|
+
}
|
|
239
|
+
function propertyFrom(name, propSchema, required, ctx) {
|
|
240
|
+
var _a;
|
|
241
|
+
const type = ctx.diagnostics.within('properties', () => ctx.diagnostics.within(name, () => convertSchema(propSchema, ctx)));
|
|
242
|
+
const property = { name, type, required: required.includes(name) };
|
|
243
|
+
const description = asString((_a = asRecord(propSchema)) === null || _a === void 0 ? void 0 : _a.description);
|
|
244
|
+
if (description !== undefined)
|
|
245
|
+
property.description = description;
|
|
246
|
+
return property;
|
|
247
|
+
}
|
|
248
|
+
function unionNode(schema, origin, ctx) {
|
|
249
|
+
var _a;
|
|
250
|
+
const variants = ((_a = asArray(schema[origin])) !== null && _a !== void 0 ? _a : []).map((v) => convertSchema(v, ctx));
|
|
251
|
+
const node = { kind: 'union', variants, origin };
|
|
252
|
+
const discriminator = discriminatorFrom(schema.discriminator, ctx);
|
|
253
|
+
if (discriminator)
|
|
254
|
+
node.discriminator = discriminator;
|
|
255
|
+
return node;
|
|
256
|
+
}
|
|
257
|
+
function discriminatorFrom(value, ctx) {
|
|
258
|
+
const raw = asRecord(value);
|
|
259
|
+
const propertyName = asString(raw === null || raw === void 0 ? void 0 : raw.propertyName);
|
|
260
|
+
if (!raw || propertyName === undefined)
|
|
261
|
+
return undefined;
|
|
262
|
+
const rawMapping = asRecord(raw.mapping);
|
|
263
|
+
if (!rawMapping)
|
|
264
|
+
return { propertyName };
|
|
265
|
+
const mapping = {};
|
|
266
|
+
for (const [key, target] of Object.entries(rawMapping)) {
|
|
267
|
+
if (typeof target !== 'string')
|
|
268
|
+
continue;
|
|
269
|
+
const resolved = target.startsWith('#') ? resolveSchemaRef(target, ctx.schemas) : undefined;
|
|
270
|
+
mapping[key] = resolved && !resolved.unresolved ? resolved.id : target;
|
|
271
|
+
}
|
|
272
|
+
return { propertyName, mapping };
|
|
273
|
+
}
|
|
274
|
+
function allOfNode(schema, ctx) {
|
|
275
|
+
var _a;
|
|
276
|
+
const members = ((_a = asArray(schema.allOf)) !== null && _a !== void 0 ? _a : []).map((m) => convertSchema(m, ctx));
|
|
277
|
+
const ownProperties = asRecord(schema.properties);
|
|
278
|
+
if (ownProperties && Object.keys(ownProperties).length > 0)
|
|
279
|
+
members.unshift(objectNode(schema, ctx));
|
|
280
|
+
if (members.length === 0)
|
|
281
|
+
return { kind: 'any' };
|
|
282
|
+
if (ctx.allOf === 'merge' && members.length === 1)
|
|
283
|
+
return members[0];
|
|
284
|
+
return { kind: 'intersection', members };
|
|
285
|
+
}
|
|
286
|
+
function convertModel(id, schema, ctx) {
|
|
287
|
+
const raw = asRecord(schema);
|
|
288
|
+
const type = ctx.diagnostics.atLocation(`#/components/schemas/${id}`, () => {
|
|
289
|
+
const merged = raw ? (0, all_of_1.flattenModelSchema)(raw, ctx, Boolean(enumList(raw))) : undefined;
|
|
290
|
+
return raw && merged ? withCommonFields(objectNode(merged, ctx), raw) : convertSchema(schema, ctx);
|
|
291
|
+
});
|
|
292
|
+
const segments = id.split('.');
|
|
293
|
+
const model = {
|
|
294
|
+
id,
|
|
295
|
+
name: segments[segments.length - 1],
|
|
296
|
+
namespace: segments.slice(0, -1),
|
|
297
|
+
type,
|
|
298
|
+
raw: schema,
|
|
299
|
+
};
|
|
300
|
+
const description = asString(raw === null || raw === void 0 ? void 0 : raw.description);
|
|
301
|
+
if (description !== undefined)
|
|
302
|
+
model.description = description;
|
|
303
|
+
const deprecated = asBoolean(raw === null || raw === void 0 ? void 0 : raw.deprecated);
|
|
304
|
+
if (deprecated !== undefined)
|
|
305
|
+
model.deprecated = deprecated;
|
|
306
|
+
return model;
|
|
307
|
+
}
|