forked from domdomegg/airtable-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-projects-datasheet.js
More file actions
116 lines (90 loc) · 3.77 KB
/
test-projects-datasheet.js
File metadata and controls
116 lines (90 loc) · 3.77 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
#!/usr/bin/env node
// Get API key and space ID from environment variables
const apiKey = process.env.AITABLE_API_KEY || '';
const defaultSpaceId = process.env.SPACE || '';
if (!apiKey) {
console.error('ERROR: No API key found. Please set the AITABLE_API_KEY environment variable.');
process.exit(1);
}
// Helper function to display results nicely
function displayResults(title, data) {
console.log(`\n=== ${title} ===`);
console.log(JSON.stringify(data, null, 2));
console.log('='.repeat(40));
}
// Helper function to fetch from API
async function fetchFromAPI(endpoint, options = {}) {
const url = `https://aitable.ai/fusion/v1${endpoint}`;
console.log('Making API request to:', url);
const response = await fetch(url, {
...options,
headers: {
'Authorization': `Bearer ${apiKey}`,
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'aitable-mcp-server/0.1.0',
...options.headers,
},
});
if (!response.ok) {
const text = await response.text();
throw new Error(`API request failed with status ${response.status}: ${text}`);
}
return response.json();
}
async function testGetDatasheetRecords() {
try {
console.log('Testing access to datasheet records...');
// Step 1: Get datasheets to find the correct IDs
console.log('Step 1: Getting all datasheets...');
const spaceId = defaultSpaceId;
// Get nodes from the space
const nodesResponse = await fetchFromAPI(`/spaces/${spaceId}/nodes`);
if (!nodesResponse.success || !nodesResponse.data.nodes) {
console.error('No nodes found in the space');
return;
}
// Find all datasheets
const datasheets = nodesResponse.data.nodes.filter(node => node.type === 'Datasheet');
console.log(`\nFound ${datasheets.length} datasheets at root level in space ${spaceId}:`);
datasheets.forEach((ds, index) => {
console.log(`${index + 1}. ${ds.name} (ID: ${ds.id})`);
});
// Get the Projects datasheet or the first available one
const projectsDatasheet = datasheets.find(ds => ds.name === 'Projects') || datasheets[0];
if (!projectsDatasheet) {
console.error('ERROR: No datasheets found in this space.');
return;
}
console.log(`\nUsing datasheet: ${projectsDatasheet.name} (ID: ${projectsDatasheet.id})`);
// Step 2: Get records from the datasheet using its ID
console.log(`\nStep 2: Getting records from datasheet...`);
const recordsResponse = await fetchFromAPI(`/datasheets/${projectsDatasheet.id}/records?pageSize=10`);
if (!recordsResponse.success) {
console.error('Failed to get records:', recordsResponse.message);
return;
}
const records = recordsResponse.data.records.map(record => ({
id: record.recordId,
fields: record.fields
}));
// Display the records
console.log(`\nRetrieved ${records.length} records from "${projectsDatasheet.name}" datasheet:`);
displayResults(`${projectsDatasheet.name} Records`, records);
// Step 3: Get fields for the datasheet
console.log(`\nStep 3: Getting fields from datasheet...`);
const fieldsResponse = await fetchFromAPI(`/datasheets/${projectsDatasheet.id}/fields`);
if (!fieldsResponse.success) {
console.error('Failed to get fields:', fieldsResponse.message);
return;
}
// Display the fields
console.log(`\nRetrieved ${fieldsResponse.data.fields.length} fields from "${projectsDatasheet.name}" datasheet:`);
displayResults(`${projectsDatasheet.name} Fields`, fieldsResponse.data.fields);
console.log('\n✅ Test completed successfully');
} catch (error) {
console.error('Error testing datasheet access:', error);
}
}
// Run the test
testGetDatasheetRecords();