Skip to content

Commit 30de4f1

Browse files
committed
refactor: improve qrcode generation
1 parent 86082aa commit 30de4f1

File tree

1 file changed

+33
-60
lines changed

1 file changed

+33
-60
lines changed
Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,41 @@
11
import QRCode from "qrcode"
22

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
339
}
3410
}
3511

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",
5118
},
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+
},
6729
}
6830
}
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

Comments
 (0)