1
+ ## { {npmName} }@{ {npmVersion} }
2
+
3
+ ### Building
4
+
5
+ To install the required dependencies and to build the typescript sources run:
6
+ ```
7
+ npm install
8
+ npm run build
9
+ ```
10
+
11
+ ### publishing
12
+
13
+ { {#useNgPackagr} }
14
+ First build the package than run ```npm publish dist``` (don't forget to specify the `dist` folder!)
15
+ { {/useNgPackagr} }
16
+ { {^useNgPackagr} }
17
+ First build the package than run ```npm publish```
18
+ { {/useNgPackagr} }
19
+
20
+ ### consuming
21
+
22
+ Navigate to the folder of your consuming project and run one of next commands.
23
+
24
+ _published:_
25
+
26
+ ```
27
+ npm install { {npmName} }@{ {npmVersion} } --save
28
+ ```
29
+
30
+ _without publishing (not recommended):_
31
+
32
+ ```
33
+ { {#useNgPackagr} }
34
+ npm install PATH_TO_GENERATED_PACKAGE/dist --save
35
+ { {/useNgPackagr} }
36
+ { {^useNgPackagr} }
37
+ npm install PATH_TO_GENERATED_PACKAGE --save
38
+ { {/useNgPackagr} }
39
+ ```
40
+
41
+ _using `npm link`:_
42
+
43
+ { {#useNgPackagr} }
44
+ In PATH_TO_GENERATED_PACKAGE/dist:
45
+ { {/useNgPackagr} }
46
+ { {^useNgPackagr} }
47
+ In PATH_TO_GENERATED_PACKAGE:
48
+ { {/useNgPackagr} }
49
+ ```
50
+ npm link
51
+ ```
52
+
53
+ In your project:
54
+ ```
55
+ npm link { {npmName} }
56
+ ```
57
+
58
+ __Note for Windows users:__ The Angular CLI has troubles to use linked npm packages.
59
+ Please refer to this issue https://github.com/angular/angular-cli/issues/8284 for a solution / workaround.
60
+ Published packages are not effected by this issue.
61
+
62
+
63
+ #### General usage
64
+
65
+ In your Angular project:
66
+
67
+
68
+ ```
69
+ // without configuring providers
70
+ import { ApiModule } from '{ {npmName} }';
71
+ { {#useHttpClient} }import { HttpClientModule } from '@angular/common/http';{ {/useHttpClient} }
72
+ { {^useHttpClient} }import { HttpModule } from '@angular/http';{ {/useHttpClient} }
73
+
74
+ @NgModule({
75
+ imports: [
76
+ ApiModule,
77
+ {{#useHttpClient} }// make sure to import the HttpClientModule in the AppModule only,
78
+ // see https://github.com/angular/angular/issues/20575
79
+ HttpClientModule{ {/useHttpClient} }{ {^useHttpClient} }HttpModule{ {/useHttpClient} }
80
+ ],
81
+ declarations: [ AppComponent ],
82
+ providers: [],
83
+ bootstrap: [ AppComponent ]
84
+ })
85
+ export class AppModule { }
86
+ ```
87
+
88
+ ```
89
+ // configuring providers
90
+ import { ApiModule, Configuration, ConfigurationParameters } from '{ {npmName} }';
91
+
92
+ export function apiConfigFactory (): Configuration => {
93
+ const params: ConfigurationParameters = {
94
+ // set configuration parameters here.
95
+ }
96
+ return new Configuration(params);
97
+ }
98
+
99
+ @NgModule({
100
+ imports: [ ApiModule.forRoot(apiConfigFactory) ],
101
+ declarations: [ AppComponent ],
102
+ providers: [],
103
+ bootstrap: [ AppComponent ]
104
+ } )
105
+ export class AppModule { }
106
+ ```
107
+
108
+ ```
109
+ import { DefaultApi } from '{ {npmName} }';
110
+
111
+ export class AppComponent {
112
+ constructor(private apiGateway: DefaultApi) { }
113
+ }
114
+ ```
115
+
116
+ Note: The ApiModule is restricted to being instantiated once app wide.
117
+ This is to ensure that all services are treated as singletons.
118
+
119
+ #### Using multiple swagger files / APIs / ApiModules
120
+ In order to use multiple `ApiModules` generated from different swagger files,
121
+ you can create an alias name when importing the modules
122
+ in order to avoid naming conflicts:
123
+ ```
124
+ import { ApiModule } from 'my-api-path';
125
+ import { ApiModule as OtherApiModule } from 'my-other-api-path';
126
+ { {#useHttpClient} }import { HttpClientModule } from '@angular/common/http';{ {/useHttpClient} }
127
+ { {^useHttpClient} }import { HttpModule } from '@angular/http';{ {/useHttpClient} }
128
+
129
+ @NgModule({
130
+ imports: [
131
+ ApiModule,
132
+ OtherApiModule,
133
+ {{#useHttpClient} }// make sure to import the HttpClientModule in the AppModule only,
134
+ // see https://github.com/angular/angular/issues/20575
135
+ HttpClientModule{ {/useHttpClient} }{ {^useHttpClient} }HttpModule{ {/useHttpClient} }
136
+ ]
137
+ })
138
+ export class AppModule {
139
+
140
+ }
141
+ ```
142
+
143
+
144
+ ### Set service base path
145
+ If different than the generated base path, during app bootstrap, you can provide the base path to your service.
146
+
147
+ ```
148
+ import { BASE_PATH } from '{ {npmName} }';
149
+
150
+ bootstrap(AppComponent, [
151
+ { provide: BASE_PATH, useValue: ' https://your-web-service.com' } ,
152
+ ]);
153
+ ```
154
+ or
155
+
156
+ ```
157
+ import { BASE_PATH } from '{ {npmName} }';
158
+
159
+ @NgModule({
160
+ imports: [],
161
+ declarations: [ AppComponent ],
162
+ providers: [ provide: BASE_PATH, useValue: ' https://your-web-service.com' ],
163
+ bootstrap: [ AppComponent ]
164
+ } )
165
+ export class AppModule { }
166
+ ```
167
+
168
+
169
+ #### Using @angular/cli
170
+ First extend your `src/environments/*.ts` files by adding the corresponding base path:
171
+
172
+ ```
173
+ export const environment = {
174
+ production: false ,
175
+ API_BASE_PATH: ' http://127.0.0.1:8080'
176
+ } ;
177
+ ```
178
+
179
+ In the src/app/app.module.ts:
180
+ ```
181
+ import { BASE_PATH } from '{ {npmName} }';
182
+ import { environment } from '../environments/environment';
183
+
184
+ @NgModule({
185
+ declarations: [
186
+ AppComponent
187
+ ],
188
+ imports: [ ],
189
+ providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH } ],
190
+ bootstrap: [ AppComponent ]
191
+ })
192
+ export class AppModule { }
193
+ ```
0 commit comments