Skip to content

Typescript-Fetch add option to have parameters put in an Object #7930

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions bin/typescript-fetch-petstore-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
./bin/typescript-fetch-petstore-target-es6.sh
./bin/typescript-fetch-petstore-with-npm-version.sh
./bin/typescript-fetch-petstore-interfaces.sh
./bin/typescript-fetch-petstore-put-parameters-in-object.sh
./bin/typescript-fetch-petstore.sh
31 changes: 31 additions & 0 deletions bin/typescript-fetch-petstore-put-parameters-in-object.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

SCRIPT="$0"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l typescript-fetch -o samples/client/petstore/typescript-fetch/builds/put-parameters-in-object -D putParametersInObject=true"

java $JAVA_OPTS -jar $executable $ags
1 change: 1 addition & 0 deletions bin/windows/typescript-fetch-petstore-all.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ call bin\windows\typescript-fetch-petstore.bat
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-put-parameters-in-object.bat
12 changes: 12 additions & 0 deletions bin/windows/typescript-fetch-petstore-put-parameters-in-object.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@ECHO OFF

set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l typescript-fetch -o samples\client\petstore\typescript-fetch\builds\put-parameters-in-object -D putParametersInObject=true

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
public static final String NPM_REPOSITORY = "npmRepository";
public static final String SNAPSHOT = "snapshot";
public static final String WITH_INTERFACES = "withInterfaces";
public static final String PUT_PARAMETERS_IN_OBJECT = "putParametersInObject";

protected String npmName = null;
protected String npmVersion = "1.0.0";
Expand All @@ -39,6 +40,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(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(PUT_PARAMETERS_IN_OBJECT, "Setting this property to true will put class parameters in an object instead of explicity listed.", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
}

@Override
Expand All @@ -60,6 +62,10 @@ public void processOpts() {
if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration();
}

if (additionalProperties.containsKey(PUT_PARAMETERS_IN_OBJECT)) {
additionalProperties.put(PUT_PARAMETERS_IN_OBJECT, Boolean.valueOf(additionalProperties.get(PUT_PARAMETERS_IN_OBJECT).toString()));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,33 @@ export const {{classname}}FetchParamCreator = function (configuration?: Configur
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
{{#putParametersInObject}}
{{nickname}}({{#hasParams}}params: { {{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{^-last}}, {{/-last}}{{#-last}} {{/-last}}{{/allParams}}}, {{/hasParams}}options: any = {}): FetchArgs {
{{/putParametersInObject}}
{{^putParametersInObject}}
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options: any = {}): FetchArgs {
{{/putParametersInObject}}
{{#allParams}}
{{#required}}
// verify required parameter '{{paramName}}' is not null or undefined
{{#putParametersInObject}}
if (params["{{paramName}}"] === null || params["{{paramName}}"] === undefined) {
{{/putParametersInObject}}
{{^putParametersInObject}}
if ({{paramName}} === null || {{paramName}} === undefined) {
{{/putParametersInObject}}
throw new RequiredError('{{paramName}}','Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
}
{{/required}}
{{/allParams}}
{{#putParametersInObject}}
const localVarPath = `{{{path}}}`{{#pathParams}}
.replace(`{${"{{baseName}}"}}`, encodeURIComponent(String(params["{{paramName}}"]))){{/pathParams}};
{{/putParametersInObject}}
{{^putParametersInObject}}
const localVarPath = `{{{path}}}`{{#pathParams}}
.replace(`{${"{{baseName}}"}}`, encodeURIComponent(String({{paramName}}))){{/pathParams}};
{{/putParametersInObject}}
const localVarUrlObj = url.parse(localVarPath, true);
const localVarRequestOptions = Object.assign({ method: '{{httpMethod}}' }, options);
const localVarHeaderParameter = {} as any;
Expand Down Expand Up @@ -147,6 +163,34 @@ export const {{classname}}FetchParamCreator = function (configuration?: Configur

{{/authMethods}}
{{#queryParams}}
{{#putParametersInObject}}
{{#isListContainer}}
if (params["{{paramName}}"]) {
{{#isCollectionFormatMulti}}
localVarQueryParameter['{{baseName}}'] = params["{{paramName}}"];
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
localVarQueryParameter['{{baseName}}'] = params["{{paramName}}"].join(COLLECTION_FORMATS["{{collectionFormat}}"]);
{{/isCollectionFormatMulti}}
}
{{/isListContainer}}
{{^isListContainer}}
if (params["{{paramName}}"] !== undefined) {
{{#isDateTime}}
localVarQueryParameter['{{baseName}}'] = (params["{{paramName}}"] as any).toISOString();
{{/isDateTime}}
{{^isDateTime}}
{{#isDate}}
localVarQueryParameter['{{baseName}}'] = (params["{{paramName}}"] as any).toISOString();
{{/isDate}}
{{^isDate}}
localVarQueryParameter['{{baseName}}'] = params["{{paramName}}"];
{{/isDate}}
{{/isDateTime}}
}
{{/isListContainer}}
{{/putParametersInObject}}
{{^putParametersInObject}}
{{#isListContainer}}
if ({{paramName}}) {
{{#isCollectionFormatMulti}}
Expand All @@ -172,9 +216,23 @@ export const {{classname}}FetchParamCreator = function (configuration?: Configur
{{/isDateTime}}
}
{{/isListContainer}}
{{/putParametersInObject}}

{{/queryParams}}
{{#headerParams}}
{{#putParametersInObject}}
{{#isListContainer}}
if (params["{{paramName}}"]) {
localVarHeaderParameter['{{baseName}}'] = params["{{paramName}}"].join(COLLECTION_FORMATS["{{collectionFormat}}"]));
}
{{/isListContainer}}
{{^isListContainer}}
if (params["{{paramName}}"] !== undefined && params["{{paramName}}"] !== null) {
localVarHeaderParameter['{{baseName}}'] = String(params["{{paramName}}"]);
}
{{/isListContainer}}
{{/putParametersInObject}}
{{^putParametersInObject}}
{{#isListContainer}}
if ({{paramName}}) {
localVarHeaderParameter['{{baseName}}'] = {{paramName}}.join(COLLECTION_FORMATS["{{collectionFormat}}"]));
Expand All @@ -185,9 +243,30 @@ export const {{classname}}FetchParamCreator = function (configuration?: Configur
localVarHeaderParameter['{{baseName}}'] = String({{paramName}});
}
{{/isListContainer}}
{{/putParametersInObject}}

{{/headerParams}}
{{#formParams}}
{{#putParametersInObject}}
{{#isListContainer}}
if (params["{{paramName}}"]) {
{{#isCollectionFormatMulti}}
params["{{paramName}}"].forEach((element) => {
localVarFormParams.append('{{baseName}}', element as any);
})
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
localVarFormParams.set('{{baseName}}', params["{{paramName}}"].join(COLLECTION_FORMATS["{{collectionFormat}}"]));
{{/isCollectionFormatMulti}}
}
{{/isListContainer}}
{{^isListContainer}}
if (params["{{paramName}}"] !== undefined) {
localVarFormParams.set('{{baseName}}', params["{{paramName}}"] as any);
}
{{/isListContainer}}
{{/putParametersInObject}}
{{^putParametersInObject}}
{{#isListContainer}}
if ({{paramName}}) {
{{#isCollectionFormatMulti}}
Expand All @@ -205,6 +284,7 @@ export const {{classname}}FetchParamCreator = function (configuration?: Configur
localVarFormParams.set('{{baseName}}', {{paramName}} as any);
}
{{/isListContainer}}
{{/putParametersInObject}}

{{/formParams}}
{{#hasFormParams}}
Expand All @@ -229,7 +309,12 @@ export const {{classname}}FetchParamCreator = function (configuration?: Configur
{{/hasFormParams}}
{{#bodyParam}}
const needsSerialization = (<any>"{{dataType}}" !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
{{#putParametersInObject}}
localVarRequestOptions.body = needsSerialization ? JSON.stringify(params["{{paramName}}"] || {}) : (params["{{paramName}}"] || "");
{{/putParametersInObject}}
{{^putParametersInObject}}
localVarRequestOptions.body = needsSerialization ? JSON.stringify({{paramName}} || {}) : ({{paramName}} || "");
{{/putParametersInObject}}
{{/bodyParam}}

return {
Expand Down Expand Up @@ -260,6 +345,21 @@ export const {{classname}}Fp = function(configuration?: Configuration) {
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
{{#putParametersInObject}}
{{nickname}}({{#hasParams}}params: { {{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{^-last}}, {{/-last}}{{#-last}} {{/-last}}{{/allParams}}}, {{/hasParams}}options?: any): (fetch?: FetchAPI, basePath?: string) => Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Response{{/returnType}}> {
const localVarFetchArgs = {{classname}}FetchParamCreator(configuration).{{nickname}}({{#hasParams}}params, {{/hasParams}}options);
return (fetch: FetchAPI = portableFetch, basePath: string = BASE_PATH) => {
return fetch(basePath + localVarFetchArgs.url, localVarFetchArgs.options).then((response) => {
if (response.status >= 200 && response.status < 300) {
return response{{#returnType}}.json(){{/returnType}};
} else {
throw response;
}
});
};
},
{{/putParametersInObject}}
{{^putParametersInObject}}
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any): (fetch?: FetchAPI, basePath?: string) => Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Response{{/returnType}}> {
const localVarFetchArgs = {{classname}}FetchParamCreator(configuration).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options);
return (fetch: FetchAPI = portableFetch, basePath: string = BASE_PATH) => {
Expand All @@ -272,6 +372,8 @@ export const {{classname}}Fp = function(configuration?: Configuration) {
});
};
},
{{/putParametersInObject}}

{{/operation}}
}
};
Expand All @@ -295,9 +397,17 @@ export const {{classname}}Factory = function (configuration?: Configuration, fet
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
{{#putParametersInObject}}
{{nickname}}({{#hasParams}}params: { {{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{^-last}}, {{/-last}}{{#-last}} {{/-last}}{{/allParams}}}, {{/hasParams}}options?: any) {
return {{classname}}Fp(configuration).{{nickname}}({{#hasParams}}params, {{/hasParams}}options)(fetch, basePath);
},
{{/putParametersInObject}}
{{^putParametersInObject}}
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any) {
return {{classname}}Fp(configuration).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options)(fetch, basePath);
},
{{/putParametersInObject}}

{{/operation}}
};
};
Expand All @@ -323,7 +433,12 @@ export interface {{classname}}Interface {
* @throws {RequiredError}
* @memberof {{classname}}Interface
*/
{{#putParametersInObject}}
{{nickname}}({{#hasParams}}params: { {{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{^-last}}, {{/-last}}{{#-last}} {{/-last}}{{/allParams}}}, {{/hasParams}}options?: any): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}>;
{{/putParametersInObject}}
{{^putParametersInObject}}
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}>;
{{/putParametersInObject}}

{{/operation}}
}
Expand Down Expand Up @@ -355,9 +470,16 @@ export class {{classname}} extends BaseAPI {
* @throws {RequiredError}
* @memberof {{classname}}
*/
{{#putParametersInObject}}
public {{nickname}}({{#hasParams}}params: { {{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{^-last}}, {{/-last}}{{#-last}} {{/-last}}{{/allParams}}}, {{/hasParams}}options?: any) {
return {{classname}}Fp(this.configuration).{{nickname}}({{#hasParams}}params, {{/hasParams}}options)(this.fetch, this.basePath);
}
{{/putParametersInObject}}
{{^putParametersInObject}}
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any) {
return {{classname}}Fp(this.configuration).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options)(this.fetch, this.basePath);
}
{{/putParametersInObject}}

{{/operation}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TypeScriptFetchClientOptionsProvider implements OptionsProvider {
private static final String NMP_VERSION = "1.0.0";
private static final String NPM_REPOSITORY = "https://registry.npmjs.org";
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";

public static final Boolean PUT_PARAMETERS_IN_OBJECT = false;

@Override
public String getLanguage() {
Expand All @@ -35,6 +35,7 @@ public Map<String, String> createOptions() {
.put(TypeScriptFetchClientCodegen.SNAPSHOT, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString())
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(TypeScriptFetchClientCodegen.PUT_PARAMETERS_IN_OBJECT, Boolean.FALSE.toString())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wwwroot/*.js
node_modules
typings
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Swagger Codegen Ignore
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.4.0-SNAPSHOT
Loading