@twin.org/ts-to-openapi 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 +22 -148
- package/dist/esm/index.mjs +23 -149
- package/dist/locales/en.json +1 -0
- package/dist/types/models/IOpenApi.d.ts +1 -1
- package/dist/types/models/IOpenApiPathMethod.d.ts +1 -2
- package/dist/types/models/ITsToOpenApiConfig.d.ts +11 -0
- package/docs/changelog.md +27 -0
- package/docs/reference/interfaces/ITsToOpenApiConfig.md +20 -0
- package/locales/en.json +1 -0
- package/package.json +7 -7
- package/dist/types/models/IJsonSchema.d.ts +0 -5
- package/dist/types/models/IPackageJson.d.ts +0 -15
- package/dist/types/models/jsonTypeName.d.ts +0 -5
package/dist/cjs/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ 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 web = require('@twin.org/web');
|
|
9
10
|
var tsJsonSchemaGenerator = require('ts-json-schema-generator');
|
|
10
11
|
|
|
@@ -239,7 +240,20 @@ async function tsToOpenApi(config, outputFile, workingDirectory) {
|
|
|
239
240
|
}
|
|
240
241
|
}
|
|
241
242
|
cliCore.CLIDisplay.task(core.I18n.formatMessage("commands.ts-to-openapi.progress.generatingSchemas"));
|
|
242
|
-
const
|
|
243
|
+
const autoExpandTypes = config.autoExpandTypes ?? [];
|
|
244
|
+
const defaultExpandTypes = ["ObjectOrArray<.*>"];
|
|
245
|
+
for (const defaultType of defaultExpandTypes) {
|
|
246
|
+
if (!autoExpandTypes.includes(defaultType)) {
|
|
247
|
+
autoExpandTypes.push(defaultType);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const schemas = await generateSchemas(typeRoots, types, autoExpandTypes, workingDirectory);
|
|
251
|
+
for (const type in schemas) {
|
|
252
|
+
if (core.Is.object(config.overrides?.[type])) {
|
|
253
|
+
cliCore.CLIDisplay.task(core.I18n.formatMessage("commands.ts-to-openapi.progress.overridingSchema"));
|
|
254
|
+
schemas[type] = config.overrides?.[type];
|
|
255
|
+
}
|
|
256
|
+
}
|
|
243
257
|
const usedCommonResponseTypes = [];
|
|
244
258
|
for (let i = 0; i < inputResults.length; i++) {
|
|
245
259
|
const result = inputResults[i];
|
|
@@ -665,7 +679,7 @@ async function finaliseOutput(usedCommonResponseTypes, schemas, openApi, securit
|
|
|
665
679
|
delete finalSchemas[type];
|
|
666
680
|
}
|
|
667
681
|
for (const type in finalSchemas) {
|
|
668
|
-
processArrays(finalSchemas[type]);
|
|
682
|
+
toolsCore.JsonSchemaHelper.processArrays(finalSchemas[type]);
|
|
669
683
|
}
|
|
670
684
|
const schemaKeys = Object.keys(finalSchemas);
|
|
671
685
|
schemaKeys.sort();
|
|
@@ -698,7 +712,7 @@ async function finaliseOutput(usedCommonResponseTypes, schemas, openApi, securit
|
|
|
698
712
|
// Remove the array [] from the type names
|
|
699
713
|
// eslint-disable-next-line unicorn/better-regex
|
|
700
714
|
json = json.replace(/#\/components\/schemas\/(.*)\[\]/g, "#/components/schemas/ListOf$1");
|
|
701
|
-
json = normaliseTypeName(json);
|
|
715
|
+
json = toolsCore.JsonSchemaHelper.normaliseTypeName(json);
|
|
702
716
|
// Remove external references
|
|
703
717
|
for (const finalExternal in finalExternals) {
|
|
704
718
|
json = json.replace(new RegExp(`"#/components/schemas/${core.StringHelper.stripPrefix(finalExternal)}"`, "g"), `"${finalExternals[finalExternal]}"`);
|
|
@@ -828,7 +842,7 @@ async function processPackageRestDetails(restRoutes) {
|
|
|
828
842
|
* @returns Nothing.
|
|
829
843
|
* @internal
|
|
830
844
|
*/
|
|
831
|
-
async function generateSchemas(modelDirWildcards, types, outputWorkingDir) {
|
|
845
|
+
async function generateSchemas(modelDirWildcards, types, autoExpandTypes, outputWorkingDir) {
|
|
832
846
|
const allSchemas = {};
|
|
833
847
|
const arraySingularTypes = [];
|
|
834
848
|
for (const type of types) {
|
|
@@ -852,13 +866,14 @@ async function generateSchemas(modelDirWildcards, types, outputWorkingDir) {
|
|
|
852
866
|
const schema = generator.createSchema("*");
|
|
853
867
|
if (schema.definitions) {
|
|
854
868
|
for (const def in schema.definitions) {
|
|
855
|
-
const defSub = normaliseTypeName(def);
|
|
869
|
+
const defSub = toolsCore.JsonSchemaHelper.normaliseTypeName(def);
|
|
856
870
|
allSchemas[defSub] = schema.definitions[def];
|
|
857
871
|
}
|
|
858
872
|
}
|
|
859
873
|
}
|
|
860
874
|
const referencedSchemas = {};
|
|
861
|
-
extractTypes(allSchemas, types, referencedSchemas);
|
|
875
|
+
toolsCore.JsonSchemaHelper.extractTypes(allSchemas, [...types, ...autoExpandTypes], referencedSchemas);
|
|
876
|
+
toolsCore.JsonSchemaHelper.expandTypes(referencedSchemas, autoExpandTypes);
|
|
862
877
|
for (const arraySingularType of arraySingularTypes) {
|
|
863
878
|
referencedSchemas[`${arraySingularType}[]`] = {
|
|
864
879
|
type: "array",
|
|
@@ -869,74 +884,6 @@ async function generateSchemas(modelDirWildcards, types, outputWorkingDir) {
|
|
|
869
884
|
}
|
|
870
885
|
return referencedSchemas;
|
|
871
886
|
}
|
|
872
|
-
/**
|
|
873
|
-
* Extract the required types from all the known schemas.
|
|
874
|
-
* @param allSchemas All the known schemas.
|
|
875
|
-
* @param requiredTypes The required types.
|
|
876
|
-
* @param referencedSchemas The references schemas.
|
|
877
|
-
* @internal
|
|
878
|
-
*/
|
|
879
|
-
function extractTypes(allSchemas, requiredTypes, referencedSchemas) {
|
|
880
|
-
for (const type of requiredTypes) {
|
|
881
|
-
if (allSchemas[type] && !referencedSchemas[type]) {
|
|
882
|
-
referencedSchemas[type] = allSchemas[type];
|
|
883
|
-
extractTypesFromSchema(allSchemas, allSchemas[type], referencedSchemas);
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
/**
|
|
888
|
-
* Extract type from properties definition.
|
|
889
|
-
* @param allTypes All the known types.
|
|
890
|
-
* @param schema The schema to extract from.
|
|
891
|
-
* @param output The output types.
|
|
892
|
-
* @internal
|
|
893
|
-
*/
|
|
894
|
-
function extractTypesFromSchema(allTypes, schema, output) {
|
|
895
|
-
const additionalTypes = [];
|
|
896
|
-
if (core.Is.stringValue(schema.$ref)) {
|
|
897
|
-
additionalTypes.push(normaliseTypeName(schema.$ref).replace("#/definitions/", ""));
|
|
898
|
-
}
|
|
899
|
-
else if (core.Is.object(schema.items)) {
|
|
900
|
-
if (core.Is.arrayValue(schema.items)) {
|
|
901
|
-
for (const itemSchema of schema.items) {
|
|
902
|
-
extractTypesFromSchema(allTypes, itemSchema, output);
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
else {
|
|
906
|
-
extractTypesFromSchema(allTypes, schema.items, output);
|
|
907
|
-
}
|
|
908
|
-
}
|
|
909
|
-
else if (core.Is.object(schema.properties) || core.Is.object(schema.additionalProperties)) {
|
|
910
|
-
if (core.Is.object(schema.properties)) {
|
|
911
|
-
for (const prop in schema.properties) {
|
|
912
|
-
const p = schema.properties[prop];
|
|
913
|
-
if (core.Is.object(p)) {
|
|
914
|
-
extractTypesFromSchema(allTypes, p, output);
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
if (core.Is.object(schema.additionalProperties)) {
|
|
919
|
-
extractTypesFromSchema(allTypes, schema.additionalProperties, output);
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
else if (core.Is.arrayValue(schema.anyOf)) {
|
|
923
|
-
for (const prop of schema.anyOf) {
|
|
924
|
-
if (core.Is.object(prop)) {
|
|
925
|
-
extractTypesFromSchema(allTypes, prop, output);
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
else if (core.Is.arrayValue(schema.oneOf)) {
|
|
930
|
-
for (const prop of schema.oneOf) {
|
|
931
|
-
if (core.Is.object(prop)) {
|
|
932
|
-
extractTypesFromSchema(allTypes, prop, output);
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
if (additionalTypes.length > 0) {
|
|
937
|
-
extractTypes(allTypes, additionalTypes, output);
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
887
|
/**
|
|
941
888
|
* Tidy up the schemas for use in OpenAPI context.
|
|
942
889
|
* @param props The properties to tidy up.
|
|
@@ -1087,79 +1034,6 @@ async function loadPackages(tsToOpenApiConfig, outputWorkingDir, typeRoots) {
|
|
|
1087
1034
|
}
|
|
1088
1035
|
return restRoutes;
|
|
1089
1036
|
}
|
|
1090
|
-
/**
|
|
1091
|
-
* Process arrays in the schema object.
|
|
1092
|
-
* @param schemaObject The schema object to process.
|
|
1093
|
-
*/
|
|
1094
|
-
function processArrays(schemaObject) {
|
|
1095
|
-
if (core.Is.object(schemaObject)) {
|
|
1096
|
-
// latest specs have singular items in `items` property
|
|
1097
|
-
// and multiple items in prefixItems, so update the schema accordingly
|
|
1098
|
-
// https://www.learnjsonschema.com/2020-12/applicator/items/
|
|
1099
|
-
// https://www.learnjsonschema.com/2020-12/applicator/prefixitems/
|
|
1100
|
-
const schemaItems = schemaObject.items;
|
|
1101
|
-
if (core.Is.array(schemaItems) || core.Is.object(schemaItems)) {
|
|
1102
|
-
schemaObject.prefixItems = core.ArrayHelper.fromObjectOrArray(schemaItems);
|
|
1103
|
-
delete schemaObject.items;
|
|
1104
|
-
}
|
|
1105
|
-
const additionalItems = schemaObject.additionalItems;
|
|
1106
|
-
if (core.Is.array(additionalItems) || core.Is.object(additionalItems)) {
|
|
1107
|
-
schemaObject.items = core.ArrayHelper.fromObjectOrArray(additionalItems)[0];
|
|
1108
|
-
delete schemaObject.additionalItems;
|
|
1109
|
-
}
|
|
1110
|
-
processSchemaDictionary(schemaObject.properties);
|
|
1111
|
-
processArrays(schemaObject.additionalProperties);
|
|
1112
|
-
processSchemaArray(schemaObject.allOf);
|
|
1113
|
-
processSchemaArray(schemaObject.anyOf);
|
|
1114
|
-
processSchemaArray(schemaObject.oneOf);
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
/**
|
|
1118
|
-
* Process arrays in the schema object.
|
|
1119
|
-
* @param schemaDictionary The schema object to process.
|
|
1120
|
-
*/
|
|
1121
|
-
function processSchemaDictionary(schemaDictionary) {
|
|
1122
|
-
if (core.Is.object(schemaDictionary)) {
|
|
1123
|
-
for (const item of Object.values(schemaDictionary)) {
|
|
1124
|
-
if (core.Is.object(item)) {
|
|
1125
|
-
processArrays(item);
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
/**
|
|
1131
|
-
* Process arrays in the schema object.
|
|
1132
|
-
* @param schemaArray The schema object to process.
|
|
1133
|
-
*/
|
|
1134
|
-
function processSchemaArray(schemaArray) {
|
|
1135
|
-
if (core.Is.arrayValue(schemaArray)) {
|
|
1136
|
-
for (const item of schemaArray) {
|
|
1137
|
-
if (core.Is.object(item)) {
|
|
1138
|
-
processArrays(item);
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
/**
|
|
1144
|
-
* Cleanup TypeScript markers from the type name.
|
|
1145
|
-
* @param typeName The definition string to clean up.
|
|
1146
|
-
* @returns The cleaned up definition string.
|
|
1147
|
-
*/
|
|
1148
|
-
function normaliseTypeName(typeName) {
|
|
1149
|
-
// Remove the partial markers
|
|
1150
|
-
let sTypeName = typeName.replace(/^Partial<(.*?)>/g, "$1");
|
|
1151
|
-
sTypeName = sTypeName.replace(/Partial%3CI(.*?)%3E/g, "$1");
|
|
1152
|
-
// Remove the omit markers
|
|
1153
|
-
sTypeName = sTypeName.replace(/^Omit<(.*?),.*>/g, "$1");
|
|
1154
|
-
sTypeName = sTypeName.replace(/Omit%3CI(.*?)%2C.*%3E/g, "$1");
|
|
1155
|
-
// Remove the pick markers
|
|
1156
|
-
sTypeName = sTypeName.replace(/^Pick<(.*?),.*>/g, "$1");
|
|
1157
|
-
sTypeName = sTypeName.replace(/Pick%3CI(.*?)%2C.*%3E/g, "$1");
|
|
1158
|
-
// Cleanup the generic markers
|
|
1159
|
-
sTypeName = sTypeName.replace(/</g, "%3C").replace(/>/g, "%3E");
|
|
1160
|
-
sTypeName = sTypeName.replace(/%3Cunknown%3E/g, "");
|
|
1161
|
-
return sTypeName;
|
|
1162
|
-
}
|
|
1163
1037
|
|
|
1164
1038
|
// Copyright 2024 IOTA Stiftung.
|
|
1165
1039
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -1179,7 +1053,7 @@ class CLI extends cliCore.CLIBase {
|
|
|
1179
1053
|
return this.execute({
|
|
1180
1054
|
title: "TWIN TypeScript To OpenAPI",
|
|
1181
1055
|
appName: "ts-to-openapi",
|
|
1182
|
-
version: "0.0.1", // x-release-please-version
|
|
1056
|
+
version: "0.0.2-next.1", // x-release-please-version
|
|
1183
1057
|
icon: "⚙️ ",
|
|
1184
1058
|
supportsEnvFiles: false,
|
|
1185
1059
|
overrideOutputWidth: options?.overrideOutputWidth
|
package/dist/esm/index.mjs
CHANGED
|
@@ -2,7 +2,8 @@ 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, ObjectHelper
|
|
5
|
+
import { I18n, GeneralError, Is, StringHelper, ObjectHelper } from '@twin.org/core';
|
|
6
|
+
import { JsonSchemaHelper } from '@twin.org/tools-core';
|
|
6
7
|
import { HttpStatusCode, MimeTypes } from '@twin.org/web';
|
|
7
8
|
import { createGenerator } from 'ts-json-schema-generator';
|
|
8
9
|
|
|
@@ -236,7 +237,20 @@ async function tsToOpenApi(config, outputFile, workingDirectory) {
|
|
|
236
237
|
}
|
|
237
238
|
}
|
|
238
239
|
CLIDisplay.task(I18n.formatMessage("commands.ts-to-openapi.progress.generatingSchemas"));
|
|
239
|
-
const
|
|
240
|
+
const autoExpandTypes = config.autoExpandTypes ?? [];
|
|
241
|
+
const defaultExpandTypes = ["ObjectOrArray<.*>"];
|
|
242
|
+
for (const defaultType of defaultExpandTypes) {
|
|
243
|
+
if (!autoExpandTypes.includes(defaultType)) {
|
|
244
|
+
autoExpandTypes.push(defaultType);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const schemas = await generateSchemas(typeRoots, types, autoExpandTypes, workingDirectory);
|
|
248
|
+
for (const type in schemas) {
|
|
249
|
+
if (Is.object(config.overrides?.[type])) {
|
|
250
|
+
CLIDisplay.task(I18n.formatMessage("commands.ts-to-openapi.progress.overridingSchema"));
|
|
251
|
+
schemas[type] = config.overrides?.[type];
|
|
252
|
+
}
|
|
253
|
+
}
|
|
240
254
|
const usedCommonResponseTypes = [];
|
|
241
255
|
for (let i = 0; i < inputResults.length; i++) {
|
|
242
256
|
const result = inputResults[i];
|
|
@@ -662,7 +676,7 @@ async function finaliseOutput(usedCommonResponseTypes, schemas, openApi, securit
|
|
|
662
676
|
delete finalSchemas[type];
|
|
663
677
|
}
|
|
664
678
|
for (const type in finalSchemas) {
|
|
665
|
-
processArrays(finalSchemas[type]);
|
|
679
|
+
JsonSchemaHelper.processArrays(finalSchemas[type]);
|
|
666
680
|
}
|
|
667
681
|
const schemaKeys = Object.keys(finalSchemas);
|
|
668
682
|
schemaKeys.sort();
|
|
@@ -695,7 +709,7 @@ async function finaliseOutput(usedCommonResponseTypes, schemas, openApi, securit
|
|
|
695
709
|
// Remove the array [] from the type names
|
|
696
710
|
// eslint-disable-next-line unicorn/better-regex
|
|
697
711
|
json = json.replace(/#\/components\/schemas\/(.*)\[\]/g, "#/components/schemas/ListOf$1");
|
|
698
|
-
json = normaliseTypeName(json);
|
|
712
|
+
json = JsonSchemaHelper.normaliseTypeName(json);
|
|
699
713
|
// Remove external references
|
|
700
714
|
for (const finalExternal in finalExternals) {
|
|
701
715
|
json = json.replace(new RegExp(`"#/components/schemas/${StringHelper.stripPrefix(finalExternal)}"`, "g"), `"${finalExternals[finalExternal]}"`);
|
|
@@ -825,7 +839,7 @@ async function processPackageRestDetails(restRoutes) {
|
|
|
825
839
|
* @returns Nothing.
|
|
826
840
|
* @internal
|
|
827
841
|
*/
|
|
828
|
-
async function generateSchemas(modelDirWildcards, types, outputWorkingDir) {
|
|
842
|
+
async function generateSchemas(modelDirWildcards, types, autoExpandTypes, outputWorkingDir) {
|
|
829
843
|
const allSchemas = {};
|
|
830
844
|
const arraySingularTypes = [];
|
|
831
845
|
for (const type of types) {
|
|
@@ -849,13 +863,14 @@ async function generateSchemas(modelDirWildcards, types, outputWorkingDir) {
|
|
|
849
863
|
const schema = generator.createSchema("*");
|
|
850
864
|
if (schema.definitions) {
|
|
851
865
|
for (const def in schema.definitions) {
|
|
852
|
-
const defSub = normaliseTypeName(def);
|
|
866
|
+
const defSub = JsonSchemaHelper.normaliseTypeName(def);
|
|
853
867
|
allSchemas[defSub] = schema.definitions[def];
|
|
854
868
|
}
|
|
855
869
|
}
|
|
856
870
|
}
|
|
857
871
|
const referencedSchemas = {};
|
|
858
|
-
extractTypes(allSchemas, types, referencedSchemas);
|
|
872
|
+
JsonSchemaHelper.extractTypes(allSchemas, [...types, ...autoExpandTypes], referencedSchemas);
|
|
873
|
+
JsonSchemaHelper.expandTypes(referencedSchemas, autoExpandTypes);
|
|
859
874
|
for (const arraySingularType of arraySingularTypes) {
|
|
860
875
|
referencedSchemas[`${arraySingularType}[]`] = {
|
|
861
876
|
type: "array",
|
|
@@ -866,74 +881,6 @@ async function generateSchemas(modelDirWildcards, types, outputWorkingDir) {
|
|
|
866
881
|
}
|
|
867
882
|
return referencedSchemas;
|
|
868
883
|
}
|
|
869
|
-
/**
|
|
870
|
-
* Extract the required types from all the known schemas.
|
|
871
|
-
* @param allSchemas All the known schemas.
|
|
872
|
-
* @param requiredTypes The required types.
|
|
873
|
-
* @param referencedSchemas The references schemas.
|
|
874
|
-
* @internal
|
|
875
|
-
*/
|
|
876
|
-
function extractTypes(allSchemas, requiredTypes, referencedSchemas) {
|
|
877
|
-
for (const type of requiredTypes) {
|
|
878
|
-
if (allSchemas[type] && !referencedSchemas[type]) {
|
|
879
|
-
referencedSchemas[type] = allSchemas[type];
|
|
880
|
-
extractTypesFromSchema(allSchemas, allSchemas[type], referencedSchemas);
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
/**
|
|
885
|
-
* Extract type from properties definition.
|
|
886
|
-
* @param allTypes All the known types.
|
|
887
|
-
* @param schema The schema to extract from.
|
|
888
|
-
* @param output The output types.
|
|
889
|
-
* @internal
|
|
890
|
-
*/
|
|
891
|
-
function extractTypesFromSchema(allTypes, schema, output) {
|
|
892
|
-
const additionalTypes = [];
|
|
893
|
-
if (Is.stringValue(schema.$ref)) {
|
|
894
|
-
additionalTypes.push(normaliseTypeName(schema.$ref).replace("#/definitions/", ""));
|
|
895
|
-
}
|
|
896
|
-
else if (Is.object(schema.items)) {
|
|
897
|
-
if (Is.arrayValue(schema.items)) {
|
|
898
|
-
for (const itemSchema of schema.items) {
|
|
899
|
-
extractTypesFromSchema(allTypes, itemSchema, output);
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
else {
|
|
903
|
-
extractTypesFromSchema(allTypes, schema.items, output);
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {
|
|
907
|
-
if (Is.object(schema.properties)) {
|
|
908
|
-
for (const prop in schema.properties) {
|
|
909
|
-
const p = schema.properties[prop];
|
|
910
|
-
if (Is.object(p)) {
|
|
911
|
-
extractTypesFromSchema(allTypes, p, output);
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
if (Is.object(schema.additionalProperties)) {
|
|
916
|
-
extractTypesFromSchema(allTypes, schema.additionalProperties, output);
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
else if (Is.arrayValue(schema.anyOf)) {
|
|
920
|
-
for (const prop of schema.anyOf) {
|
|
921
|
-
if (Is.object(prop)) {
|
|
922
|
-
extractTypesFromSchema(allTypes, prop, output);
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
else if (Is.arrayValue(schema.oneOf)) {
|
|
927
|
-
for (const prop of schema.oneOf) {
|
|
928
|
-
if (Is.object(prop)) {
|
|
929
|
-
extractTypesFromSchema(allTypes, prop, output);
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
if (additionalTypes.length > 0) {
|
|
934
|
-
extractTypes(allTypes, additionalTypes, output);
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
884
|
/**
|
|
938
885
|
* Tidy up the schemas for use in OpenAPI context.
|
|
939
886
|
* @param props The properties to tidy up.
|
|
@@ -1084,79 +1031,6 @@ async function loadPackages(tsToOpenApiConfig, outputWorkingDir, typeRoots) {
|
|
|
1084
1031
|
}
|
|
1085
1032
|
return restRoutes;
|
|
1086
1033
|
}
|
|
1087
|
-
/**
|
|
1088
|
-
* Process arrays in the schema object.
|
|
1089
|
-
* @param schemaObject The schema object to process.
|
|
1090
|
-
*/
|
|
1091
|
-
function processArrays(schemaObject) {
|
|
1092
|
-
if (Is.object(schemaObject)) {
|
|
1093
|
-
// latest specs have singular items in `items` property
|
|
1094
|
-
// and multiple items in prefixItems, so update the schema accordingly
|
|
1095
|
-
// https://www.learnjsonschema.com/2020-12/applicator/items/
|
|
1096
|
-
// https://www.learnjsonschema.com/2020-12/applicator/prefixitems/
|
|
1097
|
-
const schemaItems = schemaObject.items;
|
|
1098
|
-
if (Is.array(schemaItems) || Is.object(schemaItems)) {
|
|
1099
|
-
schemaObject.prefixItems = ArrayHelper.fromObjectOrArray(schemaItems);
|
|
1100
|
-
delete schemaObject.items;
|
|
1101
|
-
}
|
|
1102
|
-
const additionalItems = schemaObject.additionalItems;
|
|
1103
|
-
if (Is.array(additionalItems) || Is.object(additionalItems)) {
|
|
1104
|
-
schemaObject.items = ArrayHelper.fromObjectOrArray(additionalItems)[0];
|
|
1105
|
-
delete schemaObject.additionalItems;
|
|
1106
|
-
}
|
|
1107
|
-
processSchemaDictionary(schemaObject.properties);
|
|
1108
|
-
processArrays(schemaObject.additionalProperties);
|
|
1109
|
-
processSchemaArray(schemaObject.allOf);
|
|
1110
|
-
processSchemaArray(schemaObject.anyOf);
|
|
1111
|
-
processSchemaArray(schemaObject.oneOf);
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
|
-
/**
|
|
1115
|
-
* Process arrays in the schema object.
|
|
1116
|
-
* @param schemaDictionary The schema object to process.
|
|
1117
|
-
*/
|
|
1118
|
-
function processSchemaDictionary(schemaDictionary) {
|
|
1119
|
-
if (Is.object(schemaDictionary)) {
|
|
1120
|
-
for (const item of Object.values(schemaDictionary)) {
|
|
1121
|
-
if (Is.object(item)) {
|
|
1122
|
-
processArrays(item);
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
/**
|
|
1128
|
-
* Process arrays in the schema object.
|
|
1129
|
-
* @param schemaArray The schema object to process.
|
|
1130
|
-
*/
|
|
1131
|
-
function processSchemaArray(schemaArray) {
|
|
1132
|
-
if (Is.arrayValue(schemaArray)) {
|
|
1133
|
-
for (const item of schemaArray) {
|
|
1134
|
-
if (Is.object(item)) {
|
|
1135
|
-
processArrays(item);
|
|
1136
|
-
}
|
|
1137
|
-
}
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
/**
|
|
1141
|
-
* Cleanup TypeScript markers from the type name.
|
|
1142
|
-
* @param typeName The definition string to clean up.
|
|
1143
|
-
* @returns The cleaned up definition string.
|
|
1144
|
-
*/
|
|
1145
|
-
function normaliseTypeName(typeName) {
|
|
1146
|
-
// Remove the partial markers
|
|
1147
|
-
let sTypeName = typeName.replace(/^Partial<(.*?)>/g, "$1");
|
|
1148
|
-
sTypeName = sTypeName.replace(/Partial%3CI(.*?)%3E/g, "$1");
|
|
1149
|
-
// Remove the omit markers
|
|
1150
|
-
sTypeName = sTypeName.replace(/^Omit<(.*?),.*>/g, "$1");
|
|
1151
|
-
sTypeName = sTypeName.replace(/Omit%3CI(.*?)%2C.*%3E/g, "$1");
|
|
1152
|
-
// Remove the pick markers
|
|
1153
|
-
sTypeName = sTypeName.replace(/^Pick<(.*?),.*>/g, "$1");
|
|
1154
|
-
sTypeName = sTypeName.replace(/Pick%3CI(.*?)%2C.*%3E/g, "$1");
|
|
1155
|
-
// Cleanup the generic markers
|
|
1156
|
-
sTypeName = sTypeName.replace(/</g, "%3C").replace(/>/g, "%3E");
|
|
1157
|
-
sTypeName = sTypeName.replace(/%3Cunknown%3E/g, "");
|
|
1158
|
-
return sTypeName;
|
|
1159
|
-
}
|
|
1160
1034
|
|
|
1161
1035
|
// Copyright 2024 IOTA Stiftung.
|
|
1162
1036
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -1176,7 +1050,7 @@ class CLI extends CLIBase {
|
|
|
1176
1050
|
return this.execute({
|
|
1177
1051
|
title: "TWIN TypeScript To OpenAPI",
|
|
1178
1052
|
appName: "ts-to-openapi",
|
|
1179
|
-
version: "0.0.1", // x-release-please-version
|
|
1053
|
+
version: "0.0.2-next.1", // x-release-please-version
|
|
1180
1054
|
icon: "⚙️ ",
|
|
1181
1055
|
supportsEnvFiles: false,
|
|
1182
1056
|
overrideOutputWidth: options?.overrideOutputWidth
|
package/dist/locales/en.json
CHANGED
|
@@ -284,6 +284,7 @@
|
|
|
284
284
|
"creatingWorkingDir": "Creating Working Directory",
|
|
285
285
|
"creatingSecuritySchemas": "Creating Security Schemas",
|
|
286
286
|
"generatingSchemas": "Generating Schemas",
|
|
287
|
+
"overridingSchema": "Overriding Schema",
|
|
287
288
|
"finalisingSchemas": "Finalising Schemas",
|
|
288
289
|
"writingOutputFile": "Writing Output File",
|
|
289
290
|
"models": "Models",
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { IJsonSchema } from "
|
|
1
|
+
import type { IJsonSchema, JsonTypeName } from "@twin.org/tools-core";
|
|
2
2
|
import type { IOpenApiExample } from "./IOpenApiExample";
|
|
3
3
|
import type { IOpenApiResponse } from "./IOpenApiResponse";
|
|
4
|
-
import type { JsonTypeName } from "./jsonTypeName";
|
|
5
4
|
/**
|
|
6
5
|
* The Open API config definition.
|
|
7
6
|
*/
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-core";
|
|
1
2
|
import type { ITsToOpenApiConfigEntryPoint } from "./ITsToOpenApiConfigEntryPoint";
|
|
2
3
|
/**
|
|
3
4
|
* Configuration for the API.
|
|
@@ -58,4 +59,14 @@ export interface ITsToOpenApiConfig {
|
|
|
58
59
|
externalReferences?: {
|
|
59
60
|
[id: string]: string;
|
|
60
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* Override for specific types, to be used when the type cannot be generated automatically, or is generated incorrectly.
|
|
64
|
+
*/
|
|
65
|
+
overrides?: {
|
|
66
|
+
[id: string]: IJsonSchema;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* The types to automatically expand inline in type definitions, reg ex string matches.
|
|
70
|
+
*/
|
|
71
|
+
autoExpandTypes?: string[];
|
|
61
72
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# @twin.org/ts-to-openapi - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.2-next.1](https://github.com/twinfoundation/tools/compare/ts-to-openapi-v0.0.2-next.0...ts-to-openapi-v0.0.2-next.1) (2025-07-14)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add latest json schema features ([494293f](https://github.com/twinfoundation/tools/commit/494293f4252b9c7d4a20790ec157fc9d8c96c3d2))
|
|
9
|
+
* add support for auto expand types ([dd1e10a](https://github.com/twinfoundation/tools/commit/dd1e10a5b2fea6f80890ff6f3971f48e239cb4c1))
|
|
10
|
+
* add ts-to-schema overrides ([3c54504](https://github.com/twinfoundation/tools/commit/3c5450468eb998204a75576b7791a7ca4027da62))
|
|
11
|
+
* generate schemas as individual entities ([9f372ab](https://github.com/twinfoundation/tools/commit/9f372abdfc27aba93b303c7b214991919c0c18c3))
|
|
12
|
+
* improve schema type name normalisation ([1a18b26](https://github.com/twinfoundation/tools/commit/1a18b267d87e9179bda01b396b256c450ae2889e))
|
|
13
|
+
* move package to framework repo ([4490bda](https://github.com/twinfoundation/tools/commit/4490bda472d4dc8ddfe931e2fce81f3411de9ab3))
|
|
14
|
+
* strip Omit types ([3a079f9](https://github.com/twinfoundation/tools/commit/3a079f9abe8127c5b44a2b9382babf2f19629d08))
|
|
15
|
+
* use most recent JSON schema specs ([4598cbf](https://github.com/twinfoundation/tools/commit/4598cbf29f7b82dba4a9f3b19f81dfe66f5a6060))
|
|
16
|
+
* use shared store mechanism ([#31](https://github.com/twinfoundation/tools/issues/31)) ([d9fe68b](https://github.com/twinfoundation/tools/commit/d9fe68b903d1268c7cb3c64772df5cb78fd63667))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
* fix locale resource name ([53ad5b5](https://github.com/twinfoundation/tools/commit/53ad5b56f19a5082f16a4f1e4a761e114dce8250))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Dependencies
|
|
25
|
+
|
|
26
|
+
* The following workspace dependencies were updated
|
|
27
|
+
* dependencies
|
|
28
|
+
* @twin.org/tools-core bumped from 0.0.2-next.0 to 0.0.2-next.1
|
|
29
|
+
|
|
3
30
|
## 0.0.1 (2025-07-03)
|
|
4
31
|
|
|
5
32
|
|
|
@@ -101,3 +101,23 @@ External type references
|
|
|
101
101
|
#### Index Signature
|
|
102
102
|
|
|
103
103
|
\[`id`: `string`\]: `string`
|
|
104
|
+
|
|
105
|
+
***
|
|
106
|
+
|
|
107
|
+
### overrides?
|
|
108
|
+
|
|
109
|
+
> `optional` **overrides**: `object`
|
|
110
|
+
|
|
111
|
+
Override for specific types, to be used when the type cannot be generated automatically, or is generated incorrectly.
|
|
112
|
+
|
|
113
|
+
#### Index Signature
|
|
114
|
+
|
|
115
|
+
\[`id`: `string`\]: `AnySchemaObject`
|
|
116
|
+
|
|
117
|
+
***
|
|
118
|
+
|
|
119
|
+
### autoExpandTypes?
|
|
120
|
+
|
|
121
|
+
> `optional` **autoExpandTypes**: `string`[]
|
|
122
|
+
|
|
123
|
+
The types to automatically expand inline in type definitions, reg ex string matches.
|
package/locales/en.json
CHANGED
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"creatingWorkingDir": "Creating Working Directory",
|
|
27
27
|
"creatingSecuritySchemas": "Creating Security Schemas",
|
|
28
28
|
"generatingSchemas": "Generating Schemas",
|
|
29
|
+
"overridingSchema": "Overriding Schema",
|
|
29
30
|
"finalisingSchemas": "Finalising Schemas",
|
|
30
31
|
"writingOutputFile": "Writing Output File",
|
|
31
32
|
"models": "Models",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/ts-to-openapi",
|
|
3
|
-
"version": "0.0.1",
|
|
3
|
+
"version": "0.0.2-next.1",
|
|
4
4
|
"description": "Tool to convert TypeScript REST route definitions to OpenAPI Specifications",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,12 +14,12 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/api-models": "
|
|
18
|
-
"@twin.org/cli-core": "
|
|
19
|
-
"@twin.org/core": "
|
|
20
|
-
"@twin.org/nameof": "
|
|
21
|
-
"@twin.org/
|
|
22
|
-
"
|
|
17
|
+
"@twin.org/api-models": "next",
|
|
18
|
+
"@twin.org/cli-core": "next",
|
|
19
|
+
"@twin.org/core": "next",
|
|
20
|
+
"@twin.org/nameof": "next",
|
|
21
|
+
"@twin.org/tools-core": "0.0.2-next.1",
|
|
22
|
+
"@twin.org/web": "next",
|
|
23
23
|
"commander": "14.0.0",
|
|
24
24
|
"glob": "11.0.2",
|
|
25
25
|
"ts-json-schema-generator": "2.4.0"
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Configuration for each individual package.
|
|
3
|
-
*/
|
|
4
|
-
export interface IPackageJson {
|
|
5
|
-
/**
|
|
6
|
-
* The name of the package.
|
|
7
|
-
*/
|
|
8
|
-
name: string;
|
|
9
|
-
/**
|
|
10
|
-
* The dependencies for the package.
|
|
11
|
-
*/
|
|
12
|
-
dependencies?: {
|
|
13
|
-
[id: string]: string;
|
|
14
|
-
};
|
|
15
|
-
}
|