diff --git a/src/api/auth.controller.ts b/src/api/auth.controller.ts index f673f05a..25c3eee4 100644 --- a/src/api/auth.controller.ts +++ b/src/api/auth.controller.ts @@ -2,19 +2,27 @@ import { Body, Controller, Get, + Headers, Post, + Req, Res, StreamableFile, } from '@nestjs/common'; import { UnprocessableEntityException } from '@nestjs/common/exceptions/unprocessable-entity.exception'; -import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; -import { Response } from 'express'; +import { + ApiOperation, + ApiResponse, + ApiSecurity, + ApiTags, +} from '@nestjs/swagger'; +import { Request, Response } from 'express'; import { SessionManager } from '../core/abc/manager.abc'; import { WhatsappSession } from '../core/abc/session.abc'; import { OTPRequest, RequestCodeRequest } from '../structures/auth.dto'; import { WAHASessionStatus } from '../structures/enums.dto'; -import { SessionApiParam, SessionParam } from './helpers'; +import { Base64File } from '../structures/files.dto'; +import { ApiFileAcceptHeader, SessionApiParam, SessionParam } from './helpers'; @ApiSecurity('api_key') @Controller('api/:session/auth') @@ -24,19 +32,30 @@ class AuthController { @Get('qr') @SessionApiParam + @ApiFileAcceptHeader() @ApiOperation({ summary: 'Get QR code for pairing WhatsApp Web.', }) async getQR( @Res({ passthrough: true }) res: Response, + // We don't use @Headers and work directly with @Req here to avoid adding it to Swagger + @Req() req: Request, @SessionParam session: WhatsappSession, - ) { + ): Promise { if (session.status != WAHASessionStatus.SCAN_QR_CODE) { const err = `Can get QR code only in SCAN_QR_CODE status. The current status is '${session.status}'`; throw new UnprocessableEntityException(err); } const buffer = await session.getQR(); + const accept = req.headers['accept']; + if (accept == 'application/json') { + return { + mimetype: 'image/png', + data: buffer.toString('base64'), + }; + } + const file = new StreamableFile(buffer); res.set({ 'Content-Type': 'image/png', diff --git a/src/api/helpers.ts b/src/api/helpers.ts index 72e3f4cf..6cc261a9 100644 --- a/src/api/helpers.ts +++ b/src/api/helpers.ts @@ -1,5 +1,5 @@ import { Injectable, Param, PipeTransform } from '@nestjs/common'; -import { ApiParam } from '@nestjs/swagger'; +import { ApiParam, ApiResponse } from '@nestjs/swagger'; import { SessionManager } from '../core/abc/manager.abc'; import { WhatsappSession } from '../core/abc/session.abc'; @@ -36,3 +36,32 @@ export const SessionApiParam = ApiParam({ @SessionParam session: WhatsappSession, */ export const SessionParam = Param('session', SessionPipe); + +export function ApiFileAcceptHeader() { + return ApiResponse({ + status: 200, + content: { + 'image/png': { + schema: { + type: 'string', + format: 'binary', + }, + }, + 'application/json': { + schema: { + type: 'object', + properties: { + data: { + type: 'string', + format: 'base64', + }, + mimetype: { + type: 'string', + example: 'image/png', + }, + }, + }, + }, + }, + }); +} diff --git a/src/structures/files.dto.ts b/src/structures/files.dto.ts index c193dcda..be2130ad 100644 --- a/src/structures/files.dto.ts +++ b/src/structures/files.dto.ts @@ -1,7 +1,7 @@ import { ApiProperty } from '@nestjs/swagger'; export class Base64File { - data: string; mimetype: string; + data: string; } export class RemoteFile {