mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-30 12:27:39 +03:00
feat!(): add custom execution context
This commit is contained in:
7
sample/common/decorators/from.decorator.ts
Normal file
7
sample/common/decorators/from.decorator.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
||||
import { TelegrafExecutionContext } from '../../../lib/execution-context';
|
||||
|
||||
export const From = createParamDecorator(
|
||||
(_, ctx: ExecutionContext) =>
|
||||
TelegrafExecutionContext.create(ctx).getContext().from,
|
||||
);
|
11
sample/common/filters/telegraf-exception.filter.ts
Normal file
11
sample/common/filters/telegraf-exception.filter.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
|
||||
import { TelegrafArgumentsHost, TelegrafException } from '../../../lib';
|
||||
|
||||
@Catch()
|
||||
export class TelegrafExceptionFilter<T> implements ExceptionFilter {
|
||||
catch(exception: T, host: ArgumentsHost) {
|
||||
const tgHost = TelegrafArgumentsHost.create(host);
|
||||
console.log(tgHost);
|
||||
return exception;
|
||||
}
|
||||
}
|
21
sample/common/guards/admin.guard.ts
Normal file
21
sample/common/guards/admin.guard.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { TelegrafExecutionContext } from '../../../lib/execution-context/telegraf-execution-context';
|
||||
import { Context } from '../../interfaces/context.interface';
|
||||
import { TelegrafException } from '../../../lib/errors';
|
||||
|
||||
@Injectable()
|
||||
export class AdminGuard implements CanActivate {
|
||||
private readonly ADMIN_IDS = [];
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const ctx = TelegrafExecutionContext.create(context);
|
||||
const { from } = ctx.getContext<Context>();
|
||||
|
||||
const isAdmin = this.ADMIN_IDS.includes(from.id);
|
||||
if (!isAdmin) {
|
||||
throw new TelegrafException('You are not admin >:(');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
20
sample/common/interceptors/response-time.interceptor.ts
Normal file
20
sample/common/interceptors/response-time.interceptor.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
import { tap } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class ResponseTimeInterceptor implements NestInterceptor {
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
console.log('Before...');
|
||||
|
||||
const start = Date.now();
|
||||
return next
|
||||
.handle()
|
||||
.pipe(tap(() => console.log(`Response time: ${Date.now() - start}ms`)));
|
||||
}
|
||||
}
|
3
sample/common/middleware/session.middleware.ts
Normal file
3
sample/common/middleware/session.middleware.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { session } from 'telegraf';
|
||||
|
||||
export const sessionMiddleware = session();
|
8
sample/common/pipes/reverse-text.pipe.ts
Normal file
8
sample/common/pipes/reverse-text.pipe.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Injectable, PipeTransform } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class ReverseTextPipe implements PipeTransform {
|
||||
transform(value: string): string {
|
||||
return value.split('').reverse().join('');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user