Added new component for QR codes error correction when consuming Shlink 2.8

This commit is contained in:
Alejandro Celaya
2021-08-16 17:13:31 +02:00
parent 0ecb771b23
commit 461c0e0bc9
4 changed files with 70 additions and 40 deletions

View File

@@ -27,3 +27,5 @@ export const supportsTagsInPatch = supportsShortUrlTitle;
export const supportsBotVisits = serverMatchesVersions({ minVersion: '2.7.0' });
export const supportsCrawlableVisits = supportsBotVisits;
export const supportsQrErrorCorrection = serverMatchesVersions({ minVersion: '2.8.0' });

View File

@@ -5,26 +5,31 @@ export interface QrCodeCapabilities {
useSizeInPath: boolean;
svgIsSupported: boolean;
marginIsSupported: boolean;
errorCorrectionIsSupported: boolean;
}
export type QrCodeFormat = 'svg' | 'png';
export type QrErrorCorrection = 'L' | 'M' | 'Q' | 'H';
export interface QrCodeOptions {
size: number;
format: QrCodeFormat;
margin: number;
errorCorrection: QrErrorCorrection;
}
export const buildQrCodeUrl = (
shortUrl: string,
{ size, format, margin }: QrCodeOptions,
{ useSizeInPath, svgIsSupported, marginIsSupported }: QrCodeCapabilities,
{ size, format, margin, errorCorrection }: QrCodeOptions,
{ useSizeInPath, svgIsSupported, marginIsSupported, errorCorrectionIsSupported }: QrCodeCapabilities,
): string => {
const baseUrl = `${shortUrl}/qr-code${useSizeInPath ? `/${size}` : ''}`;
const query = stringifyQuery({
size: useSizeInPath ? undefined : size,
format: svgIsSupported ? format : undefined,
margin: marginIsSupported && margin > 0 ? margin : undefined,
errorCorrection: errorCorrectionIsSupported ? errorCorrection : undefined,
});
return `${baseUrl}${isEmpty(query) ? '' : `?${query}`}`;