Skip to content

Commit

Permalink
Add type to GeneratePdfResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalczyk-krzysztof committed Oct 21, 2024
1 parent c7c5d82 commit 58cba87
Showing 1 changed file with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ export interface GeneratePdfRequest {
data: GenerateReportRequestData;
}

export type GeneratePdfResponse = SuccessResponse | ErrorResponse;

export interface SuccessResponse {
error?: undefined;
data: {
buffer: Uint8Array;
metrics: {
pages: number;
};
export interface GeneratePdfData {
buffer: Uint8Array;
metrics: {
pages: number;
};
}

export interface ErrorResponse {
error: string;
data: null;
export enum GeneratePdfResponseType {
Log,
Data,
}

export interface GeneratePdfResponse {
type: GeneratePdfResponseType;
data?: GeneratePdfData;
error?: string;
message?: string;
}

if (!isMainThread) {
Expand Down Expand Up @@ -133,7 +135,8 @@ async function execute({ data: { layout, logo, title, content } }: GeneratePdfRe
pdfDoc.end();
});

const successResponse: SuccessResponse = {
const successResponse: GeneratePdfResponse = {
type: GeneratePdfResponseType.Data,
data: {
buffer,
metrics: {
Expand All @@ -143,7 +146,10 @@ async function execute({ data: { layout, logo, title, content } }: GeneratePdfRe
};
port.postMessage(successResponse, [buffer.buffer /* Transfer buffer instead of copying */]);
} catch (error) {
const errorResponse: ErrorResponse = { error: error.message, data: null };
const errorResponse: GeneratePdfResponse = {
type: GeneratePdfResponseType.Data,
error: error.message,
};
port.postMessage(errorResponse);
} finally {
process.nextTick(() => {
Expand Down

0 comments on commit 58cba87

Please sign in to comment.