@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.
@@ -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
- headers: Record<string, string>;
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 interface SwaggerCodeGenParams {
24
- apiPrefix: string;
25
- requestOptions: RequestOptions
26
- }
27
-
28
- export const swaggerCodeGenParams: SwaggerCodeGenParams = {
26
+ export const defaultReqOpts: RequestOptions = {
29
27
  apiPrefix: '',
30
- requestOptions: {
31
- headers: {
32
- 'Accept': 'application/json',
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 = () => swaggerCodeGenParams.requestOptions;
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 resp = await fetch(`${swaggerCodeGenParams.apiPrefix}<%- method.pathWithSubstitutions %>${query}`, {
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
- if (resp.ok) {
69
- const json = await resp.json();
70
- return json as <%= method.response.responseType %>;
71
- } else {
72
- throw resp;
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 = swaggerCodeGenParams.requestOptions) {
3
+ constructor(private readonly requestOptions: RequestOptions = defaultRequestOptions()) {
4
4
  }
5
5
 
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@penkov/swagger-code-gen",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "generate-client": "./dist/cli.mjs"