This repository was archived by the owner on Jan 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
85 lines (64 loc) · 1.96 KB
/
main.ts
File metadata and controls
85 lines (64 loc) · 1.96 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import express from 'express';
interface Result {
[hostname: string]: Array<string | { [key: string]: any[] }>;
}
interface JsonResponse {
items: {
fileUrl: string;
}[];
}
let result: Result = {};
function processURL(currentURL: string): void {
const parsedURL = new URL(currentURL);
// Init hostname
if (!result[parsedURL.hostname]) {
result[parsedURL.hostname] = [];
}
let currentLocation = result[parsedURL.hostname];
let pathParts = parsedURL.pathname.split('/').splice(1); // remove the first empty string created by split
for (let i = 0; i < pathParts.length - 1; i++) {
let currentPart = pathParts[i];
let existingObject = currentLocation.find(item =>
typeof item === 'object' &&
Object.keys(item)[0] === currentPart
) as { [key: string]: any[] } | undefined;
if (!existingObject) {
let newObject: { [key: string]: any[] } = {};
newObject[currentPart] = [];
currentLocation.push(newObject);
currentLocation = newObject[currentPart];
} else {
currentLocation = existingObject[currentPart];
}
}
// Add file
if (pathParts[pathParts.length - 1] !== '') {
currentLocation.push(pathParts[pathParts.length - 1]);
}
}
async function processAllURLs(url: string): Promise<void> {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('HTTP error: ' + response.status);
}
const jsonData: JsonResponse = await response.json();
jsonData.items.forEach(item => processURL(item.fileUrl));
} catch (e) {
console.log('Error: ' + e);
}
}
// Serve result at /api/files
const app = express();
const port = 3000;
app.get('/api/files', async (req, res) => {
try {
await processAllURLs('https://rest-test-eight.vercel.app/api/test');
res.json(result);
} catch (e) {
res.status(500).json({ error: 'URL processing failed: ' + e });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});