diff --git a/CHANGELOG.md b/CHANGELOG.md index 16390ba..44afaa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [1.0.7](https://github.com/allohamora/nest-auth-example/compare/v1.0.6...v1.0.7) (2022-12-04) + +### Features + +- migrate to REST ([9ad9355](https://github.com/allohamora/nest-auth-example/commit/9ad93555ea90fb7d25917a01c5ac2333620c2fe8)) + ### [1.0.6](https://github.com/allohamora/nest-auth-example/compare/v1.0.5...v1.0.6) (2022-03-19) ### Features diff --git a/README.md b/README.md index 1c3690c..c40676b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ ## Docs - run the app -- open /api +- open /swagger - enjoy swagger docs ## Installation diff --git a/package-lock.json b/package-lock.json index c797b08..a15a73e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "nest-auth-example", - "version": "1.0.6", + "version": "1.0.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "nest-auth-example", - "version": "1.0.6", + "version": "1.0.7", "license": "MIT", "dependencies": { "@casl/ability": "^5.4.3", diff --git a/package.json b/package.json index a64a1b0..1cee2a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nest-auth-example", - "version": "1.0.6", + "version": "1.0.7", "description": "nest auth example", "author": "https://github.com/allohamora", "private": true, diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 43f166c..926fbe7 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -20,7 +20,7 @@ export class AuthController { @ApiOkResponse({ type: AccessRefreshTokens }) @ApiBody({ type: RegisterLoginDto }) @HttpCode(200) - public async login( + public login( @CurrentUser() user: User, @UserAgent() userAgent: string, @Ip() ip: string, @@ -31,26 +31,26 @@ export class AuthController { @Post('register') @ApiException({ statusCode: HttpStatus.BAD_REQUEST }) @ApiCreatedResponse({ type: User }) - public async register(@Body() registerDto: RegisterLoginDto): Promise { - return await this.authService.register(registerDto); + public register(@Body() registerDto: RegisterLoginDto): Promise { + return this.authService.register(registerDto); } @Post('refresh') @ApiException({ statusCode: HttpStatus.BAD_REQUEST }) @ApiOkResponse({ type: AccessRefreshTokens }) @HttpCode(200) - public async refresh( + public refresh( @Body() refreshDto: RefreshDto, @UserAgent() userAgent: string, @Ip() ip: string, ): Promise { - return await this.authService.refresh(refreshDto, userAgent, ip); + return this.authService.refresh(refreshDto, userAgent, ip); } @Auth() - @Get('test') + @Get('me') @ApiOkResponse({ type: User }) - public async test(@CurrentUser() user: User): Promise { + public async me(@CurrentUser() user: User): Promise { return user; } } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index fcf4659..e3c8ce1 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -97,12 +97,12 @@ export class AuthService { const { id } = this.jwtService.decode(refreshToken) as JwtPayloadDto; const user = await this.userService.findOne(id); - const refresAndAccessTokens = await this.createAccessAndRefreshTokens(user); + const refreshAndAccessTokens = await this.createAccessAndRefreshTokens(user); - session.refreshToken = refresAndAccessTokens.refreshToken; + session.refreshToken = refreshAndAccessTokens.refreshToken; await this.sessionRepository.save(session); - return refresAndAccessTokens; + return refreshAndAccessTokens; } public async register({ login, password }: RegisterLoginDto) { diff --git a/src/main.ts b/src/main.ts index a86aca0..521aab9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,11 @@ import pkg from '../package.json'; -import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common'; +import { ClassSerializerInterceptor, ValidationPipe, VersioningType } from '@nestjs/common'; import { NestFactory, Reflector } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { AppModule } from './app.module'; import { EntityNotFoundExceptionFilter } from './utils/entity-not-found-exception.filter'; -const SWAGGER_PATH = 'api'; +const SWAGGER_PATH = 'swagger'; const SERVER_PORT = 3000; async function bootstrap() { @@ -18,6 +18,9 @@ async function bootstrap() { .addBearerAuth() .build(); + app.setGlobalPrefix('api'); + app.enableVersioning({ defaultVersion: '1', type: VersioningType.URI }); + const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup(SWAGGER_PATH, app, document); diff --git a/src/post/post.controller.ts b/src/post/post.controller.ts index 907ff76..127fb47 100644 --- a/src/post/post.controller.ts +++ b/src/post/post.controller.ts @@ -1,7 +1,7 @@ import { Controller, UseGuards, - Post as RestPost, + Post as HttpPost, Put, Body, Param, @@ -24,16 +24,22 @@ import { CreateUpdatePostDto } from './dto/create-update-post.dto'; import { Post } from './post.entity'; import { PostService } from './post.service'; -@ApiTags('post') -@Controller('post') +@ApiTags('Post') +@Controller('posts') export class PostController { constructor(private postService: PostService) {} + @Get() + @ApiOkResponse({ type: Post, isArray: true }) + public getMany(): Promise { + return this.postService.getMany(); + } + @Get(':id') @ApiException({ statusCode: HttpStatus.NOT_FOUND }) @ApiOkResponse({ type: Post }) - public async get(@Param('id', ParseIntPipe) id: number): Promise { - return await this.postService.get(id); + public getOne(@Param('id', ParseIntPipe) id: number): Promise { + return this.postService.getOne(id); } @Delete(':id') @@ -41,16 +47,16 @@ export class PostController { @CheckPolicies((ability: AppAbility) => ability.can(Action.Delete, Post)) @Auth() public async delete(@Param('id', ParseIntPipe) id: number, @CurrentAbility() ability: AppAbility): Promise { - return await this.postService.delete(id, ability); + return this.postService.delete(id, ability); } - @RestPost() + @HttpPost() @ApiCreatedResponse({ type: Post }) @UseGuards(PoliciesGuard) @CheckPolicies((ability: AppAbility) => ability.can(Action.Create, Post)) @Auth() public async create(@Body() createPostDto: CreateUpdatePostDto, @CurrentUser() user: User): Promise { - return await this.postService.create(createPostDto, user); + return this.postService.create(createPostDto, user); } @Put(':id') @@ -58,11 +64,11 @@ export class PostController { @UseGuards(PoliciesGuard) @CheckPolicies((ability: AppAbility) => ability.can(Action.Update, Post)) @Auth() - public async update( + public update( @Body() updatePostDto: CreateUpdatePostDto, @Param('id', ParseIntPipe) id: number, @CurrentAbility() ability: AppAbility, ): Promise { - return await this.postService.update(updatePostDto, id, ability); + return this.postService.update(updatePostDto, id, ability); } } diff --git a/src/post/post.service.ts b/src/post/post.service.ts index 730a03a..0b52776 100644 --- a/src/post/post.service.ts +++ b/src/post/post.service.ts @@ -14,14 +14,18 @@ export class PostService { private postRepository: Repository, ) {} - public async create({ name, body }: CreateUpdatePostDto, author: User) { + public create({ name, body }: CreateUpdatePostDto, author: User) { const post = this.postRepository.create({ name, body, author }); - return await this.postRepository.save(post); + return this.postRepository.save(post); } - public async get(id: number) { - return await this.postRepository.findOneOrFail(id); + public getOne(id: number) { + return this.postRepository.findOneOrFail(id); + } + + public getMany() { + return this.postRepository.find(); } public async update({ name, body }: CreateUpdatePostDto, id: number, ability: AppAbility) { @@ -34,7 +38,7 @@ export class PostService { post.name = name; post.body = body; - return await this.postRepository.save(post); + return this.postRepository.save(post); } public async delete(id: number, ability: AppAbility) {