Skip to content

[typescript-fetch] prefix request interface #3740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools</groupId>
<artifactId>TypeScriptFetchBuildNamespaceParameterInterfacesPestoreClientTests</artifactId>
<artifactId>TypeScriptFetchBuildPrefixParameterInterfacesPestoreClientTests</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>TS Fetch Petstore Client (with namespacing for parameter interfaces)</name>
Expand Down
2 changes: 1 addition & 1 deletion bin/openapi3/typescript-fetch-petstore-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
./bin/openapi3/typescript-fetch-petstore-with-npm-version.sh
./bin/openapi3/typescript-fetch-petstore-interfaces.sh
./bin/openapi3/typescript-fetch-petstore.sh
./bin/openapi3/typescript-fetch-petstore-namespace-parameter-interfaces.sh
./bin/openapi3/typescript-fetch-petstore-prefix-parameter-interfaces.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g typescript-fetch -o samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces --additional-properties namespaceParameterInterfaces=true $@"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g typescript-fetch -o samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces --additional-properties prefixParameterInterfaces=true $@"

java $JAVA_OPTS -jar $executable $ags
2 changes: 1 addition & 1 deletion bin/typescript-fetch-petstore-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
./bin/typescript-fetch-petstore-interfaces.sh
./bin/typescript-fetch-petstore.sh
./bin/typescript-fetch-petstore-multiple-parameters.sh
./bin/typescript-fetch-petstore-namespace-parameter-interfaces.sh
./bin/typescript-fetch-petstore-prefix-parameter-interfaces.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"npmVersion": "1.0.0",
"npmRepository" : "https://skimdb.npmjs.com/registry",
"snapshot" : false,
"namespaceParameterInterfaces": true
"prefixParameterInterfaces": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g typescript-fetch -c bin/typescript-fetch-petstore-namespace-parameter-interfaces.json -o samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces $@"
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g typescript-fetch -c bin/typescript-fetch-petstore-prefix-parameter-interfaces.json -o samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces $@"

java $JAVA_OPTS -jar $executable $ags

cp CI/samples.ci/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces/pom.xml samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces/pom.xml
cp CI/samples.ci/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/pom.xml samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/pom.xml
2 changes: 1 addition & 1 deletion bin/windows/typescript-fetch-petstore-all.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ call bin\windows\typescript-fetch-petstore-target-es6.bat
call bin\windows\typescript-fetch-petstore-with-npm-version.bat
call bin\windows\typescript-fetch-petstore-interfaces.bat
call bin\windows\typescript-fetch-petstore-multiple-parameters.bat
call bin\windows\typescript-fetch-petstore-namespace-parameter-interfaces.bat
call bin\windows\typescript-fetch-petstore-prefix-parameter-interfaces.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ If Not Exist %executable% (
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g typescript-fetch -o samples\client\petstore\typescript-fetch\builds\namespace-parameter-interfaces --additional-properties namespaceParameterInterfaces=true
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g typescript-fetch -o samples\client\petstore\typescript-fetch\builds\prefix-parameter-interfaces --additional-properties prefixParameterInterfaces=true

java %JAVA_OPTS% -jar %executable% %ags%
2 changes: 1 addition & 1 deletion docs/generators/typescript-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ sidebar_label: typescript-fetch
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |true|
|namespaceParameterInterfaces|Setting this property to true will generate parameter interface declarations within a dedicated namespace to avoid name conflicts.| |false|
|prefixParameterInterfaces|Setting this property to true will generate parameter interface declarations prefixed with API class name to avoid name conflicts.| |false|
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
public static final String NPM_REPOSITORY = "npmRepository";
public static final String WITH_INTERFACES = "withInterfaces";
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
public static final String NAMESPACE_PARAMETER_INTERFACES = "namespaceParameterInterfaces";
public static final String PREFIX_PARAMETER_INTERFACES = "prefixParameterInterfaces";

protected String npmRepository = null;
private boolean useSingleRequestParameter = true;
private boolean namespaceParameterInterfaces = false;
private boolean prefixParameterInterfaces = false;
protected boolean addedApiIndex = false;
protected boolean addedModelIndex = false;

Expand All @@ -61,7 +61,7 @@ public TypeScriptFetchClientCodegen() {
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.TRUE.toString()));
this.cliOptions.add(new CliOption(NAMESPACE_PARAMETER_INTERFACES, "Setting this property to true will generate parameter interface declarations within a dedicated namespace to avoid name conflicts.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(PREFIX_PARAMETER_INTERFACES, "Setting this property to true will generate parameter interface declarations prefixed with API class name to avoid name conflicts.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
}

@Override
Expand Down Expand Up @@ -97,10 +97,10 @@ public void processOpts() {
}
writePropertyBack(USE_SINGLE_REQUEST_PARAMETER, getUseSingleRequestParameter());

if (additionalProperties.containsKey(NAMESPACE_PARAMETER_INTERFACES)) {
this.setNamespaceParameterInterfaces(convertPropertyToBoolean(NAMESPACE_PARAMETER_INTERFACES));
if (additionalProperties.containsKey(PREFIX_PARAMETER_INTERFACES)) {
this.setPrefixParameterInterfaces(convertPropertyToBoolean(PREFIX_PARAMETER_INTERFACES));
}
writePropertyBack(NAMESPACE_PARAMETER_INTERFACES, getNamespaceParameterInterfaces());
writePropertyBack(PREFIX_PARAMETER_INTERFACES, getPrefixParameterInterfaces());

if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration();
Expand Down Expand Up @@ -216,7 +216,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
this.addOperationModelImportInfomation(operations);
this.updateOperationParameterEnumInformation(operations);
this.addOperationObjectResponseInformation(operations);
this.addOperationNamespaceParameterInterfacesInformation(operations);
this.addOperationPrefixParameterInterfacesInformation(operations);
return operations;
}

Expand Down Expand Up @@ -263,20 +263,9 @@ private void addOperationObjectResponseInformation(Map<String, Object> operation
}
}

private void addOperationNamespaceParameterInterfacesInformation(Map<String, Object> operations) {
private void addOperationPrefixParameterInterfacesInformation(Map<String, Object> operations) {
Map<String, Object> _operations = (Map<String, Object>) operations.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) _operations.get("operation");
if (!operationList.isEmpty() && getNamespaceParameterInterfaces()) {
operations.put("namespaceParameterInterfaces", true);
operations.put("paramIfaceIndent", " ");
operations.put("paramIfaceSuffix", "");
operations.put("paramIfaceNsPrefix", operationList.get(0).baseName + "Requests.");
} else {
operations.put("namespaceParameterInterfaces", false);
operations.put("paramIfaceIndent", "");
operations.put("paramIfaceSuffix", "Request");
operations.put("paramIfaceNsPrefix", "");
}
operations.put("prefixParameterInterfaces", getPrefixParameterInterfaces());
}

private void addExtraReservedWords() {
Expand Down Expand Up @@ -315,11 +304,11 @@ private void setUseSingleRequestParameter(boolean useSingleRequestParameter) {
this.useSingleRequestParameter = useSingleRequestParameter;
}

private boolean getNamespaceParameterInterfaces() {
return namespaceParameterInterfaces;
private boolean getPrefixParameterInterfaces() {
return prefixParameterInterfaces;
}

private void setNamespaceParameterInterfaces(boolean namespaceParameterInterfaces) {
this.namespaceParameterInterfaces = namespaceParameterInterfaces;
private void setPrefixParameterInterfaces(boolean prefixParameterInterfaces) {
this.prefixParameterInterfaces = prefixParameterInterfaces;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,16 @@ import {
{{/imports.0}}

{{#operations}}
{{#namespaceParameterInterfaces}}
{{#operation.0}}
/**
* Generated request parameter interface declarations (in namespace to avoid collisions).
*/
export namespace {{baseName}}Requests {

{{/operation.0}}
{{/namespaceParameterInterfaces}}
{{#operation}}
{{#allParams.0}}
{{paramIfaceIndent}}export interface {{operationIdCamelCase}}{{paramIfaceSuffix}} {
export interface {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request {
{{#allParams}}
{{paramIfaceIndent}} {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
{{/allParams}}
{{paramIfaceIndent}}}
}

{{/allParams.0}}
{{/operation}}
{{#namespaceParameterInterfaces}}
{{#operation.0}}
}

{{/operation.0}}
{{/namespaceParameterInterfaces}}
{{/operations}}
{{#operations}}
/**
Expand All @@ -54,7 +39,7 @@ export class {{classname}} extends runtime.BaseAPI {
* {{&summary}}
{{/summary}}
*/
async {{nickname}}Raw({{#allParams.0}}requestParameters: {{paramIfaceNsPrefix}}{{operationIdCamelCase}}{{paramIfaceSuffix}}{{/allParams.0}}): Promise<runtime.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>> {
async {{nickname}}Raw({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<runtime.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>> {
{{#allParams}}
{{#required}}
if (requestParameters.{{paramName}} === null || requestParameters.{{paramName}} === undefined) {
Expand Down Expand Up @@ -264,7 +249,7 @@ export class {{classname}} extends runtime.BaseAPI {
}
{{/useSingleRequestParameter}}
{{#useSingleRequestParameter}}
async {{nickname}}({{#allParams.0}}requestParameters: {{paramIfaceNsPrefix}}{{operationIdCamelCase}}{{paramIfaceSuffix}}{{/allParams.0}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {
async {{nickname}}({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {
{{#returnType}}
const response = await this.{{nickname}}Raw({{#allParams.0}}requestParameters{{/allParams.0}});
return await response.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Map<String, String> createOptions() {
.put(TypeScriptFetchClientCodegen.SNAPSHOT, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.USE_SINGLE_REQUEST_PARAMETER, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.NAMESPACE_PARAMETER_INTERFACES, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.PREFIX_PARAMETER_INTERFACES, Boolean.FALSE.toString())
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.build();
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -844,15 +844,15 @@
</modules>
</profile>
<profile>
<id>typescript-fetch-client-builds-namespace-parameter-interfaces</id>
<id>typescript-fetch-client-builds-prefix-parameter-interfaces</id>
<activation>
<property>
<name>env</name>
<value>java</value>
</property>
</activation>
<modules>
<module>samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces</module>
<module>samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces</module>
</modules>
</profile>
<profile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools</groupId>
<artifactId>TypeScriptFetchBuildNamespaceParameterInterfacesPestoreClientTests</artifactId>
<artifactId>TypeScriptFetchBuildPrefixParameterInterfacesPestoreClientTests</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>TS Fetch Petstore Client (with namespacing for parameter interfaces)</name>
Expand Down
Loading