|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { type NextRequest, NextResponse } from 'next/server' |
| 3 | +import { z } from 'zod' |
| 4 | +import { checkInternalAuth } from '@/lib/auth/hybrid' |
| 5 | +import { generateRequestId } from '@/lib/core/utils/request' |
| 6 | + |
| 7 | +export const dynamic = 'force-dynamic' |
| 8 | + |
| 9 | +const logger = createLogger('ShortIoQrAPI') |
| 10 | + |
| 11 | +const ShortIoQrSchema = z.object({ |
| 12 | + apiKey: z.string().min(1, 'API key is required'), |
| 13 | + linkId: z.string().min(1, 'Link ID is required'), |
| 14 | + color: z.string().optional(), |
| 15 | + backgroundColor: z.string().optional(), |
| 16 | + size: z.number().min(1).max(99).optional(), |
| 17 | + type: z.enum(['png', 'svg']).optional(), |
| 18 | + useDomainSettings: z.boolean().optional(), |
| 19 | +}) |
| 20 | + |
| 21 | +export async function POST(request: NextRequest) { |
| 22 | + const requestId = generateRequestId() |
| 23 | + |
| 24 | + try { |
| 25 | + const authResult = await checkInternalAuth(request, { requireWorkflowId: false }) |
| 26 | + |
| 27 | + if (!authResult.success) { |
| 28 | + logger.warn(`[${requestId}] Unauthorized Short.io QR request: ${authResult.error}`) |
| 29 | + return NextResponse.json( |
| 30 | + { success: false, error: authResult.error || 'Authentication required' }, |
| 31 | + { status: 401 } |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + const body = await request.json() |
| 36 | + const validated = ShortIoQrSchema.parse(body) |
| 37 | + |
| 38 | + const qrBody: Record<string, unknown> = { |
| 39 | + useDomainSettings: validated.useDomainSettings ?? true, |
| 40 | + } |
| 41 | + if (validated.color) qrBody.color = validated.color |
| 42 | + if (validated.backgroundColor) qrBody.backgroundColor = validated.backgroundColor |
| 43 | + if (validated.size) qrBody.size = validated.size |
| 44 | + if (validated.type) qrBody.type = validated.type |
| 45 | + |
| 46 | + const response = await fetch(`https://api.short.io/links/qr/${validated.linkId}`, { |
| 47 | + method: 'POST', |
| 48 | + headers: { |
| 49 | + Authorization: validated.apiKey, |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + }, |
| 52 | + body: JSON.stringify(qrBody), |
| 53 | + }) |
| 54 | + |
| 55 | + if (!response.ok) { |
| 56 | + const errorText = await response.text().catch(() => response.statusText) |
| 57 | + logger.error(`[${requestId}] Short.io QR API error: ${errorText}`) |
| 58 | + return NextResponse.json( |
| 59 | + { success: false, error: `Short.io API error: ${errorText}` }, |
| 60 | + { status: response.status } |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + const contentType = response.headers.get('Content-Type') ?? 'image/png' |
| 65 | + const fileBuffer = Buffer.from(await response.arrayBuffer()) |
| 66 | + const mimeType = contentType.split(';')[0]?.trim() || 'image/png' |
| 67 | + const ext = validated.type === 'svg' ? 'svg' : 'png' |
| 68 | + const fileName = `qr-${validated.linkId}.${ext}` |
| 69 | + |
| 70 | + logger.info(`[${requestId}] QR code generated`, { |
| 71 | + linkId: validated.linkId, |
| 72 | + size: fileBuffer.length, |
| 73 | + mimeType, |
| 74 | + }) |
| 75 | + |
| 76 | + return NextResponse.json({ |
| 77 | + success: true, |
| 78 | + output: { |
| 79 | + file: { |
| 80 | + name: fileName, |
| 81 | + mimeType, |
| 82 | + data: fileBuffer.toString('base64'), |
| 83 | + size: fileBuffer.length, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }) |
| 87 | + } catch (error: unknown) { |
| 88 | + if (error instanceof z.ZodError) { |
| 89 | + return NextResponse.json( |
| 90 | + { success: false, error: `Validation error: ${error.errors.map((e) => e.message).join(', ')}` }, |
| 91 | + { status: 400 } |
| 92 | + ) |
| 93 | + } |
| 94 | + const message = error instanceof Error ? error.message : 'Unknown error' |
| 95 | + logger.error(`[${requestId}] Short.io QR error: ${message}`) |
| 96 | + return NextResponse.json({ success: false, error: message }, { status: 500 }) |
| 97 | + } |
| 98 | +} |
0 commit comments