Skip to content

Commit

Permalink
feat(swagger): add DTOs to responses brocoders#1504
Browse files Browse the repository at this point in the history
  • Loading branch information
Shchepotin committed Apr 27, 2024
1 parent 6166f19 commit 0071202
Show file tree
Hide file tree
Showing 41 changed files with 515 additions and 68 deletions.
52 changes: 51 additions & 1 deletion .install-scripts/scripts/remove-mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ const removeMongoDb = async () => {
},
],
});

replace({
path: path.join(process.cwd(), 'src', 'users', 'users.module.ts'),
actions: [
Expand All @@ -244,6 +243,57 @@ const removeMongoDb = async () => {
},
],
});
replace({
path: path.join(process.cwd(), 'src', 'users', 'domain', 'user.ts'),
actions: [
{
find: /\/\/ <database-block>.*\/\/ <\/database-block>/gs,
replace: `const idType = Number;`,
},
{
find: /\s*import \{ DatabaseConfig \} from .*/g,
replace: '',
},
{
find: /\s*import databaseConfig from .*/g,
replace: '',
},
],
});
replace({
path: path.join(process.cwd(), 'src', 'statuses', 'domain', 'status.ts'),
actions: [
{
find: /\/\/ <database-block>.*\/\/ <\/database-block>/gs,
replace: `const idType = Number;`,
},
{
find: /\s*import \{ DatabaseConfig \} from .*/g,
replace: '',
},
{
find: /\s*import databaseConfig from .*/g,
replace: '',
},
],
});
replace({
path: path.join(process.cwd(), 'src', 'roles', 'domain', 'role.ts'),
actions: [
{
find: /\/\/ <database-block>.*\/\/ <\/database-block>/gs,
replace: `const idType = Number;`,
},
{
find: /\s*import \{ DatabaseConfig \} from .*/g,
replace: '',
},
{
find: /\s*import databaseConfig from .*/g,
replace: '',
},
],
});
replace({
path: path.join(process.cwd(), 'package.json'),
actions: [
Expand Down
51 changes: 51 additions & 0 deletions .install-scripts/scripts/remove-postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,57 @@ const removePostgreSql = async () => {
},
],
});
replace({
path: path.join(process.cwd(), 'src', 'users', 'domain', 'user.ts'),
actions: [
{
find: /\/\/ <database-block>.*\/\/ <\/database-block>/gs,
replace: `const idType = String;`,
},
{
find: /\s*import \{ DatabaseConfig \} from .*/g,
replace: '',
},
{
find: /\s*import databaseConfig from .*/g,
replace: '',
},
],
});
replace({
path: path.join(process.cwd(), 'src', 'statuses', 'domain', 'status.ts'),
actions: [
{
find: /\/\/ <database-block>.*\/\/ <\/database-block>/gs,
replace: `const idType = String;`,
},
{
find: /\s*import \{ DatabaseConfig \} from .*/g,
replace: '',
},
{
find: /\s*import databaseConfig from .*/g,
replace: '',
},
],
});
replace({
path: path.join(process.cwd(), 'src', 'roles', 'domain', 'role.ts'),
actions: [
{
find: /\/\/ <database-block>.*\/\/ <\/database-block>/gs,
replace: `const idType = String;`,
},
{
find: /\s*import \{ DatabaseConfig \} from .*/g,
replace: '',
},
{
find: /\s*import databaseConfig from .*/g,
replace: '',
},
],
});
replace({
path: path.join(process.cwd(), 'package.json'),
actions: [
Expand Down
9 changes: 6 additions & 3 deletions src/auth-apple/auth-apple.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
Post,
SerializeOptions,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { AuthService } from '../auth/auth.service';
import { AuthAppleService } from './auth-apple.service';
import { AuthAppleLoginDto } from './dto/auth-apple-login.dto';
import { LoginResponseType } from '../auth/types/login-response.type';
import { LoginResponseDto } from '../auth/dto/login-response.dto';

@ApiTags('Auth')
@Controller({
Expand All @@ -23,12 +23,15 @@ export class AuthAppleController {
private readonly authAppleService: AuthAppleService,
) {}

@ApiOkResponse({
type: LoginResponseDto,
})
@SerializeOptions({
groups: ['me'],
})
@Post('login')
@HttpCode(HttpStatus.OK)
async login(@Body() loginDto: AuthAppleLoginDto): Promise<LoginResponseType> {
async login(@Body() loginDto: AuthAppleLoginDto): Promise<LoginResponseDto> {
const socialData = await this.authAppleService.getProfileByToken(loginDto);

return this.authService.validateSocialLogin('apple', socialData);
Expand Down
9 changes: 6 additions & 3 deletions src/auth-facebook/auth-facebook.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
Post,
SerializeOptions,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { AuthService } from '../auth/auth.service';
import { AuthFacebookService } from './auth-facebook.service';
import { AuthFacebookLoginDto } from './dto/auth-facebook-login.dto';
import { LoginResponseType } from '../auth/types/login-response.type';
import { LoginResponseDto } from '../auth/dto/login-response.dto';

@ApiTags('Auth')
@Controller({
Expand All @@ -23,14 +23,17 @@ export class AuthFacebookController {
private readonly authFacebookService: AuthFacebookService,
) {}

@ApiOkResponse({
type: LoginResponseDto,
})
@SerializeOptions({
groups: ['me'],
})
@Post('login')
@HttpCode(HttpStatus.OK)
async login(
@Body() loginDto: AuthFacebookLoginDto,
): Promise<LoginResponseType> {
): Promise<LoginResponseDto> {
const socialData =
await this.authFacebookService.getProfileByToken(loginDto);

Expand Down
11 changes: 6 additions & 5 deletions src/auth-google/auth-google.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
Post,
SerializeOptions,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { AuthService } from '../auth/auth.service';
import { AuthGoogleService } from './auth-google.service';
import { AuthGoogleLoginDto } from './dto/auth-google-login.dto';
import { LoginResponseType } from '../auth/types/login-response.type';
import { LoginResponseDto } from '../auth/dto/login-response.dto';

@ApiTags('Auth')
@Controller({
Expand All @@ -23,14 +23,15 @@ export class AuthGoogleController {
private readonly authGoogleService: AuthGoogleService,
) {}

@ApiOkResponse({
type: LoginResponseDto,
})
@SerializeOptions({
groups: ['me'],
})
@Post('login')
@HttpCode(HttpStatus.OK)
async login(
@Body() loginDto: AuthGoogleLoginDto,
): Promise<LoginResponseType> {
async login(@Body() loginDto: AuthGoogleLoginDto): Promise<LoginResponseDto> {
const socialData = await this.authGoogleService.getProfileByToken(loginDto);

return this.authService.validateSocialLogin('google', socialData);
Expand Down
9 changes: 6 additions & 3 deletions src/auth-twitter/auth-twitter.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
Post,
SerializeOptions,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { AuthService } from '../auth/auth.service';
import { AuthTwitterService } from './auth-twitter.service';
import { AuthTwitterLoginDto } from './dto/auth-twitter-login.dto';
import { LoginResponseType } from '../auth/types/login-response.type';
import { LoginResponseDto } from '../auth/dto/login-response.dto';

@ApiTags('Auth')
@Controller({
Expand All @@ -23,14 +23,17 @@ export class AuthTwitterController {
private readonly authTwitterService: AuthTwitterService,
) {}

@ApiOkResponse({
type: LoginResponseDto,
})
@SerializeOptions({
groups: ['me'],
})
@Post('login')
@HttpCode(HttpStatus.OK)
async login(
@Body() loginDto: AuthTwitterLoginDto,
): Promise<LoginResponseType> {
): Promise<LoginResponseDto> {
const socialData =
await this.authTwitterService.getProfileByToken(loginDto);

Expand Down
23 changes: 17 additions & 6 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import {
SerializeOptions,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { ApiBearerAuth, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { AuthEmailLoginDto } from './dto/auth-email-login.dto';
import { AuthForgotPasswordDto } from './dto/auth-forgot-password.dto';
import { AuthConfirmEmailDto } from './dto/auth-confirm-email.dto';
import { AuthResetPasswordDto } from './dto/auth-reset-password.dto';
import { AuthUpdateDto } from './dto/auth-update.dto';
import { AuthGuard } from '@nestjs/passport';
import { AuthRegisterLoginDto } from './dto/auth-register-login.dto';
import { LoginResponseType } from './types/login-response.type';
import { LoginResponseDto } from './dto/login-response.dto';
import { NullableType } from '../utils/types/nullable.type';
import { User } from '../users/domain/user';
import { RefreshResponseDto } from './dto/refresh-response.dto';

@ApiTags('Auth')
@Controller({
Expand All @@ -36,10 +37,11 @@ export class AuthController {
groups: ['me'],
})
@Post('email/login')
@ApiOkResponse({
type: LoginResponseDto,
})
@HttpCode(HttpStatus.OK)
public login(
@Body() loginDto: AuthEmailLoginDto,
): Promise<LoginResponseType> {
public login(@Body() loginDto: AuthEmailLoginDto): Promise<LoginResponseDto> {
return this.service.validateLogin(loginDto);
}

Expand Down Expand Up @@ -88,19 +90,25 @@ export class AuthController {
})
@Get('me')
@UseGuards(AuthGuard('jwt'))
@ApiOkResponse({
type: User,
})
@HttpCode(HttpStatus.OK)
public me(@Request() request): Promise<NullableType<User>> {
return this.service.me(request.user);
}

@ApiBearerAuth()
@ApiOkResponse({
type: RefreshResponseDto,
})
@SerializeOptions({
groups: ['me'],
})
@Post('refresh')
@UseGuards(AuthGuard('jwt-refresh'))
@HttpCode(HttpStatus.OK)
public refresh(@Request() request): Promise<Omit<LoginResponseType, 'user'>> {
public refresh(@Request() request): Promise<RefreshResponseDto> {
return this.service.refreshToken({
sessionId: request.user.sessionId,
hash: request.user.hash,
Expand All @@ -124,6 +132,9 @@ export class AuthController {
@Patch('me')
@UseGuards(AuthGuard('jwt'))
@HttpCode(HttpStatus.OK)
@ApiOkResponse({
type: User,
})
public update(
@Request() request,
@Body() userDto: AuthUpdateDto,
Expand Down
8 changes: 4 additions & 4 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { AuthProvidersEnum } from './auth-providers.enum';
import { SocialInterface } from '../social/interfaces/social.interface';
import { AuthRegisterLoginDto } from './dto/auth-register-login.dto';
import { NullableType } from '../utils/types/nullable.type';
import { LoginResponseType } from './types/login-response.type';
import { LoginResponseDto } from './dto/login-response.dto';
import { ConfigService } from '@nestjs/config';
import { JwtRefreshPayloadType } from './strategies/types/jwt-refresh-payload.type';
import { JwtPayloadType } from './strategies/types/jwt-payload.type';
Expand All @@ -39,7 +39,7 @@ export class AuthService {
private configService: ConfigService<AllConfigType>,
) {}

async validateLogin(loginDto: AuthEmailLoginDto): Promise<LoginResponseType> {
async validateLogin(loginDto: AuthEmailLoginDto): Promise<LoginResponseDto> {
const user = await this.usersService.findOne({
email: loginDto.email,
});
Expand Down Expand Up @@ -113,7 +113,7 @@ export class AuthService {
async validateSocialLogin(
authProvider: string,
socialData: SocialInterface,
): Promise<LoginResponseType> {
): Promise<LoginResponseDto> {
let user: NullableType<User> = null;
const socialEmail = socialData.email?.toLowerCase();
let userByEmail: NullableType<User> = null;
Expand Down Expand Up @@ -520,7 +520,7 @@ export class AuthService {

async refreshToken(
data: Pick<JwtRefreshPayloadType, 'sessionId' | 'hash'>,
): Promise<Omit<LoginResponseType, 'user'>> {
): Promise<Omit<LoginResponseDto, 'user'>> {
const session = await this.sessionService.findOne({
id: data.sessionId,
});
Expand Down
2 changes: 1 addition & 1 deletion src/auth/dto/auth-email-login.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Transform } from 'class-transformer';
import { lowerCaseTransformer } from '../../utils/transformers/lower-case.transformer';

export class AuthEmailLoginDto {
@ApiProperty({ example: '[email protected]' })
@ApiProperty({ example: '[email protected]', type: String })
@Transform(lowerCaseTransformer)
@IsEmail()
@IsNotEmpty()
Expand Down
2 changes: 1 addition & 1 deletion src/auth/dto/auth-forgot-password.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Transform } from 'class-transformer';
import { lowerCaseTransformer } from '../../utils/transformers/lower-case.transformer';

export class AuthForgotPasswordDto {
@ApiProperty()
@ApiProperty({ example: '[email protected]', type: String })
@Transform(lowerCaseTransformer)
@IsEmail()
email: string;
Expand Down
2 changes: 1 addition & 1 deletion src/auth/dto/auth-register-login.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Transform } from 'class-transformer';
import { lowerCaseTransformer } from '../../utils/transformers/lower-case.transformer';

export class AuthRegisterLoginDto {
@ApiProperty({ example: '[email protected]' })
@ApiProperty({ example: '[email protected]', type: String })
@Transform(lowerCaseTransformer)
@IsEmail()
email: string;
Expand Down
Loading

0 comments on commit 0071202

Please sign in to comment.