[core] Return json or binary based on Accept header

This commit is contained in:
allburov committed 2023-11-30 18:01:49 +07:00
1 parent 9b6784a018
commit 19259c93ea
3 files changed
+54 -6

No files matched your search

+23 -4
View File
@@ -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<Base64File | StreamableFile> {
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',
+30 -1
View File
@@ -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',
},
},
},
},
},
});
}
+1 -1
View File
@@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
export class Base64File {
data: string;
mimetype: string;
data: string;
}
export class RemoteFile {