@penkov/swagger-code-gen 1.1.0 → 1.1.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
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}); %>
|
|
@@ -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
|
}
|