easy-airtable-api 0.0.14 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +79 -0
- package/README.md +252 -74
- package/lib/cli.d.ts +2 -0
- package/lib/cli.js +82 -0
- package/lib/codegen/build.d.ts +2 -0
- package/lib/codegen/build.js +25 -0
- package/lib/codegen/emit-typescript.d.ts +8 -0
- package/lib/codegen/emit-typescript.js +104 -0
- package/lib/codegen/generate.d.ts +2 -0
- package/lib/codegen/generate.js +23 -0
- package/lib/codegen/identifiers.d.ts +7 -0
- package/lib/codegen/identifiers.js +51 -0
- package/lib/codegen/index.d.ts +3 -0
- package/lib/codegen/index.js +2 -0
- package/lib/codegen/load-schema.d.ts +3 -0
- package/lib/codegen/load-schema.js +28 -0
- package/lib/codegen/map-field.d.ts +31 -0
- package/lib/codegen/map-field.js +278 -0
- package/lib/codegen/normalize-schema.d.ts +11 -0
- package/lib/codegen/normalize-schema.js +61 -0
- package/lib/codegen/types.d.ts +62 -0
- package/lib/codegen/types.js +1 -0
- package/lib/codegen/utils.d.ts +14 -0
- package/lib/codegen/utils.js +30 -0
- package/lib/codegen/write-atomic.d.ts +1 -0
- package/lib/codegen/write-atomic.js +11 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.js +1 -1
- package/lib/records.d.ts +96 -0
- package/lib/records.js +211 -0
- package/lib/requests.d.ts +8 -89
- package/lib/requests.js +13 -215
- package/lib/types/fields.d.ts +8 -8
- package/lib/types/metadata.d.ts +24 -0
- package/lib/types/metadata.js +1 -0
- package/lib/types/records.d.ts +1 -4
- package/lib/utils.d.ts +2 -2
- package/lib/utils.js +2 -0
- package/package.json +14 -6
package/lib/records.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { AirtableRecord, DeleteRecordsResponse, GetRecordsQueryParameters } from './types/records.js';
|
|
2
|
+
export declare function getRecord<Fields>({ apiKey, baseId, tableId, recordId, }: {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
baseId: string;
|
|
5
|
+
tableId: string;
|
|
6
|
+
recordId: string;
|
|
7
|
+
}): Promise<AirtableRecord<Fields>>;
|
|
8
|
+
export declare function getRecords<Fields>({ apiKey, baseId, tableId, options, }: {
|
|
9
|
+
apiKey: string;
|
|
10
|
+
baseId: string;
|
|
11
|
+
tableId: string;
|
|
12
|
+
options?: GetRecordsQueryParameters;
|
|
13
|
+
}): Promise<AirtableRecord<Fields>[]>;
|
|
14
|
+
export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, options, }: {
|
|
15
|
+
apiKey: string;
|
|
16
|
+
baseId: string;
|
|
17
|
+
tableId: string;
|
|
18
|
+
recordId: string;
|
|
19
|
+
fields: Fields;
|
|
20
|
+
options?: {
|
|
21
|
+
typecast?: boolean;
|
|
22
|
+
returnFieldsByFieldId?: boolean;
|
|
23
|
+
overwriteFieldsNotSpecified?: boolean;
|
|
24
|
+
};
|
|
25
|
+
}): Promise<AirtableRecord<Fields>>;
|
|
26
|
+
export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
27
|
+
apiKey: string;
|
|
28
|
+
baseId: string;
|
|
29
|
+
tableId: string;
|
|
30
|
+
records: {
|
|
31
|
+
id: string;
|
|
32
|
+
fields: Fields;
|
|
33
|
+
}[];
|
|
34
|
+
options?: {
|
|
35
|
+
typecast?: boolean;
|
|
36
|
+
returnFieldsByFieldId?: boolean;
|
|
37
|
+
overwriteFieldsNotSpecified?: boolean;
|
|
38
|
+
requestInterval?: number;
|
|
39
|
+
};
|
|
40
|
+
}): Promise<{
|
|
41
|
+
records: AirtableRecord<Fields>[];
|
|
42
|
+
}>;
|
|
43
|
+
export declare function updateRecordsUpsert<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
44
|
+
apiKey: string;
|
|
45
|
+
baseId: string;
|
|
46
|
+
tableId: string;
|
|
47
|
+
records: {
|
|
48
|
+
id?: string;
|
|
49
|
+
fields: Fields;
|
|
50
|
+
}[];
|
|
51
|
+
options?: {
|
|
52
|
+
fieldsToMergeOn: string[];
|
|
53
|
+
typecast?: boolean;
|
|
54
|
+
returnFieldsByFieldId?: boolean;
|
|
55
|
+
overwriteFieldsNotSpecified?: boolean;
|
|
56
|
+
requestInterval?: number;
|
|
57
|
+
};
|
|
58
|
+
}): Promise<{
|
|
59
|
+
createdRecords: string[];
|
|
60
|
+
updatedRecords: string[];
|
|
61
|
+
records: AirtableRecord<Fields>[];
|
|
62
|
+
}>;
|
|
63
|
+
export declare function deleteRecords({ apiKey, baseId, tableId, recordIds, options, }: {
|
|
64
|
+
apiKey: string;
|
|
65
|
+
baseId: string;
|
|
66
|
+
tableId: string;
|
|
67
|
+
recordIds: string[];
|
|
68
|
+
options?: {
|
|
69
|
+
requestInterval?: number;
|
|
70
|
+
};
|
|
71
|
+
}): Promise<DeleteRecordsResponse>;
|
|
72
|
+
export declare function createRecord<Fields>({ apiKey, baseId, tableId, fields, options, }: {
|
|
73
|
+
apiKey: string;
|
|
74
|
+
baseId: string;
|
|
75
|
+
tableId: string;
|
|
76
|
+
fields: Fields;
|
|
77
|
+
options?: {
|
|
78
|
+
typecast?: boolean;
|
|
79
|
+
returnFieldsByFieldId?: boolean;
|
|
80
|
+
};
|
|
81
|
+
}): Promise<AirtableRecord<Fields>>;
|
|
82
|
+
export declare function createRecords<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
83
|
+
apiKey: string;
|
|
84
|
+
baseId: string;
|
|
85
|
+
tableId: string;
|
|
86
|
+
records: {
|
|
87
|
+
fields: Fields;
|
|
88
|
+
}[];
|
|
89
|
+
options?: {
|
|
90
|
+
typecast?: boolean;
|
|
91
|
+
returnFieldsByFieldId?: boolean;
|
|
92
|
+
requestInterval?: number;
|
|
93
|
+
};
|
|
94
|
+
}): Promise<{
|
|
95
|
+
records: AirtableRecord<Fields>[];
|
|
96
|
+
}>;
|
package/lib/records.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { airtableRequest } from './requests.js';
|
|
2
|
+
import { delay } from './utils.js';
|
|
3
|
+
export async function getRecord({ apiKey, baseId, tableId, recordId, }) {
|
|
4
|
+
return await airtableRequest({
|
|
5
|
+
apiKey,
|
|
6
|
+
baseId,
|
|
7
|
+
tableId,
|
|
8
|
+
endpoint: `/${recordId}`,
|
|
9
|
+
method: 'GET',
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export async function getRecords({ apiKey, baseId, tableId, options, }) {
|
|
13
|
+
validateGetRecordsOptions(options);
|
|
14
|
+
let records = [];
|
|
15
|
+
let currentOffset;
|
|
16
|
+
do {
|
|
17
|
+
const requestBody = currentOffset
|
|
18
|
+
? { ...options, offset: currentOffset }
|
|
19
|
+
: options;
|
|
20
|
+
const response = await airtableRequest({
|
|
21
|
+
apiKey,
|
|
22
|
+
baseId,
|
|
23
|
+
tableId,
|
|
24
|
+
endpoint: '/listRecords',
|
|
25
|
+
method: 'POST',
|
|
26
|
+
body: requestBody,
|
|
27
|
+
});
|
|
28
|
+
records = records.concat(response.records);
|
|
29
|
+
currentOffset = response.offset;
|
|
30
|
+
if (currentOffset && (options?.maxRecords ?? 100) > 100) {
|
|
31
|
+
const interval = options?.requestInterval || 500;
|
|
32
|
+
await delay(interval);
|
|
33
|
+
}
|
|
34
|
+
} while (currentOffset);
|
|
35
|
+
return records;
|
|
36
|
+
}
|
|
37
|
+
function validateGetRecordsOptions(options) {
|
|
38
|
+
if (!options)
|
|
39
|
+
return;
|
|
40
|
+
if (options.cellFormat === 'string') {
|
|
41
|
+
if (!options.timeZone || !options.userLocale) {
|
|
42
|
+
throw new Error('The timeZone and userLocale parameters are required when using string as the cellFormat.');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!options.maxRecords) {
|
|
46
|
+
options.maxRecords = 100;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export async function updateRecord({ apiKey, baseId, tableId, recordId, fields, options, }) {
|
|
50
|
+
return await airtableRequest({
|
|
51
|
+
apiKey,
|
|
52
|
+
baseId,
|
|
53
|
+
tableId,
|
|
54
|
+
endpoint: `/${recordId}`,
|
|
55
|
+
method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
|
|
56
|
+
body: {
|
|
57
|
+
fields,
|
|
58
|
+
typecast: options?.typecast === true ? true : false,
|
|
59
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId === true ? true : false,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
export async function updateRecords({ apiKey, baseId, tableId, records, options, }) {
|
|
64
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
65
|
+
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
66
|
+
}
|
|
67
|
+
const chunkSize = 10;
|
|
68
|
+
const chunks = [];
|
|
69
|
+
for (let i = 0; i < records.length; i += chunkSize) {
|
|
70
|
+
chunks.push(records.slice(i, i + chunkSize));
|
|
71
|
+
}
|
|
72
|
+
let combinedResults = [];
|
|
73
|
+
for (const chunk of chunks) {
|
|
74
|
+
const result = await airtableRequest({
|
|
75
|
+
apiKey,
|
|
76
|
+
baseId,
|
|
77
|
+
tableId,
|
|
78
|
+
endpoint: '/',
|
|
79
|
+
method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
|
|
80
|
+
body: {
|
|
81
|
+
records: chunk,
|
|
82
|
+
typecast: options?.typecast ?? false,
|
|
83
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
combinedResults = combinedResults.concat(result.records);
|
|
87
|
+
if (records.length > chunkSize &&
|
|
88
|
+
chunks.indexOf(chunk) < chunks.length - 1) {
|
|
89
|
+
const interval = options?.requestInterval || 500;
|
|
90
|
+
await delay(interval);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return { records: combinedResults };
|
|
94
|
+
}
|
|
95
|
+
export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, options, }) {
|
|
96
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
97
|
+
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
98
|
+
}
|
|
99
|
+
if (!Array.isArray(options?.fieldsToMergeOn) ||
|
|
100
|
+
options?.fieldsToMergeOn.length === 0) {
|
|
101
|
+
throw new Error('fieldsToMergeOn must be an array of strings.');
|
|
102
|
+
}
|
|
103
|
+
const chunkSize = 10;
|
|
104
|
+
const chunks = [];
|
|
105
|
+
for (let i = 0; i < records.length; i += chunkSize) {
|
|
106
|
+
chunks.push(records.slice(i, i + chunkSize));
|
|
107
|
+
}
|
|
108
|
+
let allCreatedRecords = [];
|
|
109
|
+
let allUpdatedRecords = [];
|
|
110
|
+
let allRecords = [];
|
|
111
|
+
for (const chunk of chunks) {
|
|
112
|
+
const result = await airtableRequest({
|
|
113
|
+
apiKey,
|
|
114
|
+
baseId,
|
|
115
|
+
tableId,
|
|
116
|
+
endpoint: '/',
|
|
117
|
+
method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
|
|
118
|
+
body: {
|
|
119
|
+
records: chunk,
|
|
120
|
+
typecast: options?.typecast ?? false,
|
|
121
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
122
|
+
performUpsert: { fieldsToMergeOn: options.fieldsToMergeOn },
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
allCreatedRecords = allCreatedRecords.concat(result.createdRecords);
|
|
126
|
+
allUpdatedRecords = allUpdatedRecords.concat(result.updatedRecords);
|
|
127
|
+
allRecords = allRecords.concat(result.records);
|
|
128
|
+
if (records.length > chunkSize && chunks.indexOf(chunk) < chunks.length - 1) {
|
|
129
|
+
const interval = options?.requestInterval || 500;
|
|
130
|
+
await delay(interval);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
createdRecords: allCreatedRecords,
|
|
135
|
+
updatedRecords: allUpdatedRecords,
|
|
136
|
+
records: allRecords,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
export async function deleteRecords({ apiKey, baseId, tableId, recordIds, options, }) {
|
|
140
|
+
if (!Array.isArray(recordIds) || recordIds.length === 0) {
|
|
141
|
+
throw new Error('The record ids array is empty or not provided. Please provide a non-empty array of record ids to delete the records.');
|
|
142
|
+
}
|
|
143
|
+
const chunkSize = 10;
|
|
144
|
+
const chunks = [];
|
|
145
|
+
for (let i = 0; i < recordIds.length; i += chunkSize) {
|
|
146
|
+
chunks.push(recordIds.slice(i, i + chunkSize));
|
|
147
|
+
}
|
|
148
|
+
let combinedResults = [];
|
|
149
|
+
for (const chunk of chunks) {
|
|
150
|
+
const query = chunk.map((id) => `records[]=${id}`).join('&');
|
|
151
|
+
const result = await airtableRequest({
|
|
152
|
+
apiKey,
|
|
153
|
+
baseId,
|
|
154
|
+
tableId,
|
|
155
|
+
endpoint: `?${query}`,
|
|
156
|
+
method: 'DELETE',
|
|
157
|
+
});
|
|
158
|
+
combinedResults = combinedResults.concat(result.records);
|
|
159
|
+
if (recordIds.length > chunkSize && chunks.indexOf(chunk) < chunks.length - 1) {
|
|
160
|
+
const interval = options?.requestInterval || 500;
|
|
161
|
+
await delay(interval);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return { records: combinedResults };
|
|
165
|
+
}
|
|
166
|
+
export async function createRecord({ apiKey, baseId, tableId, fields, options, }) {
|
|
167
|
+
return await airtableRequest({
|
|
168
|
+
apiKey,
|
|
169
|
+
baseId,
|
|
170
|
+
tableId,
|
|
171
|
+
endpoint: '',
|
|
172
|
+
method: 'POST',
|
|
173
|
+
body: {
|
|
174
|
+
fields,
|
|
175
|
+
typecast: options?.typecast ?? false,
|
|
176
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
export async function createRecords({ apiKey, baseId, tableId, records, options, }) {
|
|
181
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
182
|
+
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to create.');
|
|
183
|
+
}
|
|
184
|
+
const chunkSize = 10;
|
|
185
|
+
const chunks = [];
|
|
186
|
+
for (let i = 0; i < records.length; i += chunkSize) {
|
|
187
|
+
chunks.push(records.slice(i, i + chunkSize));
|
|
188
|
+
}
|
|
189
|
+
let combinedResults = [];
|
|
190
|
+
for (const chunk of chunks) {
|
|
191
|
+
const result = await airtableRequest({
|
|
192
|
+
apiKey,
|
|
193
|
+
baseId,
|
|
194
|
+
tableId,
|
|
195
|
+
endpoint: '',
|
|
196
|
+
method: 'POST',
|
|
197
|
+
body: {
|
|
198
|
+
records: chunk,
|
|
199
|
+
typecast: options?.typecast ?? false,
|
|
200
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
combinedResults = combinedResults.concat(result.records);
|
|
204
|
+
if (records.length > chunkSize &&
|
|
205
|
+
chunks.indexOf(chunk) < chunks.length - 1) {
|
|
206
|
+
const interval = options?.requestInterval || 500;
|
|
207
|
+
await delay(interval);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return { records: combinedResults };
|
|
211
|
+
}
|
package/lib/requests.d.ts
CHANGED
|
@@ -1,96 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { RequestMethods } from './types/tables.js';
|
|
2
|
+
import type { AirtableBaseSchema } from './types/metadata.js';
|
|
3
|
+
export declare function airtableRequest<T>(request: {
|
|
3
4
|
apiKey: string;
|
|
4
5
|
baseId: string;
|
|
5
6
|
tableId: string;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
tableId: string;
|
|
12
|
-
options?: GetRecordsQueryParameters;
|
|
13
|
-
}): Promise<AirtableRecord<Fields>[]>;
|
|
14
|
-
export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, options, }: {
|
|
15
|
-
apiKey: string;
|
|
16
|
-
baseId: string;
|
|
17
|
-
tableId: string;
|
|
18
|
-
recordId: string;
|
|
19
|
-
fields: Fields;
|
|
20
|
-
options?: {
|
|
21
|
-
typecast?: boolean;
|
|
22
|
-
returnFieldsByFieldId?: boolean;
|
|
23
|
-
overwriteFieldsNotSpecified?: boolean;
|
|
24
|
-
};
|
|
25
|
-
}): Promise<AirtableRecord<Fields>>;
|
|
26
|
-
export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
27
|
-
apiKey: string;
|
|
28
|
-
baseId: string;
|
|
29
|
-
tableId: string;
|
|
30
|
-
records: {
|
|
31
|
-
id: string;
|
|
32
|
-
fields: Fields;
|
|
33
|
-
}[];
|
|
34
|
-
options?: {
|
|
35
|
-
typecast?: boolean;
|
|
36
|
-
returnFieldsByFieldId?: boolean;
|
|
37
|
-
overwriteFieldsNotSpecified?: boolean;
|
|
38
|
-
requestInterval?: number;
|
|
39
|
-
};
|
|
40
|
-
}): Promise<{
|
|
41
|
-
records: AirtableRecord<Fields>[];
|
|
42
|
-
}>;
|
|
43
|
-
export declare function updateRecordsUpsert<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
44
|
-
apiKey: string;
|
|
45
|
-
baseId: string;
|
|
46
|
-
tableId: string;
|
|
47
|
-
records: {
|
|
48
|
-
id?: string;
|
|
49
|
-
fields: Fields;
|
|
50
|
-
}[];
|
|
51
|
-
options?: {
|
|
52
|
-
fieldsToMergeOn: string[];
|
|
53
|
-
typecast?: boolean;
|
|
54
|
-
returnFieldsByFieldId?: boolean;
|
|
55
|
-
overwriteFieldsNotSpecified?: boolean;
|
|
56
|
-
requestInterval?: number;
|
|
57
|
-
};
|
|
58
|
-
}): Promise<{
|
|
59
|
-
createdRecords: string[];
|
|
60
|
-
updatedRecords: string[];
|
|
61
|
-
records: AirtableRecord<Fields>[];
|
|
62
|
-
}>;
|
|
63
|
-
export declare function deleteRecords({ apiKey, baseId, tableId, recordIds, options, }: {
|
|
64
|
-
apiKey: string;
|
|
65
|
-
baseId: string;
|
|
66
|
-
tableId: string;
|
|
67
|
-
recordIds: string[];
|
|
68
|
-
options?: {
|
|
69
|
-
requestInterval?: number;
|
|
70
|
-
};
|
|
71
|
-
}): Promise<DeleteRecordsResponse>;
|
|
72
|
-
type AirtableField = {
|
|
73
|
-
id: string;
|
|
74
|
-
name: string;
|
|
75
|
-
type: string;
|
|
76
|
-
options?: object;
|
|
77
|
-
};
|
|
78
|
-
type AirtableTable = {
|
|
79
|
-
id: string;
|
|
80
|
-
name: string;
|
|
81
|
-
primaryFieldId: string;
|
|
82
|
-
fields: AirtableField[];
|
|
83
|
-
};
|
|
84
|
-
type AirtableBaseSchema = {
|
|
85
|
-
tables: AirtableTable[];
|
|
86
|
-
};
|
|
7
|
+
endpoint: string;
|
|
8
|
+
method: RequestMethods;
|
|
9
|
+
body?: object;
|
|
10
|
+
apiURL?: string;
|
|
11
|
+
}): Promise<T>;
|
|
87
12
|
export declare function getBaseSchema({ apiKey, baseId, }: {
|
|
88
13
|
apiKey: string;
|
|
89
14
|
baseId: string;
|
|
90
15
|
}): Promise<AirtableBaseSchema>;
|
|
91
|
-
export declare function generateTypeScriptDefinitions({ apiKey, baseId, tableNameOrId, }: {
|
|
92
|
-
apiKey: string;
|
|
93
|
-
baseId: string;
|
|
94
|
-
tableNameOrId: string;
|
|
95
|
-
}): Promise<string>;
|
|
96
|
-
export {};
|
package/lib/requests.js
CHANGED
|
@@ -1,158 +1,6 @@
|
|
|
1
1
|
import { request } from 'https';
|
|
2
2
|
import { URL } from 'url';
|
|
3
|
-
|
|
4
|
-
export async function getRecord({ apiKey, baseId, tableId, recordId, }) {
|
|
5
|
-
return await airtableRequest({
|
|
6
|
-
apiKey,
|
|
7
|
-
baseId,
|
|
8
|
-
tableId,
|
|
9
|
-
endpoint: `/${recordId}`,
|
|
10
|
-
method: 'GET',
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
export async function getRecords({ apiKey, baseId, tableId, options, }) {
|
|
14
|
-
validateGetRecordsOptions(options);
|
|
15
|
-
let records = [];
|
|
16
|
-
let currentOffset;
|
|
17
|
-
do {
|
|
18
|
-
const requestBody = currentOffset
|
|
19
|
-
? { ...options, offset: currentOffset }
|
|
20
|
-
: options;
|
|
21
|
-
const response = await airtableRequest({
|
|
22
|
-
apiKey,
|
|
23
|
-
baseId,
|
|
24
|
-
tableId,
|
|
25
|
-
endpoint: '/listRecords',
|
|
26
|
-
method: 'POST',
|
|
27
|
-
body: requestBody,
|
|
28
|
-
});
|
|
29
|
-
records = records.concat(response.records);
|
|
30
|
-
currentOffset = response.offset;
|
|
31
|
-
if (currentOffset)
|
|
32
|
-
await delay(options?.requestInterval ?? 500);
|
|
33
|
-
} while (currentOffset);
|
|
34
|
-
return records;
|
|
35
|
-
}
|
|
36
|
-
function validateGetRecordsOptions(options) {
|
|
37
|
-
if (!options)
|
|
38
|
-
return;
|
|
39
|
-
if (options.cellFormat === 'string') {
|
|
40
|
-
if (!options.timeZone || !options.userLocale) {
|
|
41
|
-
throw new Error('The timeZone and userLocale parameters are required when using string as the cellFormat.');
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (!options.maxRecords) {
|
|
45
|
-
options.maxRecords = 100;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
export async function updateRecord({ apiKey, baseId, tableId, recordId, fields, options, }) {
|
|
49
|
-
return await airtableRequest({
|
|
50
|
-
apiKey,
|
|
51
|
-
baseId,
|
|
52
|
-
tableId,
|
|
53
|
-
endpoint: `/${recordId}`,
|
|
54
|
-
method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
|
|
55
|
-
body: {
|
|
56
|
-
fields,
|
|
57
|
-
typecast: options?.typecast === true ? true : false,
|
|
58
|
-
returnFieldsByFieldId: options?.returnFieldsByFieldId === true ? true : false,
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
export async function updateRecords({ apiKey, baseId, tableId, records, options, }) {
|
|
63
|
-
if (!Array.isArray(records) || records.length === 0) {
|
|
64
|
-
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
65
|
-
}
|
|
66
|
-
const chunkSize = 10;
|
|
67
|
-
const chunks = [];
|
|
68
|
-
for (let i = 0; i < records.length; i += chunkSize) {
|
|
69
|
-
chunks.push(records.slice(i, i + chunkSize));
|
|
70
|
-
}
|
|
71
|
-
let combinedResults = [];
|
|
72
|
-
for (const chunk of chunks) {
|
|
73
|
-
const result = await airtableRequest({
|
|
74
|
-
apiKey,
|
|
75
|
-
baseId,
|
|
76
|
-
tableId,
|
|
77
|
-
endpoint: '/',
|
|
78
|
-
method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
|
|
79
|
-
body: {
|
|
80
|
-
records: chunk,
|
|
81
|
-
typecast: options?.typecast ?? false,
|
|
82
|
-
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
83
|
-
},
|
|
84
|
-
});
|
|
85
|
-
combinedResults = combinedResults.concat(result.records);
|
|
86
|
-
await delay(options?.requestInterval ?? 500);
|
|
87
|
-
}
|
|
88
|
-
return { records: combinedResults };
|
|
89
|
-
}
|
|
90
|
-
export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, options, }) {
|
|
91
|
-
if (!Array.isArray(records) || records.length === 0) {
|
|
92
|
-
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
93
|
-
}
|
|
94
|
-
if (!Array.isArray(options?.fieldsToMergeOn) ||
|
|
95
|
-
options?.fieldsToMergeOn.length === 0) {
|
|
96
|
-
throw new Error('fieldsToMergeOn must be an array of strings.');
|
|
97
|
-
}
|
|
98
|
-
const chunkSize = 10;
|
|
99
|
-
const chunks = [];
|
|
100
|
-
for (let i = 0; i < records.length; i += chunkSize) {
|
|
101
|
-
chunks.push(records.slice(i, i + chunkSize));
|
|
102
|
-
}
|
|
103
|
-
let allCreatedRecords = [];
|
|
104
|
-
let allUpdatedRecords = [];
|
|
105
|
-
let allRecords = [];
|
|
106
|
-
for (const chunk of chunks) {
|
|
107
|
-
const result = await airtableRequest({
|
|
108
|
-
apiKey,
|
|
109
|
-
baseId,
|
|
110
|
-
tableId,
|
|
111
|
-
endpoint: '/',
|
|
112
|
-
method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
|
|
113
|
-
body: {
|
|
114
|
-
records: chunk,
|
|
115
|
-
typecast: options?.typecast ?? false,
|
|
116
|
-
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
117
|
-
performUpsert: { fieldsToMergeOn: options.fieldsToMergeOn },
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
allCreatedRecords = allCreatedRecords.concat(result.createdRecords);
|
|
121
|
-
allUpdatedRecords = allUpdatedRecords.concat(result.updatedRecords);
|
|
122
|
-
allRecords = allRecords.concat(result.records);
|
|
123
|
-
await delay(options?.requestInterval ?? 500);
|
|
124
|
-
}
|
|
125
|
-
return {
|
|
126
|
-
createdRecords: allCreatedRecords,
|
|
127
|
-
updatedRecords: allUpdatedRecords,
|
|
128
|
-
records: allRecords,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
export async function deleteRecords({ apiKey, baseId, tableId, recordIds, options, }) {
|
|
132
|
-
if (!Array.isArray(recordIds) || recordIds.length === 0) {
|
|
133
|
-
throw new Error('The record ids array is empty or not provided. Please provide a non-empty array of record ids to delete the records.');
|
|
134
|
-
}
|
|
135
|
-
const chunkSize = 10;
|
|
136
|
-
const chunks = [];
|
|
137
|
-
for (let i = 0; i < recordIds.length; i += chunkSize) {
|
|
138
|
-
chunks.push(recordIds.slice(i, i + chunkSize));
|
|
139
|
-
}
|
|
140
|
-
let combinedResults = [];
|
|
141
|
-
for (const chunk of chunks) {
|
|
142
|
-
const query = chunk.map((id) => `records[]=${id}`).join('&');
|
|
143
|
-
const result = await airtableRequest({
|
|
144
|
-
apiKey,
|
|
145
|
-
baseId,
|
|
146
|
-
tableId,
|
|
147
|
-
endpoint: `?${query}`,
|
|
148
|
-
method: 'DELETE',
|
|
149
|
-
});
|
|
150
|
-
combinedResults = combinedResults.concat(result.records);
|
|
151
|
-
await delay(options?.requestInterval ?? 500);
|
|
152
|
-
}
|
|
153
|
-
return { records: combinedResults };
|
|
154
|
-
}
|
|
155
|
-
async function airtableRequest(request) {
|
|
3
|
+
export async function airtableRequest(request) {
|
|
156
4
|
const { apiKey, baseId, tableId, endpoint, method, body, apiURL } = request;
|
|
157
5
|
const url = apiURL || 'https://api.airtable.com/v0';
|
|
158
6
|
if (!apiKey) {
|
|
@@ -177,6 +25,18 @@ function validateResponse(response) {
|
|
|
177
25
|
const statusCode = response.statusCode;
|
|
178
26
|
if (statusCode === 200)
|
|
179
27
|
return;
|
|
28
|
+
const isAirtableError = (data) => {
|
|
29
|
+
return (data !== null &&
|
|
30
|
+
typeof data === 'object' &&
|
|
31
|
+
'error' in data &&
|
|
32
|
+
typeof data.error === 'object' &&
|
|
33
|
+
data.error !== null &&
|
|
34
|
+
'message' in data.error &&
|
|
35
|
+
typeof data.error.message === 'string');
|
|
36
|
+
};
|
|
37
|
+
if (response.data && isAirtableError(response.data)) {
|
|
38
|
+
throw new Error(response.data.error.message);
|
|
39
|
+
}
|
|
180
40
|
if (statusCode === 401)
|
|
181
41
|
throw new Error('Incorrect API Key.');
|
|
182
42
|
else if (statusCode === 403)
|
|
@@ -250,65 +110,3 @@ export async function getBaseSchema({ apiKey, baseId, }) {
|
|
|
250
110
|
}
|
|
251
111
|
return response.json();
|
|
252
112
|
}
|
|
253
|
-
export async function generateTypeScriptDefinitions({ apiKey, baseId, tableNameOrId, }) {
|
|
254
|
-
const schema = await getBaseSchema({ apiKey, baseId });
|
|
255
|
-
const table = schema.tables.find((t) => t.id === tableNameOrId || t.name === tableNameOrId);
|
|
256
|
-
if (!table) {
|
|
257
|
-
throw new Error(`Table with name or ID "${tableNameOrId}" not found.`);
|
|
258
|
-
}
|
|
259
|
-
let typeDefinitions = `type ${table.name}Fields = {\n`;
|
|
260
|
-
table.fields.forEach((field) => {
|
|
261
|
-
typeDefinitions += ` ${field.name}?: ${mapAirtableTypeToTypeScript(field.type)};\n`;
|
|
262
|
-
});
|
|
263
|
-
typeDefinitions += '};\n';
|
|
264
|
-
return typeDefinitions;
|
|
265
|
-
}
|
|
266
|
-
function mapAirtableTypeToTypeScript(airtableType) {
|
|
267
|
-
switch (airtableType) {
|
|
268
|
-
case 'aiText':
|
|
269
|
-
case 'singleLineText':
|
|
270
|
-
case 'email':
|
|
271
|
-
case 'url':
|
|
272
|
-
case 'phoneNumber':
|
|
273
|
-
case 'multilineText':
|
|
274
|
-
case 'richText':
|
|
275
|
-
case 'button':
|
|
276
|
-
case 'barcode':
|
|
277
|
-
case 'createdTime':
|
|
278
|
-
case 'lastModifiedTime':
|
|
279
|
-
return 'string';
|
|
280
|
-
case 'number':
|
|
281
|
-
case 'currency':
|
|
282
|
-
case 'percent':
|
|
283
|
-
case 'rating':
|
|
284
|
-
case 'duration':
|
|
285
|
-
case 'autoNumber':
|
|
286
|
-
case 'count':
|
|
287
|
-
return 'number';
|
|
288
|
-
case 'checkbox':
|
|
289
|
-
return 'boolean';
|
|
290
|
-
case 'date':
|
|
291
|
-
case 'dateTime':
|
|
292
|
-
return 'Date';
|
|
293
|
-
case 'singleSelect':
|
|
294
|
-
case 'multipleSelects':
|
|
295
|
-
return 'string[]';
|
|
296
|
-
case 'attachment':
|
|
297
|
-
return 'any[]';
|
|
298
|
-
case 'collaborator':
|
|
299
|
-
case 'multipleCollaborators':
|
|
300
|
-
return '{ id: string; email?: string; name?: string; permissionLevel?: string; profilePicUrl?: string; }[]';
|
|
301
|
-
case 'linkToAnotherRecord':
|
|
302
|
-
case 'multipleRecordLinks':
|
|
303
|
-
return 'string[]';
|
|
304
|
-
case 'lookup':
|
|
305
|
-
case 'rollup':
|
|
306
|
-
return 'any';
|
|
307
|
-
case 'formula':
|
|
308
|
-
return 'string | number | boolean | any[]';
|
|
309
|
-
case 'syncSource':
|
|
310
|
-
return 'string';
|
|
311
|
-
default:
|
|
312
|
-
return 'any';
|
|
313
|
-
}
|
|
314
|
-
}
|