@penkov/swagger-code-gen 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/property.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Collection, option } from 'scats';
|
|
2
2
|
const SCHEMA_PREFIX = '#/components/schemas/';
|
|
3
3
|
export class Property {
|
|
4
4
|
constructor(name, type, description, defaultValue, nullable, required, items, referencesObject, itemReferencesObject) {
|
|
@@ -22,12 +22,18 @@ export class Property {
|
|
|
22
22
|
const type = option(definition.$ref)
|
|
23
23
|
.map(ref => ref.substring(SCHEMA_PREFIX.length))
|
|
24
24
|
.getOrElseValue(definition.type);
|
|
25
|
-
const nullable = option(definition.nullable).
|
|
25
|
+
const nullable = option(definition.nullable).contains(true);
|
|
26
26
|
const description = option(definition.description);
|
|
27
|
-
const required = option(definition.required).
|
|
27
|
+
const required = option(definition.required).contains(true);
|
|
28
28
|
const items = option(definition.items?.$ref)
|
|
29
29
|
.map(ref => ref.substring(SCHEMA_PREFIX.length))
|
|
30
30
|
.orElseValue(option(definition.items?.type))
|
|
31
|
+
.orElse(() => option(definition.items?.oneOf)
|
|
32
|
+
.map(x => Collection.from(x))
|
|
33
|
+
.filter(x => x.nonEmpty)
|
|
34
|
+
.map(x => x.flatMapOption(oneOfItem => option(oneOfItem.$ref)
|
|
35
|
+
.map(ref => ref.substring(SCHEMA_PREFIX.length))
|
|
36
|
+
.orElseValue(option(oneOfItem.type))).mkString(' | ')))
|
|
31
37
|
.getOrElseValue('any');
|
|
32
38
|
return new Property(name, type, description, null, nullable, required, items, referencesObject, itemReferencesObject);
|
|
33
39
|
}
|
|
@@ -40,7 +46,7 @@ export class Property {
|
|
|
40
46
|
static toJsType(tpe, itemTpe = 'any') {
|
|
41
47
|
switch (tpe) {
|
|
42
48
|
case 'integer': return 'number';
|
|
43
|
-
case 'array': return `
|
|
49
|
+
case 'array': return `ReadonlyArray<${Property.toJsType(itemTpe)}>`;
|
|
44
50
|
default: return tpe;
|
|
45
51
|
}
|
|
46
52
|
}
|
|
@@ -62,7 +68,12 @@ export class Property {
|
|
|
62
68
|
return this.nullable ? `Option<${this.type}Dto>` : `${this.type}Dto`;
|
|
63
69
|
}
|
|
64
70
|
else if (this.isArray) {
|
|
65
|
-
|
|
71
|
+
if (this.itemReferencesObject) {
|
|
72
|
+
return `Collection<${this.items}Dto>`;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
return `Collection<${Property.toJsType(this.items)}>`;
|
|
76
|
+
}
|
|
66
77
|
}
|
|
67
78
|
else {
|
|
68
79
|
return this.nullable ? `Option<${this.jsType}>` : this.jsType;
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
export async function <%= method.endpointName %>(
|
|
17
17
|
<%_ if (method.parameters.size <= 2) { -%>
|
|
18
18
|
<%_ method.parameters.foreach(p => { -%>
|
|
19
|
-
<%= p.uniqueName %><%= p.required ? '' : '?'%>:
|
|
19
|
+
<%= p.uniqueName %><%= p.required ? '' : '?'%>: <%- p.jsType %>,
|
|
20
20
|
<%_ }) -%>
|
|
21
21
|
<%_ } else { -%>
|
|
22
22
|
params: {
|
|
23
23
|
<%_ method.parameters.foreach(p => { -%>
|
|
24
|
-
<%= p.uniqueName %><%= p.required ? '' : '?'%>:
|
|
24
|
+
<%= p.uniqueName %><%= p.required ? '' : '?'%>: <%- p.jsType %>,
|
|
25
25
|
<%_ }) -%>
|
|
26
26
|
},
|
|
27
27
|
<%_ } -%>
|
|
@@ -29,7 +29,7 @@ export async function <%= method.endpointName %>(
|
|
|
29
29
|
body: <%= method.body.get.type %>,
|
|
30
30
|
<%_ } -%>
|
|
31
31
|
requestOptions: RequestOptions = defaultRequestOptions()
|
|
32
|
-
): Promise
|
|
32
|
+
): Promise<<%- method.response.responseType %>> {
|
|
33
33
|
let query = '';
|
|
34
34
|
<%_ if (method.parameters.filter(x => x.in === 'query').nonEmpty) { -%>
|
|
35
35
|
const queryParams = [];
|
|
@@ -72,7 +72,7 @@ export async function <%= method.endpointName %>(
|
|
|
72
72
|
.map(cb => cb(preprocessed, resp))
|
|
73
73
|
.getOrElseValue(resp);
|
|
74
74
|
const json = await postprocessed.json();
|
|
75
|
-
return json as
|
|
75
|
+
return json as <%- method.response.responseType %>;
|
|
76
76
|
} else {
|
|
77
77
|
throw resp;
|
|
78
78
|
}
|
|
@@ -13,7 +13,7 @@ export class ApiClient {
|
|
|
13
13
|
<%_ } else { -%>
|
|
14
14
|
params: {
|
|
15
15
|
<%_ method.parameters.foreach(p => { -%>
|
|
16
|
-
<%= p.uniqueName %><%= p.required && !p.nullable ? '' : '?'%>:
|
|
16
|
+
<%= p.uniqueName %><%= p.required && !p.nullable ? '' : '?'%>: <%- p.jsType %>,
|
|
17
17
|
<%_ }) -%>
|
|
18
18
|
},
|
|
19
19
|
<%_ } -%>
|
|
@@ -12,7 +12,11 @@ export class <%= schema.name %>Dto {
|
|
|
12
12
|
return new <%= schema.name %>Dto(
|
|
13
13
|
<%_ schema.properties.foreach(p => { _%>
|
|
14
14
|
<%_ if (p.referencesObject) { _%>
|
|
15
|
+
<%_ if (p.required || !p.nullable) { _%>
|
|
15
16
|
<%- p.scatsWrapperType %>.fromJson(json.<%= p.name %>),
|
|
17
|
+
<%_ } else { _%>
|
|
18
|
+
option(json.<%= p.name %>).map(_ => <%- p.scatsWrapperType %>.fromJson(_)),
|
|
19
|
+
<%_ } _%>
|
|
16
20
|
<%_ } else if (p.isArray) { _%>
|
|
17
21
|
Collection.from(option(json.<%= p.name %>).getOrElseValue([]))
|
|
18
22
|
<% if (p.itemReferencesObject) { _%>.map(i => <%- p.itemScatsWrapperType %>.fromJson(i))<%_ } _%>,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<%_ if (schema.schemaType === 'object') { -%>
|
|
2
2
|
export interface <%= schema.name %> {
|
|
3
3
|
<%_ schema.properties.foreach(p => { -%>
|
|
4
|
-
readonly <%= p.name %><%= p.nullable ? '?' : '' %>:
|
|
4
|
+
readonly <%= p.name %><%= p.nullable ? '?' : '' %>: <%- p.jsType %>;
|
|
5
5
|
<%_ }); -%>
|
|
6
6
|
}
|
|
7
7
|
<%_ } if (schema.schemaType === 'enum') { -%>
|