diff --git a/lib/globals.ts b/lib/globals.ts index 0b6ef22..cf822f0 100644 --- a/lib/globals.ts +++ b/lib/globals.ts @@ -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`; @@ -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'); diff --git a/lib/options.ts b/lib/options.ts index 077d7a7..a8ec161 100644 --- a/lib/options.ts +++ b/lib/options.ts @@ -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: * diff --git a/ng-openapi-gen-schema.json b/ng-openapi-gen-schema.json index 86f3c52..fcb2fc3 100644 --- a/ng-openapi-gen-schema.json +++ b/ng-openapi-gen-schema.json @@ -215,7 +215,7 @@ }, "silent": { "description": "When set to true, no verbose output will be displayed.", - "default": "false", + "default": false, "type": "boolean" }, "camelizeModelNames": { @@ -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" } } } diff --git a/package-lock.json b/package-lock.json index 8dfc621..ef641b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1096,10 +1096,11 @@ "dev": true }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "dev": true, + "license": "MIT", "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", @@ -4230,9 +4231,9 @@ "dev": true }, "cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "dev": true, "requires": { "path-key": "^3.1.0", diff --git a/templates/configuration.handlebars b/templates/configuration.handlebars index 6d1b0e9..58539f7 100644 --- a/templates/configuration.handlebars +++ b/templates/configuration.handlebars @@ -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 @@ -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}} diff --git a/templates/index.handlebars b/templates/index.handlebars index d592cb1..a2ef90a 100644 --- a/templates/index.handlebars +++ b/templates/index.handlebars @@ -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}}}';