Skip to content

Commit

Permalink
Swagger Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Mar 2, 2024
1 parent d749276 commit 58ee0c4
Show file tree
Hide file tree
Showing 22 changed files with 364 additions and 13 deletions.
13 changes: 9 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ module.exports = {
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
root: true,
env: {
node: true,
Expand All @@ -21,5 +18,13 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
},
};
4 changes: 4 additions & 0 deletions apps/service-auth/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AuthModule } from './auth/auth.module';
import { ClansModule } from './clans/clans.module';
import { GuildsModule } from './guilds/guilds.module';
import { LinksModule } from './links/links.module';

@Module({
imports: [
Expand All @@ -12,6 +14,8 @@ import { ClansModule } from './clans/clans.module';
RedisModule,
ClansModule,
AuthModule,
GuildsModule,
LinksModule,
],
controllers: [],
providers: [],
Expand Down
10 changes: 8 additions & 2 deletions apps/service-auth/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import { CurrentUser, JwtAuthGuard, JwtUser } from '@app/auth';
import { getAppHealth } from '@app/helper';
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiExcludeEndpoint, ApiTags } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { LoginInput } from './dto/login.dto';

@ApiTags('AUTH')
@Controller('/auth')
export class AuthController {
constructor(private authService: AuthService) {}

@ApiExcludeEndpoint()
@Get()
ack() {
return { message: `Hello from ${AuthController.name}` };
}

@ApiExcludeEndpoint()
@Get('/health')
stats() {
return getAppHealth(AuthController.name);
}

@Post('/login')
async login(@Body('password') password: string) {
return this.authService.login(password);
async login(@Body() body: LoginInput) {
return this.authService.login(body.passkey);
}

@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@Get('/status')
getStatus(@CurrentUser() user: JwtUser) {
return user;
Expand Down
6 changes: 6 additions & 0 deletions apps/service-auth/src/auth/dto/login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IsNotEmpty } from 'class-validator';

export class LoginInput {
@IsNotEmpty()
passkey: string;
}
14 changes: 14 additions & 0 deletions apps/service-auth/src/clans/clans.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { JwtAuthGuard } from '@app/auth';
import { Controller, Get, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';

@ApiTags('CLANS')
@ApiBearerAuth()
@Controller('/clans')
@UseGuards(JwtAuthGuard)
export class ClansController {
@Get()
getHello() {
return {};
}
}
7 changes: 6 additions & 1 deletion apps/service-auth/src/clans/clans.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Module } from '@nestjs/common';
import { ClansService } from './clans.service';
import { ClansController } from './clans.controller';

@Module({})
@Module({
providers: [ClansService],
controllers: [ClansController],
})
export class ClansModule {}
4 changes: 4 additions & 0 deletions apps/service-auth/src/clans/clans.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class ClansService {}
32 changes: 32 additions & 0 deletions apps/service-auth/src/guilds/dto/update-clan-categories.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IsInt, IsNotEmpty, ValidateNested } from 'class-validator';

export class ReorderCategoriesInput {
@IsNotEmpty()
_id: string;

@IsNotEmpty()
name: string;

@IsInt()
order: number;

@ValidateNested({ each: true })
clans: ReorderClansInput[];
}

export class ReorderClansInput {
@IsNotEmpty()
_id: string;

@IsInt()
order: number;

@IsNotEmpty()
tag: string;

@IsNotEmpty()
name: string;

@IsNotEmpty()
guildId: string;
}
34 changes: 34 additions & 0 deletions apps/service-auth/src/guilds/guilds.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { JwtAuthGuard } from '@app/auth';
import { Body, Controller, Get, Param, Put, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiTags } from '@nestjs/swagger';
import { ReorderCategoriesInput } from './dto/update-clan-categories.dto';
import { GuildsService } from './guilds.service';

@ApiTags('GUILDS')
@Controller('/guilds')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
export class GuildsController {
constructor(private guildsService: GuildsService) {}

@Get('/:guildId/clans')
getGuildMembers(@Param('guildId') guildId: string, @Query('q') q: string) {
return this.guildsService.getMembers(guildId, q);
}

@Get('/:guildId/clans')
getClans(@Param('guildId') guildId: string) {
return this.guildsService.getClans(guildId);
}

@Get('/:guildId/clans-and-categories')
getClansWithCategories(@Param('guildId') guildId: string) {
return this.guildsService.getClans(guildId);
}

@Put('/:guildId/reorder-clans')
@ApiBody({ type: ReorderCategoriesInput, isArray: true })
updateClansAndCategories(@Param('guildId') guildId: string, @Body() _: ReorderCategoriesInput[]) {
return this.guildsService.getClans(guildId);
}
}
9 changes: 9 additions & 0 deletions apps/service-auth/src/guilds/guilds.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { GuildsController } from './guilds.controller';
import { GuildsService } from './guilds.service';

@Module({
controllers: [GuildsController],
providers: [GuildsService],
})
export class GuildsModule {}
14 changes: 14 additions & 0 deletions apps/service-auth/src/guilds/guilds.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class GuildsService {
constructor() {}

getMembers(guildId: string, q: string) {
return [guildId, q];
}

getClans(guildId: string) {
return [guildId];
}
}
7 changes: 7 additions & 0 deletions apps/service-auth/src/links/dto/bulk-links.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ArrayMaxSize, IsString } from 'class-validator';

export class BulkLinksDto {
@IsString({ each: true })
@ArrayMaxSize(1000)
input: string[];
}
21 changes: 21 additions & 0 deletions apps/service-auth/src/links/dto/create-links.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IsNotEmpty } from 'class-validator';

export class CreateLinkInput {
@IsNotEmpty()
name: string;

@IsNotEmpty()
tag: string;

@IsNotEmpty()
userId: string;

@IsNotEmpty()
username: string;

@IsNotEmpty()
displayName: string;

@IsNotEmpty()
discriminator: string;
}
9 changes: 9 additions & 0 deletions apps/service-auth/src/links/dto/delete-link.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IsNotEmpty } from 'class-validator';

export class DeleteLinkInput {
@IsNotEmpty()
clanTag: string;

@IsNotEmpty()
playerTag: string;
}
35 changes: 35 additions & 0 deletions apps/service-auth/src/links/links.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CurrentUser, JwtAuthGuard, JwtUser } from '@app/auth';
import { Body, Controller, Delete, Get, Param, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { BulkLinksDto } from './dto/bulk-links.dto';
import { CreateLinkInput } from './dto/create-links.dto';
import { DeleteLinkInput } from './dto/delete-link.dto';
import { LinksService } from './links.service';

@ApiTags('LINKS')
@Controller('/links')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
export class LinksController {
constructor(private linksService: LinksService) {}

@Post('/')
async createLink(@Body() body: CreateLinkInput) {
return this.linksService.createLink(body);
}

@Delete('/')
async deleteLink(@CurrentUser() user: JwtUser, @Body() body: DeleteLinkInput) {
return this.linksService.deleteLink(user.sub, body);
}

@Get('/:userIdOrTag')
getLink(@Param('userIdOrTag') userIdOrTag: string) {
return this.linksService.getLink(userIdOrTag);
}

@Post('/bulk')
getLinks(@Body() body: BulkLinksDto) {
return this.linksService.getLinks(body.input);
}
}
9 changes: 9 additions & 0 deletions apps/service-auth/src/links/links.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { LinksController } from './links.controller';
import { LinksService } from './links.service';

@Module({
controllers: [LinksController],
providers: [LinksService],
})
export class LinksModule {}
22 changes: 22 additions & 0 deletions apps/service-auth/src/links/links.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@nestjs/common';
import { CreateLinkInput } from './dto/create-links.dto';
import { DeleteLinkInput } from './dto/delete-link.dto';

@Injectable()
export class LinksService {
createLink(input: CreateLinkInput) {
return input;
}

deleteLink(userId: string, input: DeleteLinkInput) {
return { userId, input };
}

getLink(userIdOrTag: string) {
return [userIdOrTag];
}

getLinks(playerTags: string[]) {
return [playerTags];
}
}
17 changes: 15 additions & 2 deletions apps/service-auth/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { Logger } from '@nestjs/common';
import { Logger, ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const logger = new Logger('NestApplication');
const logger = new Logger(AppModule.name);

const config = new DocumentBuilder()
.setTitle('Service Auth API')
.setDescription('Public and private routes for the Service Auth')
.setVersion('1.0')
.addBearerAuth()
.addTag('AUTH')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('/', app, document);

app.useGlobalPipes(new ValidationPipe({ transform: true }));

const port = process.env.PORT || 8081;
await app.listen(port);
Expand Down
1 change: 1 addition & 0 deletions libs/auth/src/decorators/roles.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CustomDecorator, SetMetadata } from '@nestjs/common';
export enum Role {
USER = 'user',
ADMIN = 'admin',
VIEWER = 'viewer',
}

export const ROLES_KEY = 'roles';
Expand Down
3 changes: 3 additions & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"collection": "@nestjs/schematics",
"sourceRoot": "apps/service-auth/src",
"compilerOptions": {
"plugins": [
"@nestjs/swagger"
],
"deleteOutDir": true,
"webpack": true,
"tsConfigPath": "apps/service-auth/tsconfig.app.json"
Expand Down
Loading

0 comments on commit 58ee0c4

Please sign in to comment.