-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
115 lines (103 loc) · 3.83 KB
/
api.js
File metadata and controls
115 lines (103 loc) · 3.83 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
async function sendRequest(config) {
const { method, url, headers, body, onStatusUpdate } = config;
if (!url) {
throw new Error('Please enter a URL');
}
const options = {
method,
headers
};
if (['POST', 'PUT', 'PATCH'].includes(method) && body) {
options.body = body;
if (!headers['Content-Type']) {
headers['Content-Type'] = 'application/json';
}
}
try {
onStatusUpdate('Loading...');
const startTime = Date.now();
const response = await fetch(url, options);
const endTime = Date.now();
const responseData = await response.json().catch(async () => {
return await response.text();
});
const statusText = `${response.status} ${response.statusText}`;
onStatusUpdate(`Status: ${statusText} • Time: ${endTime - startTime}ms • Size: ${formatBytes(JSON.stringify(responseData).length)}`);
return {
status: statusText,
body: responseData,
headers: Array.from(response.headers.entries()),
time: endTime - startTime
};
} catch (error) {
onStatusUpdate('Error');
throw error;
}
}
function handleAuth(headers, authConfig) {
const { type, credentials } = authConfig;
switch(type) {
case 'basic':
if (credentials.username && credentials.password) {
headers['Authorization'] = `Basic ${btoa(`${credentials.username}:${credentials.password}`)}`;
}
break;
case 'bearer':
if (credentials.token) {
headers['Authorization'] = `Bearer ${credentials.token}`;
}
break;
case 'api-key':
if (credentials.key && credentials.value) {
if (credentials.location === 'header') {
headers[credentials.key] = credentials.value;
} else {
return { key: credentials.key, value: credentials.value };
}
}
break;
}
return null;
}
async function executeFlow(flow, config) {
const results = [];
for (const node of flow.nodes) {
if (node.type === 'request') {
try {
const response = await sendRequest({
...node.data,
headers: node.data.headers || {},
onStatusUpdate: config.onStatusUpdate
});
results.push({ nodeId: node.id, response });
// Check conditions for connected nodes
const connections = flow.connections.filter(c => c.from === node.id);
for (const conn of connections) {
if (conn.condition) {
if (evaluateCondition(conn.condition, response)) {
const nextNode = flow.nodes.find(n => n.id === conn.to);
if (nextNode) {
// Recursive call for next node
const subResults = await executeFlow({
nodes: [nextNode],
connections: flow.connections
}, config);
results.push(...subResults);
}
}
}
}
} catch (error) {
results.push({ nodeId: node.id, error: error.message });
}
}
}
return results;
}
function evaluateCondition(condition, response) {
// Simple condition evaluation (e.g., status code check)
if (condition.type === 'status') {
return response.status.split(' ')[0] === condition.value;
}
return true;
}