@devlearning/swagger-generator 1.1.15 → 1.1.17

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 (58) hide show
  1. package/.vscode/launch.json +28 -28
  2. package/README-OLD.md +209 -209
  3. package/README.md +277 -277
  4. package/dist/api.constants.js +22 -22
  5. package/dist/generator.d.ts +4 -0
  6. package/dist/generator.js +118 -4
  7. package/dist/generators-writers/angular/api-angular-writer.js +38 -38
  8. package/dist/generators-writers/angular/constants.js +24 -24
  9. package/dist/generators-writers/angular/model-angular-writer.js +6 -6
  10. package/dist/generators-writers/dart/model-dart-writer.d.ts +1 -0
  11. package/dist/generators-writers/dart/model-dart-writer.js +13 -0
  12. package/dist/generators-writers/dart/templates/api.mustache +143 -143
  13. package/dist/generators-writers/dart/templates/enum.mustache +14 -14
  14. package/dist/generators-writers/dart/templates/model.mustache +20 -20
  15. package/dist/generators-writers/nextjs/api-nextjs-writer.js +12 -12
  16. package/dist/generators-writers/nextjs/constants.js +4 -4
  17. package/dist/generators-writers/nextjs/model-nextjs-writer.js +6 -6
  18. package/dist/models/swagger/swagger-component.d.ts +2 -2
  19. package/dist/models/swagger/swagger-schema.d.ts +1 -0
  20. package/package.json +49 -49
  21. package/src/api.constants.ts +26 -26
  22. package/src/generator-old.ts +449 -449
  23. package/src/generator.ts +752 -625
  24. package/src/generators-writers/angular/api-angular-writer.ts +187 -187
  25. package/src/generators-writers/angular/constants.ts +36 -36
  26. package/src/generators-writers/angular/model-angular-writer.ts +65 -65
  27. package/src/generators-writers/angular/normalizator.ts +41 -41
  28. package/src/generators-writers/dart/api-dart-writer.ts +303 -303
  29. package/src/generators-writers/dart/model-dart-writer.ts +212 -195
  30. package/src/generators-writers/dart/models/import-definition-dart.ts +5 -5
  31. package/src/generators-writers/dart/normalizator.ts +72 -72
  32. package/src/generators-writers/dart/templates/api.mustache +143 -143
  33. package/src/generators-writers/dart/templates/enum.mustache +14 -14
  34. package/src/generators-writers/dart/templates/model.mustache +20 -20
  35. package/src/generators-writers/nextjs/api-nextjs-writer.ts +157 -157
  36. package/src/generators-writers/nextjs/constants.ts +5 -5
  37. package/src/generators-writers/nextjs/model-nextjs-writer.ts +61 -61
  38. package/src/generators-writers/utils.ts +93 -93
  39. package/src/index.ts +103 -103
  40. package/src/models/api-dto.ts +17 -17
  41. package/src/models/enum-value-dto.ts +3 -3
  42. package/src/models/model-dto.ts +9 -9
  43. package/src/models/parameter-dto.ts +7 -7
  44. package/src/models/property-dto.ts +4 -4
  45. package/src/models/swagger/swagger-component-property.ts +11 -11
  46. package/src/models/swagger/swagger-component.ts +17 -17
  47. package/src/models/swagger/swagger-content.ts +4 -4
  48. package/src/models/swagger/swagger-info.ts +3 -3
  49. package/src/models/swagger/swagger-method.ts +7 -7
  50. package/src/models/swagger/swagger-schema.ts +21 -20
  51. package/src/models/swagger/swagger.ts +38 -38
  52. package/src/models/type-dto.ts +7 -7
  53. package/src/swagger-downloader.ts +46 -46
  54. package/src/utils/logger.ts +73 -73
  55. package/src/utils/swagger-validator.ts +89 -89
  56. package/tsconfig.json +33 -33
  57. package/dist/templates/api.mustache +0 -29
  58. package/dist/templates/model.mustache +0 -18
@@ -1,196 +1,213 @@
1
- import fs, { readFileSync, writeFileSync } from 'fs';
2
- import { ModelDto } from '../../models/model-dto.js';
3
- import Mustache from 'mustache';
4
- import { Utils } from '../utils.js';
5
- import { CommandLineArgs } from '../../index.js';
6
- import { ImportDefinitionDart } from './models/import-definition-dart.js';
7
- import { Normalizator } from './normalizator.js';
8
- import path from 'path';
9
- import { fileURLToPath } from 'url';
10
-
11
- interface ClassDefinitionDart {
12
- className?: string;
13
- fields?: { name: string; type: string }[];
14
- imports?: string[];
15
- }
16
-
17
- interface EnumDefinitionDart {
18
- enumName?: string;
19
- fields?: EnumItemDefinitionDart[];
20
- imports?: string[];
21
- }
22
-
23
- interface EnumItemDefinitionDart {
24
- name: string;
25
- value: string;
26
- isLast: boolean;
27
- }
28
-
29
- interface FieldDefinitionDart {
30
- name: string;
31
- type: string;
32
- typeName: string;
33
- nullable: string;
34
- required: string;
35
- jsonKeyAnnotation?: string;
36
- }
37
-
38
- interface ExportClassDefinitionDart {
39
- classes: ClassDefinitionDart[];
40
- }
41
-
42
- interface ExportEnumDefinitionDart {
43
- classes: EnumDefinitionDart[];
44
- }
45
-
46
- interface ExportModelItemDefinitionDart {
47
- filename?: string;
48
- }
49
-
50
- export class ModelDartWriter {
51
- private _commandLineArgs: CommandLineArgs;
52
-
53
- constructor(commandLineArgs: CommandLineArgs) {
54
- this._commandLineArgs = commandLineArgs;
55
- }
56
-
57
- write(models: ModelDto[]) {
58
- this.writeEnums(models.filter(m => m.modelType === 'enum'));
59
- this.writeClasses(models);
60
- }
61
-
62
- writeClasses(models: ModelDto[]) {
63
- const __filename = fileURLToPath(import.meta.url);
64
- const __dirname = path.dirname(__filename);
65
- const templatePath = path.join(__dirname, 'templates', 'model.mustache');
66
- const template = readFileSync(templatePath, 'utf-8');
67
-
68
- let importDirectory = this._commandLineArgs.outputDirectory;
69
- if (importDirectory.startsWith('lib/')) {
70
- importDirectory = importDirectory.slice('lib/'.length);
71
- }
72
-
73
- models.forEach(model => {
74
- if (model.modelType === 'class' || model.modelType === 'interface') {
75
- const normalizedInfo = Normalizator.getNormalizedInfo(model);
76
- const dartModel = <ClassDefinitionDart>{
77
- filename: normalizedInfo.filename,
78
- path: this._commandLineArgs.outputDirectory,
79
- className: normalizedInfo.modelName,
80
- fields: [],
81
- imports: [],
82
- };
83
-
84
- var imports = <ImportDefinitionDart[]>[];
85
-
86
- const needsDartIo = model.properties.some(p => {
87
- const normalized = (p.typeName ?? '').trim();
88
- return normalized === 'File' || (p.isArray && normalized === 'File');
89
- });
90
-
91
- if (needsDartIo) {
92
- imports.push({
93
- type: { typeName: 'dart:io' } as any,
94
- import: `import 'dart:io';`
95
- });
96
- }
97
-
98
- model.properties.forEach(property => {
99
- var fieldTypeName = Normalizator.mapTsTypeToDart(property.typeName);
100
-
101
- fieldTypeName = Normalizator.getNormalizedTypeName(fieldTypeName);
102
-
103
- const isFileField = property.typeName === 'File';
104
- const jsonKeyAnnotation = isFileField
105
- ? "@JsonKey(includeFromJson: false, includeToJson: false)"
106
- : undefined;
107
-
108
- dartModel.fields!.push(<FieldDefinitionDart>{
109
- name: property.name,
110
- type: property.isArray ? `List<${fieldTypeName}>` : fieldTypeName,
111
- typeName: property.typeName,
112
- nullable: property.nullable && !property.isArray ? '?' : '',
113
- required: property.nullable && !property.isArray ? '' : 'required ',
114
- jsonKeyAnnotation,
115
- });
116
-
117
- if (property.isTypeReference) {
118
- if (imports.findIndex(x => x.type.typeName == property.typeName) == -1) {
119
- models.forEach(currModel => {
120
- if (currModel.typeName === property.typeName) {
121
- const normalizedInfo = Normalizator.getNormalizedInfo(currModel);
122
- const importPath = this._buildImportPath(importDirectory, normalizedInfo.subPath, normalizedInfo.filename);
123
- imports.push({
124
- type: property,
125
- import: `import 'package:${this._commandLineArgs.package}/${importPath}';`
126
- });
127
- }
128
- });
129
- }
130
- }
131
- });
132
-
133
- dartModel.imports = imports.map(i => i.import);
134
-
135
- let destinationPath = `${this._commandLineArgs.outputDirectory}/${normalizedInfo.subPath}`;
136
-
137
- Utils.ensureDirectorySync(`${destinationPath}`);
138
-
139
- let result = Mustache.render(template, dartModel);
140
- writeFileSync(`${destinationPath}/${normalizedInfo.filename}.dart`, result, 'utf-8');
141
- }
142
- });
143
- }
144
-
145
- writeEnums(models: ModelDto[]) {
146
- const __filename = fileURLToPath(import.meta.url);
147
- const __dirname = path.dirname(__filename);
148
- const templatePath = path.join(__dirname, 'templates', 'enum.mustache');
149
- const template = readFileSync(templatePath, 'utf-8');
150
-
151
- let importDirectory = this._commandLineArgs.outputDirectory;
152
- if (importDirectory.startsWith('lib/')) {
153
- importDirectory = importDirectory.slice('lib/'.length);
154
- }
155
-
156
- models.forEach(model => {
157
- const normalizedInfo = Normalizator.getNormalizedInfo(model);
158
- const dartModel = <EnumDefinitionDart>{
159
- enumName: normalizedInfo.modelName,
160
- path: this._commandLineArgs.outputDirectory,
161
- fields: [],
162
- };
163
-
164
- model.enumValues.forEach(enumItem => {
165
- dartModel.fields!.push(<EnumItemDefinitionDart>{
166
- name: enumItem.name,
167
- value: enumItem.value,
168
- isLast: false,
169
- });
170
- });
171
-
172
- const lastIndex = dartModel.fields!.length - 1;
173
- dartModel.fields![lastIndex].isLast = true;
174
-
175
- let destinationPath = `${this._commandLineArgs.outputDirectory}/${normalizedInfo.subPath}`;
176
-
177
- Utils.ensureDirectorySync(`${destinationPath}`);
178
-
179
- let result = Mustache.render(template, dartModel);
180
- writeFileSync(`${destinationPath}/${normalizedInfo.filename}.dart`, result, 'utf-8');
181
- });
182
- }
183
-
184
- private _buildImportPath(importDirectory: string, subPath: string, filename: string): string {
185
- // Rimuove slash iniziali e finali da ogni parte
186
- const parts = [importDirectory, subPath]
187
- .filter(p => p && p.length > 0)
188
- .map(p => p.replace(/^\/+|\/+$/g, ''));
189
-
190
- // Aggiunge il filename alla fine
191
- parts.push(`${filename}.dart`);
192
-
193
- // Unisce con un singolo slash
194
- return parts.join('/');
195
- }
1
+ import fs, { readFileSync, writeFileSync } from 'fs';
2
+ import { ModelDto } from '../../models/model-dto.js';
3
+ import Mustache from 'mustache';
4
+ import { Utils } from '../utils.js';
5
+ import { CommandLineArgs } from '../../index.js';
6
+ import { ImportDefinitionDart } from './models/import-definition-dart.js';
7
+ import { Normalizator } from './normalizator.js';
8
+ import path from 'path';
9
+ import { fileURLToPath } from 'url';
10
+
11
+ interface ClassDefinitionDart {
12
+ className?: string;
13
+ fields?: { name: string; type: string }[];
14
+ imports?: string[];
15
+ }
16
+
17
+ interface EnumDefinitionDart {
18
+ enumName?: string;
19
+ fields?: EnumItemDefinitionDart[];
20
+ imports?: string[];
21
+ valueType?: string;
22
+ }
23
+
24
+ interface EnumItemDefinitionDart {
25
+ name: string;
26
+ value: string;
27
+ isLast: boolean;
28
+ }
29
+
30
+ interface FieldDefinitionDart {
31
+ name: string;
32
+ type: string;
33
+ typeName: string;
34
+ nullable: string;
35
+ required: string;
36
+ jsonKeyAnnotation?: string;
37
+ }
38
+
39
+ interface ExportClassDefinitionDart {
40
+ classes: ClassDefinitionDart[];
41
+ }
42
+
43
+ interface ExportEnumDefinitionDart {
44
+ classes: EnumDefinitionDart[];
45
+ }
46
+
47
+ interface ExportModelItemDefinitionDart {
48
+ filename?: string;
49
+ }
50
+
51
+ export class ModelDartWriter {
52
+ private _commandLineArgs: CommandLineArgs;
53
+
54
+ constructor(commandLineArgs: CommandLineArgs) {
55
+ this._commandLineArgs = commandLineArgs;
56
+ }
57
+
58
+ write(models: ModelDto[]) {
59
+ this.writeEnums(models.filter(m => m.modelType === 'enum'));
60
+ this.writeClasses(models);
61
+ }
62
+
63
+ writeClasses(models: ModelDto[]) {
64
+ const __filename = fileURLToPath(import.meta.url);
65
+ const __dirname = path.dirname(__filename);
66
+ const templatePath = path.join(__dirname, 'templates', 'model.mustache');
67
+ const template = readFileSync(templatePath, 'utf-8');
68
+
69
+ let importDirectory = this._commandLineArgs.outputDirectory;
70
+ if (importDirectory.startsWith('lib/')) {
71
+ importDirectory = importDirectory.slice('lib/'.length);
72
+ }
73
+
74
+ models.forEach(model => {
75
+ if (model.modelType === 'class' || model.modelType === 'interface') {
76
+ const normalizedInfo = Normalizator.getNormalizedInfo(model);
77
+ const dartModel = <ClassDefinitionDart>{
78
+ filename: normalizedInfo.filename,
79
+ path: this._commandLineArgs.outputDirectory,
80
+ className: normalizedInfo.modelName,
81
+ fields: [],
82
+ imports: [],
83
+ };
84
+
85
+ var imports = <ImportDefinitionDart[]>[];
86
+
87
+ const needsDartIo = model.properties.some(p => {
88
+ const normalized = (p.typeName ?? '').trim();
89
+ return normalized === 'File' || (p.isArray && normalized === 'File');
90
+ });
91
+
92
+ if (needsDartIo) {
93
+ imports.push({
94
+ type: { typeName: 'dart:io' } as any,
95
+ import: `import 'dart:io';`
96
+ });
97
+ }
98
+
99
+ model.properties.forEach(property => {
100
+ var fieldTypeName = Normalizator.mapTsTypeToDart(property.typeName);
101
+
102
+ fieldTypeName = Normalizator.getNormalizedTypeName(fieldTypeName);
103
+
104
+ const isFileField = property.typeName === 'File';
105
+ const jsonKeyAnnotation = isFileField
106
+ ? "@JsonKey(includeFromJson: false, includeToJson: false)"
107
+ : undefined;
108
+
109
+ dartModel.fields!.push(<FieldDefinitionDart>{
110
+ name: property.name,
111
+ type: property.isArray ? `List<${fieldTypeName}>` : fieldTypeName,
112
+ typeName: property.typeName,
113
+ nullable: property.nullable && !property.isArray ? '?' : '',
114
+ required: property.nullable && !property.isArray ? '' : 'required ',
115
+ jsonKeyAnnotation,
116
+ });
117
+
118
+ if (property.isTypeReference) {
119
+ if (imports.findIndex(x => x.type.typeName == property.typeName) == -1) {
120
+ models.forEach(currModel => {
121
+ if (currModel.typeName === property.typeName) {
122
+ const normalizedInfo = Normalizator.getNormalizedInfo(currModel);
123
+ const importPath = this._buildImportPath(importDirectory, normalizedInfo.subPath, normalizedInfo.filename);
124
+ imports.push({
125
+ type: property,
126
+ import: `import 'package:${this._commandLineArgs.package}/${importPath}';`
127
+ });
128
+ }
129
+ });
130
+ }
131
+ }
132
+ });
133
+
134
+ dartModel.imports = imports.map(i => i.import);
135
+
136
+ let destinationPath = `${this._commandLineArgs.outputDirectory}/${normalizedInfo.subPath}`;
137
+
138
+ Utils.ensureDirectorySync(`${destinationPath}`);
139
+
140
+ let result = Mustache.render(template, dartModel);
141
+ writeFileSync(`${destinationPath}/${normalizedInfo.filename}.dart`, result, 'utf-8');
142
+ }
143
+ });
144
+ }
145
+
146
+ writeEnums(models: ModelDto[]) {
147
+ const __filename = fileURLToPath(import.meta.url);
148
+ const __dirname = path.dirname(__filename);
149
+ const templatePath = path.join(__dirname, 'templates', 'enum.mustache');
150
+ const template = readFileSync(templatePath, 'utf-8');
151
+
152
+ let importDirectory = this._commandLineArgs.outputDirectory;
153
+ if (importDirectory.startsWith('lib/')) {
154
+ importDirectory = importDirectory.slice('lib/'.length);
155
+ }
156
+
157
+ models.forEach(model => {
158
+ const normalizedInfo = Normalizator.getNormalizedInfo(model);
159
+
160
+ const valueType = this._inferDartEnumValueType(model.enumValues);
161
+ const dartModel = <EnumDefinitionDart>{
162
+ enumName: normalizedInfo.modelName,
163
+ path: this._commandLineArgs.outputDirectory,
164
+ fields: [],
165
+ valueType,
166
+ };
167
+
168
+ model.enumValues.forEach(enumItem => {
169
+ dartModel.fields!.push(<EnumItemDefinitionDart>{
170
+ name: enumItem.name,
171
+ value: enumItem.value,
172
+ isLast: false,
173
+ });
174
+ });
175
+
176
+ const lastIndex = dartModel.fields!.length - 1;
177
+ dartModel.fields![lastIndex].isLast = true;
178
+
179
+ let destinationPath = `${this._commandLineArgs.outputDirectory}/${normalizedInfo.subPath}`;
180
+
181
+ Utils.ensureDirectorySync(`${destinationPath}`);
182
+
183
+ let result = Mustache.render(template, dartModel);
184
+ writeFileSync(`${destinationPath}/${normalizedInfo.filename}.dart`, result, 'utf-8');
185
+ });
186
+ }
187
+
188
+ private _inferDartEnumValueType(enumValues: { value: string }[]): string {
189
+ // If values are unquoted and numeric, assume int; otherwise String.
190
+ // Generator will quote string enums using JSON.stringify ("...").
191
+ if (!enumValues || enumValues.length === 0) return 'String';
192
+
193
+ const allNumeric = enumValues.every(v => {
194
+ const trimmed = (v.value ?? '').trim();
195
+ return /^-?\d+$/.test(trimmed);
196
+ });
197
+
198
+ return allNumeric ? 'int' : 'String';
199
+ }
200
+
201
+ private _buildImportPath(importDirectory: string, subPath: string, filename: string): string {
202
+ // Rimuove slash iniziali e finali da ogni parte
203
+ const parts = [importDirectory, subPath]
204
+ .filter(p => p && p.length > 0)
205
+ .map(p => p.replace(/^\/+|\/+$/g, ''));
206
+
207
+ // Aggiunge il filename alla fine
208
+ parts.push(`${filename}.dart`);
209
+
210
+ // Unisce con un singolo slash
211
+ return parts.join('/');
212
+ }
196
213
  }
@@ -1,6 +1,6 @@
1
- import { TypeDto } from "../../../models/type-dto.js";
2
-
3
- export interface ImportDefinitionDart {
4
- type: TypeDto;
5
- import: string
1
+ import { TypeDto } from "../../../models/type-dto.js";
2
+
3
+ export interface ImportDefinitionDart {
4
+ type: TypeDto;
5
+ import: string
6
6
  }
@@ -1,73 +1,73 @@
1
- import { ModelDto } from "../../models/model-dto.js";
2
- import { Utils } from "../utils.js";
3
-
4
- export class Normalizator {
5
-
6
- public static getNormalizedInfo(model: ModelDto) {
7
- let subPath = '';
8
- let filename = '';
9
- let modelName = '';
10
-
11
- if (model.name && model.name.includes('.')) {
12
- subPath = model.name.split('.').slice(0, -1).join('');
13
- subPath = `${Utils.toDartFileName(subPath)}/models`;
14
- filename = model.name.split('.').pop()!;
15
- modelName = Utils.toDartClassName(filename) ?? filename;
16
- filename = Utils.toDartFileName(filename!);
17
- } else {
18
- subPath = '';
19
- filename = Utils.toDartFileName(model.name);
20
- modelName = Utils.toDartClassName(model.name) ?? model.name;
21
- }
22
-
23
- return {
24
- filename: filename,
25
- subPath: subPath,
26
- modelName: modelName,
27
- }
28
- }
29
-
30
- public static getNormalizedTypeName(typeName: string): string {
31
- let formattedTypeName = typeName.split('.').pop()!;
32
- return Utils.toDartClassName(formattedTypeName) ?? typeName;
33
- }
34
-
35
- public static mapTsTypeToDart(type: string): string {
36
- const normalized = type.trim().toLowerCase();
37
-
38
- if (normalized.endsWith("[]")) {
39
- const inner = normalized.slice(0, -2).trim();
40
- return `List<${Normalizator.mapTsTypeToDart(inner)}>`;
41
- }
42
-
43
- switch (normalized) {
44
- case "string":
45
- case "uuid":
46
- case "date":
47
- return "String";
48
- case "dateTime":
49
- return "DateTime";
50
- case "number":
51
- case "float":
52
- case "double":
53
- return "double";
54
- case "integer":
55
- case "int":
56
- case "long":
57
- return "int";
58
- case "boolean":
59
- case "bool":
60
- return "bool";
61
- case "any":
62
- case "object":
63
- return "dynamic";
64
- case "null":
65
- case "undefined":
66
- return "Null";
67
- default:
68
- // per tipi personalizzati (es. modelli) restituisci con PascalCase
69
- return Utils.toPascalCase(type);
70
- }
71
- }
72
-
1
+ import { ModelDto } from "../../models/model-dto.js";
2
+ import { Utils } from "../utils.js";
3
+
4
+ export class Normalizator {
5
+
6
+ public static getNormalizedInfo(model: ModelDto) {
7
+ let subPath = '';
8
+ let filename = '';
9
+ let modelName = '';
10
+
11
+ if (model.name && model.name.includes('.')) {
12
+ subPath = model.name.split('.').slice(0, -1).join('');
13
+ subPath = `${Utils.toDartFileName(subPath)}/models`;
14
+ filename = model.name.split('.').pop()!;
15
+ modelName = Utils.toDartClassName(filename) ?? filename;
16
+ filename = Utils.toDartFileName(filename!);
17
+ } else {
18
+ subPath = '';
19
+ filename = Utils.toDartFileName(model.name);
20
+ modelName = Utils.toDartClassName(model.name) ?? model.name;
21
+ }
22
+
23
+ return {
24
+ filename: filename,
25
+ subPath: subPath,
26
+ modelName: modelName,
27
+ }
28
+ }
29
+
30
+ public static getNormalizedTypeName(typeName: string): string {
31
+ let formattedTypeName = typeName.split('.').pop()!;
32
+ return Utils.toDartClassName(formattedTypeName) ?? typeName;
33
+ }
34
+
35
+ public static mapTsTypeToDart(type: string): string {
36
+ const normalized = type.trim().toLowerCase();
37
+
38
+ if (normalized.endsWith("[]")) {
39
+ const inner = normalized.slice(0, -2).trim();
40
+ return `List<${Normalizator.mapTsTypeToDart(inner)}>`;
41
+ }
42
+
43
+ switch (normalized) {
44
+ case "string":
45
+ case "uuid":
46
+ case "date":
47
+ return "String";
48
+ case "dateTime":
49
+ return "DateTime";
50
+ case "number":
51
+ case "float":
52
+ case "double":
53
+ return "double";
54
+ case "integer":
55
+ case "int":
56
+ case "long":
57
+ return "int";
58
+ case "boolean":
59
+ case "bool":
60
+ return "bool";
61
+ case "any":
62
+ case "object":
63
+ return "dynamic";
64
+ case "null":
65
+ case "undefined":
66
+ return "Null";
67
+ default:
68
+ // per tipi personalizzati (es. modelli) restituisci con PascalCase
69
+ return Utils.toPascalCase(type);
70
+ }
71
+ }
72
+
73
73
  }