-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathhandles.ts
59 lines (53 loc) · 1.43 KB
/
handles.ts
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
import fs, { readFileSync, readdirSync } from "fs";
const readAndParseJSONFile = (filePath: string) => {
const buffer = readFileSync(filePath);
return JSON.parse(buffer.toString());
};
const getImageUrl = (fileData: any) => {
if (fileData.image) {
const imagePath = fileData.image.slice(2);
return `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`;
}
return null;
};
const getHandles = () => {
const allHandles: {
handle: string;
link: string;
moralisId: string;
imageUrl: string;
members: string[];
}[] = [];
const files = readdirSync(`./_data/handles`);
for (const file of files) {
if (file.endsWith(".json")) {
const fileData = readAndParseJSONFile(`./_data/handles/${file}`);
const imageUrl = getImageUrl(fileData);
allHandles.push({ ...fileData, imageUrl });
}
}
return allHandles;
};
exports.handler = async (event) => {
// only allow GET
if (event.httpMethod !== "GET") {
return {
statusCode: 405,
body: "Method not allowed",
headers: { Allow: "GET" },
};
}
try {
// handle
const allHandles = getHandles();
return {
statusCode: 200,
body: JSON.stringify(allHandles),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.toString(), details: error.stack }),
};
}
};