Skip to content

Commit 36ed862

Browse files
committed
fix(typescript-axios-slim): add response unwrap toggle
1 parent 50ca85b commit 36ed862

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

docs/generators/typescript-axios-slim.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4242
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4343
|stringEnums|Generate string enums instead of objects for enum values.| |false|
4444
|supportsES6|Generate code that conforms to ES6.| |false|
45+
|unwrapResponseData|Whether generated API methods return payload data directly (`Promise<T>`) instead of the full Axios response (`Promise<AxiosResponse<T>>`). Set to `false` to retain access to response headers and status metadata.| |true|
4546
|useSquareBracketsInArrayNames|Setting this property to true will add brackets to array attribute names, e.g. my_values[].| |false|
4647
|withAWSV4Signature|whether to include AWS v4 signature support| |false|
4748
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosSlimClientCodegen.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717

1818
package org.openapitools.codegen.languages;
1919

20+
import io.swagger.v3.parser.util.SchemaTypeUtil;
21+
import org.openapitools.codegen.CliOption;
22+
2023
public class TypeScriptAxiosSlimClientCodegen extends TypeScriptAxiosClientCodegen {
24+
public static final String UNWRAP_RESPONSE_DATA = "unwrapResponseData";
25+
26+
private boolean unwrapResponseData = true;
2127

2228
public TypeScriptAxiosSlimClientCodegen() {
2329
super();
2430
outputFolder = "generated-code/typescript-axios-slim";
2531
embeddedTemplateDir = templateDir = "typescript-axios-slim";
2632
cliOptions.removeIf(option -> USE_SINGLE_REQUEST_PARAMETER.equals(option.getOpt()));
33+
cliOptions.add(new CliOption(UNWRAP_RESPONSE_DATA, "Whether to return payload data directly (`Promise<T>`) instead of the full Axios response.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.TRUE.toString()));
2734
additionalProperties.put(USE_SINGLE_REQUEST_PARAMETER, true);
2835
}
2936

@@ -32,6 +39,10 @@ public void processOpts() {
3239
additionalProperties.put(USE_SINGLE_REQUEST_PARAMETER, true);
3340
super.processOpts();
3441
additionalProperties.put(USE_SINGLE_REQUEST_PARAMETER, true);
42+
if (additionalProperties.containsKey(UNWRAP_RESPONSE_DATA)) {
43+
unwrapResponseData = convertPropertyToBoolean(UNWRAP_RESPONSE_DATA);
44+
}
45+
writePropertyBack(UNWRAP_RESPONSE_DATA, unwrapResponseData);
3546
}
3647

3748
@Override

modules/openapi-generator/src/main/resources/typescript-axios-slim/README.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This generator creates TypeScript/JavaScript client that utilizes [axios](https://github.com/axios/axios). The generated Node module can be used in the following environments:
44

5-
> `typescript-axios-slim` intentionally differs from `typescript-axios`: it removes `AxiosParamCreator`, `Fp`, and `Factory` layers, always uses a single request-parameter object per operation, and emits direct class methods with request-parameter schema validation.
5+
> `typescript-axios-slim` intentionally differs from `typescript-axios`: it removes `AxiosParamCreator`, `Fp`, and `Factory` layers, always uses a single request-parameter object per operation, and emits direct class methods with request-parameter schema validation. By default methods resolve payload data directly; set `unwrapResponseData=false` to return full `AxiosResponse` values when you need headers or status metadata.
66

77
Environment
88
* Node.js

modules/openapi-generator/src/main/resources/typescript-axios-slim/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
{{^withSeparateModelsAndApi}}
77
import type { Configuration } from './configuration{{importFileExtension}}';
8-
import type { AxiosInstance, RawAxiosRequestConfig } from 'axios';
8+
import type { AxiosInstance, RawAxiosRequestConfig, AxiosResponse } from 'axios';
99
import globalAxios from 'axios';
1010
{{#withNodeImports}}
1111
// URLSearchParams not necessarily used

modules/openapi-generator/src/main/resources/typescript-axios-slim/apiInner.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
import type { Configuration } from '{{apiRelativeToRoot}}configuration{{importFileExtension}}';
8-
import type { AxiosInstance, RawAxiosRequestConfig } from 'axios';
8+
import type { AxiosInstance, RawAxiosRequestConfig, AxiosResponse } from 'axios';
99
import globalAxios from 'axios';
1010
{{#withNodeImports}}
1111
// URLSearchParams not necessarily used
@@ -59,7 +59,7 @@ export interface {{classname}}Interface {
5959
* @deprecated{{/isDeprecated}}
6060
* @throws {RequiredError}
6161
*/
62-
{{nickname}}({{#allParams.0}}requestParameters{{^hasRequiredParams}}?{{/hasRequiredParams}}: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}options?: RawAxiosRequestConfig): Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}>;
62+
{{nickname}}({{#allParams.0}}requestParameters{{^hasRequiredParams}}?{{/hasRequiredParams}}: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}options?: RawAxiosRequestConfig): {{#unwrapResponseData}}Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}>{{/unwrapResponseData}}{{^unwrapResponseData}}Promise<AxiosResponse<{{{returnType}}}{{^returnType}}void{{/returnType}}>>{{/unwrapResponseData}};
6363

6464
{{/operation}}
6565
}
@@ -105,7 +105,7 @@ export class {{classname}} extends BaseAPI {
105105
* @deprecated{{/isDeprecated}}
106106
* @throws {RequiredError}
107107
*/
108-
public async {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, {{/allParams.0}}options: RawAxiosRequestConfig = {}): Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
108+
public async {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, {{/allParams.0}}options: RawAxiosRequestConfig = {}): {{#unwrapResponseData}}Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}>{{/unwrapResponseData}}{{^unwrapResponseData}}Promise<AxiosResponse<{{{returnType}}}{{^returnType}}void{{/returnType}}>>{{/unwrapResponseData}} {
109109
{{#allParams.0}}
110110
validateRequestParameters('{{nickname}}', {{nickname}}RequestSchema, requestParameters);
111111
{{/allParams.0}}
@@ -299,8 +299,8 @@ export class {{classname}} extends BaseAPI {
299299
const effectiveBasePath = localVarOperationServerBasePath || this.basePath || BASE_PATH;
300300
localVarRequestOptions.url = (this.axios.defaults.baseURL ? '' : this.configuration?.basePath ?? effectiveBasePath) + toPathString(localVarUrlObj);
301301

302-
const localVarResponse = await this.axios.request<{{{returnType}}}{{^returnType}}void{{/returnType}}>(localVarRequestOptions);
303-
return localVarResponse.data;
302+
{{#unwrapResponseData}}const localVarResponse = await this.axios.request<{{{returnType}}}{{^returnType}}void{{/returnType}}>(localVarRequestOptions);
303+
return localVarResponse.data;{{/unwrapResponseData}}{{^unwrapResponseData}}return this.axios.request<{{{returnType}}}{{^returnType}}void{{/returnType}}, AxiosResponse<{{{returnType}}}{{^returnType}}void{{/returnType}}>>(localVarRequestOptions);{{/unwrapResponseData}}
304304
}
305305
{{^-last}}
306306

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosSlimParityTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ public void shouldReturnPayloadDataFromObjectOrientedMethods() throws Exception
131131
assertTrue(apiSource.contains("return localVarResponse.data;"), "Slim API class should resolve axios response data directly");
132132
}
133133

134+
@Test(description = "slim: full axios responses remain available when payload unwrapping is disabled")
135+
public void shouldReturnAxiosResponsesWhenPayloadUnwrappingDisabled() throws Exception {
136+
IdentitySurface slimSurface = generateIdentity("typescript-axios-slim", EDGE_CASE_SPEC,
137+
cfg -> cfg.addAdditionalProperty("unwrapResponseData", false));
138+
String apiSource = String.join(" ", slimSurface.apiFiles.values());
139+
140+
assertFalse(apiSource.contains("return localVarResponse.data;"), "Slim API class should not unwrap payload data when disabled");
141+
assertTrue(apiSource.contains("Promise<AxiosResponse<"), "Slim API class should return Promise<AxiosResponse<T>> when payload unwrapping is disabled");
142+
assertTrue(apiSource.contains("return this.axios.request<"), "Slim API class should expose the raw axios response when payload unwrapping is disabled");
143+
}
144+
134145
@Test(description = "slim: useSingleRequestParameter remains enabled even if configured false")
135146
public void shouldForceSingleRequestParameterForSlimGenerator() throws Exception {
136147
Consumer<CodegenConfigurator> forceSingleRequestFalse = cfg -> cfg.addAdditionalProperty("useSingleRequestParameter", false);

0 commit comments

Comments
 (0)