Skip to content

Commit 9d9259e

Browse files
committed
feat: add reserved param names sample
1 parent 3a8800e commit 9d9259e

File tree

15 files changed

+620
-0
lines changed

15 files changed

+620
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
generatorName: typescript-nestjs
2+
outputDir: samples/client/petstore/typescript-nestjs/builds/reservedParamNames
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript-nestjs/reserved-param-names.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/typescript-nestjs
5+
additionalProperties:
6+
"useSingleRequestParameter" : true
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
openapi: 3.0.0
2+
info:
3+
description: Test reserved param names
4+
version: 1.0.0
5+
title: Reserved param names
6+
paths:
7+
/test:
8+
post:
9+
security:
10+
- bearerAuth: []
11+
summary: Test reserved param names
12+
description: ''
13+
operationId: testReservedParamNames
14+
parameters:
15+
- name: from
16+
in: query
17+
description: Might conflict with rxjs import
18+
required: true
19+
schema:
20+
type: string
21+
- name: headers
22+
in: header
23+
description: Might conflict with headers const
24+
required: true
25+
schema:
26+
type: string
27+
responses:
28+
'200':
29+
description: successful operation
30+
'405':
31+
description: Invalid input
32+
components:
33+
securitySchemes:
34+
bearerAuth:
35+
type: http
36+
scheme: bearer
37+
bearerFormat: JWT
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
wwwroot/*.js
2+
node_modules
3+
typings
4+
dist
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.gitignore
2+
README.md
3+
api.module.ts
4+
api/api.ts
5+
api/default.service.ts
6+
configuration.ts
7+
git_push.sh
8+
index.ts
9+
model/models.ts
10+
variables.ts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.11.0-SNAPSHOT
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
## @
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+
#### General usage
12+
13+
In your Nestjs project:
14+
15+
16+
```
17+
// without configuring providers
18+
import { ApiModule } from '';
19+
import { HttpModule } from '@nestjs/axios';
20+
21+
@Module({
22+
imports: [
23+
ApiModule,
24+
HttpModule
25+
],
26+
providers: []
27+
})
28+
export class AppModule {}
29+
```
30+
31+
```
32+
// configuring providers
33+
import { ApiModule, Configuration, ConfigurationParameters } from '';
34+
35+
export function apiConfigFactory (): Configuration => {
36+
const params: ConfigurationParameters = {
37+
// set configuration parameters here.
38+
}
39+
return new Configuration(params);
40+
}
41+
42+
@Module({
43+
imports: [ ApiModule.forRoot(apiConfigFactory) ],
44+
declarations: [ AppComponent ],
45+
providers: [],
46+
bootstrap: [ AppComponent ]
47+
})
48+
export class AppModule {}
49+
```
50+
51+
```
52+
import { DefaultApi } from '';
53+
54+
export class AppComponent {
55+
constructor(private apiGateway: DefaultApi) { }
56+
}
57+
```
58+
59+
Note: The ApiModule a dynamic module and instantiated once app wide.
60+
This is to ensure that all services are treated as singletons.
61+
62+
#### Using multiple swagger files / APIs / ApiModules
63+
In order to use multiple `ApiModules` generated from different swagger files,
64+
you can create an alias name when importing the modules
65+
in order to avoid naming conflicts:
66+
```
67+
import { ApiModule } from 'my-api-path';
68+
import { ApiModule as OtherApiModule } from 'my-other-api-path';
69+
import { HttpModule } from '@nestjs/axios';
70+
71+
@Module({
72+
imports: [
73+
ApiModule,
74+
OtherApiModule,
75+
HttpModule
76+
]
77+
})
78+
export class AppModule {
79+
80+
}
81+
```
82+
83+
84+
### Set service base path
85+
If different than the generated base path, during app bootstrap, you can provide the base path to your service.
86+
87+
```
88+
import { BASE_PATH } from '';
89+
90+
bootstrap(AppComponent, [
91+
{ provide: BASE_PATH, useValue: 'https://your-web-service.com' },
92+
]);
93+
```
94+
or
95+
96+
```
97+
import { BASE_PATH } from '';
98+
99+
@Module({
100+
imports: [],
101+
declarations: [ AppComponent ],
102+
providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ],
103+
bootstrap: [ AppComponent ]
104+
})
105+
export class AppModule {}
106+
```
107+
108+
### Configuring the module with `forRootAsync`
109+
110+
You can also use the Nestjs Config Module/Service to configure your app with `forRootAsync`.
111+
112+
```
113+
@Module({
114+
imports: [
115+
ApiModule.forRootAsync({
116+
imports: [ConfigModule],
117+
inject: [ConfigService],
118+
useFactory: (config: ConfigService): Configuration => {
119+
const params: ConfigurationParameters = {
120+
// set configuration parameters here.
121+
basePath: config.get('API_URL'),
122+
};
123+
return new Configuration(params);
124+
},
125+
})
126+
],
127+
declarations: [ AppComponent ],
128+
providers: [],
129+
bootstrap: [ AppComponent ]
130+
})
131+
export class AppModule {}
132+
```
133+
134+
#### Using @nestjs/cli
135+
First extend your `src/environments/*.ts` files by adding the corresponding base path:
136+
137+
```
138+
export const environment = {
139+
production: false,
140+
API_BASE_PATH: 'http://127.0.0.1:8080'
141+
};
142+
```
143+
144+
In the src/app/app.module.ts:
145+
```
146+
import { BASE_PATH } from '';
147+
import { environment } from '../environments/environment';
148+
149+
@Module({
150+
declarations: [
151+
AppComponent
152+
],
153+
imports: [ ],
154+
providers: [
155+
{
156+
provide: 'BASE_PATH',
157+
useValue: environment.API_BASE_PATH
158+
}
159+
]
160+
})
161+
export class AppModule { }
162+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { DynamicModule, Module, Global, Provider } from '@nestjs/common';
2+
import { HttpModule, HttpService } from '@nestjs/axios';
3+
import { AsyncConfiguration, Configuration, ConfigurationFactory } from './configuration';
4+
5+
import { DefaultService } from './api/default.service';
6+
7+
@Global()
8+
@Module({
9+
imports: [ HttpModule ],
10+
exports: [
11+
DefaultService
12+
],
13+
providers: [
14+
DefaultService
15+
]
16+
})
17+
export class ApiModule {
18+
public static forRoot(configurationFactory: () => Configuration): DynamicModule {
19+
return {
20+
module: ApiModule,
21+
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
22+
};
23+
}
24+
25+
/**
26+
* Register the module asynchronously.
27+
*/
28+
static forRootAsync(options: AsyncConfiguration): DynamicModule {
29+
const providers = [...this.createAsyncProviders(options)];
30+
return {
31+
module: ApiModule,
32+
imports: options.imports || [],
33+
providers,
34+
exports: providers,
35+
};
36+
}
37+
38+
private static createAsyncProviders(options: AsyncConfiguration): Provider[] {
39+
if (options.useClass) {
40+
return [
41+
this.createAsyncConfigurationProvider(options),
42+
{
43+
provide: options.useClass,
44+
useClass: options.useClass,
45+
},
46+
];
47+
}
48+
return [this.createAsyncConfigurationProvider(options)];
49+
}
50+
51+
private static createAsyncConfigurationProvider(
52+
options: AsyncConfiguration,
53+
): Provider {
54+
if (options.useFactory) {
55+
return {
56+
provide: Configuration,
57+
useFactory: options.useFactory,
58+
inject: options.inject || [],
59+
};
60+
}
61+
return {
62+
provide: Configuration,
63+
useFactory: async (optionsFactory: ConfigurationFactory) =>
64+
await optionsFactory.createConfiguration(),
65+
inject: (options.useExisting && [options.useExisting]) || (options.useClass && [options.useClass]) || [],
66+
};
67+
}
68+
69+
constructor( httpService: HttpService) { }
70+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './default.service';
2+
import { DefaultService } from './default.service';
3+
export const APIS = [DefaultService];

0 commit comments

Comments
 (0)