this repo has no description
www.baileykane.co/
1import Papa from "papaparse";
2import type { ParseResult } from "papaparse";
3
4/**Fetch the CSV data from the provided Google Sheets URL. */
5async function getSheetsCSV(url: URL): Promise<string> {
6 const response = await fetch(url);
7
8 if (!response.ok) {
9 throw new Error(`Failed to fetch data from ${url}`);
10 }
11
12 const csv = await response.text();
13 return csv;
14}
15
16/**Parse the provided CSV data into an array of JSON objects. */
17function parseCSV(csvData: string): ParseResult<any> {
18 return Papa.parse(csvData, { header: true });
19}
20
21/**Convenience function to fetch CSV data from the provided Google Sheets URL
22 * and parse it into an array of JSON objects, all in one.
23 *
24 * Combines getSheetsCSV and parseCSV.
25 */
26export async function importCSVDataAsJson(
27 url: string
28): Promise<ParseResult<any>> {
29 const urlObj = new URL(url);
30
31 const csv = await getSheetsCSV(urlObj);
32 const json = parseCSV(csv);
33 return json;
34}