@fumari/json-schema-ts 0.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/LICENCE.md +7 -0
- package/dist/index.d.mts +18 -0
- package/dist/index.mjs +738 -0
- package/package.json +61 -0
package/LICENCE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2022 Boris Cherny
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { JSONSchema } from "json-schema-typed/draft-2020-12";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
interface CompileOptions {
|
|
5
|
+
name?: string;
|
|
6
|
+
/**
|
|
7
|
+
* assuming the input schema is already dereferenced, this function should return the original `$ref` value.
|
|
8
|
+
*/
|
|
9
|
+
getSchemaId?: (schema: object) => string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* whether the schema should be generated separatedly as another type, or prefer inline if possible.
|
|
12
|
+
*/
|
|
13
|
+
shouldSeparateDeclaration?: (schema: object) => boolean;
|
|
14
|
+
unreachableDefinitions?: boolean;
|
|
15
|
+
}
|
|
16
|
+
declare function compile(schema: JSONSchema, options?: CompileOptions): string;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { CompileOptions, compile };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
import { print } from "esrap";
|
|
2
|
+
import ts from "esrap/languages/ts";
|
|
3
|
+
|
|
4
|
+
//#region \0rolldown/runtime.js
|
|
5
|
+
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region ../../node_modules/.pnpm/@typescript-eslint+types@8.56.1/node_modules/@typescript-eslint/types/dist/generated/ast-spec.js
|
|
9
|
+
var require_ast_spec = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
10
|
+
/**********************************************
|
|
11
|
+
* DO NOT MODIFY THIS FILE MANUALLY *
|
|
12
|
+
* *
|
|
13
|
+
* THIS FILE HAS BEEN COPIED FROM ast-spec. *
|
|
14
|
+
* ANY CHANGES WILL BE LOST ON THE NEXT BUILD *
|
|
15
|
+
* *
|
|
16
|
+
* MAKE CHANGES TO ast-spec AND THEN RUN *
|
|
17
|
+
* pnpm run build *
|
|
18
|
+
**********************************************/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.AST_TOKEN_TYPES = exports.AST_NODE_TYPES = void 0;
|
|
21
|
+
var AST_NODE_TYPES;
|
|
22
|
+
(function(AST_NODE_TYPES) {
|
|
23
|
+
AST_NODE_TYPES["AccessorProperty"] = "AccessorProperty";
|
|
24
|
+
AST_NODE_TYPES["ArrayExpression"] = "ArrayExpression";
|
|
25
|
+
AST_NODE_TYPES["ArrayPattern"] = "ArrayPattern";
|
|
26
|
+
AST_NODE_TYPES["ArrowFunctionExpression"] = "ArrowFunctionExpression";
|
|
27
|
+
AST_NODE_TYPES["AssignmentExpression"] = "AssignmentExpression";
|
|
28
|
+
AST_NODE_TYPES["AssignmentPattern"] = "AssignmentPattern";
|
|
29
|
+
AST_NODE_TYPES["AwaitExpression"] = "AwaitExpression";
|
|
30
|
+
AST_NODE_TYPES["BinaryExpression"] = "BinaryExpression";
|
|
31
|
+
AST_NODE_TYPES["BlockStatement"] = "BlockStatement";
|
|
32
|
+
AST_NODE_TYPES["BreakStatement"] = "BreakStatement";
|
|
33
|
+
AST_NODE_TYPES["CallExpression"] = "CallExpression";
|
|
34
|
+
AST_NODE_TYPES["CatchClause"] = "CatchClause";
|
|
35
|
+
AST_NODE_TYPES["ChainExpression"] = "ChainExpression";
|
|
36
|
+
AST_NODE_TYPES["ClassBody"] = "ClassBody";
|
|
37
|
+
AST_NODE_TYPES["ClassDeclaration"] = "ClassDeclaration";
|
|
38
|
+
AST_NODE_TYPES["ClassExpression"] = "ClassExpression";
|
|
39
|
+
AST_NODE_TYPES["ConditionalExpression"] = "ConditionalExpression";
|
|
40
|
+
AST_NODE_TYPES["ContinueStatement"] = "ContinueStatement";
|
|
41
|
+
AST_NODE_TYPES["DebuggerStatement"] = "DebuggerStatement";
|
|
42
|
+
AST_NODE_TYPES["Decorator"] = "Decorator";
|
|
43
|
+
AST_NODE_TYPES["DoWhileStatement"] = "DoWhileStatement";
|
|
44
|
+
AST_NODE_TYPES["EmptyStatement"] = "EmptyStatement";
|
|
45
|
+
AST_NODE_TYPES["ExportAllDeclaration"] = "ExportAllDeclaration";
|
|
46
|
+
AST_NODE_TYPES["ExportDefaultDeclaration"] = "ExportDefaultDeclaration";
|
|
47
|
+
AST_NODE_TYPES["ExportNamedDeclaration"] = "ExportNamedDeclaration";
|
|
48
|
+
AST_NODE_TYPES["ExportSpecifier"] = "ExportSpecifier";
|
|
49
|
+
AST_NODE_TYPES["ExpressionStatement"] = "ExpressionStatement";
|
|
50
|
+
AST_NODE_TYPES["ForInStatement"] = "ForInStatement";
|
|
51
|
+
AST_NODE_TYPES["ForOfStatement"] = "ForOfStatement";
|
|
52
|
+
AST_NODE_TYPES["ForStatement"] = "ForStatement";
|
|
53
|
+
AST_NODE_TYPES["FunctionDeclaration"] = "FunctionDeclaration";
|
|
54
|
+
AST_NODE_TYPES["FunctionExpression"] = "FunctionExpression";
|
|
55
|
+
AST_NODE_TYPES["Identifier"] = "Identifier";
|
|
56
|
+
AST_NODE_TYPES["IfStatement"] = "IfStatement";
|
|
57
|
+
AST_NODE_TYPES["ImportAttribute"] = "ImportAttribute";
|
|
58
|
+
AST_NODE_TYPES["ImportDeclaration"] = "ImportDeclaration";
|
|
59
|
+
AST_NODE_TYPES["ImportDefaultSpecifier"] = "ImportDefaultSpecifier";
|
|
60
|
+
AST_NODE_TYPES["ImportExpression"] = "ImportExpression";
|
|
61
|
+
AST_NODE_TYPES["ImportNamespaceSpecifier"] = "ImportNamespaceSpecifier";
|
|
62
|
+
AST_NODE_TYPES["ImportSpecifier"] = "ImportSpecifier";
|
|
63
|
+
AST_NODE_TYPES["JSXAttribute"] = "JSXAttribute";
|
|
64
|
+
AST_NODE_TYPES["JSXClosingElement"] = "JSXClosingElement";
|
|
65
|
+
AST_NODE_TYPES["JSXClosingFragment"] = "JSXClosingFragment";
|
|
66
|
+
AST_NODE_TYPES["JSXElement"] = "JSXElement";
|
|
67
|
+
AST_NODE_TYPES["JSXEmptyExpression"] = "JSXEmptyExpression";
|
|
68
|
+
AST_NODE_TYPES["JSXExpressionContainer"] = "JSXExpressionContainer";
|
|
69
|
+
AST_NODE_TYPES["JSXFragment"] = "JSXFragment";
|
|
70
|
+
AST_NODE_TYPES["JSXIdentifier"] = "JSXIdentifier";
|
|
71
|
+
AST_NODE_TYPES["JSXMemberExpression"] = "JSXMemberExpression";
|
|
72
|
+
AST_NODE_TYPES["JSXNamespacedName"] = "JSXNamespacedName";
|
|
73
|
+
AST_NODE_TYPES["JSXOpeningElement"] = "JSXOpeningElement";
|
|
74
|
+
AST_NODE_TYPES["JSXOpeningFragment"] = "JSXOpeningFragment";
|
|
75
|
+
AST_NODE_TYPES["JSXSpreadAttribute"] = "JSXSpreadAttribute";
|
|
76
|
+
AST_NODE_TYPES["JSXSpreadChild"] = "JSXSpreadChild";
|
|
77
|
+
AST_NODE_TYPES["JSXText"] = "JSXText";
|
|
78
|
+
AST_NODE_TYPES["LabeledStatement"] = "LabeledStatement";
|
|
79
|
+
AST_NODE_TYPES["Literal"] = "Literal";
|
|
80
|
+
AST_NODE_TYPES["LogicalExpression"] = "LogicalExpression";
|
|
81
|
+
AST_NODE_TYPES["MemberExpression"] = "MemberExpression";
|
|
82
|
+
AST_NODE_TYPES["MetaProperty"] = "MetaProperty";
|
|
83
|
+
AST_NODE_TYPES["MethodDefinition"] = "MethodDefinition";
|
|
84
|
+
AST_NODE_TYPES["NewExpression"] = "NewExpression";
|
|
85
|
+
AST_NODE_TYPES["ObjectExpression"] = "ObjectExpression";
|
|
86
|
+
AST_NODE_TYPES["ObjectPattern"] = "ObjectPattern";
|
|
87
|
+
AST_NODE_TYPES["PrivateIdentifier"] = "PrivateIdentifier";
|
|
88
|
+
AST_NODE_TYPES["Program"] = "Program";
|
|
89
|
+
AST_NODE_TYPES["Property"] = "Property";
|
|
90
|
+
AST_NODE_TYPES["PropertyDefinition"] = "PropertyDefinition";
|
|
91
|
+
AST_NODE_TYPES["RestElement"] = "RestElement";
|
|
92
|
+
AST_NODE_TYPES["ReturnStatement"] = "ReturnStatement";
|
|
93
|
+
AST_NODE_TYPES["SequenceExpression"] = "SequenceExpression";
|
|
94
|
+
AST_NODE_TYPES["SpreadElement"] = "SpreadElement";
|
|
95
|
+
AST_NODE_TYPES["StaticBlock"] = "StaticBlock";
|
|
96
|
+
AST_NODE_TYPES["Super"] = "Super";
|
|
97
|
+
AST_NODE_TYPES["SwitchCase"] = "SwitchCase";
|
|
98
|
+
AST_NODE_TYPES["SwitchStatement"] = "SwitchStatement";
|
|
99
|
+
AST_NODE_TYPES["TaggedTemplateExpression"] = "TaggedTemplateExpression";
|
|
100
|
+
AST_NODE_TYPES["TemplateElement"] = "TemplateElement";
|
|
101
|
+
AST_NODE_TYPES["TemplateLiteral"] = "TemplateLiteral";
|
|
102
|
+
AST_NODE_TYPES["ThisExpression"] = "ThisExpression";
|
|
103
|
+
AST_NODE_TYPES["ThrowStatement"] = "ThrowStatement";
|
|
104
|
+
AST_NODE_TYPES["TryStatement"] = "TryStatement";
|
|
105
|
+
AST_NODE_TYPES["UnaryExpression"] = "UnaryExpression";
|
|
106
|
+
AST_NODE_TYPES["UpdateExpression"] = "UpdateExpression";
|
|
107
|
+
AST_NODE_TYPES["VariableDeclaration"] = "VariableDeclaration";
|
|
108
|
+
AST_NODE_TYPES["VariableDeclarator"] = "VariableDeclarator";
|
|
109
|
+
AST_NODE_TYPES["WhileStatement"] = "WhileStatement";
|
|
110
|
+
AST_NODE_TYPES["WithStatement"] = "WithStatement";
|
|
111
|
+
AST_NODE_TYPES["YieldExpression"] = "YieldExpression";
|
|
112
|
+
AST_NODE_TYPES["TSAbstractAccessorProperty"] = "TSAbstractAccessorProperty";
|
|
113
|
+
AST_NODE_TYPES["TSAbstractKeyword"] = "TSAbstractKeyword";
|
|
114
|
+
AST_NODE_TYPES["TSAbstractMethodDefinition"] = "TSAbstractMethodDefinition";
|
|
115
|
+
AST_NODE_TYPES["TSAbstractPropertyDefinition"] = "TSAbstractPropertyDefinition";
|
|
116
|
+
AST_NODE_TYPES["TSAnyKeyword"] = "TSAnyKeyword";
|
|
117
|
+
AST_NODE_TYPES["TSArrayType"] = "TSArrayType";
|
|
118
|
+
AST_NODE_TYPES["TSAsExpression"] = "TSAsExpression";
|
|
119
|
+
AST_NODE_TYPES["TSAsyncKeyword"] = "TSAsyncKeyword";
|
|
120
|
+
AST_NODE_TYPES["TSBigIntKeyword"] = "TSBigIntKeyword";
|
|
121
|
+
AST_NODE_TYPES["TSBooleanKeyword"] = "TSBooleanKeyword";
|
|
122
|
+
AST_NODE_TYPES["TSCallSignatureDeclaration"] = "TSCallSignatureDeclaration";
|
|
123
|
+
AST_NODE_TYPES["TSClassImplements"] = "TSClassImplements";
|
|
124
|
+
AST_NODE_TYPES["TSConditionalType"] = "TSConditionalType";
|
|
125
|
+
AST_NODE_TYPES["TSConstructorType"] = "TSConstructorType";
|
|
126
|
+
AST_NODE_TYPES["TSConstructSignatureDeclaration"] = "TSConstructSignatureDeclaration";
|
|
127
|
+
AST_NODE_TYPES["TSDeclareFunction"] = "TSDeclareFunction";
|
|
128
|
+
AST_NODE_TYPES["TSDeclareKeyword"] = "TSDeclareKeyword";
|
|
129
|
+
AST_NODE_TYPES["TSEmptyBodyFunctionExpression"] = "TSEmptyBodyFunctionExpression";
|
|
130
|
+
AST_NODE_TYPES["TSEnumBody"] = "TSEnumBody";
|
|
131
|
+
AST_NODE_TYPES["TSEnumDeclaration"] = "TSEnumDeclaration";
|
|
132
|
+
AST_NODE_TYPES["TSEnumMember"] = "TSEnumMember";
|
|
133
|
+
AST_NODE_TYPES["TSExportAssignment"] = "TSExportAssignment";
|
|
134
|
+
AST_NODE_TYPES["TSExportKeyword"] = "TSExportKeyword";
|
|
135
|
+
AST_NODE_TYPES["TSExternalModuleReference"] = "TSExternalModuleReference";
|
|
136
|
+
AST_NODE_TYPES["TSFunctionType"] = "TSFunctionType";
|
|
137
|
+
AST_NODE_TYPES["TSImportEqualsDeclaration"] = "TSImportEqualsDeclaration";
|
|
138
|
+
AST_NODE_TYPES["TSImportType"] = "TSImportType";
|
|
139
|
+
AST_NODE_TYPES["TSIndexedAccessType"] = "TSIndexedAccessType";
|
|
140
|
+
AST_NODE_TYPES["TSIndexSignature"] = "TSIndexSignature";
|
|
141
|
+
AST_NODE_TYPES["TSInferType"] = "TSInferType";
|
|
142
|
+
AST_NODE_TYPES["TSInstantiationExpression"] = "TSInstantiationExpression";
|
|
143
|
+
AST_NODE_TYPES["TSInterfaceBody"] = "TSInterfaceBody";
|
|
144
|
+
AST_NODE_TYPES["TSInterfaceDeclaration"] = "TSInterfaceDeclaration";
|
|
145
|
+
AST_NODE_TYPES["TSInterfaceHeritage"] = "TSInterfaceHeritage";
|
|
146
|
+
AST_NODE_TYPES["TSIntersectionType"] = "TSIntersectionType";
|
|
147
|
+
AST_NODE_TYPES["TSIntrinsicKeyword"] = "TSIntrinsicKeyword";
|
|
148
|
+
AST_NODE_TYPES["TSLiteralType"] = "TSLiteralType";
|
|
149
|
+
AST_NODE_TYPES["TSMappedType"] = "TSMappedType";
|
|
150
|
+
AST_NODE_TYPES["TSMethodSignature"] = "TSMethodSignature";
|
|
151
|
+
AST_NODE_TYPES["TSModuleBlock"] = "TSModuleBlock";
|
|
152
|
+
AST_NODE_TYPES["TSModuleDeclaration"] = "TSModuleDeclaration";
|
|
153
|
+
AST_NODE_TYPES["TSNamedTupleMember"] = "TSNamedTupleMember";
|
|
154
|
+
AST_NODE_TYPES["TSNamespaceExportDeclaration"] = "TSNamespaceExportDeclaration";
|
|
155
|
+
AST_NODE_TYPES["TSNeverKeyword"] = "TSNeverKeyword";
|
|
156
|
+
AST_NODE_TYPES["TSNonNullExpression"] = "TSNonNullExpression";
|
|
157
|
+
AST_NODE_TYPES["TSNullKeyword"] = "TSNullKeyword";
|
|
158
|
+
AST_NODE_TYPES["TSNumberKeyword"] = "TSNumberKeyword";
|
|
159
|
+
AST_NODE_TYPES["TSObjectKeyword"] = "TSObjectKeyword";
|
|
160
|
+
AST_NODE_TYPES["TSOptionalType"] = "TSOptionalType";
|
|
161
|
+
AST_NODE_TYPES["TSParameterProperty"] = "TSParameterProperty";
|
|
162
|
+
AST_NODE_TYPES["TSPrivateKeyword"] = "TSPrivateKeyword";
|
|
163
|
+
AST_NODE_TYPES["TSPropertySignature"] = "TSPropertySignature";
|
|
164
|
+
AST_NODE_TYPES["TSProtectedKeyword"] = "TSProtectedKeyword";
|
|
165
|
+
AST_NODE_TYPES["TSPublicKeyword"] = "TSPublicKeyword";
|
|
166
|
+
AST_NODE_TYPES["TSQualifiedName"] = "TSQualifiedName";
|
|
167
|
+
AST_NODE_TYPES["TSReadonlyKeyword"] = "TSReadonlyKeyword";
|
|
168
|
+
AST_NODE_TYPES["TSRestType"] = "TSRestType";
|
|
169
|
+
AST_NODE_TYPES["TSSatisfiesExpression"] = "TSSatisfiesExpression";
|
|
170
|
+
AST_NODE_TYPES["TSStaticKeyword"] = "TSStaticKeyword";
|
|
171
|
+
AST_NODE_TYPES["TSStringKeyword"] = "TSStringKeyword";
|
|
172
|
+
AST_NODE_TYPES["TSSymbolKeyword"] = "TSSymbolKeyword";
|
|
173
|
+
AST_NODE_TYPES["TSTemplateLiteralType"] = "TSTemplateLiteralType";
|
|
174
|
+
AST_NODE_TYPES["TSThisType"] = "TSThisType";
|
|
175
|
+
AST_NODE_TYPES["TSTupleType"] = "TSTupleType";
|
|
176
|
+
AST_NODE_TYPES["TSTypeAliasDeclaration"] = "TSTypeAliasDeclaration";
|
|
177
|
+
AST_NODE_TYPES["TSTypeAnnotation"] = "TSTypeAnnotation";
|
|
178
|
+
AST_NODE_TYPES["TSTypeAssertion"] = "TSTypeAssertion";
|
|
179
|
+
AST_NODE_TYPES["TSTypeLiteral"] = "TSTypeLiteral";
|
|
180
|
+
AST_NODE_TYPES["TSTypeOperator"] = "TSTypeOperator";
|
|
181
|
+
AST_NODE_TYPES["TSTypeParameter"] = "TSTypeParameter";
|
|
182
|
+
AST_NODE_TYPES["TSTypeParameterDeclaration"] = "TSTypeParameterDeclaration";
|
|
183
|
+
AST_NODE_TYPES["TSTypeParameterInstantiation"] = "TSTypeParameterInstantiation";
|
|
184
|
+
AST_NODE_TYPES["TSTypePredicate"] = "TSTypePredicate";
|
|
185
|
+
AST_NODE_TYPES["TSTypeQuery"] = "TSTypeQuery";
|
|
186
|
+
AST_NODE_TYPES["TSTypeReference"] = "TSTypeReference";
|
|
187
|
+
AST_NODE_TYPES["TSUndefinedKeyword"] = "TSUndefinedKeyword";
|
|
188
|
+
AST_NODE_TYPES["TSUnionType"] = "TSUnionType";
|
|
189
|
+
AST_NODE_TYPES["TSUnknownKeyword"] = "TSUnknownKeyword";
|
|
190
|
+
AST_NODE_TYPES["TSVoidKeyword"] = "TSVoidKeyword";
|
|
191
|
+
})(AST_NODE_TYPES || (exports.AST_NODE_TYPES = AST_NODE_TYPES = {}));
|
|
192
|
+
var AST_TOKEN_TYPES;
|
|
193
|
+
(function(AST_TOKEN_TYPES) {
|
|
194
|
+
AST_TOKEN_TYPES["Boolean"] = "Boolean";
|
|
195
|
+
AST_TOKEN_TYPES["Identifier"] = "Identifier";
|
|
196
|
+
AST_TOKEN_TYPES["JSXIdentifier"] = "JSXIdentifier";
|
|
197
|
+
AST_TOKEN_TYPES["PrivateIdentifier"] = "PrivateIdentifier";
|
|
198
|
+
AST_TOKEN_TYPES["JSXText"] = "JSXText";
|
|
199
|
+
AST_TOKEN_TYPES["Keyword"] = "Keyword";
|
|
200
|
+
AST_TOKEN_TYPES["Null"] = "Null";
|
|
201
|
+
AST_TOKEN_TYPES["Numeric"] = "Numeric";
|
|
202
|
+
AST_TOKEN_TYPES["Punctuator"] = "Punctuator";
|
|
203
|
+
AST_TOKEN_TYPES["RegularExpression"] = "RegularExpression";
|
|
204
|
+
AST_TOKEN_TYPES["String"] = "String";
|
|
205
|
+
AST_TOKEN_TYPES["Template"] = "Template";
|
|
206
|
+
AST_TOKEN_TYPES["Block"] = "Block";
|
|
207
|
+
AST_TOKEN_TYPES["Line"] = "Line";
|
|
208
|
+
})(AST_TOKEN_TYPES || (exports.AST_TOKEN_TYPES = AST_TOKEN_TYPES = {}));
|
|
209
|
+
}));
|
|
210
|
+
|
|
211
|
+
//#endregion
|
|
212
|
+
//#region ../../node_modules/.pnpm/@typescript-eslint+types@8.56.1/node_modules/@typescript-eslint/types/dist/lib.js
|
|
213
|
+
var require_lib = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
214
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
215
|
+
}));
|
|
216
|
+
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region ../../node_modules/.pnpm/@typescript-eslint+types@8.56.1/node_modules/@typescript-eslint/types/dist/parser-options.js
|
|
219
|
+
var require_parser_options = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
220
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
221
|
+
}));
|
|
222
|
+
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region ../../node_modules/.pnpm/@typescript-eslint+types@8.56.1/node_modules/@typescript-eslint/types/dist/ts-estree.js
|
|
225
|
+
var require_ts_estree = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
226
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
227
|
+
if (k2 === void 0) k2 = k;
|
|
228
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
229
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) desc = {
|
|
230
|
+
enumerable: true,
|
|
231
|
+
get: function() {
|
|
232
|
+
return m[k];
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
Object.defineProperty(o, k2, desc);
|
|
236
|
+
}) : (function(o, m, k, k2) {
|
|
237
|
+
if (k2 === void 0) k2 = k;
|
|
238
|
+
o[k2] = m[k];
|
|
239
|
+
}));
|
|
240
|
+
var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
241
|
+
Object.defineProperty(o, "default", {
|
|
242
|
+
enumerable: true,
|
|
243
|
+
value: v
|
|
244
|
+
});
|
|
245
|
+
}) : function(o, v) {
|
|
246
|
+
o["default"] = v;
|
|
247
|
+
});
|
|
248
|
+
var __importStar = exports && exports.__importStar || (function() {
|
|
249
|
+
var ownKeys = function(o) {
|
|
250
|
+
ownKeys = Object.getOwnPropertyNames || function(o) {
|
|
251
|
+
var ar = [];
|
|
252
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
253
|
+
return ar;
|
|
254
|
+
};
|
|
255
|
+
return ownKeys(o);
|
|
256
|
+
};
|
|
257
|
+
return function(mod) {
|
|
258
|
+
if (mod && mod.__esModule) return mod;
|
|
259
|
+
var result = {};
|
|
260
|
+
if (mod != null) {
|
|
261
|
+
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
262
|
+
}
|
|
263
|
+
__setModuleDefault(result, mod);
|
|
264
|
+
return result;
|
|
265
|
+
};
|
|
266
|
+
})();
|
|
267
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
268
|
+
exports.TSESTree = void 0;
|
|
269
|
+
exports.TSESTree = __importStar(require_ast_spec());
|
|
270
|
+
}));
|
|
271
|
+
|
|
272
|
+
//#endregion
|
|
273
|
+
//#region ../../node_modules/.pnpm/@typescript-eslint+types@8.56.1/node_modules/@typescript-eslint/types/dist/index.js
|
|
274
|
+
var require_dist = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
275
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
276
|
+
if (k2 === void 0) k2 = k;
|
|
277
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
278
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) desc = {
|
|
279
|
+
enumerable: true,
|
|
280
|
+
get: function() {
|
|
281
|
+
return m[k];
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
Object.defineProperty(o, k2, desc);
|
|
285
|
+
}) : (function(o, m, k, k2) {
|
|
286
|
+
if (k2 === void 0) k2 = k;
|
|
287
|
+
o[k2] = m[k];
|
|
288
|
+
}));
|
|
289
|
+
var __exportStar = exports && exports.__exportStar || function(m, exports$1) {
|
|
290
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$1, p)) __createBinding(exports$1, m, p);
|
|
291
|
+
};
|
|
292
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
293
|
+
exports.AST_TOKEN_TYPES = exports.AST_NODE_TYPES = void 0;
|
|
294
|
+
var ast_spec_1 = require_ast_spec();
|
|
295
|
+
Object.defineProperty(exports, "AST_NODE_TYPES", {
|
|
296
|
+
enumerable: true,
|
|
297
|
+
get: function() {
|
|
298
|
+
return ast_spec_1.AST_NODE_TYPES;
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
Object.defineProperty(exports, "AST_TOKEN_TYPES", {
|
|
302
|
+
enumerable: true,
|
|
303
|
+
get: function() {
|
|
304
|
+
return ast_spec_1.AST_TOKEN_TYPES;
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
__exportStar(require_lib(), exports);
|
|
308
|
+
__exportStar(require_parser_options(), exports);
|
|
309
|
+
__exportStar(require_ts_estree(), exports);
|
|
310
|
+
}));
|
|
311
|
+
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region src/utils.ts
|
|
314
|
+
var import_dist = require_dist();
|
|
315
|
+
/**
|
|
316
|
+
* Convert any string into a valid TypeScript type name.
|
|
317
|
+
* Removes special characters, converts to PascalCase, and ensures name starts with a letter.
|
|
318
|
+
*/
|
|
319
|
+
function toSafeString(str) {
|
|
320
|
+
const dotIdx = str.lastIndexOf("/");
|
|
321
|
+
if (dotIdx !== -1) str = str.slice(dotIdx + 1);
|
|
322
|
+
return str.normalize().replace(/[^a-zA-Z0-9_$]/g, " ").replace(/(^\w|\s+\w|_\w|\d\w)/g, (letter) => {
|
|
323
|
+
if (letter.startsWith("_")) return letter.slice(1).toUpperCase();
|
|
324
|
+
return letter.trim().toUpperCase();
|
|
325
|
+
}).replace(/\s+/g, "").replace(/^\d+/, "");
|
|
326
|
+
}
|
|
327
|
+
function isValidPropertyName(name) {
|
|
328
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* replace objects in-place
|
|
332
|
+
*/
|
|
333
|
+
function replace(obj, by) {
|
|
334
|
+
for (const key in obj) delete obj[key];
|
|
335
|
+
Object.assign(obj, by);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
//#endregion
|
|
339
|
+
//#region src/pending.ts
|
|
340
|
+
function createPendingFactory() {
|
|
341
|
+
const allPendings = /* @__PURE__ */ new WeakSet();
|
|
342
|
+
return {
|
|
343
|
+
createPending(defaultValue) {
|
|
344
|
+
const pending = defaultValue !== null && defaultValue !== void 0 ? defaultValue : {};
|
|
345
|
+
allPendings.add(pending);
|
|
346
|
+
return pending;
|
|
347
|
+
},
|
|
348
|
+
isPending(obj) {
|
|
349
|
+
return allPendings.has(obj);
|
|
350
|
+
},
|
|
351
|
+
resolvePending(obj, value) {
|
|
352
|
+
replace(obj, value);
|
|
353
|
+
allPendings.delete(obj);
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region \0@oxc-project+runtime@0.114.0/helpers/typeof.js
|
|
360
|
+
function _typeof(o) {
|
|
361
|
+
"@babel/helpers - typeof";
|
|
362
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
363
|
+
return typeof o;
|
|
364
|
+
} : function(o) {
|
|
365
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
366
|
+
}, _typeof(o);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
//#endregion
|
|
370
|
+
//#region \0@oxc-project+runtime@0.114.0/helpers/toPrimitive.js
|
|
371
|
+
function toPrimitive(t, r) {
|
|
372
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
373
|
+
var e = t[Symbol.toPrimitive];
|
|
374
|
+
if (void 0 !== e) {
|
|
375
|
+
var i = e.call(t, r || "default");
|
|
376
|
+
if ("object" != _typeof(i)) return i;
|
|
377
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
378
|
+
}
|
|
379
|
+
return ("string" === r ? String : Number)(t);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region \0@oxc-project+runtime@0.114.0/helpers/toPropertyKey.js
|
|
384
|
+
function toPropertyKey(t) {
|
|
385
|
+
var i = toPrimitive(t, "string");
|
|
386
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region \0@oxc-project+runtime@0.114.0/helpers/defineProperty.js
|
|
391
|
+
function _defineProperty(e, r, t) {
|
|
392
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
393
|
+
value: t,
|
|
394
|
+
enumerable: !0,
|
|
395
|
+
configurable: !0,
|
|
396
|
+
writable: !0
|
|
397
|
+
}) : e[r] = t, e;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region \0@oxc-project+runtime@0.114.0/helpers/objectSpread2.js
|
|
402
|
+
function ownKeys(e, r) {
|
|
403
|
+
var t = Object.keys(e);
|
|
404
|
+
if (Object.getOwnPropertySymbols) {
|
|
405
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
406
|
+
r && (o = o.filter(function(r) {
|
|
407
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
408
|
+
})), t.push.apply(t, o);
|
|
409
|
+
}
|
|
410
|
+
return t;
|
|
411
|
+
}
|
|
412
|
+
function _objectSpread2(e) {
|
|
413
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
414
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
415
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function(r) {
|
|
416
|
+
_defineProperty(e, r, t[r]);
|
|
417
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r) {
|
|
418
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
return e;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
//#endregion
|
|
425
|
+
//#region src/index.ts
|
|
426
|
+
function context(options) {
|
|
427
|
+
return _objectSpread2(_objectSpread2(_objectSpread2({}, createPendingFactory()), options), {}, {
|
|
428
|
+
declarations: /* @__PURE__ */ new Map(),
|
|
429
|
+
inlineResults: /* @__PURE__ */ new Map(),
|
|
430
|
+
comments: /* @__PURE__ */ new Map(),
|
|
431
|
+
getSchemaId(schema) {
|
|
432
|
+
var _options$getSchemaId;
|
|
433
|
+
return (_options$getSchemaId = options.getSchemaId) === null || _options$getSchemaId === void 0 ? void 0 : _options$getSchemaId.call(options, schema);
|
|
434
|
+
},
|
|
435
|
+
getSchemaDeclaration(schema) {
|
|
436
|
+
for (const [k, v] of this.declarations) if (v.schema === schema) return k;
|
|
437
|
+
},
|
|
438
|
+
addComment(node, ...comments) {
|
|
439
|
+
const list = this.comments.get(node);
|
|
440
|
+
if (!list) this.comments.set(node, comments);
|
|
441
|
+
else list.push(...comments);
|
|
442
|
+
},
|
|
443
|
+
transferComment(from, to) {
|
|
444
|
+
const a = this.comments.get(from);
|
|
445
|
+
if (a) {
|
|
446
|
+
this.addComment(to, ...a);
|
|
447
|
+
this.comments.delete(from);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
function compileAst(schema, options = {}) {
|
|
453
|
+
var _options$name;
|
|
454
|
+
const ctx = context(options);
|
|
455
|
+
registerDeclaration((_options$name = options.name) !== null && _options$name !== void 0 ? _options$name : "NoName", schema, ctx, inline(schema, ctx));
|
|
456
|
+
if (typeof schema === "object" && options.unreachableDefinitions && "definitions" in schema && schema.definitions) for (const [k, v] of Object.entries(schema.definitions)) {
|
|
457
|
+
if (typeof v === "object" && ctx.getSchemaDeclaration(v)) continue;
|
|
458
|
+
registerDeclaration(k, v, ctx, inline(v, ctx));
|
|
459
|
+
}
|
|
460
|
+
const body = [];
|
|
461
|
+
for (const [name, { schema, typeAnnotation }] of ctx.declarations) {
|
|
462
|
+
const node = {
|
|
463
|
+
type: import_dist.AST_NODE_TYPES.ExportNamedDeclaration,
|
|
464
|
+
declaration: createDeclaration(name, schema, typeAnnotation),
|
|
465
|
+
exportKind: "type"
|
|
466
|
+
};
|
|
467
|
+
ctx.transferComment(typeAnnotation, node);
|
|
468
|
+
body.push(node);
|
|
469
|
+
}
|
|
470
|
+
return {
|
|
471
|
+
program: {
|
|
472
|
+
type: import_dist.AST_NODE_TYPES.Program,
|
|
473
|
+
body,
|
|
474
|
+
sourceType: "module"
|
|
475
|
+
},
|
|
476
|
+
ctx
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
function compile(schema, options = {}) {
|
|
480
|
+
const { program, ctx } = compileAst(schema, options);
|
|
481
|
+
const visitor = ts({ getLeadingComments(node) {
|
|
482
|
+
var _ctx$comments$get;
|
|
483
|
+
return (_ctx$comments$get = ctx.comments.get(node)) === null || _ctx$comments$get === void 0 ? void 0 : _ctx$comments$get.map(formatComment);
|
|
484
|
+
} });
|
|
485
|
+
Object.assign(visitor, { "use-raw"(node, ctx) {
|
|
486
|
+
if ("raw" in node && typeof node.raw === "string") ctx.write(node.raw);
|
|
487
|
+
} });
|
|
488
|
+
return print(program, visitor).code;
|
|
489
|
+
}
|
|
490
|
+
function formatComment(c) {
|
|
491
|
+
if (c.type === "Block") {
|
|
492
|
+
const lines = c.value.trim().split("\n").map((v) => `* ${v}`);
|
|
493
|
+
return _objectSpread2(_objectSpread2({}, c), {}, { value: `*\n${lines.join("\n")}\n` });
|
|
494
|
+
}
|
|
495
|
+
return c;
|
|
496
|
+
}
|
|
497
|
+
function base(t) {
|
|
498
|
+
return t;
|
|
499
|
+
}
|
|
500
|
+
function registerDeclaration(preferredName, schema, ctx, typeAnnotation) {
|
|
501
|
+
var _preferredName2;
|
|
502
|
+
if (typeof schema !== "boolean") {
|
|
503
|
+
var _preferredName, _ref, _schema$title;
|
|
504
|
+
(_preferredName = preferredName) !== null && _preferredName !== void 0 || (preferredName = (_ref = (_schema$title = schema.title) !== null && _schema$title !== void 0 ? _schema$title : schema.$id) !== null && _ref !== void 0 ? _ref : ctx.getSchemaId(schema));
|
|
505
|
+
}
|
|
506
|
+
let name = toSafeString((_preferredName2 = preferredName) !== null && _preferredName2 !== void 0 ? _preferredName2 : "NoName");
|
|
507
|
+
const originalName = name;
|
|
508
|
+
for (let i = 1; ctx.declarations.has(name); i++) name = `${originalName}${i}`;
|
|
509
|
+
ctx.declarations.set(name, {
|
|
510
|
+
schema,
|
|
511
|
+
typeAnnotation
|
|
512
|
+
});
|
|
513
|
+
return name;
|
|
514
|
+
}
|
|
515
|
+
function createDeclaration(name, _schema, typeAnnotation) {
|
|
516
|
+
if (typeAnnotation.type === import_dist.AST_NODE_TYPES.TSTypeLiteral) return base({
|
|
517
|
+
type: import_dist.AST_NODE_TYPES.TSInterfaceDeclaration,
|
|
518
|
+
declare: false,
|
|
519
|
+
extends: [],
|
|
520
|
+
typeParameters: void 0,
|
|
521
|
+
id: identifier(name),
|
|
522
|
+
body: base({
|
|
523
|
+
type: import_dist.AST_NODE_TYPES.TSInterfaceBody,
|
|
524
|
+
body: typeAnnotation.members
|
|
525
|
+
})
|
|
526
|
+
});
|
|
527
|
+
else return {
|
|
528
|
+
type: import_dist.AST_NODE_TYPES.TSTypeAliasDeclaration,
|
|
529
|
+
id: identifier(name),
|
|
530
|
+
typeAnnotation
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
function inline(schema, ctx, skipCache = false) {
|
|
534
|
+
var _schema$oneOf;
|
|
535
|
+
if (typeof schema === "boolean") return schema ? base({ type: import_dist.AST_NODE_TYPES.TSAnyKeyword }) : base({ type: import_dist.AST_NODE_TYPES.TSNeverKeyword });
|
|
536
|
+
if (!skipCache) {
|
|
537
|
+
const createdDeclaration = ctx.getSchemaDeclaration(schema);
|
|
538
|
+
if (createdDeclaration) return {
|
|
539
|
+
type: import_dist.AST_NODE_TYPES.TSTypeReference,
|
|
540
|
+
typeName: identifier(createdDeclaration)
|
|
541
|
+
};
|
|
542
|
+
const cached = ctx.inlineResults.get(schema);
|
|
543
|
+
if (cached !== void 0 && ctx.isPending(cached)) {
|
|
544
|
+
const createdId = registerDeclaration(null, schema, ctx, cached);
|
|
545
|
+
return {
|
|
546
|
+
type: import_dist.AST_NODE_TYPES.TSTypeReference,
|
|
547
|
+
typeName: identifier(createdId)
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
if (cached !== void 0) return cached;
|
|
551
|
+
if (ctx.shouldSeparateDeclaration ? ctx.shouldSeparateDeclaration(schema) : ctx.getSchemaId(schema) !== void 0) {
|
|
552
|
+
const pending = ctx.createPending();
|
|
553
|
+
ctx.inlineResults.set(schema, pending);
|
|
554
|
+
const createdId = registerDeclaration(null, schema, ctx, pending);
|
|
555
|
+
ctx.resolvePending(pending, inline(schema, ctx, true));
|
|
556
|
+
inlineComment(pending, schema, ctx);
|
|
557
|
+
return {
|
|
558
|
+
type: import_dist.AST_NODE_TYPES.TSTypeReference,
|
|
559
|
+
typeName: identifier(createdId)
|
|
560
|
+
};
|
|
561
|
+
} else {
|
|
562
|
+
const pending = ctx.createPending();
|
|
563
|
+
ctx.inlineResults.set(schema, pending);
|
|
564
|
+
ctx.resolvePending(pending, inline(schema, ctx, true));
|
|
565
|
+
inlineComment(pending, schema, ctx);
|
|
566
|
+
return pending;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
if ("tsType" in schema && typeof schema.tsType === "string") return {
|
|
570
|
+
type: "use-raw",
|
|
571
|
+
raw: schema.tsType
|
|
572
|
+
};
|
|
573
|
+
if (schema.not) return base({
|
|
574
|
+
type: import_dist.AST_NODE_TYPES.TSTypeReference,
|
|
575
|
+
typeName: identifier("Exclude"),
|
|
576
|
+
typeArguments: base({
|
|
577
|
+
type: import_dist.AST_NODE_TYPES.TSTypeParameterInstantiation,
|
|
578
|
+
params: [base({ type: import_dist.AST_NODE_TYPES.TSUnknownKeyword }), inline(schema.not, ctx)]
|
|
579
|
+
})
|
|
580
|
+
});
|
|
581
|
+
let union = (_schema$oneOf = schema.oneOf) !== null && _schema$oneOf !== void 0 ? _schema$oneOf : schema.anyOf;
|
|
582
|
+
if (union) return simplifyUnion({
|
|
583
|
+
type: import_dist.AST_NODE_TYPES.TSUnionType,
|
|
584
|
+
types: union.map((member) => inline(member, ctx))
|
|
585
|
+
});
|
|
586
|
+
if (schema.allOf) {
|
|
587
|
+
const out = base({
|
|
588
|
+
type: import_dist.AST_NODE_TYPES.TSIntersectionType,
|
|
589
|
+
types: []
|
|
590
|
+
});
|
|
591
|
+
for (const member of schema.allOf) out.types.push(inline(member, ctx));
|
|
592
|
+
return out;
|
|
593
|
+
}
|
|
594
|
+
if (schema.enum) {
|
|
595
|
+
const types = [];
|
|
596
|
+
for (const member of schema.enum) types.push(base({
|
|
597
|
+
type: import_dist.AST_NODE_TYPES.TSLiteralType,
|
|
598
|
+
literal: {
|
|
599
|
+
type: import_dist.AST_NODE_TYPES.Literal,
|
|
600
|
+
value: member
|
|
601
|
+
}
|
|
602
|
+
}));
|
|
603
|
+
return simplifyUnion({
|
|
604
|
+
type: import_dist.AST_NODE_TYPES.TSUnionType,
|
|
605
|
+
types
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
if (schema.const) return base({
|
|
609
|
+
type: import_dist.AST_NODE_TYPES.TSLiteralType,
|
|
610
|
+
literal: {
|
|
611
|
+
type: import_dist.AST_NODE_TYPES.Literal,
|
|
612
|
+
value: schema.const
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
if (Array.isArray(schema.type)) return simplifyUnion({
|
|
616
|
+
type: import_dist.AST_NODE_TYPES.TSUnionType,
|
|
617
|
+
types: schema.type.map((v) => inline(_objectSpread2(_objectSpread2({}, schema), {}, { type: v }), ctx))
|
|
618
|
+
});
|
|
619
|
+
let actualType;
|
|
620
|
+
if (schema.type) actualType = schema.type;
|
|
621
|
+
else if (schema.properties || schema.additionalProperties || schema.patternProperties) actualType = "object";
|
|
622
|
+
else if (schema.items) actualType = "array";
|
|
623
|
+
switch (actualType) {
|
|
624
|
+
case "null": return base({ type: import_dist.AST_NODE_TYPES.TSNullKeyword });
|
|
625
|
+
case "boolean": return base({ type: import_dist.AST_NODE_TYPES.TSBooleanKeyword });
|
|
626
|
+
case "number":
|
|
627
|
+
case "integer": return base({ type: import_dist.AST_NODE_TYPES.TSNumberKeyword });
|
|
628
|
+
case "string": return base({ type: import_dist.AST_NODE_TYPES.TSStringKeyword });
|
|
629
|
+
case "object": {
|
|
630
|
+
let cur = [];
|
|
631
|
+
if (schema.properties) {
|
|
632
|
+
const required = new Set(schema.required);
|
|
633
|
+
cur.push(base({
|
|
634
|
+
type: import_dist.AST_NODE_TYPES.TSTypeLiteral,
|
|
635
|
+
members: Object.entries(schema.properties).map(([k, v]) => {
|
|
636
|
+
const child = inline(v, ctx);
|
|
637
|
+
const prop = {
|
|
638
|
+
type: import_dist.AST_NODE_TYPES.TSPropertySignature,
|
|
639
|
+
key: isValidPropertyName(k) ? identifier(k) : stringLiteral(k),
|
|
640
|
+
optional: !required.has(k),
|
|
641
|
+
typeAnnotation: base({
|
|
642
|
+
type: import_dist.AST_NODE_TYPES.TSTypeAnnotation,
|
|
643
|
+
typeAnnotation: child
|
|
644
|
+
})
|
|
645
|
+
};
|
|
646
|
+
ctx.transferComment(child, prop);
|
|
647
|
+
return prop;
|
|
648
|
+
})
|
|
649
|
+
}));
|
|
650
|
+
}
|
|
651
|
+
const fallbackTypes = [];
|
|
652
|
+
if (schema.patternProperties) for (const value of Object.values(schema.patternProperties)) fallbackTypes.push(inline(value, ctx));
|
|
653
|
+
if (schema.additionalProperties) fallbackTypes.push(inline(schema.additionalProperties, ctx));
|
|
654
|
+
if (fallbackTypes.length > 0) cur.push(base({
|
|
655
|
+
type: import_dist.AST_NODE_TYPES.TSTypeReference,
|
|
656
|
+
typeName: identifier("Record"),
|
|
657
|
+
typeArguments: base({
|
|
658
|
+
type: import_dist.AST_NODE_TYPES.TSTypeParameterInstantiation,
|
|
659
|
+
params: [base({ type: import_dist.AST_NODE_TYPES.TSStringKeyword }), simplifyUnion({
|
|
660
|
+
type: import_dist.AST_NODE_TYPES.TSUnionType,
|
|
661
|
+
types: fallbackTypes
|
|
662
|
+
})]
|
|
663
|
+
})
|
|
664
|
+
}));
|
|
665
|
+
if (cur.length === 0) return base({
|
|
666
|
+
type: import_dist.AST_NODE_TYPES.TSTypeLiteral,
|
|
667
|
+
members: []
|
|
668
|
+
});
|
|
669
|
+
if (cur.length === 1) return cur[0];
|
|
670
|
+
return base({
|
|
671
|
+
type: import_dist.AST_NODE_TYPES.TSIntersectionType,
|
|
672
|
+
types: cur
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
case "array":
|
|
676
|
+
if (Array.isArray(schema.items)) return base({
|
|
677
|
+
type: import_dist.AST_NODE_TYPES.TSTupleType,
|
|
678
|
+
elementTypes: schema.items.map((item) => base({
|
|
679
|
+
type: import_dist.AST_NODE_TYPES.TSOptionalType,
|
|
680
|
+
typeAnnotation: parenthesizeIfNeeded(inline(item, ctx))
|
|
681
|
+
}))
|
|
682
|
+
});
|
|
683
|
+
return base({
|
|
684
|
+
type: import_dist.AST_NODE_TYPES.TSArrayType,
|
|
685
|
+
elementType: schema.items ? parenthesizeIfNeeded(inline(schema.items, ctx)) : base({ type: import_dist.AST_NODE_TYPES.TSAnyKeyword })
|
|
686
|
+
});
|
|
687
|
+
case "any": return base({ type: import_dist.AST_NODE_TYPES.TSAnyKeyword });
|
|
688
|
+
}
|
|
689
|
+
if ("nullable" in schema && schema.nullable === true) return base({ type: import_dist.AST_NODE_TYPES.TSNullKeyword });
|
|
690
|
+
if (schema.required) return base({
|
|
691
|
+
type: import_dist.AST_NODE_TYPES.TSTypeReference,
|
|
692
|
+
typeName: identifier("Record"),
|
|
693
|
+
typeArguments: base({
|
|
694
|
+
type: import_dist.AST_NODE_TYPES.TSTypeParameterInstantiation,
|
|
695
|
+
params: [simplifyUnion({
|
|
696
|
+
type: import_dist.AST_NODE_TYPES.TSUnionType,
|
|
697
|
+
types: schema.required.map((key) => base({
|
|
698
|
+
type: import_dist.AST_NODE_TYPES.TSLiteralType,
|
|
699
|
+
literal: stringLiteral(key)
|
|
700
|
+
}))
|
|
701
|
+
}), base({ type: import_dist.AST_NODE_TYPES.TSUnknownKeyword })]
|
|
702
|
+
})
|
|
703
|
+
});
|
|
704
|
+
return base({ type: import_dist.AST_NODE_TYPES.TSUnknownKeyword });
|
|
705
|
+
}
|
|
706
|
+
function inlineComment(node, schema, ctx) {
|
|
707
|
+
if (typeof schema === "boolean") return;
|
|
708
|
+
if (schema.description) ctx.addComment(node, {
|
|
709
|
+
type: "Block",
|
|
710
|
+
value: schema.description
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
function identifier(key) {
|
|
714
|
+
return {
|
|
715
|
+
type: import_dist.AST_NODE_TYPES.Identifier,
|
|
716
|
+
name: key
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
function stringLiteral(value) {
|
|
720
|
+
return {
|
|
721
|
+
type: import_dist.AST_NODE_TYPES.Literal,
|
|
722
|
+
value
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
function simplifyUnion(t) {
|
|
726
|
+
if (t.types.length === 1) return t.types[0];
|
|
727
|
+
return t;
|
|
728
|
+
}
|
|
729
|
+
function parenthesizeIfNeeded(node) {
|
|
730
|
+
if (node.type === import_dist.AST_NODE_TYPES.TSUnionType || node.type === import_dist.AST_NODE_TYPES.TSIntersectionType) return {
|
|
731
|
+
type: "TSParenthesizedType",
|
|
732
|
+
typeAnnotation: node
|
|
733
|
+
};
|
|
734
|
+
return node;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
//#endregion
|
|
738
|
+
export { compile };
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fumari/json-schema-ts",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "compile json schema to typescript typings",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20.0.0"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"./dist"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/fuma-nama/json-schema-to-typescript.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"json",
|
|
18
|
+
"schema",
|
|
19
|
+
"typescript",
|
|
20
|
+
"compile",
|
|
21
|
+
"transpile",
|
|
22
|
+
"api",
|
|
23
|
+
"interface",
|
|
24
|
+
"typing",
|
|
25
|
+
"share"
|
|
26
|
+
],
|
|
27
|
+
"author": "Fuma Nama",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"esrap": "^2.2.3"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@apidevtools/json-schema-ref-parser": "^15.2.2",
|
|
34
|
+
"@types/js-yaml": "^4.0.9",
|
|
35
|
+
"@types/node": "^25.3.2",
|
|
36
|
+
"@typescript-eslint/types": "^8.56.1",
|
|
37
|
+
"js-yaml": "^4.1.1",
|
|
38
|
+
"json-schema-typed": "^8.0.2",
|
|
39
|
+
"tsdown": "0.21.0-beta.2"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"json-schema-typed": "^8.0.2"
|
|
43
|
+
},
|
|
44
|
+
"peerDependenciesMeta": {
|
|
45
|
+
"json-schema-typed": {
|
|
46
|
+
"optional": true
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"exports": {
|
|
53
|
+
".": "./dist/index.mjs",
|
|
54
|
+
"./package.json": "./package.json"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsdown",
|
|
58
|
+
"lint": "eslint src/**/*.ts",
|
|
59
|
+
"dev": "tsdown --watch"
|
|
60
|
+
}
|
|
61
|
+
}
|