Skip to content

Commit

Permalink
refactor: add nestjs/swagger decorators in controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed May 28, 2024
1 parent 4c672d5 commit 7407245
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ import {
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ApiBearerAuth, ApiCookieAuth, ApiTags } from '@nestjs/swagger';

import { CatProfilesService } from './catprofiles.service';
import {
isAdminPermission,
isEditorPermission,
isOwnerPermission,
} from './helpers';
import { CreateCatProfile } from './models/create-catprofile';
import { UpdateCatProfile } from './models/update-catprofile';
import { CreateCatProfile } from './models/create-catprofile.dto';
import { UpdateCatProfile } from './models/update-catprofile.dto';

const AuthenticationGuard = (): Type<CanActivate> =>
OryAuthenticationGuard({
Expand Down Expand Up @@ -75,6 +76,10 @@ const AuthorizationGuard = (): Type<CanActivate> =>
},
});


@ApiBearerAuth()
@ApiCookieAuth()
@ApiTags('cat-profiles')
@Controller('cat-profiles')
export class CatProfilesController {
constructor(private readonly catProfilesService: CatProfilesService) {}
Expand Down
4 changes: 2 additions & 2 deletions libs/catprofile/nestjs-module/src/lib/catprofiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { InjectRepository } from '@nestjs/typeorm';
import type { Repository } from 'typeorm';

import { adminRelationQuery, ownerRelationQuery } from './helpers';
import { CreateCatProfile } from './models/create-catprofile';
import { UpdateCatProfile } from './models/update-catprofile';
import { CreateCatProfile } from './models/create-catprofile.dto';
import { UpdateCatProfile } from './models/update-catprofile.dto';

@Injectable()
export class CatProfilesService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CatProfile } from '@cat-fostering/entities';
import { PickType } from '@nestjs/mapped-types';
import { PickType } from '@nestjs/swagger';

export class CreateCatProfile extends PickType(CatProfile, [
'name',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CatProfile } from '@cat-fostering/entities';
import { PartialType, PickType } from '@nestjs/mapped-types';
import { PartialType, PickType } from '@nestjs/swagger';

export class UpdateCatProfile extends PartialType(
PickType(CatProfile, ['name', 'age', 'description'])
Expand Down
6 changes: 5 additions & 1 deletion libs/fostering/nestjs-module/src/lib/fostering.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ApiBearerAuth, ApiCookieAuth, ApiTags } from '@nestjs/swagger';

import { FosteringService } from './fostering.service';
import {
Expand All @@ -28,7 +29,7 @@ import {
canRejectFosteringPermission,
canRequestFosteringPermission,
} from './helpers';
import { RequestFostering } from './models/request-fostering';
import { RequestFostering } from './models/request-fostering.dto';

const AuthenticationGuard = (): Type<CanActivate> =>
OryAuthenticationGuard({
Expand Down Expand Up @@ -74,6 +75,9 @@ const AuthorizationGuard = (): Type<CanActivate> =>
},
});

@ApiBearerAuth()
@ApiCookieAuth()
@ApiTags('fostering')
@Controller('fostering')
export class FosteringController {
constructor(private readonly fosteringService: FosteringService) {}
Expand Down
2 changes: 1 addition & 1 deletion libs/fostering/nestjs-module/src/lib/fostering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import type { Repository } from 'typeorm';

import { catProfileRelationQuery, participantRelationQuery } from './helpers';
import { RequestFostering } from './models/request-fostering';
import { RequestFostering } from './models/request-fostering.dto';

@Injectable()
export class FosteringService {
Expand Down
2 changes: 1 addition & 1 deletion libs/fostering/nestjs-module/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import type { ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';

import { RequestFostering } from './models/request-fostering';
import { RequestFostering } from './models/request-fostering.dto';

export const participantRelationQuery = (
fosteringId: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fostering } from '@cat-fostering/entities';
import { PickType } from '@nestjs/mapped-types';
import { PickType } from '@nestjs/swagger';
import { IsUUID } from 'class-validator';

export class RequestFostering extends PickType(Fostering, [
Expand Down
11 changes: 11 additions & 0 deletions libs/user/nestjs-module/src/lib/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import {
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import {
ApiBearerAuth,
ApiCookieAuth,
ApiSecurity,
ApiTags,
} from '@nestjs/swagger';
import type { Request } from 'express';

import { OryActionGuard } from './guards/ory-action.guard';
Expand All @@ -27,9 +33,11 @@ import { OryWebhookError } from './models/ory-webhook.error';
import { UsersService } from './users.service';

@Controller('users')
@ApiTags('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}

@ApiSecurity('ory-action')
@UseGuards(OryActionGuard)
@UsePipes(
new ValidationPipe({
Expand All @@ -51,6 +59,7 @@ export class UsersController {
return this.usersService.onSignUp(body);
}

@ApiSecurity('ory-action')
@UseGuards(OryActionGuard)
@UsePipes(
new ValidationPipe({
Expand All @@ -72,6 +81,8 @@ export class UsersController {
return this.usersService.onSignIn(body);
}

@ApiBearerAuth()
@ApiCookieAuth()
@UseGuards(
OryAuthenticationGuard({
cookieResolver: (ctx) =>
Expand Down

0 comments on commit 7407245

Please sign in to comment.