Skip to content

Commit 73e2ef3

Browse files
committed
update swagger decoration
1 parent 5493fbb commit 73e2ef3

File tree

8 files changed

+32
-10
lines changed

8 files changed

+32
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"nest": "nest",
2222
"typeorm": "ts-node ./node_modules/typeorm/cli",
2323
"migration:run": "npm run typeorm migration:run -- -d ./migrations/datasource.ts",
24-
"migration:create": "npm run typeorm -- migration:create ./src/migrations/$npm_config_name",
24+
"migration:create": "npm run typeorm -- migration:create",
2525
"migration:revert": "npm run typeorm -- -d ./migrations/datasource.ts migration:revert"
2626
},
2727
"dependencies": {

src/apps/app.guard.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class ApiKeyGuard implements CanActivate {
3434

3535
const key = req.headers['x-api-key'] ?? req.query.api_key
3636

37+
console.log(req.headers);
3738
if(key == undefined || key == '') {
3839
throw new HttpException('X-API-KEY is not provided.', HttpStatus.UNAUTHORIZED);
3940
}

src/apps/auth/auth.controller.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ import { AuthService } from './auth.service';
66
import { LoginDTO } from './dto/login.dto';
77
import { RegisterDTO } from './dto/register.dto';
88
import { I18n, I18nContext } from 'nestjs-i18n';
9-
import { ApiTags } from '@nestjs/swagger';
9+
import { ApiBody, ApiHeader, ApiSecurity, ApiTags } from '@nestjs/swagger';
1010

1111
@Controller('auth')
12+
@ApiTags("Auth")
13+
@ApiSecurity('api-key')
1214
export class AuthController {
1315

1416
constructor(
1517
private authService: AuthService,
1618
) {}
1719

18-
@ApiTags("Auth")
1920
@Post('/login')
2021
@HttpCode(HttpStatus.OK)
22+
@ApiBody({type: LoginDTO})
2123
async login(
2224
@I18n() i18n: I18nContext,
2325
@Req() req: Request,

src/apps/auth/dto/coordinate.dto.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { ApiProperty } from "@nestjs/swagger";
12
import { IsNumber } from "class-validator";
23

34
export class CoordinateDTO {
5+
@ApiProperty({example: 6.23499})
46
@IsNumber()
57
latitude: number;
68

9+
@ApiProperty({example: -18.23499})
710
@IsNumber()
811
longitude: number;
912
}

src/apps/auth/dto/device.dto.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
import { ApiProperty } from "@nestjs/swagger";
12
import { IsNotEmpty } from "class-validator";
23

34
enum PlatformEnum {Android, iOS, Desktop, Web}
45

56
export class DeviceDTO {
7+
@ApiProperty({example: 'we7r12i'})
68
@IsNotEmpty()
79
id: string;
8-
10+
11+
@ApiProperty({example: "Iphone 14 Pro Max"})
912
@IsNotEmpty()
1013
name: string;
11-
14+
15+
@ApiProperty({example: "Apple"})
1216
@IsNotEmpty()
1317
brand: string;
14-
18+
19+
@ApiProperty({example: "iOS 17"})
1520
@IsNotEmpty()
1621
os: string;
1722
}

src/apps/auth/dto/login.dto.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
import { ApiProperty } from "@nestjs/swagger";
12
import { IsNotEmpty } from "class-validator";
23
import { CoordinateDTO } from "./coordinate.dto";
34
import { DeviceDTO } from "./device.dto";
45
export class LoginDTO {
6+
@ApiProperty({example: "[email protected]"})
57
@IsNotEmpty()
68
identity: string;
7-
9+
10+
@ApiProperty({example: "secret"})
811
@IsNotEmpty()
912
password: string;
10-
13+
14+
@ApiProperty()
1115
device: DeviceDTO;
12-
16+
17+
@ApiProperty()
1318
coordinate: CoordinateDTO;
1419
}
1520

src/entities/app-config.entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
22

33
@Entity({
4-
name: "setting_apps"
4+
name: "app_configs"
55
})
66
export class AppConfig {
77

src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ async function bootstrap() {
2727
.setTitle('StarterKit Project')
2828
.setDescription('This is nestjs starter kit, it\'s will help you when creating new project with nestjs.')
2929
.setVersion('1.0')
30+
.addSecurity('api-key', {
31+
type: 'apiKey',
32+
in: 'header',
33+
name: 'x-api-key'
34+
})
35+
.addBearerAuth()
3036
.addTag('Auth', "All about authentication")
3137
.build();
3238
const document = SwaggerModule.createDocument(app, config);

0 commit comments

Comments
 (0)