mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-24 09:28:31 +00:00
27 lines
848 B
TypeScript
27 lines
848 B
TypeScript
import {
|
|
ExecutionContext,
|
|
Injectable,
|
|
NotFoundException,
|
|
NestInterceptor,
|
|
CallHandler,
|
|
BadRequestException,
|
|
} from '@nestjs/common';
|
|
import { AppsService } from 'src/services/apps.service';
|
|
import { Observable } from 'rxjs';
|
|
|
|
@Injectable()
|
|
export class ValidAppInterceptor implements NestInterceptor {
|
|
constructor(private appsService: AppsService) {}
|
|
|
|
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
|
|
const request = context.switchToHttp().getRequest();
|
|
const { id, slug } = request.params;
|
|
if (!(id || slug)) {
|
|
throw new BadRequestException();
|
|
}
|
|
const app = id ? await this.appsService.find(id) : this.appsService.findBySlug(slug);
|
|
if (!app) throw new NotFoundException('App not found. Invalid app id');
|
|
request.app = app;
|
|
return next.handle();
|
|
}
|
|
}
|