-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (42 loc) · 1.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { toReadable, toWritable } from "@sveu/shared"
import QRCode from "qrcode"
/**
* Wrapper for qrcode.
*
* @param text - The text to encode.
*
* @see options in https://github.com/soldair/node-qrcode#qr-code-options
*
* @example
* ```ts
* const { output, pending, error } = qrcode("Hello world!")
* ```
*
* @returns
* - output - Readable store of the generated QR code.
* - error - Error if one occurred.
* - pending - Whether the QR code is being generated.
*/
export function qrcode(text: string, options?: QRCode.QRCodeToDataURLOptions) {
const output = toWritable("")
const error = toWritable<boolean | unknown>(false)
const pending = toWritable(true)
/** Generate the QR code. */
async function generate() {
pending.set(true)
error.set(false)
try {
output.set(await QRCode.toDataURL(text, options))
} catch (e) {
error.set(e)
} finally {
pending.set(false)
}
}
generate()
return {
output: toReadable(output),
error: toReadable(error),
pending: toReadable(pending),
}
}