forked from domdomegg/airtable-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-datasheets.js
More file actions
165 lines (135 loc) · 5.27 KB
/
test-datasheets.js
File metadata and controls
165 lines (135 loc) · 5.27 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import 'dotenv/config';
import fetch from 'node-fetch';
// Get API key and space from environment
const apiKey = process.env.AITABLE_API_KEY;
const defaultSpaceId = process.env.SPACE;
if (!apiKey) {
console.error('Error: AITABLE_API_KEY environment variable is required');
process.exit(1);
}
console.log(`Using API Key: ${apiKey.substring(0, 5)}...${apiKey.substring(apiKey.length - 3)}`);
if (defaultSpaceId) {
console.log(`Using default Space ID: ${defaultSpaceId}`);
}
// Track all found datasheets
const allDatasheets = [];
// Helper function to display results
const displayResults = (name, data) => {
console.log(`\n----- ${name} -----`);
console.log(JSON.stringify(data, null, 2));
console.log('-'.repeat(50));
};
// Helper function to perform API requests
async function fetchFromAPI(endpoint) {
const response = await fetch(`https://aitable.ai/fusion/v1/${endpoint}`, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
console.error(`API request failed with status: ${response.status}`);
const text = await response.text();
console.error('Response text:', text.substring(0, 500));
throw new Error(`API request failed with status ${response.status}`);
}
return response.json();
}
// Recursive function to process a folder node and find datasheets
async function processNode(spaceId, nodeId, nodePath = '') {
try {
// Get node details
const nodeDetails = await fetchFromAPI(`spaces/${spaceId}/nodes/${nodeId}`);
if (!nodeDetails.success || !nodeDetails.data) {
console.error(`Error fetching node details for ${nodeId}`);
return;
}
const node = nodeDetails.data;
const currentPath = nodePath ? `${nodePath} > ${node.name}` : node.name;
if (node.type === 'Datasheet') {
console.log(`Found datasheet: ${currentPath} (ID: ${node.id})`);
allDatasheets.push({
id: node.id,
name: node.name,
path: currentPath,
spaceId: spaceId
});
return;
}
// If it's a folder and has children, process each child
if (node.type === 'Folder' && node.children && node.children.length > 0) {
console.log(`Processing folder: ${currentPath} (${node.children.length} children)`);
for (const childNode of node.children) {
await processNode(spaceId, childNode.id, currentPath);
}
}
} catch (error) {
console.error(`Error processing node ${nodeId}:`, error.message);
}
}
// Process all nodes in a space to find datasheets
async function findDatasheetsInSpace(spaceId, spaceName) {
try {
console.log(`\n===== Processing Space: ${spaceName} (ID: ${spaceId}) =====`);
// Get top-level nodes in the space
const nodesData = await fetchFromAPI(`spaces/${spaceId}/nodes`);
if (!nodesData.success || !nodesData.data || !nodesData.data.nodes) {
console.error(`No nodes found in space ${spaceId}`);
return;
}
// First, directly check for datasheets at the root level
const rootDatasheets = nodesData.data.nodes.filter(node => node.type === 'Datasheet');
for (const datasheet of rootDatasheets) {
console.log(`Found root datasheet: ${datasheet.name} (ID: ${datasheet.id})`);
allDatasheets.push({
id: datasheet.id,
name: datasheet.name,
path: datasheet.name,
spaceId: spaceId
});
}
// Then process all folders to find nested datasheets
const folders = nodesData.data.nodes.filter(node => node.type === 'Folder');
console.log(`Processing ${folders.length} folders in space ${spaceName}`);
for (const folder of folders) {
await processNode(spaceId, folder.id, '');
}
} catch (error) {
console.error(`Error finding datasheets in space ${spaceId}:`, error.message);
}
}
async function testGetDatasheets() {
try {
console.log('Testing AITable API access for datasheets...');
// If default space is provided, just use that
if (defaultSpaceId) {
const spacesData = await fetchFromAPI('spaces');
const spaceName = spacesData.data.spaces.find(space => space.id === defaultSpaceId)?.name || 'Default Space';
await findDatasheetsInSpace(defaultSpaceId, spaceName);
} else {
// Otherwise, get all spaces and check each one
const spacesData = await fetchFromAPI('spaces');
displayResults('Spaces Found', spacesData);
// Check if we have spaces
if (!spacesData.success || !spacesData.data || !spacesData.data.spaces || spacesData.data.spaces.length === 0) {
console.log('No spaces found. Cannot retrieve datasheets.');
return;
}
// Process each space
for (const space of spacesData.data.spaces) {
await findDatasheetsInSpace(space.id, space.name);
}
}
// Print summary of all datasheets found
console.log('\n===== All Datasheets Found =====');
console.log(`Total: ${allDatasheets.length} datasheets`);
allDatasheets.forEach((ds, index) => {
console.log(`${index + 1}. ${ds.path} (ID: ${ds.id})`);
});
console.log('\n✅ AITable datasheet retrieval test completed');
} catch (error) {
console.error('Error testing AITable API:', error);
}
}
// Run the test
testGetDatasheets();