Adding forRoot instead of forConfig and forcing only 1 instantiation of the ApiModule (#6775)

* Added forRoot and constructure duplicate check

* added imports

* updated providers display format

* updated examples

* Moved providers so users don't hav eto use forRoot if they don't want.

* Updated the readme
This commit is contained in:
Keni Steward 2017-10-26 05:57:30 -04:00 committed by wing328
parent 681b1c20ea
commit 4f3c86aa14
41 changed files with 352 additions and 144 deletions

View File

@ -40,36 +40,77 @@ In your project:
npm link {{npmName}}@{{npmVersion}}
```
In your angular2 project:
In your Angular project:
```
import { DefaultApi } from '{{npmName}}/api/api';
// without configuring providers
import { ApiModule } from '{{npmName}}';
@NgModule({
imports: [],
declarations: [],
exports: [],
providers: [AppModule]
imports: [ ApiModule ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class CoreModule {}
export class AppModule {}
```
```
import { DefaultApi } from '{{npmName}}/api/api';
// configuring providers
import { ApiModule, Configuration, ConfigurationParameters } from '{{npmName}}';
export function apiConfigFactory (): Configuration => {
const params: ConfigurationParameters = {
// set configuration parameters here.
}
return new Configuration(params);
}
@NgModule({
imports: [ ApiModule.forRoot(apiConfigFactory) ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
```
import { DefaultApi } from '{{npmName}}';
export class AppComponent {
constructor(private apiGateway: DefaultApi) { }
}
```
Note: The ApiModule is restricted to being instantiated once app wide.
This is to ensure that all services are treated as singletons.
### Set service base path
If different than the generated base path, during app bootstrap, you can provide the base path to your service.
```
import { BASE_PATH } from './path-to-swagger-gen-service/index';
import { BASE_PATH } from '{{npmName}}';
bootstrap(AppComponent, [
{ provide: BASE_PATH, useValue: 'https://your-web-service.com' },
]);
```
or
```
import { BASE_PATH } from '{{npmName}}';
@NgModule({
imports: [],
declarations: [ AppComponent ],
providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
#### Using @angular/cli
First extend your `src/environments/*.ts` files by adding the corresponding base path:
@ -88,11 +129,11 @@ import { environment } from '../environments/environment';
@NgModule({
declarations: [
AppComponent,
AppComponent
],
imports: [ ],
providers: [{ provide: BASE_PATH, useValue: useValue: environment.API_BASE_PATH }],
bootstrap: [AppComponent]
providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }],
bootstrap: [ AppComponent ]
})
export class AppModule { }
```

View File

@ -1,4 +1,4 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
{{#useHttpClient}}
import { HttpClientModule } from '@angular/common/http';
@ -18,13 +18,21 @@ import { {{classname}} } from './{{importPath}}';
imports: [ CommonModule, {{#useHttpClient}}HttpClientModule{{/useHttpClient}}{{^useHttpClient}}HttpModule{{/useHttpClient}} ],
declarations: [],
exports: [],
providers: [ {{#apiInfo}}{{#apis}}{{classname}}{{#hasMore}}, {{/hasMore}}{{/apis}}{{/apiInfo}} ]
providers: [
{{#apiInfo}}{{#apis}}{{classname}}{{#hasMore}},
{{/hasMore}}{{/apis}}{{/apiInfo}} ]
})
export class ApiModule {
public static forConfig(configurationFactory: () => Configuration): ModuleWithProviders {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ {provide: Configuration, useFactory: configurationFactory}]
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
}
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import your base AppModule only.');
}
}
}

View File

@ -1,4 +1,4 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { Configuration } from './configuration';
@ -11,13 +11,22 @@ import { UserService } from './api/user.service';
imports: [ CommonModule, HttpModule ],
declarations: [],
exports: [],
providers: [ PetService, StoreService, UserService ]
providers: [
PetService,
StoreService,
UserService ]
})
export class ApiModule {
public static forConfig(configurationFactory: () => Configuration): ModuleWithProviders {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ {provide: Configuration, useFactory: configurationFactory}]
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
}
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import your base AppModule only.');
}
}
}

View File

@ -19,5 +19,3 @@ export interface ApiResponse {
type?: string;
message?: string;
}

View File

@ -18,5 +18,3 @@ export interface Category {
id?: number;
name?: string;
}

View File

@ -33,5 +33,3 @@ export namespace Order {
Delivered: 'delivered' as StatusEnum
}
}

View File

@ -35,5 +35,3 @@ export namespace Pet {
Sold: 'sold' as StatusEnum
}
}

View File

@ -18,5 +18,3 @@ export interface Tag {
id?: number;
name?: string;
}

View File

@ -27,5 +27,3 @@ export interface User {
*/
userStatus?: number;
}

View File

@ -40,36 +40,77 @@ In your project:
npm link @swagger/angular2-typescript-petstore@0.0.1
```
In your angular2 project:
In your Angular project:
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore/api/api';
// without configuring providers
import { ApiModule } from '@swagger/angular2-typescript-petstore';
@NgModule({
imports: [],
declarations: [],
exports: [],
providers: [AppModule]
imports: [ ApiModule ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class CoreModule {}
export class AppModule {}
```
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore/api/api';
// configuring providers
import { ApiModule, Configuration, ConfigurationParameters } from '@swagger/angular2-typescript-petstore';
export function apiConfigFactory (): Configuration => {
const params: ConfigurationParameters = {
// set configuration parameters here.
}
return new Configuration(params);
}
@NgModule({
imports: [ ApiModule.forRoot(apiConfigFactory) ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore';
export class AppComponent {
constructor(private apiGateway: DefaultApi) { }
}
```
Note: The ApiModule is restricted to being instantiated once app wide.
This is to ensure that all services are treated as singletons.
### Set service base path
If different than the generated base path, during app bootstrap, you can provide the base path to your service.
```
import { BASE_PATH } from './path-to-swagger-gen-service/index';
import { BASE_PATH } from '@swagger/angular2-typescript-petstore';
bootstrap(AppComponent, [
{ provide: BASE_PATH, useValue: 'https://your-web-service.com' },
]);
```
or
```
import { BASE_PATH } from '@swagger/angular2-typescript-petstore';
@NgModule({
imports: [],
declarations: [ AppComponent ],
providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
#### Using @angular/cli
First extend your `src/environments/*.ts` files by adding the corresponding base path:
@ -88,11 +129,11 @@ import { environment } from '../environments/environment';
@NgModule({
declarations: [
AppComponent,
AppComponent
],
imports: [ ],
providers: [{ provide: BASE_PATH, useValue: useValue: environment.API_BASE_PATH }],
bootstrap: [AppComponent]
providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }],
bootstrap: [ AppComponent ]
})
export class AppModule { }
```

View File

@ -1,4 +1,4 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { Configuration } from './configuration';
@ -11,13 +11,22 @@ import { UserService } from './api/user.service';
imports: [ CommonModule, HttpModule ],
declarations: [],
exports: [],
providers: [ PetService, StoreService, UserService ]
providers: [
PetService,
StoreService,
UserService ]
})
export class ApiModule {
public static forConfig(configurationFactory: () => Configuration): ModuleWithProviders {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ {provide: Configuration, useFactory: configurationFactory}]
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
}
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import your base AppModule only.');
}
}
}

View File

@ -19,5 +19,3 @@ export interface ApiResponse {
type?: string;
message?: string;
}

View File

@ -18,5 +18,3 @@ export interface Category {
id?: number;
name?: string;
}

View File

@ -33,5 +33,3 @@ export namespace Order {
Delivered: 'delivered' as StatusEnum
}
}

View File

@ -35,5 +35,3 @@ export namespace Pet {
Sold: 'sold' as StatusEnum
}
}

View File

@ -18,5 +18,3 @@ export interface Tag {
id?: number;
name?: string;
}

View File

@ -27,5 +27,3 @@ export interface User {
*/
userStatus?: number;
}

View File

@ -40,36 +40,77 @@ In your project:
npm link @swagger/angular2-typescript-petstore@0.0.1
```
In your angular2 project:
In your Angular project:
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore/api/api';
// without configuring providers
import { ApiModule } from '@swagger/angular2-typescript-petstore';
@NgModule({
imports: [],
declarations: [],
exports: [],
providers: [AppModule]
imports: [ ApiModule ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class CoreModule {}
export class AppModule {}
```
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore/api/api';
// configuring providers
import { ApiModule, Configuration, ConfigurationParameters } from '@swagger/angular2-typescript-petstore';
export function apiConfigFactory (): Configuration => {
const params: ConfigurationParameters = {
// set configuration parameters here.
}
return new Configuration(params);
}
@NgModule({
imports: [ ApiModule.forRoot(apiConfigFactory) ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore';
export class AppComponent {
constructor(private apiGateway: DefaultApi) { }
}
```
Note: The ApiModule is restricted to being instantiated once app wide.
This is to ensure that all services are treated as singletons.
### Set service base path
If different than the generated base path, during app bootstrap, you can provide the base path to your service.
```
import { BASE_PATH } from './path-to-swagger-gen-service/index';
import { BASE_PATH } from '@swagger/angular2-typescript-petstore';
bootstrap(AppComponent, [
{ provide: BASE_PATH, useValue: 'https://your-web-service.com' },
]);
```
or
```
import { BASE_PATH } from '@swagger/angular2-typescript-petstore';
@NgModule({
imports: [],
declarations: [ AppComponent ],
providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
#### Using @angular/cli
First extend your `src/environments/*.ts` files by adding the corresponding base path:
@ -88,11 +129,11 @@ import { environment } from '../environments/environment';
@NgModule({
declarations: [
AppComponent,
AppComponent
],
imports: [ ],
providers: [{ provide: BASE_PATH, useValue: useValue: environment.API_BASE_PATH }],
bootstrap: [AppComponent]
providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }],
bootstrap: [ AppComponent ]
})
export class AppModule { }
```

View File

@ -1,4 +1,4 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { Configuration } from './configuration';
@ -11,13 +11,22 @@ import { UserService } from './api/user.service';
imports: [ CommonModule, HttpModule ],
declarations: [],
exports: [],
providers: [ PetService, StoreService, UserService ]
providers: [
PetService,
StoreService,
UserService ]
})
export class ApiModule {
public static forConfig(configurationFactory: () => Configuration): ModuleWithProviders {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ {provide: Configuration, useFactory: configurationFactory}]
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
}
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import your base AppModule only.');
}
}
}

View File

@ -18,5 +18,3 @@ export interface Category {
id?: number;
name?: string;
}

View File

@ -33,5 +33,3 @@ export namespace Order {
Delivered: 'delivered' as StatusEnum
}
}

View File

@ -35,5 +35,3 @@ export namespace Pet {
Sold: 'sold' as StatusEnum
}
}

View File

@ -18,5 +18,3 @@ export interface Tag {
id?: number;
name?: string;
}

View File

@ -27,5 +27,3 @@ export interface User {
*/
userStatus?: number;
}

View File

@ -19,5 +19,3 @@ export interface ApiResponse {
type?: string;
message?: string;
}

View File

@ -40,36 +40,77 @@ In your project:
npm link @swagger/angular2-typescript-petstore@0.0.1
```
In your angular2 project:
In your Angular project:
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore/api/api';
// without configuring providers
import { ApiModule } from '@swagger/angular2-typescript-petstore';
@NgModule({
imports: [],
declarations: [],
exports: [],
providers: [AppModule]
imports: [ ApiModule ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class CoreModule {}
export class AppModule {}
```
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore/api/api';
// configuring providers
import { ApiModule, Configuration, ConfigurationParameters } from '@swagger/angular2-typescript-petstore';
export function apiConfigFactory (): Configuration => {
const params: ConfigurationParameters = {
// set configuration parameters here.
}
return new Configuration(params);
}
@NgModule({
imports: [ ApiModule.forRoot(apiConfigFactory) ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore';
export class AppComponent {
constructor(private apiGateway: DefaultApi) { }
}
```
Note: The ApiModule is restricted to being instantiated once app wide.
This is to ensure that all services are treated as singletons.
### Set service base path
If different than the generated base path, during app bootstrap, you can provide the base path to your service.
```
import { BASE_PATH } from './path-to-swagger-gen-service/index';
import { BASE_PATH } from '@swagger/angular2-typescript-petstore';
bootstrap(AppComponent, [
{ provide: BASE_PATH, useValue: 'https://your-web-service.com' },
]);
```
or
```
import { BASE_PATH } from '@swagger/angular2-typescript-petstore';
@NgModule({
imports: [],
declarations: [ AppComponent ],
providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
#### Using @angular/cli
First extend your `src/environments/*.ts` files by adding the corresponding base path:
@ -88,11 +129,11 @@ import { environment } from '../environments/environment';
@NgModule({
declarations: [
AppComponent,
AppComponent
],
imports: [ ],
providers: [{ provide: BASE_PATH, useValue: useValue: environment.API_BASE_PATH }],
bootstrap: [AppComponent]
providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }],
bootstrap: [ AppComponent ]
})
export class AppModule { }
```

View File

@ -1,4 +1,4 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { Configuration } from './configuration';
@ -11,13 +11,22 @@ import { UserService } from './api/user.service';
imports: [ CommonModule, HttpClientModule ],
declarations: [],
exports: [],
providers: [ PetService, StoreService, UserService ]
providers: [
PetService,
StoreService,
UserService ]
})
export class ApiModule {
public static forConfig(configurationFactory: () => Configuration): ModuleWithProviders {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ {provide: Configuration, useFactory: configurationFactory}]
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
}
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import your base AppModule only.');
}
}
}

View File

@ -19,5 +19,3 @@ export interface ApiResponse {
type?: string;
message?: string;
}

View File

@ -18,5 +18,3 @@ export interface Category {
id?: number;
name?: string;
}

View File

@ -27,6 +27,9 @@ export interface Order {
}
export namespace Order {
export type StatusEnum = 'placed' | 'approved' | 'delivered';
export const StatusEnum = {
Placed: 'placed' as StatusEnum,
Approved: 'approved' as StatusEnum,
Delivered: 'delivered' as StatusEnum
}
}

View File

@ -29,6 +29,9 @@ export interface Pet {
}
export namespace Pet {
export type StatusEnum = 'available' | 'pending' | 'sold';
export const StatusEnum = {
Available: 'available' as StatusEnum,
Pending: 'pending' as StatusEnum,
Sold: 'sold' as StatusEnum
}
}

View File

@ -18,5 +18,3 @@ export interface Tag {
id?: number;
name?: string;
}

View File

@ -27,5 +27,3 @@ export interface User {
*/
userStatus?: number;
}

View File

@ -40,36 +40,77 @@ In your project:
npm link @swagger/angular2-typescript-petstore@0.0.1
```
In your angular2 project:
In your Angular project:
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore/api/api';
// without configuring providers
import { ApiModule } from '@swagger/angular2-typescript-petstore';
@NgModule({
imports: [],
declarations: [],
exports: [],
providers: [AppModule]
imports: [ ApiModule ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class CoreModule {}
export class AppModule {}
```
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore/api/api';
// configuring providers
import { ApiModule, Configuration, ConfigurationParameters } from '@swagger/angular2-typescript-petstore';
export function apiConfigFactory (): Configuration => {
const params: ConfigurationParameters = {
// set configuration parameters here.
}
return new Configuration(params);
}
@NgModule({
imports: [ ApiModule.forRoot(apiConfigFactory) ],
declarations: [ AppComponent ],
providers: [],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
```
import { DefaultApi } from '@swagger/angular2-typescript-petstore';
export class AppComponent {
constructor(private apiGateway: DefaultApi) { }
}
```
Note: The ApiModule is restricted to being instantiated once app wide.
This is to ensure that all services are treated as singletons.
### Set service base path
If different than the generated base path, during app bootstrap, you can provide the base path to your service.
```
import { BASE_PATH } from './path-to-swagger-gen-service/index';
import { BASE_PATH } from '@swagger/angular2-typescript-petstore';
bootstrap(AppComponent, [
{ provide: BASE_PATH, useValue: 'https://your-web-service.com' },
]);
```
or
```
import { BASE_PATH } from '@swagger/angular2-typescript-petstore';
@NgModule({
imports: [],
declarations: [ AppComponent ],
providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
```
#### Using @angular/cli
First extend your `src/environments/*.ts` files by adding the corresponding base path:
@ -88,11 +129,11 @@ import { environment } from '../environments/environment';
@NgModule({
declarations: [
AppComponent,
AppComponent
],
imports: [ ],
providers: [{ provide: BASE_PATH, useValue: useValue: environment.API_BASE_PATH }],
bootstrap: [AppComponent]
providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }],
bootstrap: [ AppComponent ]
})
export class AppModule { }
```

View File

@ -1,4 +1,4 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { Configuration } from './configuration';
@ -11,13 +11,22 @@ import { UserService } from './api/user.service';
imports: [ CommonModule, HttpModule ],
declarations: [],
exports: [],
providers: [ PetService, StoreService, UserService ]
providers: [
PetService,
StoreService,
UserService ]
})
export class ApiModule {
public static forConfig(configurationFactory: () => Configuration): ModuleWithProviders {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ {provide: Configuration, useFactory: configurationFactory}]
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
}
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import your base AppModule only.');
}
}
}

View File

@ -19,5 +19,3 @@ export interface ApiResponse {
type?: string;
message?: string;
}

View File

@ -18,5 +18,3 @@ export interface Category {
id?: number;
name?: string;
}

View File

@ -33,5 +33,3 @@ export namespace Order {
Delivered: 'delivered' as StatusEnum
}
}

View File

@ -35,5 +35,3 @@ export namespace Pet {
Sold: 'sold' as StatusEnum
}
}

View File

@ -18,5 +18,3 @@ export interface Tag {
id?: number;
name?: string;
}

View File

@ -27,5 +27,3 @@ export interface User {
*/
userStatus?: number;
}