@devlearning/swagger-generator 1.1.20 → 1.1.22
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/.vscode/launch.json +28 -28
- package/README-OLD.md +209 -209
- package/README.md +277 -277
- package/dist/api.constants.js +22 -22
- package/dist/generators-writers/angular/api-angular-writer.js +38 -38
- package/dist/generators-writers/angular/constants.js +24 -24
- package/dist/generators-writers/angular/model-angular-writer.js +6 -6
- package/dist/generators-writers/dart/model-dart-writer.js +14 -2
- package/dist/generators-writers/dart/templates/api.mustache +143 -143
- package/dist/generators-writers/dart/templates/enum.mustache +14 -14
- package/dist/generators-writers/dart/templates/model.mustache +23 -20
- package/dist/generators-writers/nextjs/api-nextjs-writer.js +12 -12
- package/dist/generators-writers/nextjs/constants.js +4 -4
- package/dist/generators-writers/nextjs/model-nextjs-writer.js +6 -6
- package/dist/generators-writers/utils.d.ts +1 -0
- package/dist/generators-writers/utils.js +16 -4
- package/dist/templates/api.mustache +29 -0
- package/dist/templates/model.mustache +18 -0
- package/package.json +49 -49
- package/src/api.constants.ts +26 -26
- package/src/generator-old.ts +449 -449
- package/src/generator.ts +752 -752
- package/src/generators-writers/angular/api-angular-writer.ts +187 -187
- package/src/generators-writers/angular/constants.ts +36 -36
- package/src/generators-writers/angular/model-angular-writer.ts +65 -65
- package/src/generators-writers/angular/normalizator.ts +41 -41
- package/src/generators-writers/dart/api-dart-writer.ts +303 -303
- package/src/generators-writers/dart/model-dart-writer.ts +226 -212
- package/src/generators-writers/dart/models/import-definition-dart.ts +5 -5
- package/src/generators-writers/dart/normalizator.ts +72 -72
- package/src/generators-writers/dart/templates/api.mustache +143 -143
- package/src/generators-writers/dart/templates/enum.mustache +14 -14
- package/src/generators-writers/dart/templates/model.mustache +23 -20
- package/src/generators-writers/nextjs/api-nextjs-writer.ts +157 -157
- package/src/generators-writers/nextjs/constants.ts +5 -5
- package/src/generators-writers/nextjs/model-nextjs-writer.ts +61 -61
- package/src/generators-writers/utils.ts +111 -93
- package/src/index.ts +103 -103
- package/src/models/api-dto.ts +17 -17
- package/src/models/enum-value-dto.ts +3 -3
- package/src/models/model-dto.ts +9 -9
- package/src/models/parameter-dto.ts +7 -7
- package/src/models/property-dto.ts +4 -4
- package/src/models/swagger/swagger-component-property.ts +11 -11
- package/src/models/swagger/swagger-component.ts +17 -17
- package/src/models/swagger/swagger-content.ts +4 -4
- package/src/models/swagger/swagger-info.ts +3 -3
- package/src/models/swagger/swagger-method.ts +7 -7
- package/src/models/swagger/swagger-schema.ts +21 -21
- package/src/models/swagger/swagger.ts +38 -38
- package/src/models/type-dto.ts +7 -7
- package/src/swagger-downloader.ts +46 -46
- package/src/utils/logger.ts +73 -73
- package/src/utils/swagger-validator.ts +89 -89
- package/tsconfig.json +33 -33
|
@@ -27,14 +27,14 @@ export class ApiAngularWriter {
|
|
|
27
27
|
if (apiNameNormalized.includes('TicketFile')) {
|
|
28
28
|
debugger;
|
|
29
29
|
}
|
|
30
|
-
let apiString = `
|
|
31
|
-
public ${apiNameNormalized}(${parametersString}): Observable<${returnTypeString}> {
|
|
32
|
-
${queryParametersPreparation}${requestPreparation}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
33
|
-
.pipe(
|
|
34
|
-
map(x => this._handleResponse(x)),
|
|
35
|
-
catchError((err, obs) => this._handleError(err, obs, skipErrorHandling))
|
|
36
|
-
);
|
|
37
|
-
}
|
|
30
|
+
let apiString = `
|
|
31
|
+
public ${apiNameNormalized}(${parametersString}): Observable<${returnTypeString}> {
|
|
32
|
+
${queryParametersPreparation}${requestPreparation}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
33
|
+
.pipe(
|
|
34
|
+
map(x => this._handleResponse(x)),
|
|
35
|
+
catchError((err, obs) => this._handleError(err, obs, skipErrorHandling))
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
38
|
`;
|
|
39
39
|
return apiString;
|
|
40
40
|
}
|
|
@@ -68,21 +68,21 @@ export class ApiAngularWriter {
|
|
|
68
68
|
_queryParametersPreparationStatement(parameter) {
|
|
69
69
|
if (parameter.nullable) {
|
|
70
70
|
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
71
|
-
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined && ${parameter.name}.isValid() ? encodeURIComponent(this._momentToString(${parameter.name})) : '';
|
|
71
|
+
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined && ${parameter.name}.isValid() ? encodeURIComponent(this._momentToString(${parameter.name})) : '';
|
|
72
72
|
`;
|
|
73
73
|
}
|
|
74
74
|
else {
|
|
75
|
-
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
75
|
+
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
76
76
|
`;
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
else {
|
|
80
80
|
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
81
|
-
return `let ${parameter.name}Param: string = encodeURIComponent(this._momentToString(${parameter.name}));
|
|
81
|
+
return `let ${parameter.name}Param: string = encodeURIComponent(this._momentToString(${parameter.name}));
|
|
82
82
|
`;
|
|
83
83
|
}
|
|
84
84
|
else {
|
|
85
|
-
return `let ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
85
|
+
return `let ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
86
86
|
`;
|
|
87
87
|
}
|
|
88
88
|
}
|
|
@@ -111,44 +111,44 @@ export class ApiAngularWriter {
|
|
|
111
111
|
return '';
|
|
112
112
|
}
|
|
113
113
|
if (api.isMultiPart) {
|
|
114
|
-
return `let wrappedRequest = this._handleMultipart(request);
|
|
114
|
+
return `let wrappedRequest = this._handleMultipart(request);
|
|
115
115
|
`;
|
|
116
116
|
}
|
|
117
117
|
else {
|
|
118
|
-
return `let wrappedRequest = this._handleRequest(request);
|
|
118
|
+
return `let wrappedRequest = this._handleRequest(request);
|
|
119
119
|
`;
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
_writeFile(apis) {
|
|
123
123
|
const className = this._commandLineArgs.apiClientName || 'ApiAutogeneratedService';
|
|
124
124
|
const apiPre = this._getApiPre(className);
|
|
125
|
-
fs.writeFileSync(this._commandLineArgs.outputDirectory + "/api.autogenerated.ts", `${apiPre}
|
|
126
|
-
${apis}
|
|
125
|
+
fs.writeFileSync(this._commandLineArgs.outputDirectory + "/api.autogenerated.ts", `${apiPre}
|
|
126
|
+
${apis}
|
|
127
127
|
${API_POST}`, { flag: 'w' });
|
|
128
128
|
}
|
|
129
129
|
_getApiPre(className) {
|
|
130
|
-
return `import { HttpClient } from '@angular/common/http';
|
|
131
|
-
import { Observable, catchError, map } from 'rxjs';
|
|
132
|
-
import * as Models from './model.autogenerated';
|
|
133
|
-
import { HttpHeaders } from "@angular/common/http";
|
|
134
|
-
|
|
135
|
-
export const httpOptions = {
|
|
136
|
-
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
export const httpOptionsMultipart = {};
|
|
140
|
-
|
|
141
|
-
export abstract class ${className} {
|
|
142
|
-
constructor(
|
|
143
|
-
public _http: HttpClient,
|
|
144
|
-
public _baseUrl: string,
|
|
145
|
-
) { }
|
|
146
|
-
|
|
147
|
-
protected abstract _momentToString(moment: moment.Moment): string;
|
|
148
|
-
protected abstract _handleRequest<T>(request: T): T;
|
|
149
|
-
protected abstract _handleMultipart<T>(request: T): FormData;
|
|
150
|
-
protected abstract _handleResponse<T>(response: T): T;
|
|
151
|
-
protected abstract _handleError(error: any, obs: any): Observable<never>;
|
|
130
|
+
return `import { HttpClient } from '@angular/common/http';
|
|
131
|
+
import { Observable, catchError, map } from 'rxjs';
|
|
132
|
+
import * as Models from './model.autogenerated';
|
|
133
|
+
import { HttpHeaders } from "@angular/common/http";
|
|
134
|
+
|
|
135
|
+
export const httpOptions = {
|
|
136
|
+
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export const httpOptionsMultipart = {};
|
|
140
|
+
|
|
141
|
+
export abstract class ${className} {
|
|
142
|
+
constructor(
|
|
143
|
+
public _http: HttpClient,
|
|
144
|
+
public _baseUrl: string,
|
|
145
|
+
) { }
|
|
146
|
+
|
|
147
|
+
protected abstract _momentToString(moment: moment.Moment): string;
|
|
148
|
+
protected abstract _handleRequest<T>(request: T): T;
|
|
149
|
+
protected abstract _handleMultipart<T>(request: T): FormData;
|
|
150
|
+
protected abstract _handleResponse<T>(response: T): T;
|
|
151
|
+
protected abstract _handleError(error: any, obs: any): Observable<never>;
|
|
152
152
|
`;
|
|
153
153
|
}
|
|
154
154
|
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
export const API_PRE = `import { HttpClient } from '@angular/common/http';
|
|
2
|
-
import { Observable, catchError, map } from 'rxjs';
|
|
3
|
-
import * as Models from './model.autogenerated';
|
|
4
|
-
import { HttpHeaders } from "@angular/common/http";
|
|
5
|
-
|
|
6
|
-
export const httpOptions = {
|
|
7
|
-
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const httpOptionsMultipart = {};
|
|
11
|
-
|
|
12
|
-
export abstract class ApiAutogeneratedService {
|
|
13
|
-
constructor(
|
|
14
|
-
public _http: HttpClient,
|
|
15
|
-
public _baseUrl: string,
|
|
16
|
-
) { }
|
|
17
|
-
|
|
18
|
-
protected abstract _momentToString(moment: moment.Moment): string;
|
|
19
|
-
protected abstract _handleRequest<T>(request: T): T;
|
|
20
|
-
protected abstract _handleMultipart<T>(request: T): FormData;
|
|
21
|
-
protected abstract _handleResponse<T>(response: T): T;
|
|
22
|
-
protected abstract _handleError<T>(error: any, obs: Observable<T>, skipErrorHandling: boolean): Observable<never>;
|
|
1
|
+
export const API_PRE = `import { HttpClient } from '@angular/common/http';
|
|
2
|
+
import { Observable, catchError, map } from 'rxjs';
|
|
3
|
+
import * as Models from './model.autogenerated';
|
|
4
|
+
import { HttpHeaders } from "@angular/common/http";
|
|
5
|
+
|
|
6
|
+
export const httpOptions = {
|
|
7
|
+
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const httpOptionsMultipart = {};
|
|
11
|
+
|
|
12
|
+
export abstract class ApiAutogeneratedService {
|
|
13
|
+
constructor(
|
|
14
|
+
public _http: HttpClient,
|
|
15
|
+
public _baseUrl: string,
|
|
16
|
+
) { }
|
|
17
|
+
|
|
18
|
+
protected abstract _momentToString(moment: moment.Moment): string;
|
|
19
|
+
protected abstract _handleRequest<T>(request: T): T;
|
|
20
|
+
protected abstract _handleMultipart<T>(request: T): FormData;
|
|
21
|
+
protected abstract _handleResponse<T>(response: T): T;
|
|
22
|
+
protected abstract _handleError<T>(error: any, obs: Observable<T>, skipErrorHandling: boolean): Observable<never>;
|
|
23
23
|
`;
|
|
24
24
|
export const API_POST = `}`;
|
|
25
|
-
export const MODEL_PRE = `import * as moment from 'moment';
|
|
25
|
+
export const MODEL_PRE = `import * as moment from 'moment';
|
|
26
26
|
`;
|
|
27
|
-
export const MODEL_POST = `
|
|
27
|
+
export const MODEL_POST = `
|
|
28
28
|
`;
|
|
@@ -15,10 +15,10 @@ export class ModelAngularWriter {
|
|
|
15
15
|
this._writeFile(modelString);
|
|
16
16
|
}
|
|
17
17
|
_modelString(model) {
|
|
18
|
-
let modelString = `
|
|
19
|
-
export ${model.modelType} ${Utils.toPascalCase(model.name)} {
|
|
20
|
-
${this._properties(model)}${this._enumValues(model)}
|
|
21
|
-
}
|
|
18
|
+
let modelString = `
|
|
19
|
+
export ${model.modelType} ${Utils.toPascalCase(model.name)} {
|
|
20
|
+
${this._properties(model)}${this._enumValues(model)}
|
|
21
|
+
}
|
|
22
22
|
`;
|
|
23
23
|
return modelString;
|
|
24
24
|
}
|
|
@@ -41,8 +41,8 @@ ${this._properties(model)}${this._enumValues(model)}
|
|
|
41
41
|
return enumValuesString.trimEnd();
|
|
42
42
|
}
|
|
43
43
|
_writeFile(models) {
|
|
44
|
-
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts", `${MODEL_PRE}
|
|
45
|
-
${models}
|
|
44
|
+
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts", `${MODEL_PRE}
|
|
45
|
+
${models}
|
|
46
46
|
${MODEL_POST}`, { flag: 'w' });
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -47,16 +47,28 @@ export class ModelDartWriter {
|
|
|
47
47
|
var fieldTypeName = Normalizator.mapTsTypeToDart(property.typeName);
|
|
48
48
|
fieldTypeName = Normalizator.getNormalizedTypeName(fieldTypeName);
|
|
49
49
|
const isFileField = property.typeName === 'File';
|
|
50
|
+
const isFileArrayField = isFileField && property.isArray === true;
|
|
50
51
|
const jsonKeyAnnotation = isFileField
|
|
51
52
|
? "@JsonKey(includeFromJson: false, includeToJson: false)"
|
|
52
53
|
: undefined;
|
|
54
|
+
const defaultAnnotation = isFileArrayField
|
|
55
|
+
? "@Default(<File>[])"
|
|
56
|
+
: undefined;
|
|
57
|
+
// File fields are excluded from JSON serialization; they must not be `required` for fromJson.
|
|
58
|
+
const nullable = isFileField && !property.isArray
|
|
59
|
+
? '?'
|
|
60
|
+
: (property.nullable && !property.isArray ? '?' : '');
|
|
61
|
+
const required = isFileField
|
|
62
|
+
? ''
|
|
63
|
+
: (property.nullable && !property.isArray ? '' : 'required ');
|
|
53
64
|
dartModel.fields.push({
|
|
54
65
|
name: property.name,
|
|
55
66
|
type: property.isArray ? `List<${fieldTypeName}>` : fieldTypeName,
|
|
56
67
|
typeName: property.typeName,
|
|
57
|
-
nullable
|
|
58
|
-
required
|
|
68
|
+
nullable,
|
|
69
|
+
required,
|
|
59
70
|
jsonKeyAnnotation,
|
|
71
|
+
defaultAnnotation,
|
|
60
72
|
});
|
|
61
73
|
if (property.isTypeReference) {
|
|
62
74
|
if (imports.findIndex(x => x.type.typeName == property.typeName) == -1) {
|
|
@@ -1,143 +1,143 @@
|
|
|
1
|
-
import 'package:{{package}}/core/di/injector.dart';
|
|
2
|
-
import 'package:dio/dio.dart';
|
|
3
|
-
{{#imports}}
|
|
4
|
-
{{{.}}}
|
|
5
|
-
{{/imports}}
|
|
6
|
-
|
|
7
|
-
class {{apiClassName}} {
|
|
8
|
-
final Dio _dio;
|
|
9
|
-
|
|
10
|
-
{{apiClassName}}() : _dio = getIt<Dio>();
|
|
11
|
-
|
|
12
|
-
{{#endpoints}}
|
|
13
|
-
Future<{{responseType}}> {{methodName}}(
|
|
14
|
-
{{#haveRequest}}{{requestType}} request,{{/haveRequest}}
|
|
15
|
-
{{#pathParams}}
|
|
16
|
-
{{type}}{{#nullable}}?{{/nullable}} {{name}},
|
|
17
|
-
{{/pathParams}}
|
|
18
|
-
{{#queryParams}}
|
|
19
|
-
{{type}}{{#nullable}}?{{/nullable}} {{name}},
|
|
20
|
-
{{/queryParams}}
|
|
21
|
-
) async {
|
|
22
|
-
{{#isMultiPart}}
|
|
23
|
-
final formData = FormData();
|
|
24
|
-
{{#multipartFields}}
|
|
25
|
-
{{#isFile}}
|
|
26
|
-
{{#isArray}}
|
|
27
|
-
{{^nullable}}
|
|
28
|
-
for (final file in request.{{name}}) {
|
|
29
|
-
formData.files.add(MapEntry('{{name}}', await MultipartFile.fromFile(file.path)));
|
|
30
|
-
}
|
|
31
|
-
{{/nullable}}
|
|
32
|
-
{{#nullable}}
|
|
33
|
-
if (request.{{name}} != null) {
|
|
34
|
-
for (final file in request.{{name}}!) {
|
|
35
|
-
formData.files.add(MapEntry('{{name}}', await MultipartFile.fromFile(file.path)));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
{{/nullable}}
|
|
39
|
-
{{/isArray}}
|
|
40
|
-
{{^isArray}}
|
|
41
|
-
{{^nullable}}
|
|
42
|
-
formData.files.add(MapEntry('{{name}}', await MultipartFile.fromFile(request.{{name}}.path)));
|
|
43
|
-
{{/nullable}}
|
|
44
|
-
{{#nullable}}
|
|
45
|
-
if (request.{{name}} != null) {
|
|
46
|
-
formData.files.add(MapEntry('{{name}}', await MultipartFile.fromFile(request.{{name}}!.path)));
|
|
47
|
-
}
|
|
48
|
-
{{/nullable}}
|
|
49
|
-
{{/isArray}}
|
|
50
|
-
{{/isFile}}
|
|
51
|
-
{{^isFile}}
|
|
52
|
-
{{#isArray}}
|
|
53
|
-
{{^nullable}}
|
|
54
|
-
for (final value in request.{{name}}) {
|
|
55
|
-
formData.fields.add(MapEntry('{{name}}', value.toString()));
|
|
56
|
-
}
|
|
57
|
-
{{/nullable}}
|
|
58
|
-
{{#nullable}}
|
|
59
|
-
if (request.{{name}} != null) {
|
|
60
|
-
for (final value in request.{{name}}!) {
|
|
61
|
-
formData.fields.add(MapEntry('{{name}}', value.toString()));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
{{/nullable}}
|
|
65
|
-
{{/isArray}}
|
|
66
|
-
{{^isArray}}
|
|
67
|
-
{{^nullable}}
|
|
68
|
-
formData.fields.add(MapEntry('{{name}}', request.{{name}}.toString()));
|
|
69
|
-
{{/nullable}}
|
|
70
|
-
{{#nullable}}
|
|
71
|
-
if (request.{{name}} != null) {
|
|
72
|
-
formData.fields.add(MapEntry('{{name}}', request.{{name}}!.toString()));
|
|
73
|
-
}
|
|
74
|
-
{{/nullable}}
|
|
75
|
-
{{/isArray}}
|
|
76
|
-
{{/isFile}}
|
|
77
|
-
{{/multipartFields}}
|
|
78
|
-
{{/isMultiPart}}
|
|
79
|
-
final response = await _dio.{{httpMethod}}(
|
|
80
|
-
'{{{path}}}',
|
|
81
|
-
{{#haveRequest}}
|
|
82
|
-
{{#isMultiPart}}
|
|
83
|
-
data: formData,
|
|
84
|
-
{{/isMultiPart}}
|
|
85
|
-
{{^isMultiPart}}
|
|
86
|
-
data: request.toJson(),
|
|
87
|
-
{{/isMultiPart}}
|
|
88
|
-
{{/haveRequest}}
|
|
89
|
-
{{^haveRequest}}
|
|
90
|
-
queryParameters: {
|
|
91
|
-
{{#queryParams}}
|
|
92
|
-
{{#nullable}}if ({{name}} != null) {{/nullable}}'{{name}}': {{name}},
|
|
93
|
-
{{/queryParams}}
|
|
94
|
-
},
|
|
95
|
-
{{/haveRequest}}
|
|
96
|
-
);
|
|
97
|
-
{{#isResponseNativeType}}
|
|
98
|
-
return _parseNative<{{responseType}}>(response.data);
|
|
99
|
-
{{/isResponseNativeType}}
|
|
100
|
-
{{^isResponseNativeType}}
|
|
101
|
-
return {{responseType}}.fromJson(response.data);
|
|
102
|
-
{{/isResponseNativeType}}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
{{/endpoints}}
|
|
106
|
-
|
|
107
|
-
T _parseNative<T>(dynamic data) {
|
|
108
|
-
if (data == null) {
|
|
109
|
-
throw Exception('Cannot parse null data as $T');
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (T == String) {
|
|
113
|
-
return data.toString() as T;
|
|
114
|
-
}
|
|
115
|
-
if (T == int) {
|
|
116
|
-
if (data is int) return data as T;
|
|
117
|
-
if (data is double) return data.toInt() as T;
|
|
118
|
-
return int.parse(data.toString()) as T;
|
|
119
|
-
}
|
|
120
|
-
if (T == double) {
|
|
121
|
-
if (data is double) return data as T;
|
|
122
|
-
if (data is int) return data.toDouble() as T;
|
|
123
|
-
return double.parse(data.toString()) as T;
|
|
124
|
-
}
|
|
125
|
-
if (T == bool) {
|
|
126
|
-
if (data is bool) return data as T;
|
|
127
|
-
if (data is String) {
|
|
128
|
-
final lower = data.toLowerCase();
|
|
129
|
-
if (lower == 'true' || lower == '1') return true as T;
|
|
130
|
-
if (lower == 'false' || lower == '0') return false as T;
|
|
131
|
-
}
|
|
132
|
-
if (data is int) return (data != 0) as T;
|
|
133
|
-
throw Exception('Cannot convert $data to bool');
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Se il tipo è dynamic o Object, ritorna direttamente
|
|
137
|
-
if (T == dynamic || T == Object) {
|
|
138
|
-
return data as T;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
throw UnsupportedError('Unsupported native type $T');
|
|
142
|
-
}
|
|
143
|
-
}
|
|
1
|
+
import 'package:{{package}}/core/di/injector.dart';
|
|
2
|
+
import 'package:dio/dio.dart';
|
|
3
|
+
{{#imports}}
|
|
4
|
+
{{{.}}}
|
|
5
|
+
{{/imports}}
|
|
6
|
+
|
|
7
|
+
class {{apiClassName}} {
|
|
8
|
+
final Dio _dio;
|
|
9
|
+
|
|
10
|
+
{{apiClassName}}() : _dio = getIt<Dio>();
|
|
11
|
+
|
|
12
|
+
{{#endpoints}}
|
|
13
|
+
Future<{{responseType}}> {{methodName}}(
|
|
14
|
+
{{#haveRequest}}{{requestType}} request,{{/haveRequest}}
|
|
15
|
+
{{#pathParams}}
|
|
16
|
+
{{type}}{{#nullable}}?{{/nullable}} {{name}},
|
|
17
|
+
{{/pathParams}}
|
|
18
|
+
{{#queryParams}}
|
|
19
|
+
{{type}}{{#nullable}}?{{/nullable}} {{name}},
|
|
20
|
+
{{/queryParams}}
|
|
21
|
+
) async {
|
|
22
|
+
{{#isMultiPart}}
|
|
23
|
+
final formData = FormData();
|
|
24
|
+
{{#multipartFields}}
|
|
25
|
+
{{#isFile}}
|
|
26
|
+
{{#isArray}}
|
|
27
|
+
{{^nullable}}
|
|
28
|
+
for (final file in request.{{name}}) {
|
|
29
|
+
formData.files.add(MapEntry('{{name}}', await MultipartFile.fromFile(file.path)));
|
|
30
|
+
}
|
|
31
|
+
{{/nullable}}
|
|
32
|
+
{{#nullable}}
|
|
33
|
+
if (request.{{name}} != null) {
|
|
34
|
+
for (final file in request.{{name}}!) {
|
|
35
|
+
formData.files.add(MapEntry('{{name}}', await MultipartFile.fromFile(file.path)));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
{{/nullable}}
|
|
39
|
+
{{/isArray}}
|
|
40
|
+
{{^isArray}}
|
|
41
|
+
{{^nullable}}
|
|
42
|
+
formData.files.add(MapEntry('{{name}}', await MultipartFile.fromFile(request.{{name}}.path)));
|
|
43
|
+
{{/nullable}}
|
|
44
|
+
{{#nullable}}
|
|
45
|
+
if (request.{{name}} != null) {
|
|
46
|
+
formData.files.add(MapEntry('{{name}}', await MultipartFile.fromFile(request.{{name}}!.path)));
|
|
47
|
+
}
|
|
48
|
+
{{/nullable}}
|
|
49
|
+
{{/isArray}}
|
|
50
|
+
{{/isFile}}
|
|
51
|
+
{{^isFile}}
|
|
52
|
+
{{#isArray}}
|
|
53
|
+
{{^nullable}}
|
|
54
|
+
for (final value in request.{{name}}) {
|
|
55
|
+
formData.fields.add(MapEntry('{{name}}', value.toString()));
|
|
56
|
+
}
|
|
57
|
+
{{/nullable}}
|
|
58
|
+
{{#nullable}}
|
|
59
|
+
if (request.{{name}} != null) {
|
|
60
|
+
for (final value in request.{{name}}!) {
|
|
61
|
+
formData.fields.add(MapEntry('{{name}}', value.toString()));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
{{/nullable}}
|
|
65
|
+
{{/isArray}}
|
|
66
|
+
{{^isArray}}
|
|
67
|
+
{{^nullable}}
|
|
68
|
+
formData.fields.add(MapEntry('{{name}}', request.{{name}}.toString()));
|
|
69
|
+
{{/nullable}}
|
|
70
|
+
{{#nullable}}
|
|
71
|
+
if (request.{{name}} != null) {
|
|
72
|
+
formData.fields.add(MapEntry('{{name}}', request.{{name}}!.toString()));
|
|
73
|
+
}
|
|
74
|
+
{{/nullable}}
|
|
75
|
+
{{/isArray}}
|
|
76
|
+
{{/isFile}}
|
|
77
|
+
{{/multipartFields}}
|
|
78
|
+
{{/isMultiPart}}
|
|
79
|
+
final response = await _dio.{{httpMethod}}(
|
|
80
|
+
'{{{path}}}',
|
|
81
|
+
{{#haveRequest}}
|
|
82
|
+
{{#isMultiPart}}
|
|
83
|
+
data: formData,
|
|
84
|
+
{{/isMultiPart}}
|
|
85
|
+
{{^isMultiPart}}
|
|
86
|
+
data: request.toJson(),
|
|
87
|
+
{{/isMultiPart}}
|
|
88
|
+
{{/haveRequest}}
|
|
89
|
+
{{^haveRequest}}
|
|
90
|
+
queryParameters: {
|
|
91
|
+
{{#queryParams}}
|
|
92
|
+
{{#nullable}}if ({{name}} != null) {{/nullable}}'{{name}}': {{name}},
|
|
93
|
+
{{/queryParams}}
|
|
94
|
+
},
|
|
95
|
+
{{/haveRequest}}
|
|
96
|
+
);
|
|
97
|
+
{{#isResponseNativeType}}
|
|
98
|
+
return _parseNative<{{responseType}}>(response.data);
|
|
99
|
+
{{/isResponseNativeType}}
|
|
100
|
+
{{^isResponseNativeType}}
|
|
101
|
+
return {{responseType}}.fromJson(response.data);
|
|
102
|
+
{{/isResponseNativeType}}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
{{/endpoints}}
|
|
106
|
+
|
|
107
|
+
T _parseNative<T>(dynamic data) {
|
|
108
|
+
if (data == null) {
|
|
109
|
+
throw Exception('Cannot parse null data as $T');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (T == String) {
|
|
113
|
+
return data.toString() as T;
|
|
114
|
+
}
|
|
115
|
+
if (T == int) {
|
|
116
|
+
if (data is int) return data as T;
|
|
117
|
+
if (data is double) return data.toInt() as T;
|
|
118
|
+
return int.parse(data.toString()) as T;
|
|
119
|
+
}
|
|
120
|
+
if (T == double) {
|
|
121
|
+
if (data is double) return data as T;
|
|
122
|
+
if (data is int) return data.toDouble() as T;
|
|
123
|
+
return double.parse(data.toString()) as T;
|
|
124
|
+
}
|
|
125
|
+
if (T == bool) {
|
|
126
|
+
if (data is bool) return data as T;
|
|
127
|
+
if (data is String) {
|
|
128
|
+
final lower = data.toLowerCase();
|
|
129
|
+
if (lower == 'true' || lower == '1') return true as T;
|
|
130
|
+
if (lower == 'false' || lower == '0') return false as T;
|
|
131
|
+
}
|
|
132
|
+
if (data is int) return (data != 0) as T;
|
|
133
|
+
throw Exception('Cannot convert $data to bool');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Se il tipo è dynamic o Object, ritorna direttamente
|
|
137
|
+
if (T == dynamic || T == Object) {
|
|
138
|
+
return data as T;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
throw UnsupportedError('Unsupported native type $T');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
2
|
-
{{#imports}}
|
|
3
|
-
{{{.}}}
|
|
4
|
-
{{/imports}}
|
|
5
|
-
|
|
6
|
-
@JsonEnum(fieldRename: FieldRename.pascal)
|
|
7
|
-
enum {{enumName}} {
|
|
8
|
-
{{#fields}}
|
|
9
|
-
@JsonValue({{{value}}})
|
|
10
|
-
{{name}}({{{value}}}){{#isLast}};{{/isLast}}{{^isLast}},{{/isLast}}
|
|
11
|
-
{{/fields}}
|
|
12
|
-
|
|
13
|
-
final {{valueType}} value;
|
|
14
|
-
const {{enumName}}(this.value);
|
|
1
|
+
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
2
|
+
{{#imports}}
|
|
3
|
+
{{{.}}}
|
|
4
|
+
{{/imports}}
|
|
5
|
+
|
|
6
|
+
@JsonEnum(fieldRename: FieldRename.pascal)
|
|
7
|
+
enum {{enumName}} {
|
|
8
|
+
{{#fields}}
|
|
9
|
+
@JsonValue({{{value}}})
|
|
10
|
+
{{name}}({{{value}}}){{#isLast}};{{/isLast}}{{^isLast}},{{/isLast}}
|
|
11
|
+
{{/fields}}
|
|
12
|
+
|
|
13
|
+
final {{valueType}} value;
|
|
14
|
+
const {{enumName}}(this.value);
|
|
15
15
|
}
|
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
2
|
-
{{#imports}}
|
|
3
|
-
{{{.}}}
|
|
4
|
-
{{/imports}}
|
|
5
|
-
|
|
6
|
-
part '{{filename}}.freezed.dart';
|
|
7
|
-
part '{{filename}}.g.dart';
|
|
8
|
-
|
|
9
|
-
@freezed
|
|
10
|
-
abstract class {{className}} with _${{className}} {
|
|
11
|
-
const factory {{className}}({
|
|
12
|
-
{{#fields}}
|
|
13
|
-
{{#jsonKeyAnnotation}}
|
|
14
|
-
{{{jsonKeyAnnotation}}}
|
|
15
|
-
{{/jsonKeyAnnotation}}
|
|
16
|
-
{{
|
|
17
|
-
{{
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
2
|
+
{{#imports}}
|
|
3
|
+
{{{.}}}
|
|
4
|
+
{{/imports}}
|
|
5
|
+
|
|
6
|
+
part '{{filename}}.freezed.dart';
|
|
7
|
+
part '{{filename}}.g.dart';
|
|
8
|
+
|
|
9
|
+
@freezed
|
|
10
|
+
abstract class {{className}} with _${{className}} {
|
|
11
|
+
const factory {{className}}({
|
|
12
|
+
{{#fields}}
|
|
13
|
+
{{#jsonKeyAnnotation}}
|
|
14
|
+
{{{jsonKeyAnnotation}}}
|
|
15
|
+
{{/jsonKeyAnnotation}}
|
|
16
|
+
{{#defaultAnnotation}}
|
|
17
|
+
{{{defaultAnnotation}}}
|
|
18
|
+
{{/defaultAnnotation}}
|
|
19
|
+
{{required}}{{{type}}}{{nullable}} {{name}},
|
|
20
|
+
{{/fields}}
|
|
21
|
+
}) = _{{className}};
|
|
22
|
+
|
|
23
|
+
factory {{className}}.fromJson(Map<String, dynamic> json) => _${{className}}FromJson(json);
|
|
21
24
|
}
|
|
@@ -23,15 +23,15 @@ export class ApiNextJsWriter {
|
|
|
23
23
|
let haveRequest = api.haveRequest;
|
|
24
24
|
let method = api.method.toLowerCase();
|
|
25
25
|
let httpOptions = api.isMultiPart ? 'httpOptionsMultiPart' : 'httpOptions';
|
|
26
|
-
let preparation = `${queryParametersPreparation}
|
|
26
|
+
let preparation = `${queryParametersPreparation}
|
|
27
27
|
${requestPreparation}`.trim();
|
|
28
28
|
preparation = preparation.length > 0 ? ` ${preparation}\n ` : '';
|
|
29
29
|
//\`\${API_BASE_URL}
|
|
30
|
-
let apiString = `
|
|
31
|
-
export const ${apiNameNormalized} = async (${parametersString}): Promise<${returnTypeString}> => {
|
|
32
|
-
${preparation}const response = await axios.${method}<${returnTypeString}>(\`${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''});
|
|
33
|
-
return response.data;
|
|
34
|
-
}
|
|
30
|
+
let apiString = `
|
|
31
|
+
export const ${apiNameNormalized} = async (${parametersString}): Promise<${returnTypeString}> => {
|
|
32
|
+
${preparation}const response = await axios.${method}<${returnTypeString}>(\`${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''});
|
|
33
|
+
return response.data;
|
|
34
|
+
}
|
|
35
35
|
`;
|
|
36
36
|
return apiString;
|
|
37
37
|
}
|
|
@@ -59,21 +59,21 @@ ${preparation}const response = await axios.${method}<${returnTypeString}>(\`${ap
|
|
|
59
59
|
_queryParametersPreparationStatement(parameter) {
|
|
60
60
|
if (parameter.nullable) {
|
|
61
61
|
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
62
|
-
return ` const ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} !== undefined && isValid(${parameter.name}) ? encodeURIComponent(dateToZulu(${parameter.name})) : '';
|
|
62
|
+
return ` const ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} !== undefined && isValid(${parameter.name}) ? encodeURIComponent(dateToZulu(${parameter.name})) : '';
|
|
63
63
|
`;
|
|
64
64
|
}
|
|
65
65
|
else {
|
|
66
|
-
return ` const ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} !== undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
66
|
+
return ` const ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} !== undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
67
67
|
`;
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
else {
|
|
71
71
|
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
72
|
-
return ` const ${parameter.name}Param: string = encodeURIComponent(dateToZulu(${parameter.name}));
|
|
72
|
+
return ` const ${parameter.name}Param: string = encodeURIComponent(dateToZulu(${parameter.name}));
|
|
73
73
|
`;
|
|
74
74
|
}
|
|
75
75
|
else {
|
|
76
|
-
return ` const ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
76
|
+
return ` const ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
77
77
|
`;
|
|
78
78
|
}
|
|
79
79
|
}
|
|
@@ -117,11 +117,11 @@ ${preparation}const response = await axios.${method}<${returnTypeString}>(\`${ap
|
|
|
117
117
|
if (!api.haveRequest) {
|
|
118
118
|
return ``;
|
|
119
119
|
}
|
|
120
|
-
return ` const wrappedRequest = handleRequest(request);
|
|
120
|
+
return ` const wrappedRequest = handleRequest(request);
|
|
121
121
|
`;
|
|
122
122
|
}
|
|
123
123
|
_writeFile(apis) {
|
|
124
|
-
fs.writeFileSync(this._commandLineArgs.outputDirectory + "/api.autogenerated.ts", `${API_PRE}
|
|
124
|
+
fs.writeFileSync(this._commandLineArgs.outputDirectory + "/api.autogenerated.ts", `${API_PRE}
|
|
125
125
|
${apis}`, { flag: 'w' });
|
|
126
126
|
}
|
|
127
127
|
}
|