Skip to content

Commit 37efbb9

Browse files
laktakChristian Zangl
and
Christian Zangl
authored
added --unwrap-response-data to unwrap the data item from the response (#268)
* added --unwrap-response-data to unwrap the data item from the response * add axios --unwrap-response-data Co-authored-by: Christian Zangl <[email protected]>
1 parent 189dec0 commit 37efbb9

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ program
6666
.option("--disableStrictSSL", "disabled strict SSL", false)
6767
.option("--disableProxy", "disabled proxy", false)
6868
.option("--axios", "generate axios http client", false)
69+
.option("--unwrap-response-data", "unwrap the data item from the response", false)
6970
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
7071
.option("--silent", "Output only errors to console", false)
7172
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
@@ -100,6 +101,7 @@ const {
100101
disableProxy,
101102
cleanOutput,
102103
defaultResponse,
104+
unwrapResponseData,
103105
singleHttpClient,
104106
axios,
105107
silent,
@@ -115,6 +117,7 @@ generateApi({
115117
httpClientType: axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
116118
defaultResponseAsSuccess: defaultAsSuccess,
117119
defaultResponseType: defaultResponse,
120+
unwrapResponseData: unwrapResponseData,
118121
generateUnionEnums: unionEnums,
119122
generateResponses: responses,
120123
extractRequestParams: !!extractRequestParams,

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const config = {
5858
defaultResponseType: TS_KEYWORDS.VOID,
5959
singleHttpClient: false,
6060
httpClientType: HTTP_CLIENT.FETCH,
61+
unwrapResponseData: false,
6162
templatePaths: {
6263
/** `templates/base` */
6364
base: "",

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = {
4444
extractRequestParams = config.extractRequestParams,
4545
extractRequestBody = config.extractRequestBody,
4646
defaultResponseType = config.defaultResponseType,
47+
unwrapResponseData = config.unwrapResponseData,
4748
singleHttpClient = config.singleHttpClient,
4849
prettier: prettierOptions = constants.PRETTIER_OPTIONS,
4950
hooks: rawHooks,
@@ -77,6 +78,7 @@ module.exports = {
7778
disableProxy,
7879
cleanOutput,
7980
defaultResponseType,
81+
unwrapResponseData,
8082
singleHttpClient,
8183
constants,
8284
silent,

templates/base/http-clients/axios-http-client.eta

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<%
2-
const { apiConfig, generateResponses } = it;
2+
const { apiConfig, generateResponses, config } = it;
33
%>
44

55
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType } from "axios";
@@ -89,7 +89,11 @@ export class HttpClient<SecurityDataType = unknown> {
8989
format,
9090
body,
9191
...params
92+
<% if (config.unwrapResponseData) { %>
93+
}: FullRequestParams): Promise<T> => {
94+
<% } else { %>
9295
}: FullRequestParams): Promise<AxiosResponse<T>> => {
96+
<% } %>
9397
const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
9498
const requestParams = this.mergeRequestParams(params, secureParams);
9599
const responseFormat = (format && this.format) || void 0;
@@ -112,6 +116,10 @@ export class HttpClient<SecurityDataType = unknown> {
112116
responseType: responseFormat,
113117
data: body,
114118
url: path,
119+
<% if (config.unwrapResponseData) { %>
120+
}).then(response => response.data);
121+
<% } else { %>
115122
});
123+
<% } %>
116124
};
117125
}

templates/base/http-clients/fetch-http-client.eta

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<%
2-
const { apiConfig, generateResponses } = it;
2+
const { apiConfig, generateResponses, config } = it;
33
%>
44

55
export type QueryParamsType = Record<string | number, any>;
@@ -164,7 +164,11 @@ export class HttpClient<SecurityDataType = unknown> {
164164
baseUrl,
165165
cancelToken,
166166
...params
167+
<% if (config.unwrapResponseData) { %>
168+
}: FullRequestParams): Promise<T> => {
169+
<% } else { %>
167170
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
171+
<% } %>
168172
const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {};
169173
const requestParams = this.mergeRequestParams(params, secureParams);
170174
const queryString = query && this.toQueryString(query);
@@ -206,7 +210,11 @@ export class HttpClient<SecurityDataType = unknown> {
206210
}
207211

208212
if (!response.ok) throw data;
213+
<% if (config.unwrapResponseData) { %>
214+
return data.data;
215+
<% } else { %>
209216
return data;
217+
<% } %>
210218
});
211219
};
212220
}

0 commit comments

Comments
 (0)