( buffer: Buffer, fileName: string, contentType?: string )
| 493 | * the MIME content type. Throws on unsupported formats so callers fail fast. |
| 494 | */ |
| 495 | export async function parseFileRows( |
| 496 | buffer: Buffer, |
| 497 | fileName: string, |
| 498 | contentType?: string |
| 499 | ): Promise<{ headers: string[]; rows: Record<string, unknown>[] }> { |
| 500 | const ext = fileName.split('.').pop()?.toLowerCase() |
| 501 | if (ext === 'json' || contentType === 'application/json') { |
| 502 | return parseJsonRows(buffer) |
| 503 | } |
| 504 | if (ext === 'csv' || ext === 'tsv' || contentType === 'text/csv') { |
| 505 | const delimiter = ext === 'tsv' ? '\t' : ',' |
| 506 | return parseCsvBuffer(buffer, delimiter) |
| 507 | } |
| 508 | throw new Error(`Unsupported file format: "${ext ?? fileName}". Supported: csv, tsv, json`) |
| 509 | } |
no test coverage detected