-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEAT: Add home number 4 #4
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { | ||
MiddlewareConsumer, | ||
Module, | ||
NestModule, | ||
RequestMethod, | ||
} from "@nestjs/common"; | ||
import { TasksModule } from "./tasks/tasks.module"; | ||
import { LoggingMiddleware } from "./middlewares/logging.middleware"; | ||
|
||
@Module({ | ||
imports: [TasksModule], | ||
}) | ||
export class AppModule {} | ||
export class AppModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(LoggingMiddleware).forRoutes({ | ||
path: "ab*cd", | ||
method: RequestMethod.ALL, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[2024-12-31T08:25:54.459Z] 500 - Mock error for testing | ||
[2024-12-31T08:26:13.855Z] 500 - Mock error for testing |
34 changes: 32 additions & 2 deletions
34
04-request-lifecycle/01-nestjs-components/filters/http-error.filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
import { ArgumentsHost, ExceptionFilter } from "@nestjs/common"; | ||
import { | ||
ArgumentsHost, | ||
Catch, | ||
ExceptionFilter, | ||
HttpException, | ||
} from "@nestjs/common"; | ||
import * as fs from "fs"; | ||
|
||
@Catch() | ||
export class HttpErrorFilter implements ExceptionFilter { | ||
catch(exception: any, host: ArgumentsHost) {} | ||
catch(exception: any, host: ArgumentsHost) { | ||
const isHttpException = exception instanceof HttpException; | ||
const status = isHttpException ? exception.getStatus() : 500; | ||
const message = isHttpException | ||
? exception.message | ||
: "Mock error for testing"; | ||
const timestamp = new Date().toISOString(); | ||
const path = host.switchToHttp().getRequest().path; | ||
const error = null; | ||
|
||
const response = host.switchToHttp().getResponse(); | ||
response.status(status).json({ | ||
statusCode: status, | ||
message, | ||
timestamp, | ||
path, | ||
error, | ||
}); | ||
|
||
fs.appendFileSync( | ||
"errors.log", | ||
`[${timestamp}] ${status} - ${message}\n`, | ||
); | ||
} | ||
} |
21 changes: 18 additions & 3 deletions
21
04-request-lifecycle/01-nestjs-components/guards/roles.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
import { CanActivate, ExecutionContext } from "@nestjs/common"; | ||
|
||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
Injectable, | ||
} from "@nestjs/common"; | ||
import { Observable } from "rxjs"; | ||
@Injectable() | ||
export class RolesGuard implements CanActivate { | ||
canActivate(context: ExecutionContext) {} | ||
canActivate(context: ExecutionContext) { | ||
const request = context.switchToHttp().getRequest(); | ||
const role = request.headers["x-role"]; | ||
|
||
if (role === "admin") { | ||
return true; | ||
} else { | ||
throw new ForbiddenException("Доступ запрещён: требуется роль admin"); | ||
} | ||
} | ||
} |
14 changes: 13 additions & 1 deletion
14
04-request-lifecycle/01-nestjs-components/interceptors/api-version.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
import { NestInterceptor, ExecutionContext, CallHandler } from "@nestjs/common"; | ||
import { Observable, map, tap } from "rxjs"; | ||
|
||
export class ApiVersionInterceptor implements NestInterceptor { | ||
intercept(context: ExecutionContext, next: CallHandler) {} | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
const start = Date.now(); | ||
let end = 0; | ||
return next.handle().pipe( | ||
tap(() => (end = Date.now() - start)), | ||
map((response) => ({ | ||
tasks: response.tasks, | ||
apiVersion: "1.0", | ||
executionTime: end + "ms", | ||
})), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { NestFactory } from "@nestjs/core"; | ||
import { AppModule } from "./app.module"; | ||
import { HttpErrorFilter } from "./filters/http-error.filter"; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
app.useGlobalFilters(new HttpErrorFilter()); | ||
await app.listen(process.env.PORT ?? 3000); | ||
} | ||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 12 additions & 2 deletions
14
04-request-lifecycle/01-nestjs-components/pipes/parse-int.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
import { PipeTransform } from "@nestjs/common"; | ||
import { BadRequestException, HttpStatus, PipeTransform } from "@nestjs/common"; | ||
|
||
export class ParseIntPipe implements PipeTransform { | ||
transform(value: string): number {} | ||
transform(value: string): number { | ||
const parsedValue = parseInt(value, 10); | ||
if (isNaN(parsedValue)) { | ||
throw new BadRequestException({ | ||
statusCode: HttpStatus.BAD_REQUEST, | ||
message: `"${value}" не является числом`, | ||
error: "Bad Request", | ||
}); | ||
} | ||
return parsedValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { TasksModule } from "./tasks/tasks.module"; | ||
import { TypeOrmModule } from "@nestjs/typeorm"; | ||
import { Task } from "./tasks/entities/task.entity"; | ||
|
||
@Module({ | ||
imports: [TasksModule], | ||
imports: [ | ||
TasksModule, | ||
TypeOrmModule.forRoot({ | ||
type: "sqlite", // Тип базы данных | ||
database: "db.sqlite", // Файл базы данных | ||
entities: [Task], // Список сущностей | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. как альтернатива можно использова |
||
synchronize: true, | ||
}), | ||
], | ||
}) | ||
export class AppModule {} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍