Skip to content

Commit

Permalink
feat(types): add overload response types for excel functions
Browse files Browse the repository at this point in the history
  • Loading branch information
leoreisdias committed Nov 3, 2023
1 parent cf109fc commit 24e640b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 34 deletions.
105 changes: 78 additions & 27 deletions src/excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,36 @@ import {
import { MergeProps } from "./types";
import { PaginatedObjectContentProps } from "./types/contents";
import {
ExcelAdvancedOptionsBase64,
ExcelAdvancedOptionsBuffer,
ExcelAdvancedOptionsDownload,
ExcelAdvancedOptionsFilePath,
ExcelOptionsBase64,
ExcelOptionsBuffer,
ExcelOptionsDownload,
ExcelOptionsFilePath,
ExportationType,
ExportationTypeBase64,
ExportationTypeBuffer,
ExportationTypeDownload,
ExportationTypeFilePath,
ExportMeExcelAdvancedProps,
ExportMeExcelProps,
} from "./types/functions";

const transformData = (
data: Record<string, any>[]
): PaginatedObjectContentProps[] => {
if (
!!data[0]?.content &&
Array.isArray(data[0]?.content) &&
!!data[0]?.sheetName
)
return data as PaginatedObjectContentProps[];

return [{ content: data, sheetName: "Sheet 1" }];
};

const executeXLSX = (
data: XLSX.CellObject[][],
columnWidths?: number[],
Expand All @@ -31,11 +56,11 @@ const executeXLSX = (
return ws;
};

const exportFile = (
function exportFile(
exportAs: ExportationType,
wb: XLSX.WorkBook,
fileName: string
) => {
): Promise<string | ArrayBuffer | void> {
if (exportAs.type === "base64") {
return XLSX.write(wb, { type: "base64", bookType: "xlsx" });
}
Expand All @@ -45,24 +70,39 @@ const exportFile = (
}

if (exportAs.type === "download") {
return XLSX.writeFile(wb, `${fileName}.xlsx`);
XLSX.writeFile(wb, `${fileName}.xlsx`);
return;
}

if (exportAs.type === "filepath") {
return XLSX.writeFile(wb, exportAs.path);
XLSX.writeFile(wb, exportAs.path);
return;
}

return null;
};

export const exportmeExcelAdvanced = ({
return;
}

export function exportmeExcelAdvanced(
options: ExcelAdvancedOptionsBase64
): Promise<string>;
export function exportmeExcelAdvanced(
options: ExcelAdvancedOptionsBuffer
): Promise<ArrayBuffer>;
export function exportmeExcelAdvanced(
options: ExcelAdvancedOptionsDownload
): Promise<void>;
export function exportmeExcelAdvanced(
options: ExcelAdvancedOptionsFilePath
): Promise<void>;

export function exportmeExcelAdvanced({
fileName,
data,
options,
exportAs,
merges,
loggingMatrix,
}: ExportMeExcelAdvancedProps) => {
}: ExportMeExcelAdvancedProps): Promise<string | ArrayBuffer | void> {
const {
bodyStyle = {},
columnWidths = [],
Expand Down Expand Up @@ -103,28 +143,30 @@ export const exportmeExcelAdvanced = ({
console.info(`💡 Excel-Ent:Logging-Matrix: ${JSON.stringify(rowsAdapter)}`);
}

return exportFile(exportAs, wb, fileName);
};

const transformData = (
data: Record<string, any>[]
): PaginatedObjectContentProps[] => {
if (
!!data[0]?.content &&
Array.isArray(data[0]?.content) &&
!!data[0]?.sheetName
)
return data as PaginatedObjectContentProps[];
if (exportAs.type === "filepath") {
return exportFile(exportAs as ExportationTypeFilePath, wb, fileName);
} else if (exportAs.type === "download") {
return exportFile(exportAs as ExportationTypeDownload, wb, fileName);
} else if (exportAs.type === "buffer") {
return exportFile(exportAs as ExportationTypeBuffer, wb, fileName);
} else {
return exportFile(exportAs as ExportationTypeBase64, wb, fileName);
}
}

return [{ content: data, sheetName: "Sheet 1" }];
};
export function exportmeExcel(options: ExcelOptionsBase64): Promise<string>;
export function exportmeExcel(
options: ExcelOptionsBuffer
): Promise<ArrayBuffer>;
export function exportmeExcel(options: ExcelOptionsDownload): Promise<void>;
export function exportmeExcel(options: ExcelOptionsFilePath): Promise<void>;

export const exportmeExcel = ({
export function exportmeExcel({
data,
fileName,
exportAs,
options,
}: ExportMeExcelProps) => {
}: ExportMeExcelProps): Promise<string | ArrayBuffer | void> {
const {
bodyStyle = {},
columnWidths = [],
Expand All @@ -150,6 +192,7 @@ export const exportmeExcel = ({
(item: Record<string, any>, index: number) =>
Object.keys(item).map((key) => {
const isRowPainted = stripedRows && index % 2 === 0;

if (typeof item[key] === "number") {
return {
v: item[key],
Expand Down Expand Up @@ -187,5 +230,13 @@ export const exportmeExcel = ({

wb.Props = sheetProps;

return exportFile(exportAs, wb, fileName);
};
if (exportAs.type === "filepath") {
return exportFile(exportAs as ExportationTypeFilePath, wb, fileName);
} else if (exportAs.type === "download") {
return exportFile(exportAs as ExportationTypeDownload, wb, fileName);
} else if (exportAs.type === "buffer") {
return exportFile(exportAs as ExportationTypeBuffer, wb, fileName);
} else {
return exportFile(exportAs as ExportationTypeBase64, wb, fileName);
}
}
56 changes: 49 additions & 7 deletions src/types/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ export type ExportMeExcelOptions = {
sheetProps?: XLSX.FullProperties;
};

export type ExportationTypeBase64 = { type: "base64" };
export type ExportationTypeBuffer = { type: "buffer" };
export type ExportationTypeDownload = { type: "download" };
export type ExportationTypeFilePath = { type: "filepath"; path: string };

export type ExportationType =
| {
type: "buffer" | "base64" | "download";
}
| {
type: "filepath";
path: string;
};
| ExportationTypeBase64
| ExportationTypeBuffer
| ExportationTypeDownload
| ExportationTypeFilePath;

export type MergeProps = {
start: { row: number; column: number };
Expand All @@ -41,3 +43,43 @@ export type ExportMeExcelProps = {
exportAs: ExportationType;
options?: ExportMeExcelOptions & { stripedRows?: boolean };
};

type ExcelAdvancedOptions = {
data: ExcelEntDataProps;
fileName: string;
merges?: MergeProps[];
options?: ExportMeExcelOptions;
loggingMatrix?: boolean;
};

export type ExcelAdvancedOptionsBase64 = ExcelAdvancedOptions & {
exportAs: ExportationTypeBase64;
};
export type ExcelAdvancedOptionsBuffer = ExcelAdvancedOptions & {
exportAs: ExportationTypeBuffer;
};
export type ExcelAdvancedOptionsDownload = ExcelAdvancedOptions & {
exportAs: ExportationTypeDownload;
};
export type ExcelAdvancedOptionsFilePath = ExcelAdvancedOptions & {
exportAs: ExportationTypeFilePath;
};

type ExcelProps = {
data: Record<string, any>[] | PaginatedObjectContentProps[];
fileName: string;
options?: ExportMeExcelOptions & { stripedRows?: boolean };
};

export type ExcelOptionsBase64 = ExcelProps & {
exportAs: ExportationTypeBase64;
};
export type ExcelOptionsBuffer = ExcelProps & {
exportAs: ExportationTypeBuffer;
};
export type ExcelOptionsDownload = ExcelProps & {
exportAs: ExportationTypeDownload;
};
export type ExcelOptionsFilePath = ExcelProps & {
exportAs: ExportationTypeFilePath;
};

0 comments on commit 24e640b

Please sign in to comment.