1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60import { db, event, graphqlQuery, validateShortId } from "#src/utils";
//
// Fetches a remote CSV, checks for basic validity, and returns the parsed data or an error
//
interface urlCsv {
url: string;
type: string;
}
export const fetchAndValidateRemoteCsv = async () => {
const csvUrl = (event.queryStringParameters as any).url;
try {
if(!csvUrl) throw new Error(`Invalid CSV URL: ${csvUrl}`)
const response = await fetch(csvUrl);
if (!response.ok) {
throw new Error(`Error fetching CSV: ${response.statusText}`);
}
const text = await response.text();
const rows = text
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0);
const parsedData: urlCsv[] = [];
for (const [index, row] of rows.entries()) {
const columns = row.split(",").map((col) => col.trim());
if (columns.length !== 2) {
throw new Error(
`Invalid format at line ${index + 1}: Expected "url, type" but found ${columns.length} columns.`,
);
}
const [url, type] = columns;
try {
new URL(url);
} catch {
throw new Error(`Invalid URL at line ${index + 1}: "${url}"`);
}
parsedData.push({ url, type });
}
return { success: true, url: csvUrl, data: parsedData };
} catch (error) {
return {
success: false,
url: csvUrl,
error:
error instanceof Error ? error.toString() : new Error("An unknown error occurred").toString(),
};
}
};