@penkov/swagger-code-gen 1.1.0 → 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;
|
package/dist/templates/index.ejs
CHANGED
|
@@ -10,32 +10,28 @@
|
|
|
10
10
|
*********************************************************
|
|
11
11
|
*********************************************************
|
|
12
12
|
*********************************************************/
|
|
13
|
-
import fetch from 'node-fetch';
|
|
13
|
+
import fetch, {Request, Response} from 'node-fetch';
|
|
14
14
|
<% if (scats) {%>
|
|
15
15
|
import {option, Option, Collection, Try, TryLike, none} from 'scats';
|
|
16
16
|
<% } %>
|
|
17
17
|
|
|
18
18
|
export interface RequestOptions {
|
|
19
|
-
|
|
19
|
+
apiPrefix?: string;
|
|
20
|
+
headers?: Record<string, string>;
|
|
21
|
+
preProcessRequest?: (request: Request) => Request;
|
|
22
|
+
postProcessResponse?: (request: Request, response: Response) => Response;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
|
|
23
|
-
export
|
|
24
|
-
apiPrefix: string;
|
|
25
|
-
requestOptions: RequestOptions
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const swaggerCodeGenParams: SwaggerCodeGenParams = {
|
|
26
|
+
export const defaultReqOpts: RequestOptions = {
|
|
29
27
|
apiPrefix: '',
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
'Content-Type': 'application/json'
|
|
34
|
-
}
|
|
28
|
+
headers: {
|
|
29
|
+
'Accept': 'application/json',
|
|
30
|
+
'Content-Type': 'application/json'
|
|
35
31
|
}
|
|
36
32
|
};
|
|
37
33
|
|
|
38
|
-
const defaultRequestOptions = () =>
|
|
34
|
+
const defaultRequestOptions = () => defaultReqOpts;
|
|
39
35
|
|
|
40
36
|
<% schemas.foreach(schema => { %>
|
|
41
37
|
<%- include('schema.ejs', {schema: schema}); %>
|
|
@@ -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 = [];
|
|
@@ -53,11 +53,11 @@ export async function <%= method.endpointName %>(
|
|
|
53
53
|
query = '?' + queryParams.join('&');
|
|
54
54
|
}
|
|
55
55
|
<%_ } -%>
|
|
56
|
-
const
|
|
56
|
+
const request = new Request(`${requestOptions.apiPrefix}<%- method.pathWithSubstitutions %>${query}`, {
|
|
57
57
|
method: '<%= method.method %>',
|
|
58
58
|
body: <%= method.body.map(() => 'JSON.stringify(body)').getOrElseValue('undefined') %>,
|
|
59
59
|
headers: {
|
|
60
|
-
...requestOptions.headers,
|
|
60
|
+
...option(requestOptions.headers).getOrElseValue({}),
|
|
61
61
|
<%_ if (method.parameters.filter(x => x.in === 'header').nonEmpty) { -%>
|
|
62
62
|
<%_ method.parameters.filter(x => x.in === 'header').foreach(p => { -%>
|
|
63
63
|
<%= p.name %>: <%= method.parameters.size > 2 ? 'params.' : '' %><%= p.uniqueName %>
|
|
@@ -65,11 +65,15 @@ export async function <%= method.endpointName %>(
|
|
|
65
65
|
<%_ } -%>
|
|
66
66
|
}
|
|
67
67
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
68
|
+
const preprocessed = option(requestOptions.preProcessRequest).map(cb => cb(request)).getOrElseValue(request);
|
|
69
|
+
const resp = await fetch(preprocessed);
|
|
70
|
+
if (resp.ok) {
|
|
71
|
+
const postprocessed = option(requestOptions.postProcessResponse)
|
|
72
|
+
.map(cb => cb(preprocessed, resp))
|
|
73
|
+
.getOrElseValue(resp);
|
|
74
|
+
const json = await postprocessed.json();
|
|
75
|
+
return json as <%- method.response.responseType %>;
|
|
76
|
+
} else {
|
|
77
|
+
throw resp;
|
|
78
|
+
}
|
|
75
79
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export class ApiClient {
|
|
2
2
|
|
|
3
|
-
constructor(private readonly requestOptions: RequestOptions =
|
|
3
|
+
constructor(private readonly requestOptions: RequestOptions = defaultRequestOptions()) {
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
|
|
@@ -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') { -%>
|