@penkov/swagger-code-gen 1.0.0 → 1.0.1
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/templates/index.ejs +32 -0
- package/dist/templates/method.ejs +75 -0
- package/dist/templates/schema.ejs +13 -0
- package/package.json +3 -2
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*********************************************************
|
|
2
|
+
*********************************************************
|
|
3
|
+
*********************************************************
|
|
4
|
+
*********************************************************
|
|
5
|
+
*********************************************************
|
|
6
|
+
*********************************************************
|
|
7
|
+
************ GENERATED! DO NOT EDIT! ********************
|
|
8
|
+
*********************************************************
|
|
9
|
+
*********************************************************
|
|
10
|
+
*********************************************************
|
|
11
|
+
*********************************************************
|
|
12
|
+
*********************************************************/
|
|
13
|
+
import fetch from 'node-fetch';
|
|
14
|
+
|
|
15
|
+
<% schemas.foreach(schema => { %>
|
|
16
|
+
<%- include('schema.ejs', {schema: schema}); %>
|
|
17
|
+
<% }); %>
|
|
18
|
+
|
|
19
|
+
export interface RequestOptions {
|
|
20
|
+
headers: Record<string, string>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const defaultRequestOptions = {
|
|
24
|
+
headers: {
|
|
25
|
+
'Accept': 'application/json',
|
|
26
|
+
'Content-Type': 'application/json'
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
<% methods.foreach(method => { %>
|
|
31
|
+
<%- include('method.ejs', {method: method}); %>
|
|
32
|
+
<% }); %>
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <%= method.method.toUpperCase() %> <%= method.path %>
|
|
3
|
+
<% if (method.parameters.size > 2) { -%>
|
|
4
|
+
* @param {Object} params
|
|
5
|
+
<% } -%>
|
|
6
|
+
<% method.parameters.foreach(p => { -%>
|
|
7
|
+
* @param {<%= p.jsType %>} <%= method.parameters.size > 2 ? 'params.' : '' %><%= p.uniqueName %> <%= p.description.getOrElseValue('')%>
|
|
8
|
+
<% }); -%>
|
|
9
|
+
<%_ if (method.body.nonEmpty) { -%>
|
|
10
|
+
* @param {<%= method.body.get.type %>} body
|
|
11
|
+
<%_ } -%>
|
|
12
|
+
* @param {RequestOptions} requestOptions Additional request params
|
|
13
|
+
* @summary <%= method.summary %>
|
|
14
|
+
* @return {<%= method.response.responseType %>} <%= method.response.description %>
|
|
15
|
+
*/
|
|
16
|
+
export async function <%= method.endpointName %>(
|
|
17
|
+
<%_ if (method.parameters.size <= 2) { -%>
|
|
18
|
+
<%_ method.parameters.foreach(p => { -%>
|
|
19
|
+
<%= p.uniqueName %><%= p.required ? '' : '?'%>: <%= p.jsType %>,
|
|
20
|
+
<%_ }) -%>
|
|
21
|
+
<%_ } else { -%>
|
|
22
|
+
params: {
|
|
23
|
+
<%_ method.parameters.foreach(p => { -%>
|
|
24
|
+
<%= p.uniqueName %><%= p.required ? '' : '?'%>: <%= p.jsType %>,
|
|
25
|
+
<%_ }) -%>
|
|
26
|
+
},
|
|
27
|
+
<%_ } -%>
|
|
28
|
+
<%_ if (method.body.nonEmpty) { -%>
|
|
29
|
+
body: <%= method.body.get.type %>,
|
|
30
|
+
<%_ } -%>
|
|
31
|
+
requestOptions: RequestOptions = defaultRequestOptions
|
|
32
|
+
): Promise<<%= method.response.responseType %>> {
|
|
33
|
+
let query = '';
|
|
34
|
+
<%_ if (method.parameters.filter(x => x.in === 'query').nonEmpty) { -%>
|
|
35
|
+
const queryParams = [];
|
|
36
|
+
<%_ method.parameters.filter(x => x.in === 'query' && x.required).foreach(p => { -%>
|
|
37
|
+
<%_ if (p.jsType === 'string') { -%>
|
|
38
|
+
queryParams.push(`<%= p.name %>=${encodeURIComponent(<%= method.parameters.size > 2 ? 'params.' : '' %><%= p.uniqueName %>)}`);
|
|
39
|
+
<%_ } else { -%>
|
|
40
|
+
queryParams.push(`<%= p.name %>=${<%= method.parameters.size > 2 ? 'params.' : '' %><%= p.uniqueName %>}`);
|
|
41
|
+
<%_ } -%>
|
|
42
|
+
<%_ }) -%>
|
|
43
|
+
<%_ method.parameters.filter(x => x.in === 'query' && !x.required).foreach(p => { -%>
|
|
44
|
+
if (!!<%= method.parameters.size > 2 ? 'params.' : '' %><%= p.uniqueName %>) {
|
|
45
|
+
<%_ if (p.jsType === 'string') { -%>
|
|
46
|
+
queryParams.push(`<%= p.name %>=${encodeURIComponent(<%= method.parameters.size > 2 ? 'params.' : '' %><%= p.uniqueName %>)}`);
|
|
47
|
+
<%_ } else { -%>
|
|
48
|
+
queryParams.push(`<%= p.name %>=${<%= method.parameters.size > 2 ? 'params.' : '' %><%= p.uniqueName %>}`);
|
|
49
|
+
<%_ } -%>
|
|
50
|
+
}
|
|
51
|
+
<%_ }) -%>
|
|
52
|
+
if (queryParams.length > 0) {
|
|
53
|
+
query = '?' + queryParams.join('&');
|
|
54
|
+
}
|
|
55
|
+
<%_ } -%>
|
|
56
|
+
const resp = await fetch(`<%- method.pathWithSubstitutions %>${query}`, {
|
|
57
|
+
method: '<%= method.method %>',
|
|
58
|
+
body: <%= method.body.map(() => 'JSON.stringify(body)').getOrElseValue('undefined') %>,
|
|
59
|
+
headers: {
|
|
60
|
+
...requestOptions.headers,
|
|
61
|
+
<%_ if (method.parameters.filter(x => x.in === 'header').nonEmpty) { -%>
|
|
62
|
+
<%_ method.parameters.filter(x => x.in === 'header').foreach(p => { -%>
|
|
63
|
+
<%= p.name %>: <%= method.parameters.size > 2 ? 'params.' : '' %><%= p.uniqueName %>
|
|
64
|
+
<%_ }) -%>
|
|
65
|
+
<%_ } -%>
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const json = await resp.json();
|
|
69
|
+
if (resp.ok) {
|
|
70
|
+
return json as <%= method.response.responseType %>;
|
|
71
|
+
} else {
|
|
72
|
+
throw json;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<%_ if (schema.schemaType === 'object') { -%>
|
|
2
|
+
export interface <%= schema.name %> {
|
|
3
|
+
<%_ schema.properties.foreach(p => { -%>
|
|
4
|
+
readonly <%= p.name %><%= p.nullable ? '?' : '' %>: <%= p.jsType %>;
|
|
5
|
+
<%_ }); -%>
|
|
6
|
+
}
|
|
7
|
+
<%_ } if (schema.schemaType === 'enum') { -%>
|
|
8
|
+
export enum <%= schema.name %> {
|
|
9
|
+
<%_ schema.values.foreach(p => { -%>
|
|
10
|
+
<%= p %> = '<%= p %>',
|
|
11
|
+
<%_ }); -%>
|
|
12
|
+
}
|
|
13
|
+
<%_ } -%>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@penkov/swagger-code-gen",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"generate-client": "./dist/cli.mjs"
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"clean": "rimraf dist",
|
|
26
26
|
"lint": "eslint \"{src,test}/**/*.ts\" --fix",
|
|
27
27
|
"prebuild": "npm run lint && npm run clean",
|
|
28
|
-
"
|
|
28
|
+
"copy-files": "mkdir -p ./dist/templates && cp ./src/templates/* ./dist/templates/",
|
|
29
|
+
"build": "tsc && chmod +x ./dist/cli.mjs && npm run copy-files"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
32
|
"commander": "^9.4.1",
|