import { resolve } from 'path'; import { QRImage, QRTerminal, QRToDataURL } from './util'; export async function resolveQrCodeSchema( qrcodeSchema: string, { qrcodeFormat, qrcodeOutput, }: { qrcodeFormat?: 'base64' | 'terminal' | 'image' | undefined; qrcodeOutput?: string } ) { const qrUrl = getRealQrUrl(qrcodeSchema); let qrResult; if (qrcodeFormat === 'base64') { qrResult = await QRToDataURL(qrUrl); } else if (qrcodeFormat === 'terminal') { qrResult = await QRTerminal(qrUrl); } else { qrcodeOutput = qrcodeOutput || resolve('mini.preview.png'); await QRImage(qrcodeOutput!, qrUrl); qrResult = qrcodeOutput; } return qrResult; } export function getRealQrUrl(qrcodeSchema: string) { const fullSchemeUrl = new URL(qrcodeSchema.replace(/&/g, '&')); return fullSchemeUrl.searchParams.get('scheme') || ''; }