|
1 | 1 | import QRCode from "qrcode" |
2 | 2 |
|
3 | | -/** |
4 | | - * Generate a QR code as a data URL |
5 | | - * @param text The text to encode in the QR code |
6 | | - * @param options QR code generation options |
7 | | - * @returns Promise resolving to a data URL string |
8 | | - */ |
9 | | -export async function generateQRCode( |
10 | | - text: string, |
11 | | - options?: { |
12 | | - width?: number |
13 | | - margin?: number |
14 | | - color?: { |
15 | | - dark?: string |
16 | | - light?: string |
17 | | - } |
18 | | - }, |
19 | | -): Promise<string> { |
20 | | - try { |
21 | | - const dataUrl = await QRCode.toDataURL(text, { |
22 | | - width: options?.width ?? 200, |
23 | | - margin: options?.margin ?? 2, |
24 | | - color: { |
25 | | - dark: options?.color?.dark ?? "#000000", |
26 | | - light: options?.color?.light ?? "#FFFFFF", |
27 | | - }, |
28 | | - }) |
29 | | - return dataUrl |
30 | | - } catch (error) { |
31 | | - console.error("Failed to generate QR code:", error) |
32 | | - throw new Error(`Failed to generate QR code: ${error instanceof Error ? error.message : String(error)}`) |
| 3 | +interface QRCodeOptions { |
| 4 | + width?: number |
| 5 | + margin?: number |
| 6 | + color?: { |
| 7 | + dark?: string |
| 8 | + light?: string |
33 | 9 | } |
34 | 10 | } |
35 | 11 |
|
36 | | -/** |
37 | | - * Generate a QR code as an SVG string |
38 | | - * @param text The text to encode in the QR code |
39 | | - * @param options QR code generation options |
40 | | - * @returns Promise resolving to an SVG string |
41 | | - */ |
42 | | -export async function generateQRCodeSVG( |
43 | | - text: string, |
44 | | - options?: { |
45 | | - width?: number |
46 | | - margin?: number |
47 | | - color?: { |
48 | | - dark?: string |
49 | | - light?: string |
50 | | - } |
| 12 | +const DEFAULT_OPTIONS = { |
| 13 | + width: 200, |
| 14 | + margin: 2, |
| 15 | + color: { |
| 16 | + dark: "#000000", |
| 17 | + light: "#FFFFFF", |
51 | 18 | }, |
52 | | -): Promise<string> { |
53 | | - try { |
54 | | - const svg = await QRCode.toString(text, { |
55 | | - type: "svg", |
56 | | - width: options?.width ?? 200, |
57 | | - margin: options?.margin ?? 2, |
58 | | - color: { |
59 | | - dark: options?.color?.dark ?? "#000000", |
60 | | - light: options?.color?.light ?? "#FFFFFF", |
61 | | - }, |
62 | | - }) |
63 | | - return svg |
64 | | - } catch (error) { |
65 | | - console.error("Failed to generate QR code SVG:", error) |
66 | | - throw new Error(`Failed to generate QR code SVG: ${error instanceof Error ? error.message : String(error)}`) |
| 19 | +} as const |
| 20 | + |
| 21 | +function buildQRCodeOptions(options?: QRCodeOptions) { |
| 22 | + return { |
| 23 | + width: options?.width ?? DEFAULT_OPTIONS.width, |
| 24 | + margin: options?.margin ?? DEFAULT_OPTIONS.margin, |
| 25 | + color: { |
| 26 | + dark: options?.color?.dark ?? DEFAULT_OPTIONS.color.dark, |
| 27 | + light: options?.color?.light ?? DEFAULT_OPTIONS.color.light, |
| 28 | + }, |
67 | 29 | } |
68 | 30 | } |
| 31 | + |
| 32 | +export async function generateQRCode(text: string, options?: QRCodeOptions): Promise<string> { |
| 33 | + return QRCode.toDataURL(text, buildQRCodeOptions(options)) |
| 34 | +} |
| 35 | + |
| 36 | +export async function generateQRCodeSVG(text: string, options?: QRCodeOptions): Promise<string> { |
| 37 | + return QRCode.toString(text, { |
| 38 | + type: "svg", |
| 39 | + ...buildQRCodeOptions(options), |
| 40 | + }) |
| 41 | +} |
0 commit comments