forked from domdomegg/airtable-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-read.js
More file actions
145 lines (120 loc) · 5.65 KB
/
Copy pathtest-read.js
File metadata and controls
145 lines (120 loc) · 5.65 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
import 'dotenv/config';
import fetch from 'node-fetch';
// Get API key from environment
const apiKey = process.env.AITABLE_API_KEY;
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)}`);
// Helper function to display results
const displayResults = (name, data) => {
console.log(`\n----- ${name} -----`);
console.log(JSON.stringify(data, null, 2));
console.log('-'.repeat(50));
};
async function testAITableAPI() {
try {
// Test direct API access first
console.log('Testing direct API access to AITable...');
// Get spaces (according to documentation)
const spacesResponse = await fetch('https://aitable.ai/fusion/v1/spaces', {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (!spacesResponse.ok) {
console.error(`Spaces API test failed with status: ${spacesResponse.status}`);
const text = await spacesResponse.text();
console.error('Response text:', text.substring(0, 500) + '...');
throw new Error(`API request failed with status ${spacesResponse.status}`);
}
const spacesData = await spacesResponse.json();
displayResults('Spaces API Result', spacesData);
// If we have spaces, try to get datasheets for the first space
if (spacesData.success && spacesData.data && spacesData.data.spaces && spacesData.data.spaces.length > 0) {
const spaceId = spacesData.data.spaces[0].id;
console.log(`Using Space ID: ${spaceId}`);
// Get nodes/datasheets in this space
const nodesResponse = await fetch(`https://aitable.ai/fusion/v1/spaces/${spaceId}/nodes?type=Datasheet`, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (!nodesResponse.ok) {
console.error(`Nodes API test failed with status: ${nodesResponse.status}`);
const text = await nodesResponse.text();
console.error('Response text:', text.substring(0, 500) + '...');
throw new Error(`API request failed with status ${nodesResponse.status}`);
}
const nodesData = await nodesResponse.json();
displayResults('Nodes/Datasheets API Result', nodesData);
// If we have datasheets, try to get records from the first one
if (nodesData.success && nodesData.data && nodesData.data.nodes && nodesData.data.nodes.length > 0) {
// Find the first datasheet node
const datasheetNode = nodesData.data.nodes.find(node => node.type === 'Datasheet');
if (datasheetNode) {
const datasheetId = datasheetNode.id;
console.log(`Using Datasheet ID: ${datasheetId}`);
// Get records from this datasheet
const recordsResponse = await fetch(`https://aitable.ai/fusion/v1/datasheets/${datasheetId}/records`, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (!recordsResponse.ok) {
console.error(`Records API test failed with status: ${recordsResponse.status}`);
const text = await recordsResponse.text();
console.error('Response text:', text.substring(0, 500) + '...');
throw new Error(`API request failed with status ${recordsResponse.status}`);
}
const recordsData = await recordsResponse.json();
displayResults('Records API Result', recordsData);
// Get fields from this datasheet
const fieldsResponse = await fetch(`https://aitable.ai/fusion/v1/datasheets/${datasheetId}/fields`, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (!fieldsResponse.ok) {
console.error(`Fields API test failed with status: ${fieldsResponse.status}`);
const text = await fieldsResponse.text();
console.error('Response text:', text.substring(0, 500) + '...');
throw new Error(`API request failed with status ${fieldsResponse.status}`);
}
const fieldsData = await fieldsResponse.json();
displayResults('Fields API Result', fieldsData);
// Get views from this datasheet
const viewsResponse = await fetch(`https://aitable.ai/fusion/v1/datasheets/${datasheetId}/views`, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (!viewsResponse.ok) {
console.error(`Views API test failed with status: ${viewsResponse.status}`);
const text = await viewsResponse.text();
console.error('Response text:', text.substring(0, 500) + '...');
throw new Error(`API request failed with status ${viewsResponse.status}`);
}
const viewsData = await viewsResponse.json();
displayResults('Views API Result', viewsData);
} else {
console.log('No datasheets found in the nodes list. Cannot test datasheet-related functions.');
}
} else {
console.log('No nodes found in this space. Cannot test datasheet-related functions.');
}
} else {
console.log('No spaces found. Cannot test further functions.');
}
} catch (error) {
console.error('Error testing AITable API:', error);
}
}
// Run the tests
testAITableAPI();