Skip to content
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

#343 - do type only exports in index and model barrel file for interfaces and type aliases #344

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
4 changes: 2 additions & 2 deletions lib/gen-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export abstract class GenType {
this._imports = new Imports(options);
}

protected addImport(param: string | Importable | null | undefined) {
protected addImport(param: string | Importable | null | undefined, typeOnly?: boolean) {
if (param && !this.skipImport(param)) {
this._imports.add(param);
this._imports.add(param, !!typeOnly);
}
}

Expand Down
10 changes: 6 additions & 4 deletions lib/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Import implements Importable {
file: string;
useAlias: boolean;
fullPath: string;
typeOnly: boolean;

// Fields from Importable
importName: string;
Expand All @@ -19,11 +20,12 @@ export class Import implements Importable {
importTypeName?: string;
importQualifiedName?: string;

constructor(name: string, typeName: string, qName: string, path: string, file: string) {
constructor(name: string, typeName: string, qName: string, path: string, file: string, typeOnly: boolean) {
this.name = name;
this.typeName = typeName;
this.qualifiedName = qName;
this.useAlias = this.typeName !== this.qualifiedName;
this.typeOnly = typeOnly;
this.path = path;
this.file = file;
this.fullPath = `${this.path.split('/').filter(p => p.length).join('/')}/${this.file.split('/').filter(p => p.length).join('/')}`;
Expand All @@ -48,14 +50,14 @@ export class Imports {
/**
* Adds an import
*/
add(param: string | Importable) {
add(param: string | Importable, typeOnly: boolean) {
let imp: Import;
if (typeof param === 'string') {
// A model
imp = new Import(param, unqualifiedName(param, this.options), qualifiedName(param, this.options), 'models/', modelFile(param, this.options));
imp = new Import(param, unqualifiedName(param, this.options), qualifiedName(param, this.options), 'models/', modelFile(param, this.options), typeOnly);
} else {
// An Importable
imp = new Import(param.importName, param.importTypeName ?? param.importName, param.importQualifiedName ?? param.importName, `${param.importPath}`, param.importFile);
imp = new Import(param.importName, param.importTypeName ?? param.importName, param.importQualifiedName ?? param.importName, `${param.importPath}`, param.importFile, typeOnly);
}
this._imports.set(imp.name, imp);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/model-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ModelIndex extends GenType {

constructor(models: Model[], options: Options) {
super('models', n => n, options);
models.forEach(model => this.addImport(model.name));
models.forEach(model => this.addImport(model.name, !model.isEnum));
this.updateImports();
}

Expand Down
4 changes: 2 additions & 2 deletions templates/index.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
export { {{configurationClass}} } from './{{{configurationFile}}}';
export { {{baseServiceClass}} } from './{{{baseServiceFile}}}';
export { {{requestBuilderClass}} } from './{{{requestBuilderFile}}}';
export { {{responseClass}} } from './{{{responseFile}}}';
export type { {{responseClass}} } from './{{{responseFile}}}';
export { {{moduleClass}} } from './{{{moduleFile}}}';
{{#modelIndex.imports}}export { {{{typeName}}}{{#useAlias}} as {{{qualifiedName}}}{{/useAlias}} } from './models{{{file}}}';
{{#modelIndex.imports}}export {{#typeOnly}}type {{/typeOnly}}{ {{{typeName}}}{{#useAlias}} as {{{qualifiedName}}}{{/useAlias}} } from './models{{{file}}}';
{{/modelIndex.imports}}
{{#services}}export { {{typeName}} } from './services/{{{fileName}}}';
{{/services}}
2 changes: 1 addition & 1 deletion templates/modelIndex.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
/* eslint-disable */
/* Code generated by ng-openapi-gen DO NOT EDIT. */

{{#modelIndex.imports}}export { {{{typeName}}}{{#useAlias}} as {{{qualifiedName}}}{{/useAlias}} } from '{{{@root.modelIndex.pathToRoot}}}{{{fullPath}}}';
{{#modelIndex.imports}}export {{#typeOnly}}type {{/typeOnly}}{ {{{typeName}}}{{#useAlias}} as {{{qualifiedName}}}{{/useAlias}} } from '{{{@root.modelIndex.pathToRoot}}}{{{fullPath}}}';
{{/modelIndex.imports}}