mirror of
https://github.com/documenso/documenso.git
synced 2025-11-09 20:12:31 +10:00
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import type { EnvelopeItem } from '@prisma/client';
|
|
|
|
import { getEnvelopeDownloadUrl } from '../utils/envelope-download';
|
|
import { downloadFile } from './download-file';
|
|
|
|
type DocumentVersion = 'original' | 'signed';
|
|
|
|
type DownloadPDFProps = {
|
|
envelopeItem: Pick<EnvelopeItem, 'id' | 'envelopeId'>;
|
|
token: string | undefined;
|
|
|
|
fileName?: string;
|
|
/**
|
|
* Specifies which version of the document to download.
|
|
* 'signed': Downloads the signed version (default).
|
|
* 'original': Downloads the original version.
|
|
*/
|
|
version?: DocumentVersion;
|
|
};
|
|
|
|
export const downloadPDF = async ({
|
|
envelopeItem,
|
|
token,
|
|
fileName,
|
|
version = 'signed',
|
|
}: DownloadPDFProps) => {
|
|
const downloadUrl = getEnvelopeDownloadUrl({
|
|
envelopeItem: envelopeItem,
|
|
token,
|
|
version,
|
|
});
|
|
|
|
const blob = await fetch(downloadUrl).then(async (res) => await res.blob());
|
|
|
|
const baseTitle = (fileName ?? 'document').replace(/\.pdf$/, '');
|
|
const suffix = version === 'signed' ? '_signed.pdf' : '.pdf';
|
|
|
|
downloadFile({
|
|
filename: `${baseTitle}${suffix}`,
|
|
data: blob,
|
|
});
|
|
};
|