Skip to content

Commit

Permalink
Introduce option 'importFileExtension' to typescript-fetch (OpenAPITo…
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanpelikan authored Jun 1, 2023
1 parent 6504eaf commit 3943e97
Show file tree
Hide file tree
Showing 77 changed files with 178 additions and 157 deletions.
1 change: 1 addition & 0 deletions docs/generators/typescript-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|enumNameSuffix|Suffix that will be appended to all enum names.| |Enum|
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase|
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
|importFileExtension|File extension to use with relative imports. Set it to '.js' or '.mjs' when using [ESM](https://nodejs.org/api/esm.html).| ||
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
public static final String WITHOUT_RUNTIME_CHECKS = "withoutRuntimeChecks";
public static final String STRING_ENUMS = "stringEnums";
public static final String STRING_ENUMS_DESC = "Generate string enums instead of objects for enum values.";
public static final String IMPORT_FILE_EXTENSION_SWITCH = "importFileExtension";
public static final String IMPORT_FILE_EXTENSION_SWITCH_DESC = "File extension to use with relative imports. Set it to '.js' or '.mjs' when using [ESM](https://nodejs.org/api/esm.html).";

protected String npmRepository = null;
protected String importFileExtension = "";
private boolean useSingleRequestParameter = true;
private boolean prefixParameterInterfaces = false;
protected boolean addedApiIndex = false;
Expand Down Expand Up @@ -98,6 +101,7 @@ public TypeScriptFetchClientCodegen() {
this.cliOptions.add(new CliOption(WITHOUT_RUNTIME_CHECKS, "Setting this property to true will remove any runtime checks on the request and response payloads. Payloads will be casted to their expected types.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(SAGAS_AND_RECORDS, "Setting this property to true will generate additional files for use with redux-saga and immutablejs.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(STRING_ENUMS, STRING_ENUMS_DESC, SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(IMPORT_FILE_EXTENSION_SWITCH, IMPORT_FILE_EXTENSION_SWITCH_DESC).defaultValue(""));
}

@Override
Expand All @@ -118,6 +122,14 @@ public void setNpmRepository(String npmRepository) {
this.npmRepository = npmRepository;
}

public String getImportFileExtension() {
return importFileExtension;
}

public void setImportFileExtension(String importFileExtension) {
this.importFileExtension = importFileExtension;
}

public Boolean getWithoutRuntimeChecks() {
return withoutRuntimeChecks;
}
Expand Down Expand Up @@ -208,6 +220,11 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("index.mustache", sourceDir, "index.ts"));
supportingFiles.add(new SupportingFile("runtime.mustache", sourceDir, "runtime.ts"));

if (additionalProperties.containsKey(IMPORT_FILE_EXTENSION_SWITCH)) {
this.setImportFileExtension(additionalProperties.get(IMPORT_FILE_EXTENSION_SWITCH).toString());
}
writePropertyBack(IMPORT_FILE_EXTENSION_SWITCH, getImportFileExtension());

if (additionalProperties.containsKey(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER)) {
this.setUseSingleRequestParameter(convertPropertyToBoolean(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
{{/isEntity}}
{{/model}}
{{/models}}
} from "./models"
} from "./models/index{{importFileExtension}}"

export const ApiEntitiesRecordProps = {
recType: "ApiEntitiesRecord" as "ApiEntitiesRecord",
Expand All @@ -23,4 +23,4 @@ export const ApiEntitiesRecordProps = {

export type ApiEntitiesRecordPropsType = typeof ApiEntitiesRecordProps;
export const ApiEntitiesRecord = Record(ApiEntitiesRecordProps, ApiEntitiesRecordProps.recType);
export type ApiEntitiesRecord = RecordOf<ApiEntitiesRecordPropsType>;
export type ApiEntitiesRecord = RecordOf<ApiEntitiesRecordPropsType>;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ApiEntitiesRecord} from "./ApiEntitiesRecord";
import {ApiEntitiesRecord} from "./ApiEntitiesRecord{{importFileExtension}}";
import {ReducerBuilder} from "redux-ts-simple";
import {normalizedEntities} from "./runtimeSagasAndRecords";
import {normalizedEntities} from "./runtimeSagasAndRecords{{importFileExtension}}";

export const ApiEntitiesReducer = new ReducerBuilder(ApiEntitiesRecord())
.on(normalizedEntities, (state, action): ApiEntitiesRecord => {
Expand All @@ -18,4 +18,4 @@ export const ApiEntitiesReducer = new ReducerBuilder(ApiEntitiesRecord())
}
});
})
.build();
.build();
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
{{#lambda.camelcase}}{{classFilename}}{{/lambda.camelcase}}AllSagas,
{{/apis}}
{{/apiInfo}}
} from "./";
} from "./index{{importFileExtension}}";

export function *allApiSagas() {
yield all([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* tslint:disable */
/* eslint-disable */
{{#useSagaAndRecords}}
export * from './SagaApiManager'
export * from './allSagas'
export * from './SagaApiManager{{importFileExtension}}'
export * from './allSagas{{importFileExtension}}'
{{/useSagaAndRecords}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
export * from './{{ classFilename }}';
export * from './{{ classFilename }}{{importFileExtension}}';
{{#useSagaAndRecords}}
export * from './{{{ classFilename }}}Sagas';
export * from './{{{ classFilename }}}Sagas{{importFileExtension}}';
{{/useSagaAndRecords}}
{{/operations}}
{{/apis}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
/* eslint-disable */
{{>licenseInfo}}

import * as runtime from '../runtime';
import * as runtime from '../runtime{{importFileExtension}}';
{{#imports.0}}
import type {
{{#imports}}
{{className}},
{{/imports}}
} from '../models';
} from '../models/index{{importFileExtension}}';
{{^withoutRuntimeChecks}}
import {
{{#imports}}
{{className}}FromJSON,
{{className}}ToJSON,
{{/imports}}
} from '../models';
} from '../models/index{{importFileExtension}}';
{{/withoutRuntimeChecks}}
{{/imports.0}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* tslint:disable */
/* eslint-disable */
export * from './runtime';
export * from './runtime{{importFileExtension}}';
{{#useSagaAndRecords}}
export * from './runtimeSagasAndRecords';
export * from './ApiEntitiesRecord';
export * from './ApiEntitiesReducer';
export * from './ApiEntitiesSelectors';
export * from './runtimeSagasAndRecords{{importFileExtension}}';
export * from './ApiEntitiesRecord{{importFileExtension}}';
export * from './ApiEntitiesReducer{{importFileExtension}}';
export * from './ApiEntitiesSelectors{{importFileExtension}}';
{{/useSagaAndRecords}}
{{#apiInfo}}
{{#apis.0}}
export * from './apis';
export * from './apis/index{{importFileExtension}}';
{{/apis.0}}
{{/apiInfo}}
{{#models.0}}
export * from './models';
{{/models.0}}
export * from './models/index{{importFileExtension}}';
{{/models.0}}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { exists, mapValues } from '../runtime';
import { exists, mapValues } from '../runtime{{importFileExtension}}';
{{#hasImports}}
{{#imports}}
import type { {{{.}}} } from './{{.}}';
import type { {{{.}}} } from './{{.}}{{importFileExtension}}';
import {
{{.}}FromJSON,
{{.}}FromJSONTyped,
{{.}}ToJSON,
} from './{{.}}';
} from './{{.}}{{importFileExtension}}';
{{/imports}}

{{/hasImports}}
Expand All @@ -15,7 +15,7 @@ import {
{{#discriminator.mappedModels}}
{{modelName}}FromJSONTyped{{^-last}},{{/-last}}
{{/discriminator.mappedModels}}
} from './';
} from './index{{importFileExtension}}';

{{/discriminator}}
{{>modelGenericInterfaces}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
{{{.}}}FromJSON,
{{{.}}}FromJSONTyped,
{{{.}}}ToJSON,
} from './{{.}}';
} from './{{.}}{{importFileExtension}}';
{{/oneOf}}

{{/hasImports}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
{{#models}}
{{#model}}
{{^withoutRuntimeChecks}}
export * from './{{{ classFilename }}}';
export * from './{{{ classFilename }}}{{importFileExtension}}';
{{#useSagaAndRecords}}
{{^isEnum}}
export * from './{{{ classFilename }}}Record';
export * from './{{{ classFilename }}}Record{{importFileExtension}}';
{{/isEnum}}
{{/useSagaAndRecords}}
{{/withoutRuntimeChecks}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ApiRecordUtils, knownRecordFactories{{#returnPassthrough}}, appFromJS, NormalizedRecordEntities{{/returnPassthrough}}} from "../runtimeSagasAndRecords";
import {getApiEntitiesState} from "../ApiEntitiesSelectors"
import {ApiRecordUtils, knownRecordFactories{{#returnPassthrough}}, appFromJS, NormalizedRecordEntities{{/returnPassthrough}}} from "../runtimeSagasAndRecords{{importFileExtension}}";
import {getApiEntitiesState} from "../ApiEntitiesSelectors{{importFileExtension}}"
import {List, Record, RecordOf, Map} from 'immutable';
import {Schema, schema, NormalizedSchema} from "normalizr";
import {select, call} from "redux-saga/effects";
Expand All @@ -11,19 +11,19 @@ import {
{{classname}}{{enumName}},
{{/isEnum}}
{{/vars}}
} from './{{classname}}';
} from './{{classname}}{{importFileExtension}}';

{{#imports}}
import {
{{{.}}},
} from './{{{.}}}';
} from './{{{.}}}{{importFileExtension}}';
{{/imports}}

{{#modelImports}}
import {
{{{.}}}Record,
{{#lambda.camelcase}}{{.}}{{/lambda.camelcase}}RecordUtils
} from './{{{.}}}Record';
} from './{{{.}}}Record{{importFileExtension}}';
{{/modelImports}}

export const {{classname}}RecordProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
Configuration,
ConfigurationParameters,
} from "../";
} from "../index{{importFileExtension}}";

import {
{{#apiInfo}}
{{#apis}}
{{classFilename}},
{{/apis}}
{{/apiInfo}}
} from "./";
} from "./index{{importFileExtension}}";

export class Api {
{{#apiInfo}}
Expand All @@ -25,4 +25,4 @@ export class Api {
{{/apis}}
{{/apiInfo}}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
/* eslint-disable */
{{>licenseInfo}}

import {Api} from './';
import {Api} from './index{{importFileExtension}}';
import {List} from 'immutable';
import {all, fork, put, takeLatest} from "redux-saga/effects";
import {apiCall, createSagaAction as originalCreateSagaAction, BaseEntitySupportPayloadApiAction, BasePayloadApiAction, NormalizedRecordEntities, normalizedEntities} from "../runtimeSagasAndRecords";
import {apiCall, createSagaAction as originalCreateSagaAction, BaseEntitySupportPayloadApiAction, BasePayloadApiAction, NormalizedRecordEntities, normalizedEntities} from "../runtimeSagasAndRecords{{importFileExtension}}";
import {Action} from "redux-ts-simple";

{{#imports.0}}
Expand All @@ -18,7 +18,7 @@ import {
{{#passthroughImports}}
{{.}},
{{/passthroughImports}}
} from '../models';
} from '../models/index{{importFileExtension}}';
{{/imports.0}}
{{#hasEnums}}
{{#operations}}
Expand All @@ -28,7 +28,7 @@ import {

import {
{{operationIdCamelCase}}{{enumName}},
} from './{{classname}}';
} from './{{classname}}{{importFileExtension}}';
{{/isEnum}}
{{/allParams}}
{{/operation}}
Expand Down Expand Up @@ -241,4 +241,4 @@ export function *{{nickname}}SagaImp(_action_: Action<Payload{{#lambda.titlecase
}
//endregion
{{/operation}}
{{/operations}}
{{/operations}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './src';
export * from './src/index{{importFileExtension}}';
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class TypeScriptFetchClientOptionsProvider implements OptionsProvider {
public static final String SORT_MODEL_PROPERTIES_VALUE = "false";
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
public static final Boolean SUPPORTS_ES6_VALUE = false;
public static final String IMPORT_FILE_EXTENSION_VALUE = "";
public static final Boolean NULL_SAFE_ADDITIONAL_PROPS_VALUE = false;
public static final String ENUM_NAME_SUFFIX = "Enum";
public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase";
Expand Down Expand Up @@ -70,6 +71,7 @@ public Map<String, String> createOptions() {
.put(TypeScriptFetchClientCodegen.PREFIX_PARAMETER_INTERFACES, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.WITHOUT_RUNTIME_CHECKS, WITHOUT_RUNTIME_CHECKS)
.put(TypeScriptFetchClientCodegen.SAGAS_AND_RECORDS, SAGAS_AND_RECORDS)
.put(TypeScriptFetchClientCodegen.IMPORT_FILE_EXTENSION_SWITCH, IMPORT_FILE_EXTENSION_VALUE)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.put(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected void verifyOptions() {
verify(clientCodegen).setModelPropertyNaming(TypeScriptFetchClientOptionsProvider.MODEL_PROPERTY_NAMING_VALUE);
verify(clientCodegen).setParamNaming(TypeScriptFetchClientOptionsProvider.PARAM_NAMING_VALUE);
verify(clientCodegen).setSupportsES6(TypeScriptFetchClientOptionsProvider.SUPPORTS_ES6_VALUE);
verify(clientCodegen).setImportFileExtension(TypeScriptFetchClientOptionsProvider.IMPORT_FILE_EXTENSION_VALUE);
verify(clientCodegen).setPrependFormOrBodyParameters(Boolean.valueOf(TypeScriptFetchClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE));
verify(clientCodegen).setWithoutRuntimeChecks(Boolean.valueOf(TypeScriptFetchClientOptionsProvider.WITHOUT_RUNTIME_CHECKS));
verify(clientCodegen).setSagasAndRecords(Boolean.valueOf(TypeScriptFetchClientOptionsProvider.SAGAS_AND_RECORDS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import * as runtime from '../runtime';
import type {
Club,
} from '../models';
} from '../models/index';
import {
ClubFromJSON,
ClubToJSON,
} from '../models';
} from '../models/index';

export interface ListRequest {
personId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export * from './runtime';
export * from './apis';
export * from './models';
export * from './apis/index';
export * from './models/index';
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import * as runtime from '../runtime';
import type {
Club,
} from '../models';
} from '../models/index';
import {
ClubFromJSON,
ClubToJSON,
} from '../models';
} from '../models/index';

export interface ListRequest {
personId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export * from './runtime';
export * from './apis';
export * from './models';
export * from './apis/index';
export * from './models/index';
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import * as runtime from '../runtime';
import type {
Client,
} from '../models';
} from '../models/index';
import {
ClientFromJSON,
ClientToJSON,
} from '../models';
} from '../models/index';

export interface 123testSpecialTagsRequest {
client: Client;
Expand Down
Loading

0 comments on commit 3943e97

Please sign in to comment.