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

#342 - introduce standalone flag #345

Open
wants to merge 3 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
4 changes: 3 additions & 1 deletion lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export class Globals {
modelIndexFile?: string;
serviceIndexFile?: string;
rootUrl?: string;
standalone: boolean;

constructor(options: Options) {
this.standalone = options.standalone || false;
this.configurationClass = options.configuration || 'ApiConfiguration';
this.configurationFile = fileName(this.configurationClass);
this.configurationParams = `${this.configurationClass}Params`;
Expand All @@ -40,7 +42,7 @@ export class Globals {
this.requestBuilderFile = fileName(this.requestBuilderClass);
this.responseClass = options.response || 'StrictHttpResponse';
this.responseFile = fileName(this.responseClass);
if (options.module !== false && options.module !== '') {
if (options.standalone !== true && options.module !== false && options.module !== '') {
this.moduleClass = options.module === true || options.module === undefined ? 'ApiModule' : options.module;
// Angular's best practices demands xxx.module.ts, not xxx-module.ts
this.moduleFile = fileName(this.moduleClass as string).replace(/\-module$/, '.module');
Expand Down
3 changes: 3 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export interface Options {
/** Class name of the module that provides all services. Set to false to skip. Defaults to `ApiModule`. */
module?: string | boolean;

/** Whether to generate for standalone usage. Defaults to false */
standalone?: boolean;

/**
* Determines how root enums will be generated. Possible values are:
*
Expand Down
7 changes: 6 additions & 1 deletion ng-openapi-gen-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
},
"silent": {
"description": "When set to true, no verbose output will be displayed.",
"default": "false",
"default": false,
"type": "boolean"
},
"camelizeModelNames": {
Expand Down Expand Up @@ -252,6 +252,11 @@
}
}
]
},
"standalone": {
"description": "When set to true, Code generation follows standalone practice. Instead of a module with a `forRoot` static method, a `provide{{configuration}}` function will be generated.",
"default": false,
"type": "boolean"
}
}
}
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion templates/configuration.handlebars
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/* tslint:disable */
/* eslint-disable */
/* Code generated by ng-openapi-gen DO NOT EDIT. */

{{#standalone}}
import { EnvironmentProviders, Injectable, makeEnvironmentProviders } from '@angular/core';
{{/standalone}}
{{^standalone}}
import { Injectable } from '@angular/core';
{{/standalone}}

/**
* Global configuration
Expand All @@ -20,3 +24,18 @@ export class {{configurationClass}} {
export interface {{configurationParams}} {
rootUrl?: string;
}

{{#standalone}}
/**
* provide Configuration for {{baseServiceClass}}
*/
export function provide{{configurationClass}}(value: {{configurationParams}}): EnvironmentProviders
export function provide{{configurationClass}}(factory: ()=>{{configurationParams}}): EnvironmentProviders
export function provide{{configurationClass}}(valueOrFactory: {{configurationParams}} | (() => {{configurationParams}})): EnvironmentProviders {
return makeEnvironmentProviders([
typeof valueOrFactory === 'function'
? { provide: {{configurationClass}}, useFactory: valueOrFactory }
: { provide: {{configurationClass}}, useValue: valueOrFactory }
])
}
{{/standalone}}
5 changes: 3 additions & 2 deletions templates/index.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
/* eslint-disable */
/* Code generated by ng-openapi-gen DO NOT EDIT. */

export { {{configurationClass}} } from './{{{configurationFile}}}';
export { {{configurationClass}}{{#standalone}}, provide{{configurationClass}}{{/standalone}} } from './{{{configurationFile}}}';
export { {{baseServiceClass}} } from './{{{baseServiceFile}}}';
export { {{requestBuilderClass}} } from './{{{requestBuilderFile}}}';
export { {{responseClass}} } from './{{{responseFile}}}';
export { {{moduleClass}} } from './{{{moduleFile}}}';
{{^standalone}}export { {{moduleClass}} } from './{{{moduleFile}}}';
{{/standalone}}
{{#modelIndex.imports}}export { {{{typeName}}}{{#useAlias}} as {{{qualifiedName}}}{{/useAlias}} } from './models{{{file}}}';
{{/modelIndex.imports}}
{{#services}}export { {{typeName}} } from './services/{{{fileName}}}';
Expand Down