Files
covergen/frontend/src/lib/covergen.ts
2022-01-22 19:28:06 +01:00

52 lines
1.1 KiB
TypeScript

interface CoverArgs {
customer: string;
number: string;
numberPrefix?: string;
hlColor?: string;
}
interface CoverError {
error: string;
}
declare global {
interface Window {
generateCover(args: CoverArgs): Uint8Array | CoverError;
generateSplitCover(
args: CoverArgs
): { front: Uint8Array; back: Uint8Array } | CoverError;
}
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch('covergen.wasm'), go.importObject).then(
(result) => {
go.run(result.instance);
}
);
export function generateCover(args: CoverArgs): File {
const result = window.generateCover(args);
if ('error' in result) {
throw result.error;
}
return new File([result], 'cover.pdf', { type: 'application/pdf' });
}
export function generateSplitCover(args: CoverArgs): {
front: File;
back: File;
} {
const result = window.generateSplitCover(args);
if ('error' in result) {
throw result.error;
}
return {
front: new File([result.front], 'front.pdf', { type: 'application/pdf' }),
back: new File([result.back], 'back.pdf', { type: 'application/pdf' }),
};
}