Skip to content

Commit 8c85e3c

Browse files
wing328varqasim
andauthored
[typescript-axios] Add support for AWSv4 Signature (#22174)
* [typescript-axios] Add support for AWSv4 Signature * update typescript-axios.md file * update doc --------- Co-authored-by: qasim <[email protected]>
1 parent d840c49 commit 8c85e3c

File tree

38 files changed

+445
-36
lines changed

38 files changed

+445
-36
lines changed

docs/generators.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ id: generators
33
title: Generators List
44
---
55

6+
[main] INFO o.o.c.l.PythonFastAPIServerCodegen - Skipping sorting of path operations, order matters, let the developer decide via their specification file.
67
The following generators are available:
78

89
## CLIENT generators

docs/generators/typescript-axios.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4444
|supportsES6|Generate code that conforms to ES6.| |false|
4545
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |false|
4646
|useSquareBracketsInArrayNames|Setting this property to true will add brackets to array attribute names, e.g. my_values[].| |false|
47+
|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|
4849
|withNodeImports|Setting this property to true adds imports for NodeJS| |false|
4950
|withSeparateModelsAndApi|Put the model and api in separate folders and in separate classes. This requires in addition a value for 'apiPackage' and 'modelPackage'| |false|
@@ -291,7 +292,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
291292
|OAuth2_ClientCredentials|✗|OAS2,OAS3
292293
|OAuth2_AuthorizationCode|✗|OAS2,OAS3
293294
|SignatureAuth|✗|OAS3
294-
|AWSV4Signature||ToolingExtension
295+
|AWSV4Signature||ToolingExtension
295296

296297
### Wire Format Feature
297298
| Name | Supported | Defined By |

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
5252
public static final String USE_SQUARE_BRACKETS_IN_ARRAY_NAMES = "useSquareBracketsInArrayNames";
5353
public static final String AXIOS_VERSION = "axiosVersion";
5454
public static final String DEFAULT_AXIOS_VERSION = "^1.6.1";
55+
public static final String WITH_AWSV4_SIGNATURE = "withAWSV4Signature";
5556

5657
@Getter @Setter
5758
protected String npmRepository = null;
@@ -60,6 +61,7 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
6061

6162
@Getter @Setter
6263
protected String axiosVersion = DEFAULT_AXIOS_VERSION;
64+
protected boolean withAWSV4Signature = false;
6365

6466
private String tsModelPackage = "";
6567

@@ -71,7 +73,7 @@ public TypeScriptAxiosClientCodegen() {
7173

7274
modifyFeatureSet(features -> features
7375
.includeDocumentationFeatures(DocumentationFeature.Readme)
74-
.includeSecurityFeatures(SecurityFeature.BearerToken));
76+
.includeSecurityFeatures(SecurityFeature.BearerToken, SecurityFeature.AWSV4Signature));
7577

7678
// clear import mapping (from default generator) as TS does not use it
7779
// at the moment
@@ -94,6 +96,7 @@ public TypeScriptAxiosClientCodegen() {
9496
this.cliOptions.add(new CliOption(IMPORT_FILE_EXTENSION_SWITCH, IMPORT_FILE_EXTENSION_SWITCH_DESC, SchemaTypeUtil.STRING_TYPE).defaultValue(this.importFileExtension));
9597
this.cliOptions.add(new CliOption(USE_SQUARE_BRACKETS_IN_ARRAY_NAMES, "Setting this property to true will add brackets to array attribute names, e.g. my_values[].", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
9698
this.cliOptions.add(new CliOption(AXIOS_VERSION, "Use this property to override the axios version in package.json").defaultValue(DEFAULT_AXIOS_VERSION));
99+
this.cliOptions.add(new CliOption(WITH_AWSV4_SIGNATURE, "whether to include AWS v4 signature support", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
97100
// Templates have no mapping between formatted property names and original base names so use only "original" and remove this option
98101
removeOption(CodegenConstants.MODEL_PROPERTY_NAMING);
99102
}
@@ -182,6 +185,10 @@ public void processOpts() {
182185
setAxiosVersion(additionalProperties.get(AXIOS_VERSION).toString());
183186
}
184187
additionalProperties.put("axiosVersion", getAxiosVersion());
188+
if (additionalProperties.containsKey(CodegenConstants.WITH_AWSV4_SIGNATURE_COMMENT)) {
189+
this.setWithAWSV4Signature(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.WITH_AWSV4_SIGNATURE_COMMENT).toString()));
190+
}
191+
additionalProperties.put(CodegenConstants.WITH_AWSV4_SIGNATURE_COMMENT, withAWSV4Signature);
185192

186193
}
187194

@@ -307,6 +314,10 @@ public String modelDocFileFolder() {
307314
return (outputFolder + "/" + modelDocPath).replace('/', File.separatorChar);
308315
}
309316

317+
public void setWithAWSV4Signature(boolean withAWSV4Signature) {
318+
this.withAWSV4Signature = withAWSV4Signature;
319+
}
320+
310321
/**
311322
* Overriding toRegularExpression() to avoid escapeText() being called,
312323
* as it would return a broken regular expression if any escaped character / metacharacter were present.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import FormData from 'form-data'
1717
{{/withNodeImports}}
1818
// Some imports not used depending on template conditions
1919
// @ts-ignore
20-
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from './common{{importFileExtension}}';
20+
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction{{#withAWSV4Signature}}, setAWS4SignatureInterceptor{{/withAWSV4Signature}} } from './common{{importFileExtension}}';
2121
import type { RequestArgs } from './base{{importFileExtension}}';
2222
// @ts-ignore
2323
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError, operationServerMap } from './base{{importFileExtension}}';

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import FormData from 'form-data'
1717
{{/withNodeImports}}
1818
// Some imports not used depending on template conditions
1919
// @ts-ignore
20-
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from '{{apiRelativeToRoot}}common{{importFileExtension}}';
20+
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction{{#withAWSV4Signature}}, setAWS4SignatureInterceptor{{/withAWSV4Signature}} } from '{{apiRelativeToRoot}}common{{importFileExtension}}';
2121
// @ts-ignore
2222
import { BASE_PATH, COLLECTION_FORMATS, type RequestArgs, BaseAPI, RequiredError, operationServerMap } from '{{apiRelativeToRoot}}base{{importFileExtension}}';
2323
{{#imports}}
@@ -71,6 +71,10 @@ export const {{classname}}AxiosParamCreator = function (configuration?: Configur
7171
{{#authMethods}}
7272
// authentication {{name}} required
7373
{{#isApiKey}}
74+
{{#withAWSV4Signature}}
75+
// aws v4 signature authentication required
76+
await setAWS4SignatureInterceptor(globalAxios, configuration)
77+
{{/withAWSV4Signature}}
7478
{{#isKeyInHeader}}
7579
await setApiKeyToObject(localVarHeaderParameter, "{{keyParamName}}", configuration)
7680
{{/isKeyInHeader}}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
/* eslint-disable */
33
{{>licenseInfo}}
44

5-
65
import type { Configuration } from "./configuration{{importFileExtension}}";
76
import type { RequestArgs } from "./base{{importFileExtension}}";
87
import type { AxiosInstance, AxiosResponse } from 'axios';
8+
{{#withAWSV4Signature}}
9+
import { aws4Interceptor } from "aws4-axios";
10+
{{/withAWSV4Signature}}
911
import { RequiredError } from "./base{{importFileExtension}}";
1012
{{#withNodeImports}}
1113
import { URL, URLSearchParams } from 'url';
@@ -56,6 +58,25 @@ export const setOAuthToObject = async function (object: any, name: string, scope
5658
}
5759
}
5860

61+
{{#withAWSV4Signature}}
62+
export const setAWS4SignatureInterceptor = async function (globalAxios: AxiosInstance, configuration?: Configuration) {
63+
if (configuration && configuration.awsv4) {
64+
const interceptor = aws4Interceptor({
65+
options: {
66+
region: configuration.awsv4?.options?.region ?? process.env.AWS_REGION ?? 'us-east-1',
67+
service: configuration.awsv4?.options?.service ?? 'execute-api',
68+
},
69+
credentials: {
70+
accessKeyId: configuration.awsv4?.credentials?.accessKeyId ?? process.env.AWS_ACCESS_KEY_ID,
71+
secretAccessKey: configuration.awsv4?.credentials?.secretAccessKey ?? process.env.AWS_SECRET_ACCESS_KEY,
72+
sessionToken: configuration.awsv4?.credentials?.sessionToken ?? process.env.AWS_SESSION_TOKEN
73+
},
74+
});
75+
globalAxios.interceptors.request.use(interceptor);
76+
}
77+
}
78+
{{/withAWSV4Signature}}
79+
5980
function setFlattenedQueryParams(urlSearchParams: URLSearchParams, parameter: any, key: string = ""): void {
6081
if (parameter == null) return;
6182
if (typeof parameter === "object") {

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
/* tslint:disable */
2-
/* eslint-disable */
32
{{>licenseInfo}}
43

4+
interface AWSv4Configuration {
5+
options?: {
6+
region?: string
7+
service?: string
8+
}
9+
credentials?: {
10+
accessKeyId?: string
11+
secretAccessKey?: string,
12+
sessionToken?: string
13+
}
14+
}
515

616
export interface ConfigurationParameters {
717
apiKey?: string | Promise<string> | ((name: string) => string) | ((name: string) => Promise<string>);
818
username?: string;
919
password?: string;
1020
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
21+
awsv4?: AWSv4Configuration;
1122
basePath?: string;
1223
serverIndex?: number;
1324
baseOptions?: any;
@@ -34,6 +45,17 @@ export class Configuration {
3445
* @param scopes oauth2 scope
3546
*/
3647
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
48+
/**
49+
* parameter for aws4 signature security
50+
* @param {Object} AWS4Signature - AWS4 Signature security
51+
* @param {string} options.region - aws region
52+
* @param {string} options.service - name of the service.
53+
* @param {string} credentials.accessKeyId - aws access key id
54+
* @param {string} credentials.secretAccessKey - aws access key
55+
* @param {string} credentials.sessionToken - aws session token
56+
* @memberof Configuration
57+
*/
58+
awsv4?: AWSv4Configuration;
3759
/**
3860
* override base path
3961
*/
@@ -60,6 +82,7 @@ export class Configuration {
6082
this.username = param.username;
6183
this.password = param.password;
6284
this.accessToken = param.accessToken;
85+
this.awsv4 = param.awsv4;
6386
this.basePath = param.basePath;
6487
this.serverIndex = param.serverIndex;
6588
this.baseOptions = {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
},
2828
"dependencies": {
2929
"axios": "{{axiosVersion}}"
30+
{{#withAWSV4Signature}}
31+
"aws4-axios": "^3.3.4"
32+
{{/withAWSV4Signature}}
3033
},
3134
"devDependencies": {
3235
"@types/node": "12.11.5 - 12.20.42",

samples/client/echo_api/typescript-axios/build/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* Do not edit the class manually.
1313
*/
1414

15-
1615
import type { Configuration } from "./configuration";
1716
import type { RequestArgs } from "./base";
1817
import type { AxiosInstance, AxiosResponse } from 'axios';
@@ -63,6 +62,7 @@ export const setOAuthToObject = async function (object: any, name: string, scope
6362
}
6463
}
6564

65+
6666
function setFlattenedQueryParams(urlSearchParams: URLSearchParams, parameter: any, key: string = ""): void {
6767
if (parameter == null) return;
6868
if (typeof parameter === "object") {

samples/client/echo_api/typescript-axios/build/configuration.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* tslint:disable */
2-
/* eslint-disable */
32
/**
43
* Echo Server API
54
* Echo Server API
@@ -12,12 +11,24 @@
1211
* Do not edit the class manually.
1312
*/
1413

14+
interface AWSv4Configuration {
15+
options?: {
16+
region?: string
17+
service?: string
18+
}
19+
credentials?: {
20+
accessKeyId?: string
21+
secretAccessKey?: string,
22+
sessionToken?: string
23+
}
24+
}
1525

1626
export interface ConfigurationParameters {
1727
apiKey?: string | Promise<string> | ((name: string) => string) | ((name: string) => Promise<string>);
1828
username?: string;
1929
password?: string;
2030
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
31+
awsv4?: AWSv4Configuration;
2132
basePath?: string;
2233
serverIndex?: number;
2334
baseOptions?: any;
@@ -44,6 +55,17 @@ export class Configuration {
4455
* @param scopes oauth2 scope
4556
*/
4657
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
58+
/**
59+
* parameter for aws4 signature security
60+
* @param {Object} AWS4Signature - AWS4 Signature security
61+
* @param {string} options.region - aws region
62+
* @param {string} options.service - name of the service.
63+
* @param {string} credentials.accessKeyId - aws access key id
64+
* @param {string} credentials.secretAccessKey - aws access key
65+
* @param {string} credentials.sessionToken - aws session token
66+
* @memberof Configuration
67+
*/
68+
awsv4?: AWSv4Configuration;
4769
/**
4870
* override base path
4971
*/
@@ -70,6 +92,7 @@ export class Configuration {
7092
this.username = param.username;
7193
this.password = param.password;
7294
this.accessToken = param.accessToken;
95+
this.awsv4 = param.awsv4;
7396
this.basePath = param.basePath;
7497
this.serverIndex = param.serverIndex;
7598
this.baseOptions = {

0 commit comments

Comments
 (0)