2021-02-14 11:50:26 +03:00
|
|
|
import { isEmpty } from 'ramda';
|
|
|
|
import { stringifyQuery } from './query';
|
2021-01-24 20:21:04 +03:00
|
|
|
|
2021-01-24 19:37:31 +03:00
|
|
|
export interface QrCodeCapabilities {
|
|
|
|
useSizeInPath: boolean;
|
|
|
|
svgIsSupported: boolean;
|
2021-02-14 11:50:26 +03:00
|
|
|
marginIsSupported: boolean;
|
2021-01-24 19:37:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export type QrCodeFormat = 'svg' | 'png';
|
|
|
|
|
2021-02-14 11:50:26 +03:00
|
|
|
export interface QrCodeOptions {
|
|
|
|
size: number;
|
|
|
|
format: QrCodeFormat;
|
|
|
|
margin: number;
|
|
|
|
}
|
|
|
|
|
2021-01-24 19:37:31 +03:00
|
|
|
export const buildQrCodeUrl = (
|
|
|
|
shortUrl: string,
|
2021-02-14 11:50:26 +03:00
|
|
|
{ size, format, margin }: QrCodeOptions,
|
|
|
|
{ useSizeInPath, svgIsSupported, marginIsSupported }: QrCodeCapabilities,
|
2021-01-24 19:37:31 +03:00
|
|
|
): string => {
|
2021-02-14 11:50:26 +03:00
|
|
|
const baseUrl = `${shortUrl}/qr-code${useSizeInPath ? `/${size}` : ''}`;
|
|
|
|
const query = stringifyQuery({
|
|
|
|
size: useSizeInPath ? undefined : size,
|
|
|
|
format: svgIsSupported ? format : undefined,
|
2021-02-14 12:16:30 +03:00
|
|
|
margin: marginIsSupported && margin > 0 ? margin : undefined,
|
2021-02-14 11:50:26 +03:00
|
|
|
});
|
2021-01-24 19:37:31 +03:00
|
|
|
|
2021-02-14 11:50:26 +03:00
|
|
|
return `${baseUrl}${isEmpty(query) ? '' : `?${query}`}`;
|
2021-01-24 19:37:31 +03:00
|
|
|
};
|