Skip to content

Commit 24e640b

Browse files
committed
feat(types): add overload response types for excel functions
1 parent cf109fc commit 24e640b

File tree

2 files changed

+127
-34
lines changed

2 files changed

+127
-34
lines changed

src/excel.ts

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,36 @@ import {
99
import { MergeProps } from "./types";
1010
import { PaginatedObjectContentProps } from "./types/contents";
1111
import {
12+
ExcelAdvancedOptionsBase64,
13+
ExcelAdvancedOptionsBuffer,
14+
ExcelAdvancedOptionsDownload,
15+
ExcelAdvancedOptionsFilePath,
16+
ExcelOptionsBase64,
17+
ExcelOptionsBuffer,
18+
ExcelOptionsDownload,
19+
ExcelOptionsFilePath,
1220
ExportationType,
21+
ExportationTypeBase64,
22+
ExportationTypeBuffer,
23+
ExportationTypeDownload,
24+
ExportationTypeFilePath,
1325
ExportMeExcelAdvancedProps,
1426
ExportMeExcelProps,
1527
} from "./types/functions";
1628

29+
const transformData = (
30+
data: Record<string, any>[]
31+
): PaginatedObjectContentProps[] => {
32+
if (
33+
!!data[0]?.content &&
34+
Array.isArray(data[0]?.content) &&
35+
!!data[0]?.sheetName
36+
)
37+
return data as PaginatedObjectContentProps[];
38+
39+
return [{ content: data, sheetName: "Sheet 1" }];
40+
};
41+
1742
const executeXLSX = (
1843
data: XLSX.CellObject[][],
1944
columnWidths?: number[],
@@ -31,11 +56,11 @@ const executeXLSX = (
3156
return ws;
3257
};
3358

34-
const exportFile = (
59+
function exportFile(
3560
exportAs: ExportationType,
3661
wb: XLSX.WorkBook,
3762
fileName: string
38-
) => {
63+
): Promise<string | ArrayBuffer | void> {
3964
if (exportAs.type === "base64") {
4065
return XLSX.write(wb, { type: "base64", bookType: "xlsx" });
4166
}
@@ -45,24 +70,39 @@ const exportFile = (
4570
}
4671

4772
if (exportAs.type === "download") {
48-
return XLSX.writeFile(wb, `${fileName}.xlsx`);
73+
XLSX.writeFile(wb, `${fileName}.xlsx`);
74+
return;
4975
}
5076

5177
if (exportAs.type === "filepath") {
52-
return XLSX.writeFile(wb, exportAs.path);
78+
XLSX.writeFile(wb, exportAs.path);
79+
return;
5380
}
5481

55-
return null;
56-
};
57-
58-
export const exportmeExcelAdvanced = ({
82+
return;
83+
}
84+
85+
export function exportmeExcelAdvanced(
86+
options: ExcelAdvancedOptionsBase64
87+
): Promise<string>;
88+
export function exportmeExcelAdvanced(
89+
options: ExcelAdvancedOptionsBuffer
90+
): Promise<ArrayBuffer>;
91+
export function exportmeExcelAdvanced(
92+
options: ExcelAdvancedOptionsDownload
93+
): Promise<void>;
94+
export function exportmeExcelAdvanced(
95+
options: ExcelAdvancedOptionsFilePath
96+
): Promise<void>;
97+
98+
export function exportmeExcelAdvanced({
5999
fileName,
60100
data,
61101
options,
62102
exportAs,
63103
merges,
64104
loggingMatrix,
65-
}: ExportMeExcelAdvancedProps) => {
105+
}: ExportMeExcelAdvancedProps): Promise<string | ArrayBuffer | void> {
66106
const {
67107
bodyStyle = {},
68108
columnWidths = [],
@@ -103,28 +143,30 @@ export const exportmeExcelAdvanced = ({
103143
console.info(`💡 Excel-Ent:Logging-Matrix: ${JSON.stringify(rowsAdapter)}`);
104144
}
105145

106-
return exportFile(exportAs, wb, fileName);
107-
};
108-
109-
const transformData = (
110-
data: Record<string, any>[]
111-
): PaginatedObjectContentProps[] => {
112-
if (
113-
!!data[0]?.content &&
114-
Array.isArray(data[0]?.content) &&
115-
!!data[0]?.sheetName
116-
)
117-
return data as PaginatedObjectContentProps[];
146+
if (exportAs.type === "filepath") {
147+
return exportFile(exportAs as ExportationTypeFilePath, wb, fileName);
148+
} else if (exportAs.type === "download") {
149+
return exportFile(exportAs as ExportationTypeDownload, wb, fileName);
150+
} else if (exportAs.type === "buffer") {
151+
return exportFile(exportAs as ExportationTypeBuffer, wb, fileName);
152+
} else {
153+
return exportFile(exportAs as ExportationTypeBase64, wb, fileName);
154+
}
155+
}
118156

119-
return [{ content: data, sheetName: "Sheet 1" }];
120-
};
157+
export function exportmeExcel(options: ExcelOptionsBase64): Promise<string>;
158+
export function exportmeExcel(
159+
options: ExcelOptionsBuffer
160+
): Promise<ArrayBuffer>;
161+
export function exportmeExcel(options: ExcelOptionsDownload): Promise<void>;
162+
export function exportmeExcel(options: ExcelOptionsFilePath): Promise<void>;
121163

122-
export const exportmeExcel = ({
164+
export function exportmeExcel({
123165
data,
124166
fileName,
125167
exportAs,
126168
options,
127-
}: ExportMeExcelProps) => {
169+
}: ExportMeExcelProps): Promise<string | ArrayBuffer | void> {
128170
const {
129171
bodyStyle = {},
130172
columnWidths = [],
@@ -150,6 +192,7 @@ export const exportmeExcel = ({
150192
(item: Record<string, any>, index: number) =>
151193
Object.keys(item).map((key) => {
152194
const isRowPainted = stripedRows && index % 2 === 0;
195+
153196
if (typeof item[key] === "number") {
154197
return {
155198
v: item[key],
@@ -187,5 +230,13 @@ export const exportmeExcel = ({
187230

188231
wb.Props = sheetProps;
189232

190-
return exportFile(exportAs, wb, fileName);
191-
};
233+
if (exportAs.type === "filepath") {
234+
return exportFile(exportAs as ExportationTypeFilePath, wb, fileName);
235+
} else if (exportAs.type === "download") {
236+
return exportFile(exportAs as ExportationTypeDownload, wb, fileName);
237+
} else if (exportAs.type === "buffer") {
238+
return exportFile(exportAs as ExportationTypeBuffer, wb, fileName);
239+
} else {
240+
return exportFile(exportAs as ExportationTypeBase64, wb, fileName);
241+
}
242+
}

src/types/functions.ts

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ export type ExportMeExcelOptions = {
1212
sheetProps?: XLSX.FullProperties;
1313
};
1414

15+
export type ExportationTypeBase64 = { type: "base64" };
16+
export type ExportationTypeBuffer = { type: "buffer" };
17+
export type ExportationTypeDownload = { type: "download" };
18+
export type ExportationTypeFilePath = { type: "filepath"; path: string };
19+
1520
export type ExportationType =
16-
| {
17-
type: "buffer" | "base64" | "download";
18-
}
19-
| {
20-
type: "filepath";
21-
path: string;
22-
};
21+
| ExportationTypeBase64
22+
| ExportationTypeBuffer
23+
| ExportationTypeDownload
24+
| ExportationTypeFilePath;
2325

2426
export type MergeProps = {
2527
start: { row: number; column: number };
@@ -41,3 +43,43 @@ export type ExportMeExcelProps = {
4143
exportAs: ExportationType;
4244
options?: ExportMeExcelOptions & { stripedRows?: boolean };
4345
};
46+
47+
type ExcelAdvancedOptions = {
48+
data: ExcelEntDataProps;
49+
fileName: string;
50+
merges?: MergeProps[];
51+
options?: ExportMeExcelOptions;
52+
loggingMatrix?: boolean;
53+
};
54+
55+
export type ExcelAdvancedOptionsBase64 = ExcelAdvancedOptions & {
56+
exportAs: ExportationTypeBase64;
57+
};
58+
export type ExcelAdvancedOptionsBuffer = ExcelAdvancedOptions & {
59+
exportAs: ExportationTypeBuffer;
60+
};
61+
export type ExcelAdvancedOptionsDownload = ExcelAdvancedOptions & {
62+
exportAs: ExportationTypeDownload;
63+
};
64+
export type ExcelAdvancedOptionsFilePath = ExcelAdvancedOptions & {
65+
exportAs: ExportationTypeFilePath;
66+
};
67+
68+
type ExcelProps = {
69+
data: Record<string, any>[] | PaginatedObjectContentProps[];
70+
fileName: string;
71+
options?: ExportMeExcelOptions & { stripedRows?: boolean };
72+
};
73+
74+
export type ExcelOptionsBase64 = ExcelProps & {
75+
exportAs: ExportationTypeBase64;
76+
};
77+
export type ExcelOptionsBuffer = ExcelProps & {
78+
exportAs: ExportationTypeBuffer;
79+
};
80+
export type ExcelOptionsDownload = ExcelProps & {
81+
exportAs: ExportationTypeDownload;
82+
};
83+
export type ExcelOptionsFilePath = ExcelProps & {
84+
exportAs: ExportationTypeFilePath;
85+
};

0 commit comments

Comments
 (0)