@twin.org/tools-core 0.0.3-next.2 → 0.0.3-next.21

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.
Files changed (133) hide show
  1. package/README.md +2 -2
  2. package/dist/es/index.js +20 -11
  3. package/dist/es/index.js.map +1 -1
  4. package/dist/es/models/ITypeScriptToSchemaContext.js +2 -0
  5. package/dist/es/models/ITypeScriptToSchemaContext.js.map +1 -0
  6. package/dist/es/models/ITypeScriptToSchemaDiagnostics.js +4 -0
  7. package/dist/es/models/ITypeScriptToSchemaDiagnostics.js.map +1 -0
  8. package/dist/es/models/ITypeScriptToSchemaOptions.js +4 -0
  9. package/dist/es/models/ITypeScriptToSchemaOptions.js.map +1 -0
  10. package/dist/es/utils/constants.js +43 -0
  11. package/dist/es/utils/constants.js.map +1 -0
  12. package/dist/es/utils/diagnosticReporter.js +32 -0
  13. package/dist/es/utils/diagnosticReporter.js.map +1 -0
  14. package/dist/es/utils/disallowedTypeGuard.js +151 -0
  15. package/dist/es/utils/disallowedTypeGuard.js.map +1 -0
  16. package/dist/es/utils/enum.js +152 -0
  17. package/dist/es/utils/enum.js.map +1 -0
  18. package/dist/es/utils/fileUtils.js +130 -0
  19. package/dist/es/utils/fileUtils.js.map +1 -0
  20. package/dist/es/utils/importTypeQuerySchemaResolver.js +328 -0
  21. package/dist/es/utils/importTypeQuerySchemaResolver.js.map +1 -0
  22. package/dist/es/utils/indexSignaturePatternResolver.js +94 -0
  23. package/dist/es/utils/indexSignaturePatternResolver.js.map +1 -0
  24. package/dist/es/utils/intersectionSchemaMerger.js +85 -0
  25. package/dist/es/utils/intersectionSchemaMerger.js.map +1 -0
  26. package/dist/es/utils/jsDoc.js +120 -0
  27. package/dist/es/utils/jsDoc.js.map +1 -0
  28. package/dist/es/utils/jsonSchemaBuilder.js +3373 -0
  29. package/dist/es/utils/jsonSchemaBuilder.js.map +1 -0
  30. package/dist/es/utils/mappedTypeSchemaResolver.js +231 -0
  31. package/dist/es/utils/mappedTypeSchemaResolver.js.map +1 -0
  32. package/dist/es/utils/objectTransformer.js +162 -0
  33. package/dist/es/utils/objectTransformer.js.map +1 -0
  34. package/dist/es/utils/regEx.js +128 -0
  35. package/dist/es/utils/regEx.js.map +1 -0
  36. package/dist/es/utils/resolver.js +164 -0
  37. package/dist/es/utils/resolver.js.map +1 -0
  38. package/dist/es/utils/templateLiteralPatternBuilder.js +94 -0
  39. package/dist/es/utils/templateLiteralPatternBuilder.js.map +1 -0
  40. package/dist/es/utils/typeScriptToSchema.js +112 -0
  41. package/dist/es/utils/typeScriptToSchema.js.map +1 -0
  42. package/dist/es/utils/utilityTypeSchemaMapper.js +412 -0
  43. package/dist/es/utils/utilityTypeSchemaMapper.js.map +1 -0
  44. package/dist/types/index.d.ts +20 -11
  45. package/dist/types/models/ITypeScriptToSchemaContext.d.ts +64 -0
  46. package/dist/types/models/ITypeScriptToSchemaDiagnostics.d.ts +31 -0
  47. package/dist/types/models/ITypeScriptToSchemaOptions.d.ts +22 -0
  48. package/dist/types/utils/constants.d.ts +13 -0
  49. package/dist/types/utils/diagnosticReporter.d.ts +17 -0
  50. package/dist/types/utils/disallowedTypeGuard.d.ts +16 -0
  51. package/dist/types/utils/enum.d.ts +42 -0
  52. package/dist/types/utils/fileUtils.d.ts +66 -0
  53. package/dist/types/utils/importTypeQuerySchemaResolver.d.ts +52 -0
  54. package/dist/types/utils/indexSignaturePatternResolver.d.ts +21 -0
  55. package/dist/types/utils/intersectionSchemaMerger.d.ts +16 -0
  56. package/dist/types/utils/jsDoc.d.ts +46 -0
  57. package/dist/types/utils/jsonSchemaBuilder.d.ts +747 -0
  58. package/dist/types/utils/mappedTypeSchemaResolver.d.ts +46 -0
  59. package/dist/types/utils/objectTransformer.d.ts +33 -0
  60. package/dist/types/utils/regEx.d.ts +24 -0
  61. package/dist/types/utils/resolver.d.ts +16 -0
  62. package/dist/types/utils/templateLiteralPatternBuilder.d.ts +12 -0
  63. package/dist/types/utils/typeScriptToSchema.d.ts +31 -0
  64. package/dist/types/utils/utilityTypeSchemaMapper.d.ts +92 -0
  65. package/docs/changelog.md +176 -1
  66. package/docs/examples.md +87 -1
  67. package/docs/reference/classes/Constants.md +29 -0
  68. package/docs/reference/classes/DiagnosticReporter.md +49 -0
  69. package/docs/reference/classes/DisallowedTypeGuard.md +35 -0
  70. package/docs/reference/classes/Enum.md +93 -0
  71. package/docs/reference/classes/FileUtils.md +237 -0
  72. package/docs/reference/classes/ImportTypeQuerySchemaResolver.md +87 -0
  73. package/docs/reference/classes/IndexSignaturePatternResolver.md +69 -0
  74. package/docs/reference/classes/IntersectionSchemaMerger.md +48 -0
  75. package/docs/reference/classes/JsDoc.md +141 -0
  76. package/docs/reference/classes/JsonSchemaBuilder.md +2870 -0
  77. package/docs/reference/classes/MappedTypeSchemaResolver.md +211 -0
  78. package/docs/reference/classes/ObjectTransformer.md +119 -0
  79. package/docs/reference/classes/RegEx.md +99 -0
  80. package/docs/reference/classes/Resolver.md +41 -0
  81. package/docs/reference/classes/TemplateLiteralPatternBuilder.md +35 -0
  82. package/docs/reference/classes/TypeScriptToSchema.md +91 -0
  83. package/docs/reference/classes/UtilityTypeSchemaMapper.md +341 -0
  84. package/docs/reference/index.md +20 -14
  85. package/docs/reference/interfaces/ITypeScriptToSchemaContext.md +113 -0
  86. package/docs/reference/interfaces/ITypeScriptToSchemaDiagnostics.md +55 -0
  87. package/docs/reference/interfaces/ITypeScriptToSchemaOptions.md +44 -0
  88. package/locales/en.json +32 -1
  89. package/package.json +4 -3
  90. package/dist/es/models/IJsonSchema.js +0 -2
  91. package/dist/es/models/IJsonSchema.js.map +0 -1
  92. package/dist/es/models/IOpenApi.js +0 -2
  93. package/dist/es/models/IOpenApi.js.map +0 -1
  94. package/dist/es/models/IOpenApiExample.js +0 -4
  95. package/dist/es/models/IOpenApiExample.js.map +0 -1
  96. package/dist/es/models/IOpenApiHeader.js +0 -4
  97. package/dist/es/models/IOpenApiHeader.js.map +0 -1
  98. package/dist/es/models/IOpenApiPathMethod.js +0 -2
  99. package/dist/es/models/IOpenApiPathMethod.js.map +0 -1
  100. package/dist/es/models/IOpenApiResponse.js +0 -2
  101. package/dist/es/models/IOpenApiResponse.js.map +0 -1
  102. package/dist/es/models/IOpenApiSecurityScheme.js +0 -4
  103. package/dist/es/models/IOpenApiSecurityScheme.js.map +0 -1
  104. package/dist/es/models/IPackageJson.js +0 -4
  105. package/dist/es/models/IPackageJson.js.map +0 -1
  106. package/dist/es/models/jsonTypeName.js +0 -2
  107. package/dist/es/models/jsonTypeName.js.map +0 -1
  108. package/dist/es/utils/jsonSchemaHelper.js +0 -258
  109. package/dist/es/utils/jsonSchemaHelper.js.map +0 -1
  110. package/dist/es/utils/openApiHelper.js +0 -12
  111. package/dist/es/utils/openApiHelper.js.map +0 -1
  112. package/dist/types/models/IJsonSchema.d.ts +0 -5
  113. package/dist/types/models/IOpenApi.d.ts +0 -54
  114. package/dist/types/models/IOpenApiExample.d.ts +0 -13
  115. package/dist/types/models/IOpenApiHeader.d.ts +0 -19
  116. package/dist/types/models/IOpenApiPathMethod.d.ts +0 -65
  117. package/dist/types/models/IOpenApiResponse.d.ts +0 -32
  118. package/dist/types/models/IOpenApiSecurityScheme.d.ts +0 -25
  119. package/dist/types/models/IPackageJson.d.ts +0 -15
  120. package/dist/types/models/jsonTypeName.d.ts +0 -5
  121. package/dist/types/utils/jsonSchemaHelper.d.ts +0 -78
  122. package/dist/types/utils/openApiHelper.d.ts +0 -9
  123. package/docs/reference/classes/JsonSchemaHelper.md +0 -233
  124. package/docs/reference/classes/OpenApiHelper.md +0 -21
  125. package/docs/reference/interfaces/IOpenApi.md +0 -103
  126. package/docs/reference/interfaces/IOpenApiExample.md +0 -19
  127. package/docs/reference/interfaces/IOpenApiHeader.md +0 -31
  128. package/docs/reference/interfaces/IOpenApiPathMethod.md +0 -119
  129. package/docs/reference/interfaces/IOpenApiResponse.md +0 -35
  130. package/docs/reference/interfaces/IOpenApiSecurityScheme.md +0 -43
  131. package/docs/reference/interfaces/IPackageJson.md +0 -23
  132. package/docs/reference/type-aliases/IJsonSchema.md +0 -5
  133. package/docs/reference/type-aliases/JsonTypeName.md +0 -5
@@ -1 +0,0 @@
1
- {"version":3,"file":"IOpenApiPathMethod.js","sourceRoot":"","sources":["../../../src/models/IOpenApiPathMethod.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IJsonSchema } from \"./IJsonSchema.js\";\nimport type { IOpenApiExample } from \"./IOpenApiExample.js\";\nimport type { IOpenApiResponse } from \"./IOpenApiResponse.js\";\nimport type { JsonTypeName } from \"./jsonTypeName.js\";\n\n/**\n * The Open API config definition.\n */\nexport interface IOpenApiPathMethod {\n\t/**\n\t * The operation id.\n\t */\n\toperationId: string;\n\n\t/**\n\t * Summary.\n\t */\n\tsummary: string;\n\n\t/**\n\t * Tags.\n\t */\n\ttags?: string[];\n\n\t/**\n\t * Parameters.\n\t */\n\tparameters?: {\n\t\tname: string;\n\t\tin: string;\n\t\tdescription?: string;\n\t\trequired: boolean;\n\t\tschema: {\n\t\t\ttype?: JsonTypeName | JsonTypeName[];\n\t\t\tenum?: IJsonSchema[];\n\t\t\t$ref?: string;\n\t\t};\n\t\tstyle?: string;\n\t}[];\n\n\t/**\n\t * Request body.\n\t */\n\trequestBody?: {\n\t\trequired: boolean;\n\t\tdescription?: string;\n\t\tcontent?: {\n\t\t\t[contentType: string]: {\n\t\t\t\tschema: {\n\t\t\t\t\t$ref: string;\n\t\t\t\t};\n\t\t\t\texamples?: {\n\t\t\t\t\t[id: string]: IOpenApiExample;\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t};\n\n\t/**\n\t * Response body.\n\t */\n\tresponses?: {\n\t\t[code: string]: IOpenApiResponse;\n\t};\n\n\t/**\n\t * Security model for the API.\n\t */\n\tsecurity?: {\n\t\t[name: string]: string[];\n\t}[];\n}\n"]}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=IOpenApiResponse.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"IOpenApiResponse.js","sourceRoot":"","sources":["../../../src/models/IOpenApiResponse.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IOpenApiExample } from \"./IOpenApiExample.js\";\nimport type { IOpenApiHeader } from \"./IOpenApiHeader.js\";\n\n/**\n * The Open API config definition.\n */\nexport interface IOpenApiResponse {\n\t/**\n\t * Descriptions for the response.\n\t */\n\tdescription?: string;\n\n\t/**\n\t * Content for the response.\n\t */\n\tcontent?: {\n\t\t[contentType: string]: {\n\t\t\tschema: {\n\t\t\t\ttype?: string;\n\t\t\t\tformat?: string;\n\t\t\t\t$ref?: string;\n\t\t\t};\n\t\t\texamples?: {\n\t\t\t\t[id: string]: IOpenApiExample;\n\t\t\t};\n\t\t};\n\t};\n\n\t/**\n\t * The headers for the response.\n\t */\n\theaders?: {\n\t\t[id: string]: IOpenApiHeader;\n\t};\n}\n"]}
@@ -1,4 +0,0 @@
1
- // Copyright 2024 IOTA Stiftung.
2
- // SPDX-License-Identifier: Apache-2.0.
3
- export {};
4
- //# sourceMappingURL=IOpenApiSecurityScheme.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"IOpenApiSecurityScheme.js","sourceRoot":"","sources":["../../../src/models/IOpenApiSecurityScheme.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * The Open API config definition for security scheme.\n */\nexport interface IOpenApiSecurityScheme {\n\t/**\n\t * The type of the security schema.\n\t */\n\ttype?: string;\n\n\t/**\n\t * The scheme method.\n\t */\n\tscheme?: string;\n\n\t/**\n\t * The bearer format.\n\t */\n\tbearerFormat?: string;\n\n\t/**\n\t * Where is the token located.\n\t */\n\tin?: string;\n\n\t/**\n\t * What is the name of the token.\n\t */\n\tname?: string;\n}\n"]}
@@ -1,4 +0,0 @@
1
- // Copyright 2024 IOTA Stiftung.
2
- // SPDX-License-Identifier: Apache-2.0.
3
- export {};
4
- //# sourceMappingURL=IPackageJson.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"IPackageJson.js","sourceRoot":"","sources":["../../../src/models/IPackageJson.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Configuration for each individual package.\n */\nexport interface IPackageJson {\n\t/**\n\t * The name of the package.\n\t */\n\tname: string;\n\n\t/**\n\t * The dependencies for the package.\n\t */\n\tdependencies?: { [id: string]: string };\n}\n"]}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=jsonTypeName.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"jsonTypeName.js","sourceRoot":"","sources":["../../../src/models/jsonTypeName.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { JSONType } from \"ajv/dist/2020.js\";\n\n/**\n * Default schema type.\n */\nexport type JsonTypeName = JSONType;\n"]}
@@ -1,258 +0,0 @@
1
- // Copyright 2024 IOTA Stiftung.
2
- // SPDX-License-Identifier: Apache-2.0.
3
- import { Is } from "@twin.org/core";
4
- /**
5
- * Helper class for JSON Schema processing.
6
- */
7
- export class JsonSchemaHelper {
8
- /**
9
- * The JSON Schema version used.
10
- */
11
- static SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
12
- /**
13
- * Process arrays in the schema object.
14
- * @param schemaObject The schema object to process.
15
- */
16
- static processArrays(schemaObject) {
17
- if (Is.object(schemaObject)) {
18
- // new json schema version has prefixItems instead of items for arrays with fixed ordering
19
- // so convert any old style schemas that use items as an array
20
- // prefixItems validates fixed positions and is an array
21
- // items validates the rest and is an object
22
- // additionalItems no longer exists
23
- // https://www.learnjsonschema.com/2020-12/applicator/items/
24
- // https://www.learnjsonschema.com/2020-12/applicator/prefixitems/
25
- if (Is.array(schemaObject.items)) {
26
- if (Is.object(schemaObject.additionalItems)) {
27
- // If the items are an array then this is fixed ordering
28
- // so move to prefixItems and then do the same for additionalItems
29
- schemaObject.prefixItems = schemaObject.items;
30
- delete schemaObject.items;
31
- schemaObject.items = schemaObject.additionalItems;
32
- }
33
- else if (Is.integer(schemaObject.minItems) &&
34
- Is.integer(schemaObject.maxItems) &&
35
- schemaObject.minItems === schemaObject.maxItems &&
36
- schemaObject.maxItems === schemaObject.items.length) {
37
- // There is a fixed number of items which matches the items array length
38
- // so move construct an allOf to enforce the fixed length
39
- schemaObject.items = { allOf: schemaObject.items };
40
- delete schemaObject.minItems;
41
- delete schemaObject.maxItems;
42
- }
43
- else {
44
- // no additional items so wrap in an anyOf
45
- schemaObject.items = { anyOf: schemaObject.items };
46
- }
47
- }
48
- // We can't use additionalItems in the new schema version
49
- // so just remove it after processing
50
- delete schemaObject.additionalItems;
51
- JsonSchemaHelper.processSchemaDictionary(schemaObject.properties);
52
- JsonSchemaHelper.processArrays(schemaObject.additionalProperties);
53
- JsonSchemaHelper.processSchemaArray(schemaObject.allOf);
54
- JsonSchemaHelper.processSchemaArray(schemaObject.anyOf);
55
- JsonSchemaHelper.processSchemaArray(schemaObject.oneOf);
56
- }
57
- }
58
- /**
59
- * Process arrays in the schema object.
60
- * @param schemaDictionary The schema object to process.
61
- */
62
- static processSchemaDictionary(schemaDictionary) {
63
- if (Is.object(schemaDictionary)) {
64
- for (const item of Object.values(schemaDictionary)) {
65
- if (Is.object(item)) {
66
- JsonSchemaHelper.processArrays(item);
67
- }
68
- }
69
- }
70
- }
71
- /**
72
- * Process arrays in the schema object.
73
- * @param schemaArray The schema object to process.
74
- */
75
- static processSchemaArray(schemaArray) {
76
- if (Is.arrayValue(schemaArray)) {
77
- for (const item of schemaArray) {
78
- if (Is.object(item)) {
79
- JsonSchemaHelper.processArrays(item);
80
- }
81
- }
82
- }
83
- }
84
- /**
85
- * Cleanup TypeScript markers from the type name.
86
- * @param typeName The definition string to clean up.
87
- * @returns The cleaned up definition string.
88
- */
89
- static normaliseTypeName(typeName) {
90
- // Remove the partial markers
91
- let sTypeName = typeName.replace(/^Partial<I(.*?)>/g, "$1");
92
- sTypeName = sTypeName.replace(/Partial%3CI(.*?)%3E/g, "$1");
93
- // Remove the omit markers
94
- sTypeName = sTypeName.replace(/^Omit<I(.*?),.*>/g, "$1");
95
- sTypeName = sTypeName.replace(/Omit%3CI(.*?)%2C.*%3E/g, "$1");
96
- // Remove the pick markers
97
- sTypeName = sTypeName.replace(/^Pick<I(.*?),.*>/g, "$1");
98
- sTypeName = sTypeName.replace(/Pick%3CI(.*?)%2C.*%3E/g, "$1");
99
- // Cleanup the generic markers
100
- sTypeName = sTypeName.replace(/^(.*?)<I(.*?)>/g, "$1<$2>");
101
- sTypeName = sTypeName.replace(/(.*?)%3CI(.*?)%3E/g, "$1<$2>");
102
- // Cleanup the unknown markers
103
- sTypeName = sTypeName.replace(/<unknown>/g, "");
104
- sTypeName = sTypeName.replace(/%3Cunknown%3E/g, "");
105
- // Replace the other url markers
106
- sTypeName = sTypeName.replace(/%7C/g, "|").replace(/%3C/g, "<").replace(/%3E/g, ">");
107
- return sTypeName;
108
- }
109
- /**
110
- * Extract type from properties definition.
111
- * @param allTypes All the known types.
112
- * @param schema The schema to extract from.
113
- * @param output The output types.
114
- */
115
- static extractTypesFromSchema(allTypes, schema, output) {
116
- const additionalTypes = [];
117
- if (Is.stringValue(schema.$ref)) {
118
- additionalTypes.push(JsonSchemaHelper.normaliseTypeName(schema.$ref.replace("#/definitions/", "")));
119
- }
120
- else if (Is.object(schema.items)) {
121
- if (Is.arrayValue(schema.items)) {
122
- for (const itemSchema of schema.items) {
123
- JsonSchemaHelper.extractTypesFromSchema(allTypes, itemSchema, output);
124
- }
125
- }
126
- else {
127
- JsonSchemaHelper.extractTypesFromSchema(allTypes, schema.items, output);
128
- }
129
- }
130
- else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {
131
- if (Is.object(schema.properties)) {
132
- for (const prop in schema.properties) {
133
- const p = schema.properties[prop];
134
- if (Is.object(p)) {
135
- JsonSchemaHelper.extractTypesFromSchema(allTypes, p, output);
136
- }
137
- }
138
- }
139
- if (Is.object(schema.additionalProperties)) {
140
- JsonSchemaHelper.extractTypesFromSchema(allTypes, schema.additionalProperties, output);
141
- }
142
- }
143
- else if (Is.arrayValue(schema.anyOf)) {
144
- for (const prop of schema.anyOf) {
145
- if (Is.object(prop)) {
146
- JsonSchemaHelper.extractTypesFromSchema(allTypes, prop, output);
147
- }
148
- }
149
- }
150
- else if (Is.arrayValue(schema.oneOf)) {
151
- for (const prop of schema.oneOf) {
152
- if (Is.object(prop)) {
153
- JsonSchemaHelper.extractTypesFromSchema(allTypes, prop, output);
154
- }
155
- }
156
- }
157
- if (additionalTypes.length > 0) {
158
- JsonSchemaHelper.extractTypes(allTypes, additionalTypes, output);
159
- }
160
- }
161
- /**
162
- * Extract the required types from all the known schemas.
163
- * @param allSchemas All the known schemas.
164
- * @param requiredTypes The required types.
165
- * @param referencedSchemas The references schemas.
166
- */
167
- static extractTypes(allSchemas, requiredTypes, referencedSchemas) {
168
- for (const typeKey of Object.keys(allSchemas)) {
169
- if (!referencedSchemas[typeKey]) {
170
- for (const requiredType of requiredTypes) {
171
- const regex = JsonSchemaHelper.stringToRegEx(requiredType);
172
- if (regex.test(typeKey)) {
173
- referencedSchemas[typeKey] = allSchemas[typeKey];
174
- JsonSchemaHelper.extractTypesFromSchema(allSchemas, allSchemas[typeKey], referencedSchemas);
175
- }
176
- }
177
- }
178
- }
179
- }
180
- /**
181
- * Expand the types inline.
182
- * @param schemas The schemas to include the expanded types.
183
- * @param expandedTypes The types to expand.
184
- */
185
- static expandTypes(schemas, expandedTypes) {
186
- for (const typeKey of Object.keys(schemas)) {
187
- const schema = schemas[typeKey];
188
- JsonSchemaHelper.expandSchemaTypes(schemas, schema, expandedTypes);
189
- }
190
- }
191
- /**
192
- * Expand the types inline.
193
- * @param allSchemas All the known schemas.
194
- * @param schema The schemas to include the expanded types.
195
- * @param expandedTypes The types to expand.
196
- */
197
- static expandSchemaTypes(allSchemas, schema, expandedTypes) {
198
- if (Is.stringValue(schema.$ref)) {
199
- for (const expandedType of expandedTypes) {
200
- const typeName = JsonSchemaHelper.normaliseTypeName(schema.$ref.replace("#/definitions/", ""));
201
- const regex = JsonSchemaHelper.stringToRegEx(expandedType);
202
- if (regex.test(typeName) && allSchemas[typeName]) {
203
- delete schema.$ref;
204
- Object.assign(schema, allSchemas[typeName]);
205
- break;
206
- }
207
- }
208
- }
209
- else if (Is.object(schema.items)) {
210
- if (Is.arrayValue(schema.items)) {
211
- for (const itemSchema of schema.items) {
212
- JsonSchemaHelper.expandSchemaTypes(allSchemas, itemSchema, expandedTypes);
213
- }
214
- }
215
- else {
216
- JsonSchemaHelper.expandSchemaTypes(allSchemas, schema.items, expandedTypes);
217
- }
218
- }
219
- else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {
220
- if (Is.object(schema.properties)) {
221
- for (const prop in schema.properties) {
222
- const p = schema.properties[prop];
223
- if (Is.object(p)) {
224
- JsonSchemaHelper.expandSchemaTypes(allSchemas, p, expandedTypes);
225
- }
226
- }
227
- }
228
- if (Is.object(schema.additionalProperties)) {
229
- JsonSchemaHelper.expandSchemaTypes(allSchemas, schema.additionalProperties, expandedTypes);
230
- }
231
- }
232
- else if (Is.arrayValue(schema.anyOf)) {
233
- for (const prop of schema.anyOf) {
234
- if (Is.object(prop)) {
235
- JsonSchemaHelper.expandSchemaTypes(allSchemas, prop, expandedTypes);
236
- }
237
- }
238
- }
239
- else if (Is.arrayValue(schema.oneOf)) {
240
- for (const prop of schema.oneOf) {
241
- if (Is.object(prop)) {
242
- JsonSchemaHelper.expandSchemaTypes(allSchemas, prop, expandedTypes);
243
- }
244
- }
245
- }
246
- }
247
- /**
248
- * Convert a string pattern to a regular expression.
249
- * @param matchPattern The pattern to convert.
250
- * @returns The regular expression.
251
- */
252
- static stringToRegEx(matchPattern) {
253
- return matchPattern.startsWith("/") && matchPattern.endsWith("/")
254
- ? new RegExp(matchPattern.slice(1, -1))
255
- : new RegExp(`^${matchPattern}$`);
256
- }
257
- }
258
- //# sourceMappingURL=jsonSchemaHelper.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"jsonSchemaHelper.js","sourceRoot":"","sources":["../../../src/utils/jsonSchemaHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAGpC;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,cAAc,GAAG,8CAA8C,CAAC;IAEvF;;;OAGG;IACI,MAAM,CAAC,aAAa,CAAC,YAAyB;QACpD,IAAI,EAAE,CAAC,MAAM,CAAc,YAAY,CAAC,EAAE,CAAC;YAC1C,0FAA0F;YAC1F,8DAA8D;YAC9D,wDAAwD;YACxD,4CAA4C;YAC5C,mCAAmC;YACnC,4DAA4D;YAC5D,kEAAkE;YAClE,IAAI,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;gBAClC,IAAI,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,eAAe,CAAC,EAAE,CAAC;oBAC7C,wDAAwD;oBACxD,kEAAkE;oBAClE,YAAY,CAAC,WAAW,GAAG,YAAY,CAAC,KAAK,CAAC;oBAC9C,OAAO,YAAY,CAAC,KAAK,CAAC;oBAC1B,YAAY,CAAC,KAAK,GAAG,YAAY,CAAC,eAAe,CAAC;gBACnD,CAAC;qBAAM,IACN,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC;oBACjC,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC;oBACjC,YAAY,CAAC,QAAQ,KAAK,YAAY,CAAC,QAAQ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CAAC,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,EAAE,CAAC;gBAC5C,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,MAAM,CAAC,oBAAoB,EAAE,aAAa,CAAC,CAAC;YAC5F,CAAC;QACF,CAAC;aAAM,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACxC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,IAAI,EAAE,CAAC,MAAM,CAAc,IAAI,CAAC,EAAE,CAAC;oBAClC,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;gBACrE,CAAC;YACF,CAAC;QACF,CAAC;aAAM,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACxC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,IAAI,EAAE,CAAC,MAAM,CAAc,IAAI,CAAC,EAAE,CAAC;oBAClC,gBAAgB,CAAC,iBAAiB,CAAC,UAAU,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;gBACrE,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,aAAa,CAAC,YAAoB;QAC/C,OAAO,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC;YAChE,CAAC,CAAC,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YACvC,CAAC,CAAC,IAAI,MAAM,CAAC,IAAI,YAAY,GAAG,CAAC,CAAC;IACpC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"@twin.org/core\";\nimport type { IJsonSchema } from \"../models/IJsonSchema.js\";\n\n/**\n * Helper class for JSON Schema processing.\n */\nexport class JsonSchemaHelper {\n\t/**\n\t * The JSON Schema version used.\n\t */\n\tpublic static readonly SCHEMA_VERSION = \"https://json-schema.org/draft/2020-12/schema\";\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaObject The schema object to process.\n\t */\n\tpublic static processArrays(schemaObject: IJsonSchema): void {\n\t\tif (Is.object<IJsonSchema>(schemaObject)) {\n\t\t\t// new json schema version has prefixItems instead of items for arrays with fixed ordering\n\t\t\t// so convert any old style schemas that use items as an array\n\t\t\t// prefixItems validates fixed positions and is an array\n\t\t\t// items validates the rest and is an object\n\t\t\t// additionalItems no longer exists\n\t\t\t// https://www.learnjsonschema.com/2020-12/applicator/items/\n\t\t\t// https://www.learnjsonschema.com/2020-12/applicator/prefixitems/\n\t\t\tif (Is.array(schemaObject.items)) {\n\t\t\t\tif (Is.object(schemaObject.additionalItems)) {\n\t\t\t\t\t// If the items are an array then this is fixed ordering\n\t\t\t\t\t// so move to prefixItems and then do the same for additionalItems\n\t\t\t\t\tschemaObject.prefixItems = schemaObject.items;\n\t\t\t\t\tdelete schemaObject.items;\n\t\t\t\t\tschemaObject.items = schemaObject.additionalItems;\n\t\t\t\t} else if (\n\t\t\t\t\tIs.integer(schemaObject.minItems) &&\n\t\t\t\t\tIs.integer(schemaObject.maxItems) &&\n\t\t\t\t\tschemaObject.minItems === schemaObject.maxItems &&\n\t\t\t\t\tschemaObject.maxItems === schemaObject.items.length\n\t\t\t\t) {\n\t\t\t\t\t// There is a fixed number of items which matches the items array length\n\t\t\t\t\t// so move construct an allOf to enforce the fixed length\n\t\t\t\t\tschemaObject.items = { allOf: schemaObject.items };\n\t\t\t\t\tdelete schemaObject.minItems;\n\t\t\t\t\tdelete schemaObject.maxItems;\n\t\t\t\t} else {\n\t\t\t\t\t// no additional items so wrap in an anyOf\n\t\t\t\t\tschemaObject.items = { anyOf: schemaObject.items };\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// We can't use additionalItems in the new schema version\n\t\t\t// so just remove it after processing\n\t\t\tdelete schemaObject.additionalItems;\n\n\t\t\tJsonSchemaHelper.processSchemaDictionary(schemaObject.properties);\n\t\t\tJsonSchemaHelper.processArrays(schemaObject.additionalProperties);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.allOf);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.anyOf);\n\t\t\tJsonSchemaHelper.processSchemaArray(schemaObject.oneOf);\n\t\t}\n\t}\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaDictionary The schema object to process.\n\t */\n\tpublic static processSchemaDictionary(schemaDictionary?: { [key: string]: IJsonSchema }): void {\n\t\tif (Is.object(schemaDictionary)) {\n\t\t\tfor (const item of Object.values(schemaDictionary)) {\n\t\t\t\tif (Is.object<IJsonSchema>(item)) {\n\t\t\t\t\tJsonSchemaHelper.processArrays(item);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Process arrays in the schema object.\n\t * @param schemaArray The schema object to process.\n\t */\n\tpublic static processSchemaArray(schemaArray?: IJsonSchema[]): void {\n\t\tif (Is.arrayValue(schemaArray)) {\n\t\t\tfor (const item of schemaArray) {\n\t\t\t\tif (Is.object<IJsonSchema>(item)) {\n\t\t\t\t\tJsonSchemaHelper.processArrays(item);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Cleanup TypeScript markers from the type name.\n\t * @param typeName The definition string to clean up.\n\t * @returns The cleaned up definition string.\n\t */\n\tpublic static normaliseTypeName(typeName: string): string {\n\t\t// Remove the partial markers\n\t\tlet sTypeName = typeName.replace(/^Partial<I(.*?)>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Partial%3CI(.*?)%3E/g, \"$1\");\n\n\t\t// Remove the omit markers\n\t\tsTypeName = sTypeName.replace(/^Omit<I(.*?),.*>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Omit%3CI(.*?)%2C.*%3E/g, \"$1\");\n\n\t\t// Remove the pick markers\n\t\tsTypeName = sTypeName.replace(/^Pick<I(.*?),.*>/g, \"$1\");\n\t\tsTypeName = sTypeName.replace(/Pick%3CI(.*?)%2C.*%3E/g, \"$1\");\n\n\t\t// Cleanup the generic markers\n\t\tsTypeName = sTypeName.replace(/^(.*?)<I(.*?)>/g, \"$1<$2>\");\n\t\tsTypeName = sTypeName.replace(/(.*?)%3CI(.*?)%3E/g, \"$1<$2>\");\n\n\t\t// Cleanup the unknown markers\n\t\tsTypeName = sTypeName.replace(/<unknown>/g, \"\");\n\t\tsTypeName = sTypeName.replace(/%3Cunknown%3E/g, \"\");\n\n\t\t// Replace the other url markers\n\t\tsTypeName = sTypeName.replace(/%7C/g, \"|\").replace(/%3C/g, \"<\").replace(/%3E/g, \">\");\n\n\t\treturn sTypeName;\n\t}\n\n\t/**\n\t * Extract type from properties definition.\n\t * @param allTypes All the known types.\n\t * @param schema The schema to extract from.\n\t * @param output The output types.\n\t */\n\tpublic static extractTypesFromSchema(\n\t\tallTypes: { [id: string]: IJsonSchema },\n\t\tschema: IJsonSchema,\n\t\toutput: { [id: string]: IJsonSchema }\n\t): void {\n\t\tconst additionalTypes = [];\n\n\t\tif (Is.stringValue(schema.$ref)) {\n\t\t\tadditionalTypes.push(\n\t\t\t\tJsonSchemaHelper.normaliseTypeName(schema.$ref.replace(\"#/definitions/\", \"\"))\n\t\t\t);\n\t\t} else if (Is.object<IJsonSchema>(schema.items)) {\n\t\t\tif (Is.arrayValue<IJsonSchema>(schema.items)) {\n\t\t\t\tfor (const itemSchema of schema.items) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, itemSchema, output);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, schema.items, output);\n\t\t\t}\n\t\t} else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {\n\t\t\tif (Is.object(schema.properties)) {\n\t\t\t\tfor (const prop in schema.properties) {\n\t\t\t\t\tconst p = schema.properties[prop];\n\t\t\t\t\tif (Is.object<IJsonSchema>(p)) {\n\t\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, p, output);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (Is.object(schema.additionalProperties)) {\n\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, schema.additionalProperties, output);\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.anyOf)) {\n\t\t\tfor (const prop of schema.anyOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, prop, output);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.oneOf)) {\n\t\t\tfor (const prop of schema.oneOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(allTypes, prop, output);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (additionalTypes.length > 0) {\n\t\t\tJsonSchemaHelper.extractTypes(allTypes, additionalTypes, output);\n\t\t}\n\t}\n\n\t/**\n\t * Extract the required types from all the known schemas.\n\t * @param allSchemas All the known schemas.\n\t * @param requiredTypes The required types.\n\t * @param referencedSchemas The references schemas.\n\t */\n\tpublic static extractTypes(\n\t\tallSchemas: { [id: string]: IJsonSchema },\n\t\trequiredTypes: string[],\n\t\treferencedSchemas: { [id: string]: IJsonSchema }\n\t): void {\n\t\tfor (const typeKey of Object.keys(allSchemas)) {\n\t\t\tif (!referencedSchemas[typeKey]) {\n\t\t\t\tfor (const requiredType of requiredTypes) {\n\t\t\t\t\tconst regex = JsonSchemaHelper.stringToRegEx(requiredType);\n\n\t\t\t\t\tif (regex.test(typeKey)) {\n\t\t\t\t\t\treferencedSchemas[typeKey] = allSchemas[typeKey];\n\t\t\t\t\t\tJsonSchemaHelper.extractTypesFromSchema(\n\t\t\t\t\t\t\tallSchemas,\n\t\t\t\t\t\t\tallSchemas[typeKey],\n\t\t\t\t\t\t\treferencedSchemas\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Expand the types inline.\n\t * @param schemas The schemas to include the expanded types.\n\t * @param expandedTypes The types to expand.\n\t */\n\tpublic static expandTypes(schemas: { [id: string]: IJsonSchema }, expandedTypes: string[]): void {\n\t\tfor (const typeKey of Object.keys(schemas)) {\n\t\t\tconst schema = schemas[typeKey];\n\n\t\t\tJsonSchemaHelper.expandSchemaTypes(schemas, schema, expandedTypes);\n\t\t}\n\t}\n\n\t/**\n\t * Expand the types inline.\n\t * @param allSchemas All the known schemas.\n\t * @param schema The schemas to include the expanded types.\n\t * @param expandedTypes The types to expand.\n\t */\n\tpublic static expandSchemaTypes(\n\t\tallSchemas: { [id: string]: IJsonSchema },\n\t\tschema: IJsonSchema,\n\t\texpandedTypes: string[]\n\t): void {\n\t\tif (Is.stringValue(schema.$ref)) {\n\t\t\tfor (const expandedType of expandedTypes) {\n\t\t\t\tconst typeName = JsonSchemaHelper.normaliseTypeName(\n\t\t\t\t\tschema.$ref.replace(\"#/definitions/\", \"\")\n\t\t\t\t);\n\t\t\t\tconst regex = JsonSchemaHelper.stringToRegEx(expandedType);\n\t\t\t\tif (regex.test(typeName) && allSchemas[typeName]) {\n\t\t\t\t\tdelete schema.$ref;\n\t\t\t\t\tObject.assign(schema, allSchemas[typeName]);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.object<IJsonSchema>(schema.items)) {\n\t\t\tif (Is.arrayValue<IJsonSchema>(schema.items)) {\n\t\t\t\tfor (const itemSchema of schema.items) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, itemSchema, expandedTypes);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, schema.items, expandedTypes);\n\t\t\t}\n\t\t} else if (Is.object(schema.properties) || Is.object(schema.additionalProperties)) {\n\t\t\tif (Is.object(schema.properties)) {\n\t\t\t\tfor (const prop in schema.properties) {\n\t\t\t\t\tconst p = schema.properties[prop];\n\t\t\t\t\tif (Is.object<IJsonSchema>(p)) {\n\t\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, p, expandedTypes);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (Is.object(schema.additionalProperties)) {\n\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, schema.additionalProperties, expandedTypes);\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.anyOf)) {\n\t\t\tfor (const prop of schema.anyOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, prop, expandedTypes);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (Is.arrayValue(schema.oneOf)) {\n\t\t\tfor (const prop of schema.oneOf) {\n\t\t\t\tif (Is.object<IJsonSchema>(prop)) {\n\t\t\t\t\tJsonSchemaHelper.expandSchemaTypes(allSchemas, prop, expandedTypes);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Convert a string pattern to a regular expression.\n\t * @param matchPattern The pattern to convert.\n\t * @returns The regular expression.\n\t */\n\tpublic static stringToRegEx(matchPattern: string): RegExp {\n\t\treturn matchPattern.startsWith(\"/\") && matchPattern.endsWith(\"/\")\n\t\t\t? new RegExp(matchPattern.slice(1, -1))\n\t\t\t: new RegExp(`^${matchPattern}$`);\n\t}\n}\n"]}
@@ -1,12 +0,0 @@
1
- // Copyright 2024 IOTA Stiftung.
2
- // SPDX-License-Identifier: Apache-2.0.
3
- /**
4
- * Helper class for OpenAPI processing.
5
- */
6
- export class OpenApiHelper {
7
- /**
8
- * The OpenAPI version used.
9
- */
10
- static API_VERSION = "3.1.1";
11
- }
12
- //# sourceMappingURL=openApiHelper.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"openApiHelper.js","sourceRoot":"","sources":["../../../src/utils/openApiHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AAEvC;;GAEG;AACH,MAAM,OAAO,aAAa;IACzB;;OAEG;IACI,MAAM,CAAU,WAAW,GAAG,OAAO,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Helper class for OpenAPI processing.\n */\nexport class OpenApiHelper {\n\t/**\n\t * The OpenAPI version used.\n\t */\n\tpublic static readonly API_VERSION = \"3.1.1\";\n}\n"]}
@@ -1,5 +0,0 @@
1
- import type { AnySchemaObject } from "ajv/dist/2020.js";
2
- /**
3
- * Default schema type.
4
- */
5
- export type IJsonSchema = AnySchemaObject;
@@ -1,54 +0,0 @@
1
- import type { IJsonSchema } from "./IJsonSchema.js";
2
- import type { IOpenApiPathMethod } from "./IOpenApiPathMethod.js";
3
- import type { IOpenApiSecurityScheme } from "./IOpenApiSecurityScheme.js";
4
- /**
5
- * The Open API config definition.
6
- */
7
- export interface IOpenApi {
8
- /**
9
- * The open api version.
10
- */
11
- openapi: string;
12
- /**
13
- * Info.
14
- */
15
- info: {
16
- title: string;
17
- version: string;
18
- description: string;
19
- license?: {
20
- name: string;
21
- url: string;
22
- };
23
- };
24
- /**
25
- * The servers for the endpoints.
26
- */
27
- servers?: {
28
- url: string;
29
- }[];
30
- /**
31
- * Tags for the endpoints.
32
- */
33
- tags?: {
34
- name: string;
35
- description: string;
36
- }[];
37
- /**
38
- * The paths.
39
- */
40
- paths: {
41
- [path: string]: {
42
- [method: string]: IOpenApiPathMethod;
43
- };
44
- };
45
- /**
46
- * The components.
47
- */
48
- components?: {
49
- schemas?: IJsonSchema;
50
- securitySchemes?: {
51
- [name: string]: IOpenApiSecurityScheme;
52
- };
53
- };
54
- }
@@ -1,13 +0,0 @@
1
- /**
2
- * The Open API config definition.
3
- */
4
- export interface IOpenApiExample {
5
- /**
6
- * The summary of the example.
7
- */
8
- summary?: string;
9
- /**
10
- * The value of the example.
11
- */
12
- value: unknown;
13
- }
@@ -1,19 +0,0 @@
1
- /**
2
- * The Open API config definition.
3
- */
4
- export interface IOpenApiHeader {
5
- /**
6
- * The schema of the header.
7
- */
8
- schema?: {
9
- type: string;
10
- };
11
- /**
12
- * The description of the header.
13
- */
14
- description?: string;
15
- /**
16
- * The format of the header.
17
- */
18
- format?: string;
19
- }
@@ -1,65 +0,0 @@
1
- import type { IJsonSchema } from "./IJsonSchema.js";
2
- import type { IOpenApiExample } from "./IOpenApiExample.js";
3
- import type { IOpenApiResponse } from "./IOpenApiResponse.js";
4
- import type { JsonTypeName } from "./jsonTypeName.js";
5
- /**
6
- * The Open API config definition.
7
- */
8
- export interface IOpenApiPathMethod {
9
- /**
10
- * The operation id.
11
- */
12
- operationId: string;
13
- /**
14
- * Summary.
15
- */
16
- summary: string;
17
- /**
18
- * Tags.
19
- */
20
- tags?: string[];
21
- /**
22
- * Parameters.
23
- */
24
- parameters?: {
25
- name: string;
26
- in: string;
27
- description?: string;
28
- required: boolean;
29
- schema: {
30
- type?: JsonTypeName | JsonTypeName[];
31
- enum?: IJsonSchema[];
32
- $ref?: string;
33
- };
34
- style?: string;
35
- }[];
36
- /**
37
- * Request body.
38
- */
39
- requestBody?: {
40
- required: boolean;
41
- description?: string;
42
- content?: {
43
- [contentType: string]: {
44
- schema: {
45
- $ref: string;
46
- };
47
- examples?: {
48
- [id: string]: IOpenApiExample;
49
- };
50
- };
51
- };
52
- };
53
- /**
54
- * Response body.
55
- */
56
- responses?: {
57
- [code: string]: IOpenApiResponse;
58
- };
59
- /**
60
- * Security model for the API.
61
- */
62
- security?: {
63
- [name: string]: string[];
64
- }[];
65
- }
@@ -1,32 +0,0 @@
1
- import type { IOpenApiExample } from "./IOpenApiExample.js";
2
- import type { IOpenApiHeader } from "./IOpenApiHeader.js";
3
- /**
4
- * The Open API config definition.
5
- */
6
- export interface IOpenApiResponse {
7
- /**
8
- * Descriptions for the response.
9
- */
10
- description?: string;
11
- /**
12
- * Content for the response.
13
- */
14
- content?: {
15
- [contentType: string]: {
16
- schema: {
17
- type?: string;
18
- format?: string;
19
- $ref?: string;
20
- };
21
- examples?: {
22
- [id: string]: IOpenApiExample;
23
- };
24
- };
25
- };
26
- /**
27
- * The headers for the response.
28
- */
29
- headers?: {
30
- [id: string]: IOpenApiHeader;
31
- };
32
- }
@@ -1,25 +0,0 @@
1
- /**
2
- * The Open API config definition for security scheme.
3
- */
4
- export interface IOpenApiSecurityScheme {
5
- /**
6
- * The type of the security schema.
7
- */
8
- type?: string;
9
- /**
10
- * The scheme method.
11
- */
12
- scheme?: string;
13
- /**
14
- * The bearer format.
15
- */
16
- bearerFormat?: string;
17
- /**
18
- * Where is the token located.
19
- */
20
- in?: string;
21
- /**
22
- * What is the name of the token.
23
- */
24
- name?: string;
25
- }
@@ -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
- }
@@ -1,5 +0,0 @@
1
- import type { JSONType } from "ajv/dist/2020.js";
2
- /**
3
- * Default schema type.
4
- */
5
- export type JsonTypeName = JSONType;