easy-airtable-api 0.0.13 → 0.0.15
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/README.md +99 -12
- package/lib/generate-types.d.ts +5 -0
- package/lib/generate-types.js +78 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/records.d.ts +96 -0
- package/lib/records.js +211 -0
- package/lib/requests.d.ts +7 -73
- package/lib/requests.js +13 -215
- package/lib/types/records.d.ts +0 -3
- package/lib/utils.js +2 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
# Easy Airtable API
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
I created this as a simpler alternative to the official Airtable Node.js library.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This library makes it easier to work with Airtable by providing TypeScript support and straightforward functions for reading and writing records.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
While this is still an early project and needs more work, you can already use it to fetch and update records in your Airtable bases.
|
|
8
|
+
|
|
9
|
+
I'm already using it in production for many projects.
|
|
10
|
+
|
|
11
|
+
It also supports generating TypeScript definitions for your Airtable tables which is super useful.
|
|
8
12
|
|
|
9
13
|
## Install
|
|
10
14
|
|
|
@@ -21,7 +25,7 @@ bun add easy-airtable-api
|
|
|
21
25
|
### Get a Single Record
|
|
22
26
|
|
|
23
27
|
```ts
|
|
24
|
-
import { getRecord } from
|
|
28
|
+
import { getRecord } from 'easy-airtable-api';
|
|
25
29
|
|
|
26
30
|
type Fields = {
|
|
27
31
|
Name?: string;
|
|
@@ -30,17 +34,17 @@ type Fields = {
|
|
|
30
34
|
};
|
|
31
35
|
|
|
32
36
|
const record = await getRecord<Fields>({
|
|
33
|
-
apiKey:
|
|
34
|
-
baseId:
|
|
35
|
-
|
|
36
|
-
recordId:
|
|
37
|
+
apiKey: 'apiKey',
|
|
38
|
+
baseId: 'baseId',
|
|
39
|
+
tableNameOrId: 'tableNameOrId',
|
|
40
|
+
recordId: 'recordId',
|
|
37
41
|
});
|
|
38
42
|
```
|
|
39
43
|
|
|
40
44
|
### Get Multiple Records
|
|
41
45
|
|
|
42
46
|
```ts
|
|
43
|
-
import { getRecords } from
|
|
47
|
+
import { getRecords } from 'easy-airtable-api';
|
|
44
48
|
|
|
45
49
|
type Fields = {
|
|
46
50
|
Name?: string;
|
|
@@ -49,17 +53,100 @@ type Fields = {
|
|
|
49
53
|
};
|
|
50
54
|
|
|
51
55
|
const records = await getRecords<Fields>({
|
|
52
|
-
apiKey:
|
|
53
|
-
baseId:
|
|
54
|
-
|
|
56
|
+
apiKey: 'apiKey',
|
|
57
|
+
baseId: 'baseId',
|
|
58
|
+
tableNameOrId: 'tableNameOrId',
|
|
55
59
|
options: {
|
|
56
60
|
maxRecords: 500,
|
|
57
61
|
},
|
|
58
62
|
});
|
|
59
63
|
```
|
|
60
64
|
|
|
65
|
+
### Update a Single Record
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { updateRecord } from 'easy-airtable-api';
|
|
69
|
+
|
|
70
|
+
type Fields = {
|
|
71
|
+
Name?: string;
|
|
72
|
+
Status?: string;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const record = await updateRecord<Fields>({
|
|
76
|
+
apiKey: 'apiKey',
|
|
77
|
+
baseId: 'baseId',
|
|
78
|
+
tableNameOrId: 'tableNameOrId',
|
|
79
|
+
recordId: 'recordId',
|
|
80
|
+
options: {
|
|
81
|
+
typecast: true,
|
|
82
|
+
},
|
|
83
|
+
fields: {
|
|
84
|
+
Name: 'New Name',
|
|
85
|
+
Status: 'Active',
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Update Multiple Records
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { updateRecords } from 'easy-airtable-api';
|
|
94
|
+
|
|
95
|
+
type Fields = {
|
|
96
|
+
Name?: string;
|
|
97
|
+
Status?: string;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const records = await updateRecords<Fields>({
|
|
101
|
+
apiKey: 'apiKey',
|
|
102
|
+
baseId: 'baseId',
|
|
103
|
+
tableNameOrId: 'tableNameOrId',
|
|
104
|
+
records: [
|
|
105
|
+
{
|
|
106
|
+
id: 'recordId1',
|
|
107
|
+
fields: {
|
|
108
|
+
Name: 'New Name 1',
|
|
109
|
+
Status: 'Active',
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
id: 'recordId2',
|
|
114
|
+
fields: {
|
|
115
|
+
Name: 'New Name 2',
|
|
116
|
+
Status: 'Inactive',
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
],
|
|
120
|
+
options: {
|
|
121
|
+
typecast: true,
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Generate TypeScript Definitions
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import { generateTypeScriptDefinitions } from 'easy-airtable-api';
|
|
130
|
+
|
|
131
|
+
const types = await generateTypeScriptDefinitions({
|
|
132
|
+
apiKey: 'apiKey',
|
|
133
|
+
baseId: 'baseId',
|
|
134
|
+
tableNameOrId: 'tableNameOrId',
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
61
138
|
## Changelog
|
|
62
139
|
|
|
140
|
+
### 0.0.15
|
|
141
|
+
|
|
142
|
+
- Type generation wraps type keys in quotations
|
|
143
|
+
- Fix various type generation issues
|
|
144
|
+
- added createRecords and createRecord methods
|
|
145
|
+
|
|
146
|
+
### 0.0.14
|
|
147
|
+
|
|
148
|
+
- Exported generateTypeScriptDefinitions function.
|
|
149
|
+
|
|
63
150
|
### 0.0.13
|
|
64
151
|
|
|
65
152
|
- Added generateTypeScriptDefinitions function to generate TypeScript definitions for Airtable tables.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { getBaseSchema } from './requests';
|
|
2
|
+
export async function generateTypeScriptDefinitions({ apiKey, baseId, tableNameOrId, }) {
|
|
3
|
+
const schema = await getBaseSchema({ apiKey, baseId });
|
|
4
|
+
const table = schema.tables.find((t) => t.id === tableNameOrId || t.name === tableNameOrId);
|
|
5
|
+
if (!table) {
|
|
6
|
+
throw new Error(`Table with name or ID "${tableNameOrId}" not found.`);
|
|
7
|
+
}
|
|
8
|
+
let typeDefinitions = `type ${table.name}Fields = {\n`;
|
|
9
|
+
table.fields.forEach((field) => {
|
|
10
|
+
let fieldName;
|
|
11
|
+
if (field.name.includes('"')) {
|
|
12
|
+
if (field.name.includes("'")) {
|
|
13
|
+
if (field.name.includes('`')) {
|
|
14
|
+
fieldName = field.name;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
fieldName = `\`${field.name}\``;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
fieldName = `'${field.name}'`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
fieldName = `"${field.name}"`;
|
|
26
|
+
}
|
|
27
|
+
const typeInfo = mapAirtableTypeToTypeScript(field.type);
|
|
28
|
+
const fieldType = typeInfo.readonly
|
|
29
|
+
? `readonly ${typeInfo.type}`
|
|
30
|
+
: typeInfo.type;
|
|
31
|
+
typeDefinitions += ` ${fieldName}?: ${fieldType};\n`;
|
|
32
|
+
});
|
|
33
|
+
typeDefinitions += '};\n';
|
|
34
|
+
return typeDefinitions;
|
|
35
|
+
}
|
|
36
|
+
function mapAirtableTypeToTypeScript(airtableType) {
|
|
37
|
+
switch (airtableType) {
|
|
38
|
+
case 'date':
|
|
39
|
+
case 'dateTime':
|
|
40
|
+
return { type: 'string', readonly: false };
|
|
41
|
+
case 'createdTime':
|
|
42
|
+
case 'lastModifiedTime':
|
|
43
|
+
return { type: 'string', readonly: true };
|
|
44
|
+
case 'number':
|
|
45
|
+
case 'currency':
|
|
46
|
+
case 'percent':
|
|
47
|
+
case 'rating':
|
|
48
|
+
case 'duration':
|
|
49
|
+
case 'autoNumber':
|
|
50
|
+
case 'count':
|
|
51
|
+
return { type: 'number', readonly: false };
|
|
52
|
+
case 'checkbox':
|
|
53
|
+
return { type: 'boolean', readonly: false };
|
|
54
|
+
case 'singleSelect':
|
|
55
|
+
case 'multipleSelects':
|
|
56
|
+
return { type: 'string[]', readonly: false };
|
|
57
|
+
case 'attachment':
|
|
58
|
+
return { type: 'any[]', readonly: false };
|
|
59
|
+
case 'collaborator':
|
|
60
|
+
case 'multipleCollaborators':
|
|
61
|
+
return {
|
|
62
|
+
type: '{ id: string; email?: string; name?: string; permissionLevel?: string; profilePicUrl?: string; }[]',
|
|
63
|
+
readonly: false,
|
|
64
|
+
};
|
|
65
|
+
case 'linkToAnotherRecord':
|
|
66
|
+
case 'multipleRecordLinks':
|
|
67
|
+
return { type: 'string[]', readonly: false };
|
|
68
|
+
case 'lookup':
|
|
69
|
+
case 'rollup':
|
|
70
|
+
return { type: 'any', readonly: false };
|
|
71
|
+
case 'formula':
|
|
72
|
+
return { type: 'string | number | boolean | any[]', readonly: true };
|
|
73
|
+
case 'syncSource':
|
|
74
|
+
return { type: 'string', readonly: false };
|
|
75
|
+
default:
|
|
76
|
+
return { type: 'any', readonly: false };
|
|
77
|
+
}
|
|
78
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { getRecord, getRecords, updateRecord, updateRecords, updateRecordsUpsert, deleteRecords } from './
|
|
1
|
+
export { getRecord, getRecords, createRecord, createRecords, updateRecord, updateRecords, updateRecordsUpsert, deleteRecords, } from './records.js';
|
|
2
|
+
export { generateTypeScriptDefinitions } from './generate-types.js';
|
package/lib/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { getRecord, getRecords, updateRecord, updateRecords, updateRecordsUpsert, deleteRecords } from './
|
|
1
|
+
export { getRecord, getRecords, createRecord, createRecords, updateRecord, updateRecords, updateRecordsUpsert, deleteRecords, } from './records.js';
|
|
2
|
+
export { generateTypeScriptDefinitions } from './generate-types.js';
|
package/lib/records.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { AirtableRecord, DeleteRecordsResponse, GetRecordsQueryParameters } from './types/records';
|
|
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';
|
|
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,74 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function
|
|
1
|
+
import { RequestMethods } from './types/tables';
|
|
2
|
+
export declare function airtableRequest<T>(request: {
|
|
3
3
|
apiKey: string;
|
|
4
4
|
baseId: string;
|
|
5
5
|
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>;
|
|
6
|
+
endpoint: string;
|
|
7
|
+
method: RequestMethods;
|
|
8
|
+
body?: object;
|
|
9
|
+
apiURL?: string;
|
|
10
|
+
}): Promise<T>;
|
|
72
11
|
type AirtableField = {
|
|
73
12
|
id: string;
|
|
74
13
|
name: string;
|
|
@@ -88,9 +27,4 @@ export declare function getBaseSchema({ apiKey, baseId, }: {
|
|
|
88
27
|
apiKey: string;
|
|
89
28
|
baseId: string;
|
|
90
29
|
}): Promise<AirtableBaseSchema>;
|
|
91
|
-
export declare function generateTypeScriptDefinitions({ apiKey, baseId, tableNameOrId, }: {
|
|
92
|
-
apiKey: string;
|
|
93
|
-
baseId: string;
|
|
94
|
-
tableNameOrId: string;
|
|
95
|
-
}): Promise<string>;
|
|
96
30
|
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 &&
|
|
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
|
-
}
|
package/lib/types/records.d.ts
CHANGED
|
@@ -105,9 +105,6 @@ export interface DeleteRecordsPathParameters {
|
|
|
105
105
|
baseId: BaseId;
|
|
106
106
|
tableIdOrName: TableIdOrName;
|
|
107
107
|
}
|
|
108
|
-
export interface DeleteRecordsQueryParameters {
|
|
109
|
-
records: string[];
|
|
110
|
-
}
|
|
111
108
|
export interface DeleteRecordResponse {
|
|
112
109
|
id: string;
|
|
113
110
|
deleted: true;
|
package/lib/utils.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-airtable-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "An easy way to connect to Airtable via their API.",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"lint": "eslint 'src/**/*.{ts,tsx}'",
|
|
18
18
|
"test": "vitest",
|
|
19
19
|
"testGetRecords": "vitest get-records.test.ts",
|
|
20
|
+
"testCreateRecords": "vitest create-records.test.ts",
|
|
20
21
|
"testUpdateRecords": "vitest update-records.test.ts",
|
|
21
22
|
"testDeleteRecords": "vitest delete-records.test.ts"
|
|
22
23
|
},
|