import { Controller, Get, Query, BadRequestException, Req, Res } from '@nestjs/common'; import { Request, Response } from 'express'; import { Public } from 'src/auth/auth.decorator'; import { WidgetService } from './widget.service'; @Controller('widget') @Public() export class WidgetController { constructor(private readonly widgetService: WidgetService) {} @Get('script') getWidgetScript( @Query('apiKey') apiKey: string, @Req() req: Request, @Res() res: Response, ) { if (!this.widgetService.isValidApiKey(apiKey)) { throw new BadRequestException( apiKey ? 'Invalid API key' : 'API key is required', ); } this.widgetService.applyScriptHeaders(req, res); return res.send(this.widgetService.generateWidgetScript()); } @Get('iframe') getIframeScript( @Query('apiKey') apiKey: string, @Req() req: Request, @Res() res: Response, ) { if (!this.widgetService.isValidApiKey(apiKey)) { throw new BadRequestException('API key is invalid or missing'); } this.widgetService.applyScriptHeaders(req, res); return res.send(this.widgetService.generateWidgetScriptWithoutStyle()); } @Get('demo') getWidgetDemo(@Res() res: Response) { res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.setHeader('X-Content-Type-Options', 'nosniff'); res.setHeader('Cache-Control', 'no-store'); return res.send(this.widgetService.generateDemoHtml()); } @Get('frame') getWidgetFrame(@Res() res: Response) { this.widgetService.applyFrameHeaders(res); return res.send(this.widgetService.generateFrameHtml()); } }