@twin.org/ts-to-schema 0.0.1 → 0.0.2-next.1
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/dist/cjs/index.cjs
CHANGED
|
@@ -5,12 +5,12 @@ var node_url = require('node:url');
|
|
|
5
5
|
var cliCore = require('@twin.org/cli-core');
|
|
6
6
|
var promises = require('node:fs/promises');
|
|
7
7
|
var core = require('@twin.org/core');
|
|
8
|
+
var toolsCore = require('@twin.org/tools-core');
|
|
8
9
|
var tsJsonSchemaGenerator = require('ts-json-schema-generator');
|
|
9
10
|
|
|
10
11
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
11
12
|
// Copyright 2024 IOTA Stiftung.
|
|
12
13
|
// SPDX-License-Identifier: Apache-2.0.
|
|
13
|
-
const SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
14
14
|
/**
|
|
15
15
|
* Build the root command to be consumed by the CLI.
|
|
16
16
|
* @param program The command to build on.
|
|
@@ -89,7 +89,14 @@ async function tsToSchema(config, outputFolder, workingDirectory) {
|
|
|
89
89
|
}
|
|
90
90
|
else {
|
|
91
91
|
cliCore.CLIDisplay.task(core.I18n.formatMessage("commands.ts-to-schema.progress.generatingSchema"));
|
|
92
|
-
const
|
|
92
|
+
const autoExpandTypes = config.autoExpandTypes ?? [];
|
|
93
|
+
const defaultExpandTypes = ["ObjectOrArray<.*>"];
|
|
94
|
+
for (const defaultType of defaultExpandTypes) {
|
|
95
|
+
if (!autoExpandTypes.includes(defaultType)) {
|
|
96
|
+
autoExpandTypes.push(defaultType);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const schemas = await generateSchemas(typeSource, type, autoExpandTypes, workingDirectory);
|
|
93
100
|
if (core.Is.empty(schemas[type])) {
|
|
94
101
|
throw new core.GeneralError("commands", "commands.ts-to-schema.schemaNotFound", { type });
|
|
95
102
|
}
|
|
@@ -115,11 +122,12 @@ async function tsToSchema(config, outputFolder, workingDirectory) {
|
|
|
115
122
|
* Generate schemas for the models.
|
|
116
123
|
* @param modelDirWildcards The filenames for all the models.
|
|
117
124
|
* @param types The types of the schema objects.
|
|
125
|
+
* @param autoExpandTypes The types to automatically expand.
|
|
118
126
|
* @param outputWorkingDir The working directory.
|
|
119
127
|
* @returns Nothing.
|
|
120
128
|
* @internal
|
|
121
129
|
*/
|
|
122
|
-
async function generateSchemas(typeSource, type, outputWorkingDir) {
|
|
130
|
+
async function generateSchemas(typeSource, type, autoExpandTypes, outputWorkingDir) {
|
|
123
131
|
const allSchemas = {};
|
|
124
132
|
cliCore.CLIDisplay.value(core.I18n.formatMessage("commands.ts-to-schema.progress.models"), typeSource, 1);
|
|
125
133
|
const generator = tsJsonSchemaGenerator.createGenerator({
|
|
@@ -132,82 +140,15 @@ async function generateSchemas(typeSource, type, outputWorkingDir) {
|
|
|
132
140
|
const schema = generator.createSchema("*");
|
|
133
141
|
if (schema.definitions) {
|
|
134
142
|
for (const def in schema.definitions) {
|
|
135
|
-
const defSub = normaliseTypeName(def);
|
|
143
|
+
const defSub = toolsCore.JsonSchemaHelper.normaliseTypeName(def);
|
|
136
144
|
allSchemas[defSub] = schema.definitions[def];
|
|
137
145
|
}
|
|
138
146
|
}
|
|
139
147
|
const referencedSchemas = {};
|
|
140
|
-
extractTypes(allSchemas, [type], referencedSchemas);
|
|
148
|
+
toolsCore.JsonSchemaHelper.extractTypes(allSchemas, [type, ...autoExpandTypes], referencedSchemas);
|
|
149
|
+
toolsCore.JsonSchemaHelper.expandTypes(referencedSchemas, autoExpandTypes);
|
|
141
150
|
return referencedSchemas;
|
|
142
151
|
}
|
|
143
|
-
/**
|
|
144
|
-
* Extract the required types from all the known schemas.
|
|
145
|
-
* @param allSchemas All the known schemas.
|
|
146
|
-
* @param requiredTypes The required types.
|
|
147
|
-
* @param referencedSchemas The references schemas.
|
|
148
|
-
* @internal
|
|
149
|
-
*/
|
|
150
|
-
function extractTypes(allSchemas, requiredTypes, referencedSchemas) {
|
|
151
|
-
for (const type of requiredTypes) {
|
|
152
|
-
if (allSchemas[type] && !referencedSchemas[type]) {
|
|
153
|
-
referencedSchemas[type] = allSchemas[type];
|
|
154
|
-
extractTypesFromSchema(allSchemas, allSchemas[type], referencedSchemas);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Extract type from properties definition.
|
|
160
|
-
* @param allTypes All the known types.
|
|
161
|
-
* @param schema The schema to extract from.
|
|
162
|
-
* @param output The output types.
|
|
163
|
-
* @internal
|
|
164
|
-
*/
|
|
165
|
-
function extractTypesFromSchema(allTypes, schema, output) {
|
|
166
|
-
const additionalTypes = [];
|
|
167
|
-
if (core.Is.stringValue(schema.$ref)) {
|
|
168
|
-
additionalTypes.push(schema.$ref.replace("#/definitions/", "").replace(/^Partial%3C(.*?)%3E/g, "$1"));
|
|
169
|
-
}
|
|
170
|
-
else if (core.Is.object(schema.items)) {
|
|
171
|
-
if (core.Is.arrayValue(schema.items)) {
|
|
172
|
-
for (const itemSchema of schema.items) {
|
|
173
|
-
extractTypesFromSchema(allTypes, itemSchema, output);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
extractTypesFromSchema(allTypes, schema.items, output);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
else if (core.Is.object(schema.properties) || core.Is.object(schema.additionalProperties)) {
|
|
181
|
-
if (core.Is.object(schema.properties)) {
|
|
182
|
-
for (const prop in schema.properties) {
|
|
183
|
-
const p = schema.properties[prop];
|
|
184
|
-
if (core.Is.object(p)) {
|
|
185
|
-
extractTypesFromSchema(allTypes, p, output);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
if (core.Is.object(schema.additionalProperties)) {
|
|
190
|
-
extractTypesFromSchema(allTypes, schema.additionalProperties, output);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
else if (core.Is.arrayValue(schema.anyOf)) {
|
|
194
|
-
for (const prop of schema.anyOf) {
|
|
195
|
-
if (core.Is.object(prop)) {
|
|
196
|
-
extractTypesFromSchema(allTypes, prop, output);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
else if (core.Is.arrayValue(schema.oneOf)) {
|
|
201
|
-
for (const prop of schema.oneOf) {
|
|
202
|
-
if (core.Is.object(prop)) {
|
|
203
|
-
extractTypesFromSchema(allTypes, prop, output);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (additionalTypes.length > 0) {
|
|
208
|
-
extractTypes(allTypes, additionalTypes, output);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
152
|
/**
|
|
212
153
|
* Process the schema object to ensure it has the correct properties.
|
|
213
154
|
* @param schemaObject The schema object to process.
|
|
@@ -216,88 +157,15 @@ function extractTypesFromSchema(allTypes, schema, output) {
|
|
|
216
157
|
* @returns The finalised schema object.
|
|
217
158
|
*/
|
|
218
159
|
function finaliseSchema(schemaObject, baseUrl, type) {
|
|
219
|
-
processArrays(schemaObject);
|
|
160
|
+
toolsCore.JsonSchemaHelper.processArrays(schemaObject);
|
|
220
161
|
const { description, ...rest } = schemaObject;
|
|
221
162
|
return {
|
|
222
|
-
$schema: SCHEMA_VERSION,
|
|
163
|
+
$schema: toolsCore.JsonSchemaHelper.SCHEMA_VERSION,
|
|
223
164
|
$id: `${baseUrl}${core.StringHelper.stripPrefix(type)}`,
|
|
224
165
|
description,
|
|
225
166
|
...rest
|
|
226
167
|
};
|
|
227
168
|
}
|
|
228
|
-
/**
|
|
229
|
-
* Process arrays in the schema object.
|
|
230
|
-
* @param schemaObject The schema object to process.
|
|
231
|
-
*/
|
|
232
|
-
function processArrays(schemaObject) {
|
|
233
|
-
if (core.Is.object(schemaObject)) {
|
|
234
|
-
// latest specs have singular items in `items` property
|
|
235
|
-
// and multiple items in prefixItems, so update the schema accordingly
|
|
236
|
-
// https://www.learnjsonschema.com/2020-12/applicator/items/
|
|
237
|
-
// https://www.learnjsonschema.com/2020-12/applicator/prefixitems/
|
|
238
|
-
const schemaItems = schemaObject.items;
|
|
239
|
-
if (core.Is.array(schemaItems) || core.Is.object(schemaItems)) {
|
|
240
|
-
schemaObject.prefixItems = core.ArrayHelper.fromObjectOrArray(schemaItems);
|
|
241
|
-
schemaObject.items = false;
|
|
242
|
-
}
|
|
243
|
-
const additionalItems = schemaObject.additionalItems;
|
|
244
|
-
if (core.Is.array(additionalItems) || core.Is.object(additionalItems)) {
|
|
245
|
-
schemaObject.items = core.ArrayHelper.fromObjectOrArray(additionalItems)[0];
|
|
246
|
-
delete schemaObject.additionalItems;
|
|
247
|
-
}
|
|
248
|
-
processSchemaDictionary(schemaObject.properties);
|
|
249
|
-
processArrays(schemaObject.additionalProperties);
|
|
250
|
-
processSchemaArray(schemaObject.allOf);
|
|
251
|
-
processSchemaArray(schemaObject.anyOf);
|
|
252
|
-
processSchemaArray(schemaObject.oneOf);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Process arrays in the schema object.
|
|
257
|
-
* @param schemaDictionary The schema object to process.
|
|
258
|
-
*/
|
|
259
|
-
function processSchemaDictionary(schemaDictionary) {
|
|
260
|
-
if (core.Is.object(schemaDictionary)) {
|
|
261
|
-
for (const item of Object.values(schemaDictionary)) {
|
|
262
|
-
if (core.Is.object(item)) {
|
|
263
|
-
processArrays(item);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Process arrays in the schema object.
|
|
270
|
-
* @param schemaArray The schema object to process.
|
|
271
|
-
*/
|
|
272
|
-
function processSchemaArray(schemaArray) {
|
|
273
|
-
if (core.Is.arrayValue(schemaArray)) {
|
|
274
|
-
for (const item of schemaArray) {
|
|
275
|
-
if (core.Is.object(item)) {
|
|
276
|
-
processArrays(item);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Cleanup TypeScript markers from the type name.
|
|
283
|
-
* @param typeName The definition string to clean up.
|
|
284
|
-
* @returns The cleaned up definition string.
|
|
285
|
-
*/
|
|
286
|
-
function normaliseTypeName(typeName) {
|
|
287
|
-
// Remove the partial markers
|
|
288
|
-
let sTypeName = typeName.replace(/^Partial<(.*?)>/g, "$1");
|
|
289
|
-
sTypeName = sTypeName.replace(/Partial%3CI(.*?)%3E/g, "$1");
|
|
290
|
-
// Remove the omit markers
|
|
291
|
-
sTypeName = sTypeName.replace(/^Omit<(.*?),.*>/g, "$1");
|
|
292
|
-
sTypeName = sTypeName.replace(/Omit%3CI(.*?)%2C.*%3E/g, "$1");
|
|
293
|
-
// Remove the pick markers
|
|
294
|
-
sTypeName = sTypeName.replace(/^Pick<(.*?),.*>/g, "$1");
|
|
295
|
-
sTypeName = sTypeName.replace(/Pick%3CI(.*?)%2C.*%3E/g, "$1");
|
|
296
|
-
// Cleanup the generic markers
|
|
297
|
-
sTypeName = sTypeName.replace(/</g, "%3C").replace(/>/g, "%3E");
|
|
298
|
-
sTypeName = sTypeName.replace(/%3Cunknown%3E/g, "");
|
|
299
|
-
return sTypeName;
|
|
300
|
-
}
|
|
301
169
|
|
|
302
170
|
// Copyright 2024 IOTA Stiftung.
|
|
303
171
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -317,7 +185,7 @@ class CLI extends cliCore.CLIBase {
|
|
|
317
185
|
return this.execute({
|
|
318
186
|
title: "TWIN TypeScript To Schema",
|
|
319
187
|
appName: "ts-to-schema",
|
|
320
|
-
version: "0.0.1", // x-release-please-version
|
|
188
|
+
version: "0.0.2-next.1", // x-release-please-version
|
|
321
189
|
icon: "⚙️ ",
|
|
322
190
|
supportsEnvFiles: false,
|
|
323
191
|
overrideOutputWidth: options?.overrideOutputWidth
|
package/dist/esm/index.mjs
CHANGED
|
@@ -2,12 +2,12 @@ import path from 'node:path';
|
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
3
|
import { CLIDisplay, CLIUtils, CLIBase } from '@twin.org/cli-core';
|
|
4
4
|
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
|
5
|
-
import { I18n, GeneralError, Is, StringHelper
|
|
5
|
+
import { I18n, GeneralError, Is, StringHelper } from '@twin.org/core';
|
|
6
|
+
import { JsonSchemaHelper } from '@twin.org/tools-core';
|
|
6
7
|
import { createGenerator } from 'ts-json-schema-generator';
|
|
7
8
|
|
|
8
9
|
// Copyright 2024 IOTA Stiftung.
|
|
9
10
|
// SPDX-License-Identifier: Apache-2.0.
|
|
10
|
-
const SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
11
11
|
/**
|
|
12
12
|
* Build the root command to be consumed by the CLI.
|
|
13
13
|
* @param program The command to build on.
|
|
@@ -86,7 +86,14 @@ async function tsToSchema(config, outputFolder, workingDirectory) {
|
|
|
86
86
|
}
|
|
87
87
|
else {
|
|
88
88
|
CLIDisplay.task(I18n.formatMessage("commands.ts-to-schema.progress.generatingSchema"));
|
|
89
|
-
const
|
|
89
|
+
const autoExpandTypes = config.autoExpandTypes ?? [];
|
|
90
|
+
const defaultExpandTypes = ["ObjectOrArray<.*>"];
|
|
91
|
+
for (const defaultType of defaultExpandTypes) {
|
|
92
|
+
if (!autoExpandTypes.includes(defaultType)) {
|
|
93
|
+
autoExpandTypes.push(defaultType);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const schemas = await generateSchemas(typeSource, type, autoExpandTypes, workingDirectory);
|
|
90
97
|
if (Is.empty(schemas[type])) {
|
|
91
98
|
throw new GeneralError("commands", "commands.ts-to-schema.schemaNotFound", { type });
|
|
92
99
|
}
|
|
@@ -112,11 +119,12 @@ async function tsToSchema(config, outputFolder, workingDirectory) {
|
|
|
112
119
|
* Generate schemas for the models.
|
|
113
120
|
* @param modelDirWildcards The filenames for all the models.
|
|
114
121
|
* @param types The types of the schema objects.
|
|
122
|
+
* @param autoExpandTypes The types to automatically expand.
|
|
115
123
|
* @param outputWorkingDir The working directory.
|
|
116
124
|
* @returns Nothing.
|
|
117
125
|
* @internal
|
|
118
126
|
*/
|
|
119
|
-
async function generateSchemas(typeSource, type, outputWorkingDir) {
|
|
127
|
+
async function generateSchemas(typeSource, type, autoExpandTypes, outputWorkingDir) {
|
|
120
128
|
const allSchemas = {};
|
|
121
129
|
CLIDisplay.value(I18n.formatMessage("commands.ts-to-schema.progress.models"), typeSource, 1);
|
|
122
130
|
const generator = createGenerator({
|
|
@@ -129,82 +137,15 @@ async function generateSchemas(typeSource, type, outputWorkingDir) {
|
|
|
129
137
|
const schema = generator.createSchema("*");
|
|
130
138
|
if (schema.definitions) {
|
|
131
139
|
for (const def in schema.definitions) {
|
|
132
|
-
const defSub = normaliseTypeName(def);
|
|
140
|
+
const defSub = JsonSchemaHelper.normaliseTypeName(def);
|
|
133
141
|
allSchemas[defSub] = schema.definitions[def];
|
|
134
142
|
}
|
|
135
143
|
}
|
|
136
144
|
const referencedSchemas = {};
|
|
137
|
-
extractTypes(allSchemas, [type], referencedSchemas);
|
|
145
|
+
JsonSchemaHelper.extractTypes(allSchemas, [type, ...autoExpandTypes], referencedSchemas);
|
|
146
|
+
JsonSchemaHelper.expandTypes(referencedSchemas, autoExpandTypes);
|
|
138
147
|
return referencedSchemas;
|
|
139
148
|
}
|
|
140
|
-
/**
|
|
141
|
-
* Extract the required types from all the known schemas.
|
|
142
|
-
* @param allSchemas All the known schemas.
|
|
143
|
-
* @param requiredTypes The required types.
|
|
144
|
-
* @param referencedSchemas The references schemas.
|
|
145
|
-
* @internal
|
|
146
|
-
*/
|
|
147
|
-
function extractTypes(allSchemas, requiredTypes, referencedSchemas) {
|
|
148
|
-
for (const type of requiredTypes) {
|
|
149
|
-
if (allSchemas[type] && !referencedSchemas[type]) {
|
|
150
|
-
referencedSchemas[type] = allSchemas[type];
|
|
151
|
-
extractTypesFromSchema(allSchemas, allSchemas[type], referencedSchemas);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Extract type from properties definition.
|
|
157
|
-
* @param allTypes All the known types.
|
|
158
|
-
* @param schema The schema to extract from.
|
|
159
|
-
* @param output The output types.
|
|
160
|
-
* @internal
|
|
161
|
-
*/
|
|
162
|
-
function extractTypesFromSchema(allTypes, schema, output) {
|
|
163
|
-
const additionalTypes = [];
|
|
164
|
-
if (Is.stringValue(schema.$ref)) {
|
|
165
|
-
additionalTypes.push(schema.$ref.replace("#/definitions/", "").replace(/^Partial%3C(.*?)%3E/g, "$1"));
|
|
166
|
-
}
|
|
167
|
-
else if (Is.object(schema.items)) {
|
|
168
|
-
if (Is.arrayValue(schema.items)) {
|
|
169
|
-
for (const itemSchema of schema.items) {
|
|
170
|
-
extractTypesFromSchema(allTypes, itemSchema, output);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
extractTypesFromSchema(allTypes, schema.items, output);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {
|
|
178
|
-
if (Is.object(schema.properties)) {
|
|
179
|
-
for (const prop in schema.properties) {
|
|
180
|
-
const p = schema.properties[prop];
|
|
181
|
-
if (Is.object(p)) {
|
|
182
|
-
extractTypesFromSchema(allTypes, p, output);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
if (Is.object(schema.additionalProperties)) {
|
|
187
|
-
extractTypesFromSchema(allTypes, schema.additionalProperties, output);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
else if (Is.arrayValue(schema.anyOf)) {
|
|
191
|
-
for (const prop of schema.anyOf) {
|
|
192
|
-
if (Is.object(prop)) {
|
|
193
|
-
extractTypesFromSchema(allTypes, prop, output);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
else if (Is.arrayValue(schema.oneOf)) {
|
|
198
|
-
for (const prop of schema.oneOf) {
|
|
199
|
-
if (Is.object(prop)) {
|
|
200
|
-
extractTypesFromSchema(allTypes, prop, output);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
if (additionalTypes.length > 0) {
|
|
205
|
-
extractTypes(allTypes, additionalTypes, output);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
149
|
/**
|
|
209
150
|
* Process the schema object to ensure it has the correct properties.
|
|
210
151
|
* @param schemaObject The schema object to process.
|
|
@@ -213,88 +154,15 @@ function extractTypesFromSchema(allTypes, schema, output) {
|
|
|
213
154
|
* @returns The finalised schema object.
|
|
214
155
|
*/
|
|
215
156
|
function finaliseSchema(schemaObject, baseUrl, type) {
|
|
216
|
-
processArrays(schemaObject);
|
|
157
|
+
JsonSchemaHelper.processArrays(schemaObject);
|
|
217
158
|
const { description, ...rest } = schemaObject;
|
|
218
159
|
return {
|
|
219
|
-
$schema: SCHEMA_VERSION,
|
|
160
|
+
$schema: JsonSchemaHelper.SCHEMA_VERSION,
|
|
220
161
|
$id: `${baseUrl}${StringHelper.stripPrefix(type)}`,
|
|
221
162
|
description,
|
|
222
163
|
...rest
|
|
223
164
|
};
|
|
224
165
|
}
|
|
225
|
-
/**
|
|
226
|
-
* Process arrays in the schema object.
|
|
227
|
-
* @param schemaObject The schema object to process.
|
|
228
|
-
*/
|
|
229
|
-
function processArrays(schemaObject) {
|
|
230
|
-
if (Is.object(schemaObject)) {
|
|
231
|
-
// latest specs have singular items in `items` property
|
|
232
|
-
// and multiple items in prefixItems, so update the schema accordingly
|
|
233
|
-
// https://www.learnjsonschema.com/2020-12/applicator/items/
|
|
234
|
-
// https://www.learnjsonschema.com/2020-12/applicator/prefixitems/
|
|
235
|
-
const schemaItems = schemaObject.items;
|
|
236
|
-
if (Is.array(schemaItems) || Is.object(schemaItems)) {
|
|
237
|
-
schemaObject.prefixItems = ArrayHelper.fromObjectOrArray(schemaItems);
|
|
238
|
-
schemaObject.items = false;
|
|
239
|
-
}
|
|
240
|
-
const additionalItems = schemaObject.additionalItems;
|
|
241
|
-
if (Is.array(additionalItems) || Is.object(additionalItems)) {
|
|
242
|
-
schemaObject.items = ArrayHelper.fromObjectOrArray(additionalItems)[0];
|
|
243
|
-
delete schemaObject.additionalItems;
|
|
244
|
-
}
|
|
245
|
-
processSchemaDictionary(schemaObject.properties);
|
|
246
|
-
processArrays(schemaObject.additionalProperties);
|
|
247
|
-
processSchemaArray(schemaObject.allOf);
|
|
248
|
-
processSchemaArray(schemaObject.anyOf);
|
|
249
|
-
processSchemaArray(schemaObject.oneOf);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Process arrays in the schema object.
|
|
254
|
-
* @param schemaDictionary The schema object to process.
|
|
255
|
-
*/
|
|
256
|
-
function processSchemaDictionary(schemaDictionary) {
|
|
257
|
-
if (Is.object(schemaDictionary)) {
|
|
258
|
-
for (const item of Object.values(schemaDictionary)) {
|
|
259
|
-
if (Is.object(item)) {
|
|
260
|
-
processArrays(item);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Process arrays in the schema object.
|
|
267
|
-
* @param schemaArray The schema object to process.
|
|
268
|
-
*/
|
|
269
|
-
function processSchemaArray(schemaArray) {
|
|
270
|
-
if (Is.arrayValue(schemaArray)) {
|
|
271
|
-
for (const item of schemaArray) {
|
|
272
|
-
if (Is.object(item)) {
|
|
273
|
-
processArrays(item);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Cleanup TypeScript markers from the type name.
|
|
280
|
-
* @param typeName The definition string to clean up.
|
|
281
|
-
* @returns The cleaned up definition string.
|
|
282
|
-
*/
|
|
283
|
-
function normaliseTypeName(typeName) {
|
|
284
|
-
// Remove the partial markers
|
|
285
|
-
let sTypeName = typeName.replace(/^Partial<(.*?)>/g, "$1");
|
|
286
|
-
sTypeName = sTypeName.replace(/Partial%3CI(.*?)%3E/g, "$1");
|
|
287
|
-
// Remove the omit markers
|
|
288
|
-
sTypeName = sTypeName.replace(/^Omit<(.*?),.*>/g, "$1");
|
|
289
|
-
sTypeName = sTypeName.replace(/Omit%3CI(.*?)%2C.*%3E/g, "$1");
|
|
290
|
-
// Remove the pick markers
|
|
291
|
-
sTypeName = sTypeName.replace(/^Pick<(.*?),.*>/g, "$1");
|
|
292
|
-
sTypeName = sTypeName.replace(/Pick%3CI(.*?)%2C.*%3E/g, "$1");
|
|
293
|
-
// Cleanup the generic markers
|
|
294
|
-
sTypeName = sTypeName.replace(/</g, "%3C").replace(/>/g, "%3E");
|
|
295
|
-
sTypeName = sTypeName.replace(/%3Cunknown%3E/g, "");
|
|
296
|
-
return sTypeName;
|
|
297
|
-
}
|
|
298
166
|
|
|
299
167
|
// Copyright 2024 IOTA Stiftung.
|
|
300
168
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -314,7 +182,7 @@ class CLI extends CLIBase {
|
|
|
314
182
|
return this.execute({
|
|
315
183
|
title: "TWIN TypeScript To Schema",
|
|
316
184
|
appName: "ts-to-schema",
|
|
317
|
-
version: "0.0.1", // x-release-please-version
|
|
185
|
+
version: "0.0.2-next.1", // x-release-please-version
|
|
318
186
|
icon: "⚙️ ",
|
|
319
187
|
supportsEnvFiles: false,
|
|
320
188
|
overrideOutputWidth: options?.overrideOutputWidth
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IJsonSchema } from "
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-core";
|
|
2
2
|
/**
|
|
3
3
|
* Configuration for the tool.
|
|
4
4
|
*/
|
|
@@ -12,7 +12,7 @@ export interface ITsToSchemaConfig {
|
|
|
12
12
|
*/
|
|
13
13
|
types: string[];
|
|
14
14
|
/**
|
|
15
|
-
* External type references
|
|
15
|
+
* External type references.
|
|
16
16
|
*/
|
|
17
17
|
externalReferences?: {
|
|
18
18
|
[id: string]: string;
|
|
@@ -23,4 +23,8 @@ export interface ITsToSchemaConfig {
|
|
|
23
23
|
overrides?: {
|
|
24
24
|
[id: string]: IJsonSchema;
|
|
25
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* The types to automatically expand inline in type definitions, reg ex string matches.
|
|
28
|
+
*/
|
|
29
|
+
autoExpandTypes?: string[];
|
|
26
30
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# @twin.org/ts-to-schema - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.2-next.1](https://github.com/twinfoundation/tools/compare/ts-to-schema-v0.0.2-next.0...ts-to-schema-v0.0.2-next.1) (2025-07-14)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add support for auto expand types ([dd1e10a](https://github.com/twinfoundation/tools/commit/dd1e10a5b2fea6f80890ff6f3971f48e239cb4c1))
|
|
9
|
+
* add ts-to-schema overrides ([3c54504](https://github.com/twinfoundation/tools/commit/3c5450468eb998204a75576b7791a7ca4027da62))
|
|
10
|
+
* generate schemas as individual entities ([9f372ab](https://github.com/twinfoundation/tools/commit/9f372abdfc27aba93b303c7b214991919c0c18c3))
|
|
11
|
+
* improve schema type name normalisation ([1a18b26](https://github.com/twinfoundation/tools/commit/1a18b267d87e9179bda01b396b256c450ae2889e))
|
|
12
|
+
* move package to framework repo ([4490bda](https://github.com/twinfoundation/tools/commit/4490bda472d4dc8ddfe931e2fce81f3411de9ab3))
|
|
13
|
+
* use most recent JSON schema specs ([4598cbf](https://github.com/twinfoundation/tools/commit/4598cbf29f7b82dba4a9f3b19f81dfe66f5a6060))
|
|
14
|
+
* use shared store mechanism ([#31](https://github.com/twinfoundation/tools/issues/31)) ([d9fe68b](https://github.com/twinfoundation/tools/commit/d9fe68b903d1268c7cb3c64772df5cb78fd63667))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Bug Fixes
|
|
18
|
+
|
|
19
|
+
* remove debugging ([4def3d1](https://github.com/twinfoundation/tools/commit/4def3d1ef6a41a3b3358f864214e6a7ec3f9c638))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Dependencies
|
|
23
|
+
|
|
24
|
+
* The following workspace dependencies were updated
|
|
25
|
+
* dependencies
|
|
26
|
+
* @twin.org/tools-core bumped from 0.0.2-next.0 to 0.0.2-next.1
|
|
27
|
+
|
|
3
28
|
## 0.0.1 (2025-07-03)
|
|
4
29
|
|
|
5
30
|
|
|
@@ -24,7 +24,7 @@ The source files to generate the types from.
|
|
|
24
24
|
|
|
25
25
|
> `optional` **externalReferences**: `object`
|
|
26
26
|
|
|
27
|
-
External type references
|
|
27
|
+
External type references.
|
|
28
28
|
|
|
29
29
|
#### Index Signature
|
|
30
30
|
|
|
@@ -41,3 +41,11 @@ Override for specific types, to be used when the type cannot be generated automa
|
|
|
41
41
|
#### Index Signature
|
|
42
42
|
|
|
43
43
|
\[`id`: `string`\]: `AnySchemaObject`
|
|
44
|
+
|
|
45
|
+
***
|
|
46
|
+
|
|
47
|
+
### autoExpandTypes?
|
|
48
|
+
|
|
49
|
+
> `optional` **autoExpandTypes**: `string`[]
|
|
50
|
+
|
|
51
|
+
The types to automatically expand inline in type definitions, reg ex string matches.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/ts-to-schema",
|
|
3
|
-
"version": "0.0.1",
|
|
3
|
+
"version": "0.0.2-next.1",
|
|
4
4
|
"description": "Tool to convert TypeScript definitions to JSON schemas",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/cli-core": "
|
|
18
|
-
"@twin.org/core": "
|
|
19
|
-
"@twin.org/nameof": "
|
|
20
|
-
"
|
|
17
|
+
"@twin.org/cli-core": "next",
|
|
18
|
+
"@twin.org/core": "next",
|
|
19
|
+
"@twin.org/nameof": "next",
|
|
20
|
+
"@twin.org/tools-core": "0.0.2-next.1",
|
|
21
21
|
"commander": "14.0.0",
|
|
22
22
|
"glob": "11.0.2",
|
|
23
23
|
"ts-json-schema-generator": "2.4.0"
|