Skip to content

Feature/typescript fetch/api config class name #7916

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 4 commits 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-api-config-class-name.sh
./bin/typescript-fetch-petstore.sh
31 changes: 31 additions & 0 deletions bin/typescript-fetch-petstore-api-config-class-name.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/api-config-class-name -D apiConfigClassName=apiConfiguration"

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 @@ -3,4 +3,5 @@
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-api-config-class-name.bat
call bin\windows\typescript-fetch-petstore-interfaces.bat
12 changes: 12 additions & 0 deletions bin/windows/typescript-fetch-petstore-api-config-class-name.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\api-config-class-name -D apiConfigClassName=apiConfiguration

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.swagger.codegen.languages;

import com.google.common.collect.ImmutableMap;
import com.samskivert.mustache.Mustache;

import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.*;

import io.swagger.codegen.mustache.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
Expand All @@ -19,10 +24,12 @@ 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 API_CONFIG_CLASS_NAME = "apiConfigClassName";

protected String npmName = null;
protected String npmVersion = "1.0.0";
protected String npmRepository = null;
protected String apiConfigClassName = null;

public TypeScriptFetchClientCodegen() {
super();
Expand All @@ -39,6 +46,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(API_CONFIG_CLASS_NAME, "Use this property to change the default class name used to configure the API setup parameters from 'Configuration' to a custom name."));
}

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

if (additionalProperties.containsKey(API_CONFIG_CLASS_NAME)) {
setApiConfigClassName(additionalProperties.get(API_CONFIG_CLASS_NAME).toString());
} else {
additionalProperties.put(API_CONFIG_CLASS_NAME, "Configuration");
}

addMustacheLambdas(additionalProperties);
}

@Override
Expand All @@ -80,6 +96,30 @@ public String getTypeDeclaration(Property p) {
}
}

private void addMustacheLambdas(Map<String, Object> objs) {

Map<String, Mustache.Lambda> lambdas = new ImmutableMap.Builder<String, Mustache.Lambda>()
.put("lowercase", new LowercaseLambda().generator(this))
.put("uppercase", new UppercaseLambda())
.put("titlecase", new TitlecaseLambda())
.put("camelcase", new CamelCaseLambda().generator(this))
.put("camelcase_param", new CamelCaseLambda().generator(this).escapeAsParamName(true))
.put("indented", new IndentedLambda())
.put("indented_8", new IndentedLambda(8, " "))
.put("indented_12", new IndentedLambda(12, " "))
.put("indented_16", new IndentedLambda(16, " "))
.build();

if (objs.containsKey("lambda")) {
LOGGER.warn("An property named 'lambda' already exists. Mustache lambdas renamed from 'lambda' to '_lambda'. " +
"You'll likely need to use a custom template, " +
"see https://github.com/swagger-api/swagger-codegen#modifying-the-client-library-format. ");
objs.put("_lambda", lambdas);
} else {
objs.put("lambda", lambdas);
}
}

private void addNpmPackageGeneration() {
if (additionalProperties.containsKey(NPM_NAME)) {
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
Expand Down Expand Up @@ -138,4 +178,12 @@ public void setNpmRepository(String npmRepository) {
this.npmRepository = npmRepository;
}

public String getApiConfigClassName() {
return apiConfigClassName;
}

public void setApiConfigClassName(String apiConfigClassName) {
this.apiConfigClassName = apiConfigClassName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import * as url from "url";
import * as portableFetch from "portable-fetch";
import { Configuration } from "./configuration";
import { {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}} } from "./configuration";

const BASE_PATH = "{{{basePath}}}".replace(/\/+$/, "");

Expand Down Expand Up @@ -44,12 +44,12 @@ export interface FetchArgs {
* @class BaseAPI
*/
export class BaseAPI {
protected configuration: Configuration;
protected {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}: {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}};

constructor(configuration?: Configuration, protected basePath: string = BASE_PATH, protected fetch: FetchAPI = portableFetch) {
if (configuration) {
this.configuration = configuration;
this.basePath = configuration.basePath || this.basePath;
constructor({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}?: {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}, protected basePath: string = BASE_PATH, protected fetch: FetchAPI = portableFetch) {
if ({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}) {
this.{{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}} = {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}};
this.basePath = {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.basePath || this.basePath;
}
}
};
Expand All @@ -76,7 +76,7 @@ export class RequiredError extends Error {
* {{&description}}{{/description}}
* @export
*/
export const {{classname}}FetchParamCreator = function (configuration?: Configuration) {
export const {{classname}}FetchParamCreator = function ({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}?: {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}) {
return {
{{#operation}}
/**
Expand Down Expand Up @@ -113,34 +113,34 @@ export const {{classname}}FetchParamCreator = function (configuration?: Configur
// authentication {{name}} required
{{#isApiKey}}
{{#isKeyInHeader}}
if (configuration && configuration.apiKey) {
const localVarApiKeyValue = typeof configuration.apiKey === 'function'
? configuration.apiKey("{{keyParamName}}")
: configuration.apiKey;
if ({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}} && {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.apiKey) {
const localVarApiKeyValue = typeof {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.apiKey === 'function'
? {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.apiKey("{{keyParamName}}")
: {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.apiKey;
localVarHeaderParameter["{{keyParamName}}"] = localVarApiKeyValue;
}
{{/isKeyInHeader}}
{{#isKeyInQuery}}
if (configuration && configuration.apiKey) {
const localVarApiKeyValue = typeof configuration.apiKey === 'function'
? configuration.apiKey("{{keyParamName}}")
: configuration.apiKey;
if ({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}} && {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.apiKey) {
const localVarApiKeyValue = typeof {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.apiKey === 'function'
? {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.apiKey("{{keyParamName}}")
: {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.apiKey;
localVarQueryParameter["{{keyParamName}}"] = localVarApiKeyValue;
}
{{/isKeyInQuery}}
{{/isApiKey}}
{{#isBasic}}
// http basic authentication required
if (configuration && (configuration.username || configuration.password)) {
localVarHeaderParameter["Authorization"] = "Basic " + btoa(configuration.username + ":" + configuration.password);
if ({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}} && ({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.username || {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.password)) {
localVarHeaderParameter["Authorization"] = "Basic " + btoa({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.username + ":" + {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.password);
}
{{/isBasic}}
{{#isOAuth}}
// oauth required
if (configuration && configuration.accessToken) {
const localVarAccessTokenValue = typeof configuration.accessToken === 'function'
? configuration.accessToken("{{name}}", [{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}])
: configuration.accessToken;
if ({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}} && {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.accessToken) {
const localVarAccessTokenValue = typeof {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.accessToken === 'function'
? {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.accessToken("{{name}}", [{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}])
: {{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}.accessToken;
localVarHeaderParameter["Authorization"] = "Bearer " + localVarAccessTokenValue;
}
{{/isOAuth}}
Expand Down Expand Up @@ -246,7 +246,7 @@ export const {{classname}}FetchParamCreator = function (configuration?: Configur
* {{{description}}}{{/description}}
* @export
*/
export const {{classname}}Fp = function(configuration?: Configuration) {
export const {{classname}}Fp = function({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}?: {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}) {
return {
{{#operation}}
/**
Expand All @@ -261,7 +261,7 @@ export const {{classname}}Fp = function(configuration?: Configuration) {
* @throws {RequiredError}
*/
{{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);
const localVarFetchArgs = {{classname}}FetchParamCreator({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}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) {
Expand All @@ -281,7 +281,7 @@ export const {{classname}}Fp = function(configuration?: Configuration) {
* {{&description}}{{/description}}
* @export
*/
export const {{classname}}Factory = function (configuration?: Configuration, fetch?: FetchAPI, basePath?: string) {
export const {{classname}}Factory = function ({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}?: {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}, fetch?: FetchAPI, basePath?: string) {
return {
{{#operation}}
/**
Expand All @@ -296,7 +296,7 @@ export const {{classname}}Factory = function (configuration?: Configuration, fet
* @throws {RequiredError}
*/
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any) {
return {{classname}}Fp(configuration).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options)(fetch, basePath);
return {{classname}}Fp({{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options)(fetch, basePath);
},
{{/operation}}
};
Expand Down Expand Up @@ -356,7 +356,7 @@ export class {{classname}} extends BaseAPI {
* @memberof {{classname}}
*/
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any) {
return {{classname}}Fp(this.configuration).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options)(this.fetch, this.basePath);
return {{classname}}Fp(this.{{#lambda.camelcase_param}}{{apiConfigClassName}}{{/lambda.camelcase_param}}).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options)(this.fetch, this.basePath);
}

{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
// tslint:disable
{{>licenseInfo}}

export interface ConfigurationParameters {
export interface {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}Parameters {
apiKey?: string | ((name: string) => string);
username?: string;
password?: string;
accessToken?: string | ((name: string, scopes?: string[]) => string);
basePath?: string;
}

export class Configuration {
export class {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}} {
/**
* parameter for apiKey security
* @param name security name
* @memberof Configuration
* @memberof {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}
*/
apiKey?: string | ((name: string) => string);
/**
* parameter for basic security
*
* @type {string}
* @memberof Configuration
* @memberof {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}
*/
username?: string;
/**
* parameter for basic security
*
* @type {string}
* @memberof Configuration
* @memberof {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}
*/
password?: string;
/**
* parameter for oauth2 security
* @param name security name
* @param scopes oauth2 scope
* @memberof Configuration
* @memberof {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}
*/
accessToken?: string | ((name: string, scopes?: string[]) => string);
/**
* override base path
*
* @type {string}
* @memberof Configuration
* @memberof {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}
*/
basePath?: string;

constructor(param: ConfigurationParameters = {}) {
constructor(param: {{#lambda.titlecase}}{{apiConfigClassName}}{{/lambda.titlecase}}Parameters = {}) {
this.apiKey = param.apiKey;
this.username = param.username;
this.password = param.password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +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 String API_CONFIG_CLASS_NAME = "ApiConfiguration";


@Override
Expand All @@ -35,6 +36,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.API_CONFIG_CLASS_NAME, API_CONFIG_CLASS_NAME)
.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