easy-airtable-api 0.0.16 → 0.0.17
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 +9 -0
- package/README.md +182 -1
- package/easy-airtable-api-0.0.17.tgz +0 -0
- package/lib/bases.d.ts +18 -0
- package/lib/bases.js +40 -0
- package/lib/codegen/load-schema.js +1 -1
- package/lib/index.d.ts +6 -1
- package/lib/index.js +3 -1
- package/lib/records.d.ts +75 -9
- package/lib/records.js +110 -21
- package/lib/requests.d.ts +64 -5
- package/lib/requests.js +280 -30
- package/lib/types/bases.d.ts +8 -40
- package/package.json +6 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.0.17
|
|
4
|
+
|
|
5
|
+
- Reworked Airtable type generation around the new `easy-airtable-api/codegen` workflow.
|
|
6
|
+
- Added public metadata/base helpers: `listBases`, `getBaseSchema`, and verified `createBase`.
|
|
7
|
+
- Added `airtableRequestRaw` as a public escape hatch for unsupported Airtable endpoints.
|
|
8
|
+
- Hardened the shared request layer with structured `AirtableApiError`, retries, `Retry-After`, jittered backoff, safer encoding, and configurable retry options.
|
|
9
|
+
- Added record API parity helpers including `deleteRecord`, `replaceRecord`, `replaceRecords`, `getRecord(..., options)`, `getRecordsPage`, and `iterateRecordsPages`.
|
|
10
|
+
- Expanded offline and opt-in integration test coverage for retries, batching, validation, metadata, codegen, and non-mutation guarantees.
|
|
11
|
+
|
|
3
12
|
## 0.0.16
|
|
4
13
|
|
|
5
14
|
- Improved type generation to support more Airtable field types.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Easy Airtable API
|
|
2
2
|
|
|
3
|
-
Easy Airtable API is a lightweight, TypeScript-first Airtable client for
|
|
3
|
+
Easy Airtable API is a lightweight, TypeScript-first Airtable client for records, metadata/base APIs, code generation, and raw Airtable requests.
|
|
4
4
|
|
|
5
5
|
It is built to stay practical:
|
|
6
6
|
|
|
@@ -48,6 +48,7 @@ A good setup is to generate Airtable types once and commit the generated file.
|
|
|
48
48
|
AIRTABLE_ACCESS_TOKEN=pat_xxx
|
|
49
49
|
AIRTABLE_BASE_ID=app_xxx
|
|
50
50
|
AIRTABLE_PROJECTS_TABLE_ID=tbl_xxx
|
|
51
|
+
AIRTABLE_WORKSPACE_ID=wsp_xxx
|
|
51
52
|
```
|
|
52
53
|
|
|
53
54
|
### Example `package.json` scripts
|
|
@@ -188,6 +189,109 @@ await generateAirtableTypes({
|
|
|
188
189
|
});
|
|
189
190
|
```
|
|
190
191
|
|
|
192
|
+
## Metadata/base APIs
|
|
193
|
+
|
|
194
|
+
### List accessible bases
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
import { listBases } from 'easy-airtable-api';
|
|
198
|
+
|
|
199
|
+
const result = await listBases({
|
|
200
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
for (const base of result.bases) {
|
|
204
|
+
console.log(base.id, base.name, base.permissionLevel);
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Fetch a base schema directly
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { getBaseSchema } from 'easy-airtable-api';
|
|
212
|
+
|
|
213
|
+
const schema = await getBaseSchema({
|
|
214
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
215
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
console.log(schema.tables.map((table) => table.name));
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
This is the same metadata path used by the code generator when `source` includes a `baseId` and token.
|
|
222
|
+
|
|
223
|
+
### Create a base
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
import { createBase } from 'easy-airtable-api';
|
|
227
|
+
|
|
228
|
+
const created = await createBase({
|
|
229
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
230
|
+
body: {
|
|
231
|
+
workspaceId: process.env.AIRTABLE_WORKSPACE_ID!,
|
|
232
|
+
name: 'Apartment Hunting',
|
|
233
|
+
tables: [
|
|
234
|
+
{
|
|
235
|
+
name: 'Apartments',
|
|
236
|
+
fields: [
|
|
237
|
+
{ name: 'Name', type: 'singleLineText' },
|
|
238
|
+
{
|
|
239
|
+
name: 'Visited',
|
|
240
|
+
type: 'checkbox',
|
|
241
|
+
options: { color: 'greenBright', icon: 'check' },
|
|
242
|
+
},
|
|
243
|
+
],
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
console.log(created.id);
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Per Airtable's metadata API documentation, creating a base requires a token authorized for `schema.bases:write`, and the caller must be able to create bases in the target workspace (documented as workspace creator permissions).
|
|
253
|
+
|
|
254
|
+
## Raw Airtable request escape hatch
|
|
255
|
+
|
|
256
|
+
Use `airtableRequestRaw()` when you need an Airtable endpoint that this library does not wrap yet, but still want the shared auth, retry, encoding, and structured error behavior.
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
import { airtableRequestRaw } from 'easy-airtable-api';
|
|
260
|
+
|
|
261
|
+
const schema = await airtableRequestRaw({
|
|
262
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
263
|
+
method: 'GET',
|
|
264
|
+
path: '/v0/meta/bases/app123/tables',
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
It accepts either Airtable-style `/v0/...` paths or paths relative to the API root used internally.
|
|
269
|
+
|
|
270
|
+
## Retry configuration
|
|
271
|
+
|
|
272
|
+
All runtime helpers accept an optional top-level `retry` object for tuning retry behavior without leaving the library's shared request layer.
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
import { getRecords } from 'easy-airtable-api';
|
|
276
|
+
|
|
277
|
+
const records = await getRecords({
|
|
278
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
279
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
280
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
281
|
+
retry: {
|
|
282
|
+
maxRetries: 2,
|
|
283
|
+
baseDelayMs: 250,
|
|
284
|
+
maxDelayMs: 1000,
|
|
285
|
+
retryOn429: true,
|
|
286
|
+
retryOn5xx: true,
|
|
287
|
+
retryOnNetworkErrors: true,
|
|
288
|
+
useJitter: false,
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
The same `retry` option is supported by metadata helpers such as `listBases()` / `getBaseSchema()` and by `airtableRequestRaw()`.
|
|
294
|
+
|
|
191
295
|
## Runtime examples
|
|
192
296
|
|
|
193
297
|
### Get a single record
|
|
@@ -201,9 +305,46 @@ const record = await getRecord<ProjectsRecordFields>({
|
|
|
201
305
|
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
202
306
|
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
203
307
|
recordId: 'rec123',
|
|
308
|
+
options: {
|
|
309
|
+
returnFieldsByFieldId: false,
|
|
310
|
+
},
|
|
204
311
|
});
|
|
205
312
|
```
|
|
206
313
|
|
|
314
|
+
### Get one page of records
|
|
315
|
+
|
|
316
|
+
```ts
|
|
317
|
+
import { getRecordsPage } from 'easy-airtable-api';
|
|
318
|
+
import type { ProjectsRecordFields } from './airtable.generated';
|
|
319
|
+
|
|
320
|
+
const page = await getRecordsPage<ProjectsRecordFields>({
|
|
321
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
322
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
323
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
324
|
+
options: {
|
|
325
|
+
pageSize: 25,
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### Iterate through record pages
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
import { iterateRecordsPages } from 'easy-airtable-api';
|
|
334
|
+
import type { ProjectsRecordFields } from './airtable.generated';
|
|
335
|
+
|
|
336
|
+
for await (const page of iterateRecordsPages<ProjectsRecordFields>({
|
|
337
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
338
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
339
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
340
|
+
options: {
|
|
341
|
+
pageSize: 100,
|
|
342
|
+
},
|
|
343
|
+
})) {
|
|
344
|
+
console.log(page.records.length, page.offset);
|
|
345
|
+
}
|
|
346
|
+
```
|
|
347
|
+
|
|
207
348
|
### Create multiple records
|
|
208
349
|
|
|
209
350
|
```ts
|
|
@@ -234,6 +375,24 @@ const result = await createRecords<ProjectsCreateFields>({
|
|
|
234
375
|
});
|
|
235
376
|
```
|
|
236
377
|
|
|
378
|
+
### Replace a single record
|
|
379
|
+
|
|
380
|
+
```ts
|
|
381
|
+
import { replaceRecord } from 'easy-airtable-api';
|
|
382
|
+
import type { ProjectsCreateFields } from './airtable.generated';
|
|
383
|
+
|
|
384
|
+
const result = await replaceRecord<ProjectsCreateFields>({
|
|
385
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
386
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
387
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
388
|
+
recordId: 'rec123',
|
|
389
|
+
fields: {
|
|
390
|
+
Name: 'Project A',
|
|
391
|
+
Status: 'Done',
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
```
|
|
395
|
+
|
|
237
396
|
### Upsert records
|
|
238
397
|
|
|
239
398
|
```ts
|
|
@@ -259,6 +418,19 @@ const result = await updateRecordsUpsert<ProjectsCreateFields>({
|
|
|
259
418
|
});
|
|
260
419
|
```
|
|
261
420
|
|
|
421
|
+
### Delete a single record
|
|
422
|
+
|
|
423
|
+
```ts
|
|
424
|
+
import { deleteRecord } from 'easy-airtable-api';
|
|
425
|
+
|
|
426
|
+
await deleteRecord({
|
|
427
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
428
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
429
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
430
|
+
recordId: 'rec123',
|
|
431
|
+
});
|
|
432
|
+
```
|
|
433
|
+
|
|
262
434
|
### Delete records
|
|
263
435
|
|
|
264
436
|
```ts
|
|
@@ -278,11 +450,20 @@ Runtime package:
|
|
|
278
450
|
|
|
279
451
|
```ts
|
|
280
452
|
import {
|
|
453
|
+
airtableRequestRaw,
|
|
454
|
+
createBase,
|
|
281
455
|
createRecord,
|
|
282
456
|
createRecords,
|
|
457
|
+
deleteRecord,
|
|
283
458
|
deleteRecords,
|
|
459
|
+
getBaseSchema,
|
|
284
460
|
getRecord,
|
|
285
461
|
getRecords,
|
|
462
|
+
getRecordsPage,
|
|
463
|
+
iterateRecordsPages,
|
|
464
|
+
listBases,
|
|
465
|
+
replaceRecord,
|
|
466
|
+
replaceRecords,
|
|
286
467
|
updateRecord,
|
|
287
468
|
updateRecords,
|
|
288
469
|
updateRecordsUpsert,
|
|
Binary file
|
package/lib/bases.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AirtableRetryOptions } from './requests.js';
|
|
2
|
+
import type { CreateBaseRequestBody, CreateBaseResponse, ListBasesResponse } from './types/bases.js';
|
|
3
|
+
import type { AirtableBaseSchema } from './types/metadata.js';
|
|
4
|
+
export declare function listBases({ apiKey, offset, retry, }: {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
offset?: string;
|
|
7
|
+
retry?: AirtableRetryOptions;
|
|
8
|
+
}): Promise<ListBasesResponse>;
|
|
9
|
+
export declare function createBase({ apiKey, body, retry, }: {
|
|
10
|
+
apiKey: string;
|
|
11
|
+
body: CreateBaseRequestBody;
|
|
12
|
+
retry?: AirtableRetryOptions;
|
|
13
|
+
}): Promise<CreateBaseResponse>;
|
|
14
|
+
export declare function getBaseSchema({ apiKey, baseId, retry, }: {
|
|
15
|
+
apiKey: string;
|
|
16
|
+
baseId: string;
|
|
17
|
+
retry?: AirtableRetryOptions;
|
|
18
|
+
}): Promise<AirtableBaseSchema>;
|
package/lib/bases.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { airtableApiRequest } from './requests.js';
|
|
2
|
+
export async function listBases({ apiKey, offset, retry, }) {
|
|
3
|
+
return airtableApiRequest({
|
|
4
|
+
apiKey,
|
|
5
|
+
method: 'GET',
|
|
6
|
+
path: '/meta/bases',
|
|
7
|
+
query: { offset },
|
|
8
|
+
retry,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export async function createBase({ apiKey, body, retry, }) {
|
|
12
|
+
return airtableApiRequest({
|
|
13
|
+
apiKey,
|
|
14
|
+
method: 'POST',
|
|
15
|
+
path: '/meta/bases',
|
|
16
|
+
body,
|
|
17
|
+
retry,
|
|
18
|
+
requestContext: {
|
|
19
|
+
method: 'POST',
|
|
20
|
+
path: '/meta/bases',
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export async function getBaseSchema({ apiKey, baseId, retry, }) {
|
|
25
|
+
if (!baseId) {
|
|
26
|
+
throw new Error('Base ID is not set. Please provide a valid Airtable base ID.');
|
|
27
|
+
}
|
|
28
|
+
const path = `/meta/bases/${encodeURIComponent(baseId)}/tables`;
|
|
29
|
+
return airtableApiRequest({
|
|
30
|
+
apiKey,
|
|
31
|
+
method: 'GET',
|
|
32
|
+
path,
|
|
33
|
+
retry,
|
|
34
|
+
requestContext: {
|
|
35
|
+
method: 'GET',
|
|
36
|
+
baseId,
|
|
37
|
+
path,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { resolve } from 'node:path';
|
|
3
|
-
import { getBaseSchema } from '../
|
|
3
|
+
import { getBaseSchema } from '../bases.js';
|
|
4
4
|
import { isRecord } from './utils.js';
|
|
5
5
|
function isAirtableBaseSchema(value) {
|
|
6
6
|
if (!isRecord(value))
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
export { getRecord, getRecords, createRecord, createRecords, updateRecord, updateRecords, updateRecordsUpsert, deleteRecords, } from './records.js';
|
|
1
|
+
export { getRecord, getRecordsPage, iterateRecordsPages, getRecords, createRecord, createRecords, updateRecord, replaceRecord, updateRecords, replaceRecords, updateRecordsUpsert, deleteRecord, deleteRecords, } from './records.js';
|
|
2
|
+
export { listBases, createBase, getBaseSchema } from './bases.js';
|
|
3
|
+
export { airtableRequestRaw, AirtableApiError } from './requests.js';
|
|
4
|
+
export type { AirtableRetryOptions } from './requests.js';
|
|
2
5
|
export type { AirtableRecord } from './types/records.js';
|
|
6
|
+
export type { CreateBaseFieldConfig, CreateBaseRequestBody, CreateBaseResponse, CreateBaseTableConfig, ListBasesResponse, } from './types/bases.js';
|
|
7
|
+
export type { AirtableBaseSchema, AirtableFieldSchema, AirtableTableSchema, AirtableViewSchema, } from './types/metadata.js';
|
|
3
8
|
export type { Attachment, AttachmentWrite, Collaborator, } from './types/fields.js';
|
package/lib/index.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
export { getRecord, getRecords, createRecord, createRecords, updateRecord, updateRecords, updateRecordsUpsert, deleteRecords, } from './records.js';
|
|
1
|
+
export { getRecord, getRecordsPage, iterateRecordsPages, getRecords, createRecord, createRecords, updateRecord, replaceRecord, updateRecords, replaceRecords, updateRecordsUpsert, deleteRecord, deleteRecords, } from './records.js';
|
|
2
|
+
export { listBases, createBase, getBaseSchema } from './bases.js';
|
|
3
|
+
export { airtableRequestRaw, AirtableApiError } from './requests.js';
|
package/lib/records.d.ts
CHANGED
|
@@ -1,17 +1,41 @@
|
|
|
1
|
-
import { AirtableRecord, DeleteRecordsResponse, GetRecordsQueryParameters } from './types/records.js';
|
|
2
|
-
|
|
1
|
+
import { AirtableRecord, DeleteRecordResponse, DeleteRecordsResponse, GetRecordQueryParameters, GetRecordsQueryParameters } from './types/records.js';
|
|
2
|
+
import type { AirtableRetryOptions } from './requests.js';
|
|
3
|
+
export declare function getRecord<Fields>({ apiKey, baseId, tableId, recordId, options, retry, }: {
|
|
3
4
|
apiKey: string;
|
|
4
5
|
baseId: string;
|
|
5
6
|
tableId: string;
|
|
6
7
|
recordId: string;
|
|
8
|
+
options?: GetRecordQueryParameters;
|
|
9
|
+
retry?: AirtableRetryOptions;
|
|
7
10
|
}): Promise<AirtableRecord<Fields>>;
|
|
8
|
-
export declare function
|
|
11
|
+
export declare function getRecordsPage<Fields>({ apiKey, baseId, tableId, options, retry, }: {
|
|
9
12
|
apiKey: string;
|
|
10
13
|
baseId: string;
|
|
11
14
|
tableId: string;
|
|
12
15
|
options?: GetRecordsQueryParameters;
|
|
16
|
+
retry?: AirtableRetryOptions;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
records: AirtableRecord<Fields>[];
|
|
19
|
+
offset?: string;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function iterateRecordsPages<Fields>({ apiKey, baseId, tableId, options, retry, }: {
|
|
22
|
+
apiKey: string;
|
|
23
|
+
baseId: string;
|
|
24
|
+
tableId: string;
|
|
25
|
+
options?: GetRecordsQueryParameters;
|
|
26
|
+
retry?: AirtableRetryOptions;
|
|
27
|
+
}): AsyncGenerator<{
|
|
28
|
+
records: AirtableRecord<Fields>[];
|
|
29
|
+
offset?: string;
|
|
30
|
+
}>;
|
|
31
|
+
export declare function getRecords<Fields>({ apiKey, baseId, tableId, options, retry, }: {
|
|
32
|
+
apiKey: string;
|
|
33
|
+
baseId: string;
|
|
34
|
+
tableId: string;
|
|
35
|
+
options?: GetRecordsQueryParameters;
|
|
36
|
+
retry?: AirtableRetryOptions;
|
|
13
37
|
}): Promise<AirtableRecord<Fields>[]>;
|
|
14
|
-
export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, options, }: {
|
|
38
|
+
export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, options, retry, }: {
|
|
15
39
|
apiKey: string;
|
|
16
40
|
baseId: string;
|
|
17
41
|
tableId: string;
|
|
@@ -22,8 +46,21 @@ export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId
|
|
|
22
46
|
returnFieldsByFieldId?: boolean;
|
|
23
47
|
overwriteFieldsNotSpecified?: boolean;
|
|
24
48
|
};
|
|
49
|
+
retry?: AirtableRetryOptions;
|
|
50
|
+
}): Promise<AirtableRecord<Fields>>;
|
|
51
|
+
export declare function replaceRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, options, retry, }: {
|
|
52
|
+
apiKey: string;
|
|
53
|
+
baseId: string;
|
|
54
|
+
tableId: string;
|
|
55
|
+
recordId: string;
|
|
56
|
+
fields: Fields;
|
|
57
|
+
options?: {
|
|
58
|
+
typecast?: boolean;
|
|
59
|
+
returnFieldsByFieldId?: boolean;
|
|
60
|
+
};
|
|
61
|
+
retry?: AirtableRetryOptions;
|
|
25
62
|
}): Promise<AirtableRecord<Fields>>;
|
|
26
|
-
export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
63
|
+
export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records, options, retry, }: {
|
|
27
64
|
apiKey: string;
|
|
28
65
|
baseId: string;
|
|
29
66
|
tableId: string;
|
|
@@ -37,10 +74,28 @@ export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records
|
|
|
37
74
|
overwriteFieldsNotSpecified?: boolean;
|
|
38
75
|
requestInterval?: number;
|
|
39
76
|
};
|
|
77
|
+
retry?: AirtableRetryOptions;
|
|
40
78
|
}): Promise<{
|
|
41
79
|
records: AirtableRecord<Fields>[];
|
|
42
80
|
}>;
|
|
43
|
-
export declare function
|
|
81
|
+
export declare function replaceRecords<Fields>({ apiKey, baseId, tableId, records, options, retry, }: {
|
|
82
|
+
apiKey: string;
|
|
83
|
+
baseId: string;
|
|
84
|
+
tableId: string;
|
|
85
|
+
records: {
|
|
86
|
+
id: string;
|
|
87
|
+
fields: Fields;
|
|
88
|
+
}[];
|
|
89
|
+
options?: {
|
|
90
|
+
typecast?: boolean;
|
|
91
|
+
returnFieldsByFieldId?: boolean;
|
|
92
|
+
requestInterval?: number;
|
|
93
|
+
};
|
|
94
|
+
retry?: AirtableRetryOptions;
|
|
95
|
+
}): Promise<{
|
|
96
|
+
records: AirtableRecord<Fields>[];
|
|
97
|
+
}>;
|
|
98
|
+
export declare function updateRecordsUpsert<Fields>({ apiKey, baseId, tableId, records, options, retry, }: {
|
|
44
99
|
apiKey: string;
|
|
45
100
|
baseId: string;
|
|
46
101
|
tableId: string;
|
|
@@ -55,12 +110,20 @@ export declare function updateRecordsUpsert<Fields>({ apiKey, baseId, tableId, r
|
|
|
55
110
|
overwriteFieldsNotSpecified?: boolean;
|
|
56
111
|
requestInterval?: number;
|
|
57
112
|
};
|
|
113
|
+
retry?: AirtableRetryOptions;
|
|
58
114
|
}): Promise<{
|
|
59
115
|
createdRecords: string[];
|
|
60
116
|
updatedRecords: string[];
|
|
61
117
|
records: AirtableRecord<Fields>[];
|
|
62
118
|
}>;
|
|
63
|
-
export declare function
|
|
119
|
+
export declare function deleteRecord({ apiKey, baseId, tableId, recordId, retry, }: {
|
|
120
|
+
apiKey: string;
|
|
121
|
+
baseId: string;
|
|
122
|
+
tableId: string;
|
|
123
|
+
recordId: string;
|
|
124
|
+
retry?: AirtableRetryOptions;
|
|
125
|
+
}): Promise<DeleteRecordResponse>;
|
|
126
|
+
export declare function deleteRecords({ apiKey, baseId, tableId, recordIds, options, retry, }: {
|
|
64
127
|
apiKey: string;
|
|
65
128
|
baseId: string;
|
|
66
129
|
tableId: string;
|
|
@@ -68,8 +131,9 @@ export declare function deleteRecords({ apiKey, baseId, tableId, recordIds, opti
|
|
|
68
131
|
options?: {
|
|
69
132
|
requestInterval?: number;
|
|
70
133
|
};
|
|
134
|
+
retry?: AirtableRetryOptions;
|
|
71
135
|
}): Promise<DeleteRecordsResponse>;
|
|
72
|
-
export declare function createRecord<Fields>({ apiKey, baseId, tableId, fields, options, }: {
|
|
136
|
+
export declare function createRecord<Fields>({ apiKey, baseId, tableId, fields, options, retry, }: {
|
|
73
137
|
apiKey: string;
|
|
74
138
|
baseId: string;
|
|
75
139
|
tableId: string;
|
|
@@ -78,8 +142,9 @@ export declare function createRecord<Fields>({ apiKey, baseId, tableId, fields,
|
|
|
78
142
|
typecast?: boolean;
|
|
79
143
|
returnFieldsByFieldId?: boolean;
|
|
80
144
|
};
|
|
145
|
+
retry?: AirtableRetryOptions;
|
|
81
146
|
}): Promise<AirtableRecord<Fields>>;
|
|
82
|
-
export declare function createRecords<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
147
|
+
export declare function createRecords<Fields>({ apiKey, baseId, tableId, records, options, retry, }: {
|
|
83
148
|
apiKey: string;
|
|
84
149
|
baseId: string;
|
|
85
150
|
tableId: string;
|
|
@@ -91,6 +156,7 @@ export declare function createRecords<Fields>({ apiKey, baseId, tableId, records
|
|
|
91
156
|
returnFieldsByFieldId?: boolean;
|
|
92
157
|
requestInterval?: number;
|
|
93
158
|
};
|
|
159
|
+
retry?: AirtableRetryOptions;
|
|
94
160
|
}): Promise<{
|
|
95
161
|
records: AirtableRecord<Fields>[];
|
|
96
162
|
}>;
|
package/lib/records.js
CHANGED
|
@@ -1,37 +1,64 @@
|
|
|
1
|
-
import { airtableRequest } from './requests.js';
|
|
1
|
+
import { appendQueryToEndpoint, airtableRequest } from './requests.js';
|
|
2
2
|
import { delay } from './utils.js';
|
|
3
|
-
export async function getRecord({ apiKey, baseId, tableId, recordId, }) {
|
|
3
|
+
export async function getRecord({ apiKey, baseId, tableId, recordId, options, retry, }) {
|
|
4
|
+
const endpoint = appendQueryToEndpoint(`/${recordId}`, {
|
|
5
|
+
cellFormat: options?.cellFormat,
|
|
6
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId,
|
|
7
|
+
});
|
|
4
8
|
return await airtableRequest({
|
|
5
9
|
apiKey,
|
|
6
10
|
baseId,
|
|
7
11
|
tableId,
|
|
8
|
-
endpoint
|
|
12
|
+
endpoint,
|
|
9
13
|
method: 'GET',
|
|
14
|
+
retry,
|
|
10
15
|
});
|
|
11
16
|
}
|
|
12
|
-
export async function
|
|
17
|
+
export async function getRecordsPage({ apiKey, baseId, tableId, options, retry, }) {
|
|
13
18
|
validateGetRecordsOptions(options);
|
|
14
|
-
|
|
19
|
+
return await airtableRequest({
|
|
20
|
+
apiKey,
|
|
21
|
+
baseId,
|
|
22
|
+
tableId,
|
|
23
|
+
endpoint: '/listRecords',
|
|
24
|
+
method: 'POST',
|
|
25
|
+
body: options,
|
|
26
|
+
retry,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export async function* iterateRecordsPages({ apiKey, baseId, tableId, options, retry, }) {
|
|
15
30
|
let currentOffset;
|
|
16
31
|
do {
|
|
17
32
|
const requestBody = currentOffset
|
|
18
33
|
? { ...options, offset: currentOffset }
|
|
19
34
|
: options;
|
|
20
|
-
const response = await
|
|
35
|
+
const response = await getRecordsPage({
|
|
21
36
|
apiKey,
|
|
22
37
|
baseId,
|
|
23
38
|
tableId,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
body: requestBody,
|
|
39
|
+
options: requestBody,
|
|
40
|
+
retry,
|
|
27
41
|
});
|
|
28
|
-
|
|
42
|
+
yield response;
|
|
29
43
|
currentOffset = response.offset;
|
|
30
44
|
if (currentOffset && (options?.maxRecords ?? 100) > 100) {
|
|
31
45
|
const interval = options?.requestInterval || 500;
|
|
32
46
|
await delay(interval);
|
|
33
47
|
}
|
|
34
48
|
} while (currentOffset);
|
|
49
|
+
}
|
|
50
|
+
export async function getRecords({ apiKey, baseId, tableId, options, retry, }) {
|
|
51
|
+
validateGetRecordsOptions(options);
|
|
52
|
+
let records = [];
|
|
53
|
+
for await (const page of iterateRecordsPages({
|
|
54
|
+
apiKey,
|
|
55
|
+
baseId,
|
|
56
|
+
tableId,
|
|
57
|
+
options,
|
|
58
|
+
retry,
|
|
59
|
+
})) {
|
|
60
|
+
records = records.concat(page.records);
|
|
61
|
+
}
|
|
35
62
|
return records;
|
|
36
63
|
}
|
|
37
64
|
function validateGetRecordsOptions(options) {
|
|
@@ -42,11 +69,8 @@ function validateGetRecordsOptions(options) {
|
|
|
42
69
|
throw new Error('The timeZone and userLocale parameters are required when using string as the cellFormat.');
|
|
43
70
|
}
|
|
44
71
|
}
|
|
45
|
-
if (!options.maxRecords) {
|
|
46
|
-
options.maxRecords = 100;
|
|
47
|
-
}
|
|
48
72
|
}
|
|
49
|
-
export async function updateRecord({ apiKey, baseId, tableId, recordId, fields, options, }) {
|
|
73
|
+
export async function updateRecord({ apiKey, baseId, tableId, recordId, fields, options, retry, }) {
|
|
50
74
|
return await airtableRequest({
|
|
51
75
|
apiKey,
|
|
52
76
|
baseId,
|
|
@@ -58,9 +82,25 @@ export async function updateRecord({ apiKey, baseId, tableId, recordId, fields,
|
|
|
58
82
|
typecast: options?.typecast === true ? true : false,
|
|
59
83
|
returnFieldsByFieldId: options?.returnFieldsByFieldId === true ? true : false,
|
|
60
84
|
},
|
|
85
|
+
retry,
|
|
61
86
|
});
|
|
62
87
|
}
|
|
63
|
-
export async function
|
|
88
|
+
export async function replaceRecord({ apiKey, baseId, tableId, recordId, fields, options, retry, }) {
|
|
89
|
+
return await airtableRequest({
|
|
90
|
+
apiKey,
|
|
91
|
+
baseId,
|
|
92
|
+
tableId,
|
|
93
|
+
endpoint: `/${recordId}`,
|
|
94
|
+
method: 'PUT',
|
|
95
|
+
body: {
|
|
96
|
+
fields,
|
|
97
|
+
typecast: options?.typecast ?? false,
|
|
98
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
99
|
+
},
|
|
100
|
+
retry,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
export async function updateRecords({ apiKey, baseId, tableId, records, options, retry, }) {
|
|
64
104
|
if (!Array.isArray(records) || records.length === 0) {
|
|
65
105
|
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
66
106
|
}
|
|
@@ -82,6 +122,40 @@ export async function updateRecords({ apiKey, baseId, tableId, records, options,
|
|
|
82
122
|
typecast: options?.typecast ?? false,
|
|
83
123
|
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
84
124
|
},
|
|
125
|
+
retry,
|
|
126
|
+
});
|
|
127
|
+
combinedResults = combinedResults.concat(result.records);
|
|
128
|
+
if (records.length > chunkSize &&
|
|
129
|
+
chunks.indexOf(chunk) < chunks.length - 1) {
|
|
130
|
+
const interval = options?.requestInterval || 500;
|
|
131
|
+
await delay(interval);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return { records: combinedResults };
|
|
135
|
+
}
|
|
136
|
+
export async function replaceRecords({ apiKey, baseId, tableId, records, options, retry, }) {
|
|
137
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
138
|
+
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
139
|
+
}
|
|
140
|
+
const chunkSize = 10;
|
|
141
|
+
const chunks = [];
|
|
142
|
+
for (let i = 0; i < records.length; i += chunkSize) {
|
|
143
|
+
chunks.push(records.slice(i, i + chunkSize));
|
|
144
|
+
}
|
|
145
|
+
let combinedResults = [];
|
|
146
|
+
for (const chunk of chunks) {
|
|
147
|
+
const result = await airtableRequest({
|
|
148
|
+
apiKey,
|
|
149
|
+
baseId,
|
|
150
|
+
tableId,
|
|
151
|
+
endpoint: '/',
|
|
152
|
+
method: 'PUT',
|
|
153
|
+
body: {
|
|
154
|
+
records: chunk,
|
|
155
|
+
typecast: options?.typecast ?? false,
|
|
156
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
157
|
+
},
|
|
158
|
+
retry,
|
|
85
159
|
});
|
|
86
160
|
combinedResults = combinedResults.concat(result.records);
|
|
87
161
|
if (records.length > chunkSize &&
|
|
@@ -92,7 +166,7 @@ export async function updateRecords({ apiKey, baseId, tableId, records, options,
|
|
|
92
166
|
}
|
|
93
167
|
return { records: combinedResults };
|
|
94
168
|
}
|
|
95
|
-
export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, options, }) {
|
|
169
|
+
export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, options, retry, }) {
|
|
96
170
|
if (!Array.isArray(records) || records.length === 0) {
|
|
97
171
|
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
98
172
|
}
|
|
@@ -121,6 +195,7 @@ export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, op
|
|
|
121
195
|
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
122
196
|
performUpsert: { fieldsToMergeOn: options.fieldsToMergeOn },
|
|
123
197
|
},
|
|
198
|
+
retry,
|
|
124
199
|
});
|
|
125
200
|
allCreatedRecords = allCreatedRecords.concat(result.createdRecords);
|
|
126
201
|
allUpdatedRecords = allUpdatedRecords.concat(result.updatedRecords);
|
|
@@ -136,7 +211,17 @@ export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, op
|
|
|
136
211
|
records: allRecords,
|
|
137
212
|
};
|
|
138
213
|
}
|
|
139
|
-
export async function
|
|
214
|
+
export async function deleteRecord({ apiKey, baseId, tableId, recordId, retry, }) {
|
|
215
|
+
return await airtableRequest({
|
|
216
|
+
apiKey,
|
|
217
|
+
baseId,
|
|
218
|
+
tableId,
|
|
219
|
+
endpoint: `/${recordId}`,
|
|
220
|
+
method: 'DELETE',
|
|
221
|
+
retry,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
export async function deleteRecords({ apiKey, baseId, tableId, recordIds, options, retry, }) {
|
|
140
225
|
if (!Array.isArray(recordIds) || recordIds.length === 0) {
|
|
141
226
|
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
227
|
}
|
|
@@ -147,13 +232,15 @@ export async function deleteRecords({ apiKey, baseId, tableId, recordIds, option
|
|
|
147
232
|
}
|
|
148
233
|
let combinedResults = [];
|
|
149
234
|
for (const chunk of chunks) {
|
|
150
|
-
const query = chunk.map((id) => `records[]=${id}`).join('&');
|
|
151
235
|
const result = await airtableRequest({
|
|
152
236
|
apiKey,
|
|
153
237
|
baseId,
|
|
154
238
|
tableId,
|
|
155
|
-
endpoint:
|
|
239
|
+
endpoint: appendQueryToEndpoint('', {
|
|
240
|
+
'records[]': chunk,
|
|
241
|
+
}),
|
|
156
242
|
method: 'DELETE',
|
|
243
|
+
retry,
|
|
157
244
|
});
|
|
158
245
|
combinedResults = combinedResults.concat(result.records);
|
|
159
246
|
if (recordIds.length > chunkSize && chunks.indexOf(chunk) < chunks.length - 1) {
|
|
@@ -163,7 +250,7 @@ export async function deleteRecords({ apiKey, baseId, tableId, recordIds, option
|
|
|
163
250
|
}
|
|
164
251
|
return { records: combinedResults };
|
|
165
252
|
}
|
|
166
|
-
export async function createRecord({ apiKey, baseId, tableId, fields, options, }) {
|
|
253
|
+
export async function createRecord({ apiKey, baseId, tableId, fields, options, retry, }) {
|
|
167
254
|
return await airtableRequest({
|
|
168
255
|
apiKey,
|
|
169
256
|
baseId,
|
|
@@ -175,9 +262,10 @@ export async function createRecord({ apiKey, baseId, tableId, fields, options, }
|
|
|
175
262
|
typecast: options?.typecast ?? false,
|
|
176
263
|
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
177
264
|
},
|
|
265
|
+
retry,
|
|
178
266
|
});
|
|
179
267
|
}
|
|
180
|
-
export async function createRecords({ apiKey, baseId, tableId, records, options, }) {
|
|
268
|
+
export async function createRecords({ apiKey, baseId, tableId, records, options, retry, }) {
|
|
181
269
|
if (!Array.isArray(records) || records.length === 0) {
|
|
182
270
|
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to create.');
|
|
183
271
|
}
|
|
@@ -199,6 +287,7 @@ export async function createRecords({ apiKey, baseId, tableId, records, options,
|
|
|
199
287
|
typecast: options?.typecast ?? false,
|
|
200
288
|
returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
|
|
201
289
|
},
|
|
290
|
+
retry,
|
|
202
291
|
});
|
|
203
292
|
combinedResults = combinedResults.concat(result.records);
|
|
204
293
|
if (records.length > chunkSize &&
|
package/lib/requests.d.ts
CHANGED
|
@@ -1,5 +1,67 @@
|
|
|
1
1
|
import { RequestMethods } from './types/tables.js';
|
|
2
|
-
|
|
2
|
+
export type AirtableQueryValue = string | number | boolean | Array<string | number | boolean> | undefined;
|
|
3
|
+
export type AirtableRetryOptions = {
|
|
4
|
+
maxRetries?: number;
|
|
5
|
+
baseDelayMs?: number;
|
|
6
|
+
maxDelayMs?: number;
|
|
7
|
+
retryOn429?: boolean;
|
|
8
|
+
retryOn5xx?: boolean;
|
|
9
|
+
retryOnNetworkErrors?: boolean;
|
|
10
|
+
useJitter?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare class AirtableApiError extends Error {
|
|
13
|
+
statusCode?: number;
|
|
14
|
+
airtableType?: string;
|
|
15
|
+
retryable: boolean;
|
|
16
|
+
retryAfterMs?: number;
|
|
17
|
+
cause?: unknown;
|
|
18
|
+
request?: {
|
|
19
|
+
method: string;
|
|
20
|
+
baseId?: string;
|
|
21
|
+
tableId?: string;
|
|
22
|
+
path?: string;
|
|
23
|
+
};
|
|
24
|
+
constructor({ message, statusCode, airtableType, retryable, request, retryAfterMs, cause, }: {
|
|
25
|
+
message: string;
|
|
26
|
+
statusCode?: number;
|
|
27
|
+
airtableType?: string;
|
|
28
|
+
retryable: boolean;
|
|
29
|
+
retryAfterMs?: number;
|
|
30
|
+
cause?: unknown;
|
|
31
|
+
request?: {
|
|
32
|
+
method: string;
|
|
33
|
+
baseId?: string;
|
|
34
|
+
tableId?: string;
|
|
35
|
+
path?: string;
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
export declare function buildQueryString(query: Record<string, AirtableQueryValue>): string;
|
|
40
|
+
export declare function appendQueryToEndpoint(endpoint: string, query: Record<string, AirtableQueryValue>): string;
|
|
41
|
+
export declare function airtableApiRequest<T>(request: {
|
|
42
|
+
apiKey: string;
|
|
43
|
+
path: string;
|
|
44
|
+
method: RequestMethods;
|
|
45
|
+
query?: Record<string, AirtableQueryValue>;
|
|
46
|
+
body?: unknown;
|
|
47
|
+
apiURL?: string;
|
|
48
|
+
retry?: AirtableRetryOptions;
|
|
49
|
+
requestContext?: {
|
|
50
|
+
method: string;
|
|
51
|
+
baseId?: string;
|
|
52
|
+
tableId?: string;
|
|
53
|
+
path?: string;
|
|
54
|
+
};
|
|
55
|
+
}): Promise<T>;
|
|
56
|
+
export declare function airtableRequestRaw<T = unknown>(request: {
|
|
57
|
+
apiKey: string;
|
|
58
|
+
method: RequestMethods;
|
|
59
|
+
path: string;
|
|
60
|
+
query?: Record<string, AirtableQueryValue>;
|
|
61
|
+
body?: unknown;
|
|
62
|
+
apiURL?: string;
|
|
63
|
+
retry?: AirtableRetryOptions;
|
|
64
|
+
}): Promise<T>;
|
|
3
65
|
export declare function airtableRequest<T>(request: {
|
|
4
66
|
apiKey: string;
|
|
5
67
|
baseId: string;
|
|
@@ -8,8 +70,5 @@ export declare function airtableRequest<T>(request: {
|
|
|
8
70
|
method: RequestMethods;
|
|
9
71
|
body?: object;
|
|
10
72
|
apiURL?: string;
|
|
73
|
+
retry?: AirtableRetryOptions;
|
|
11
74
|
}): Promise<T>;
|
|
12
|
-
export declare function getBaseSchema({ apiKey, baseId, }: {
|
|
13
|
-
apiKey: string;
|
|
14
|
-
baseId: string;
|
|
15
|
-
}): Promise<AirtableBaseSchema>;
|
package/lib/requests.js
CHANGED
|
@@ -1,29 +1,229 @@
|
|
|
1
1
|
import { request } from 'https';
|
|
2
2
|
import { URL } from 'url';
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { delay } from './utils.js';
|
|
4
|
+
const DEFAULT_RETRY_OPTIONS = {
|
|
5
|
+
maxRetries: 5,
|
|
6
|
+
baseDelayMs: 500,
|
|
7
|
+
maxDelayMs: 8000,
|
|
8
|
+
retryOn429: true,
|
|
9
|
+
retryOn5xx: true,
|
|
10
|
+
retryOnNetworkErrors: true,
|
|
11
|
+
useJitter: true,
|
|
12
|
+
};
|
|
13
|
+
export class AirtableApiError extends Error {
|
|
14
|
+
statusCode;
|
|
15
|
+
airtableType;
|
|
16
|
+
retryable;
|
|
17
|
+
retryAfterMs;
|
|
18
|
+
cause;
|
|
19
|
+
request;
|
|
20
|
+
constructor({ message, statusCode, airtableType, retryable, request, retryAfterMs, cause, }) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = 'AirtableApiError';
|
|
23
|
+
this.statusCode = statusCode;
|
|
24
|
+
this.airtableType = airtableType;
|
|
25
|
+
this.retryable = retryable;
|
|
26
|
+
this.retryAfterMs = retryAfterMs;
|
|
27
|
+
this.cause = cause;
|
|
28
|
+
this.request = request;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function resolveRetryOptions(options) {
|
|
32
|
+
const maxRetries = typeof options?.maxRetries === 'number' && Number.isFinite(options.maxRetries)
|
|
33
|
+
? Math.max(0, Math.floor(options.maxRetries))
|
|
34
|
+
: DEFAULT_RETRY_OPTIONS.maxRetries;
|
|
35
|
+
const baseDelayMs = typeof options?.baseDelayMs === 'number' && Number.isFinite(options.baseDelayMs)
|
|
36
|
+
? Math.max(0, options.baseDelayMs)
|
|
37
|
+
: DEFAULT_RETRY_OPTIONS.baseDelayMs;
|
|
38
|
+
const maxDelayMs = typeof options?.maxDelayMs === 'number' && Number.isFinite(options.maxDelayMs)
|
|
39
|
+
? Math.max(0, options.maxDelayMs)
|
|
40
|
+
: DEFAULT_RETRY_OPTIONS.maxDelayMs;
|
|
41
|
+
return {
|
|
42
|
+
maxRetries,
|
|
43
|
+
baseDelayMs,
|
|
44
|
+
maxDelayMs,
|
|
45
|
+
retryOn429: options?.retryOn429 ?? DEFAULT_RETRY_OPTIONS.retryOn429,
|
|
46
|
+
retryOn5xx: options?.retryOn5xx ?? DEFAULT_RETRY_OPTIONS.retryOn5xx,
|
|
47
|
+
retryOnNetworkErrors: options?.retryOnNetworkErrors ??
|
|
48
|
+
DEFAULT_RETRY_OPTIONS.retryOnNetworkErrors,
|
|
49
|
+
useJitter: options?.useJitter ?? DEFAULT_RETRY_OPTIONS.useJitter,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function shouldRetryError(error, attempt, retryOptions) {
|
|
53
|
+
if (!(error instanceof AirtableApiError) || !error.retryable) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
if (attempt >= retryOptions.maxRetries) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (error.statusCode === 429) {
|
|
60
|
+
return retryOptions.retryOn429;
|
|
61
|
+
}
|
|
62
|
+
if (error.statusCode !== undefined && error.statusCode >= 500) {
|
|
63
|
+
return retryOptions.retryOn5xx;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
function isRetryableNetworkError(error) {
|
|
68
|
+
if (!(error instanceof Error))
|
|
69
|
+
return false;
|
|
70
|
+
const message = error.message.toLowerCase();
|
|
71
|
+
return (message.includes('socket hang up') ||
|
|
72
|
+
message.includes('timed out') ||
|
|
73
|
+
message.includes('econnreset') ||
|
|
74
|
+
message.includes('econnrefused') ||
|
|
75
|
+
message.includes('network'));
|
|
76
|
+
}
|
|
77
|
+
function getRetryDelayMs(error, attempt, retryOptions) {
|
|
78
|
+
if (error.retryAfterMs !== undefined) {
|
|
79
|
+
return error.retryAfterMs;
|
|
80
|
+
}
|
|
81
|
+
const cappedDelay = Math.min(retryOptions.baseDelayMs * 2 ** attempt, retryOptions.maxDelayMs);
|
|
82
|
+
if (!retryOptions.useJitter) {
|
|
83
|
+
return cappedDelay;
|
|
84
|
+
}
|
|
85
|
+
return Math.round(Math.random() * cappedDelay);
|
|
86
|
+
}
|
|
87
|
+
function parseRetryAfterMs(value) {
|
|
88
|
+
const rawValue = Array.isArray(value) ? value[0] : value;
|
|
89
|
+
if (!rawValue)
|
|
90
|
+
return undefined;
|
|
91
|
+
const seconds = Number(rawValue);
|
|
92
|
+
if (!Number.isNaN(seconds) && seconds >= 0) {
|
|
93
|
+
return seconds * 1000;
|
|
94
|
+
}
|
|
95
|
+
const retryAt = Date.parse(rawValue);
|
|
96
|
+
if (Number.isNaN(retryAt))
|
|
97
|
+
return undefined;
|
|
98
|
+
return Math.max(retryAt - Date.now(), 0);
|
|
99
|
+
}
|
|
100
|
+
export function buildQueryString(query) {
|
|
101
|
+
const params = new URLSearchParams();
|
|
102
|
+
for (const [key, value] of Object.entries(query)) {
|
|
103
|
+
if (value === undefined)
|
|
104
|
+
continue;
|
|
105
|
+
if (Array.isArray(value)) {
|
|
106
|
+
value.forEach((entry) => {
|
|
107
|
+
params.append(key, String(entry));
|
|
108
|
+
});
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
params.append(key, String(value));
|
|
112
|
+
}
|
|
113
|
+
return params.toString();
|
|
114
|
+
}
|
|
115
|
+
export function appendQueryToEndpoint(endpoint, query) {
|
|
116
|
+
const queryString = buildQueryString(query);
|
|
117
|
+
if (!queryString)
|
|
118
|
+
return endpoint;
|
|
119
|
+
return `${endpoint}${endpoint.includes('?') ? '&' : '?'}${queryString}`;
|
|
120
|
+
}
|
|
121
|
+
function buildAirtableApiUrl({ apiURL, path, }) {
|
|
122
|
+
const url = apiURL || 'https://api.airtable.com/v0';
|
|
123
|
+
return `${url}${path}`;
|
|
124
|
+
}
|
|
125
|
+
function normalizeApiPath(path, apiURL) {
|
|
5
126
|
const url = apiURL || 'https://api.airtable.com/v0';
|
|
127
|
+
if (url.endsWith('/v0') && (path === '/v0' || path.startsWith('/v0/'))) {
|
|
128
|
+
return path.slice('/v0'.length);
|
|
129
|
+
}
|
|
130
|
+
return path;
|
|
131
|
+
}
|
|
132
|
+
export async function airtableApiRequest(request) {
|
|
133
|
+
const { apiKey, path, method, query, body, apiURL, retry, requestContext, } = request;
|
|
6
134
|
if (!apiKey) {
|
|
7
135
|
throw new Error('API key is not set. Please provide a valid Airtable API key.');
|
|
8
136
|
}
|
|
137
|
+
if (!path.startsWith('/')) {
|
|
138
|
+
throw new Error('Airtable API paths must start with "/".');
|
|
139
|
+
}
|
|
140
|
+
const resolvedPath = appendQueryToEndpoint(path, query ?? {});
|
|
141
|
+
const context = requestContext ?? {
|
|
142
|
+
method,
|
|
143
|
+
path: resolvedPath,
|
|
144
|
+
};
|
|
145
|
+
const retryOptions = resolveRetryOptions(retry);
|
|
146
|
+
for (let attempt = 0;; attempt += 1) {
|
|
147
|
+
try {
|
|
148
|
+
const response = await apiRequest({
|
|
149
|
+
url: buildAirtableApiUrl({
|
|
150
|
+
apiURL,
|
|
151
|
+
path: resolvedPath,
|
|
152
|
+
}),
|
|
153
|
+
apiKey,
|
|
154
|
+
method,
|
|
155
|
+
body,
|
|
156
|
+
});
|
|
157
|
+
validateResponse(response, context);
|
|
158
|
+
return response.data;
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
if (shouldRetryError(error, attempt, retryOptions)) {
|
|
162
|
+
await delay(getRetryDelayMs(error, attempt, retryOptions));
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (error instanceof Error &&
|
|
166
|
+
retryOptions.retryOnNetworkErrors &&
|
|
167
|
+
isRetryableNetworkError(error) &&
|
|
168
|
+
attempt < retryOptions.maxRetries) {
|
|
169
|
+
const retryableError = new AirtableApiError({
|
|
170
|
+
message: error.message,
|
|
171
|
+
retryable: true,
|
|
172
|
+
request: context,
|
|
173
|
+
cause: error,
|
|
174
|
+
});
|
|
175
|
+
await delay(getRetryDelayMs(retryableError, attempt, retryOptions));
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
export async function airtableRequestRaw(request) {
|
|
183
|
+
const { apiKey, method, path, query, body, apiURL, retry } = request;
|
|
184
|
+
return airtableApiRequest({
|
|
185
|
+
apiKey,
|
|
186
|
+
method,
|
|
187
|
+
query,
|
|
188
|
+
body,
|
|
189
|
+
apiURL,
|
|
190
|
+
retry,
|
|
191
|
+
path: normalizeApiPath(path, apiURL),
|
|
192
|
+
requestContext: {
|
|
193
|
+
method,
|
|
194
|
+
path: appendQueryToEndpoint(path, query ?? {}),
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
export async function airtableRequest(request) {
|
|
199
|
+
const { apiKey, baseId, tableId, endpoint, method, body, apiURL, retry } = request;
|
|
9
200
|
if (!baseId) {
|
|
10
201
|
throw new Error('Base ID is not set. Please provide a valid Airtable base ID.');
|
|
11
202
|
}
|
|
12
203
|
if (!tableId) {
|
|
13
204
|
throw new Error('Table ID/Name is not set. Please provide a valid Airtable table ID or name.');
|
|
14
205
|
}
|
|
15
|
-
|
|
16
|
-
url: `${url}/${baseId}/${tableId}${endpoint}`,
|
|
206
|
+
return airtableApiRequest({
|
|
17
207
|
apiKey,
|
|
18
208
|
method,
|
|
19
209
|
body,
|
|
210
|
+
apiURL,
|
|
211
|
+
path: `/${encodeURIComponent(baseId)}/${encodeURIComponent(tableId)}${endpoint}`,
|
|
212
|
+
retry,
|
|
213
|
+
requestContext: {
|
|
214
|
+
method,
|
|
215
|
+
baseId,
|
|
216
|
+
tableId,
|
|
217
|
+
path: endpoint,
|
|
218
|
+
},
|
|
20
219
|
});
|
|
21
|
-
validateResponse(response);
|
|
22
|
-
return response.data;
|
|
23
220
|
}
|
|
24
|
-
function validateResponse(response) {
|
|
221
|
+
function validateResponse(response, requestContext) {
|
|
25
222
|
const statusCode = response.statusCode;
|
|
26
|
-
|
|
223
|
+
const retryAfterMs = parseRetryAfterMs(response.headers?.['retry-after']);
|
|
224
|
+
if (statusCode !== undefined &&
|
|
225
|
+
statusCode >= 200 &&
|
|
226
|
+
statusCode < 300)
|
|
27
227
|
return;
|
|
28
228
|
const isAirtableError = (data) => {
|
|
29
229
|
return (data !== null &&
|
|
@@ -35,27 +235,88 @@ function validateResponse(response) {
|
|
|
35
235
|
typeof data.error.message === 'string');
|
|
36
236
|
};
|
|
37
237
|
if (response.data && isAirtableError(response.data)) {
|
|
38
|
-
throw new
|
|
238
|
+
throw new AirtableApiError({
|
|
239
|
+
message: response.data.error.message,
|
|
240
|
+
statusCode,
|
|
241
|
+
airtableType: response.data.error.type,
|
|
242
|
+
retryable: statusCode === 429 || statusCode === 500 || statusCode === 503,
|
|
243
|
+
request: requestContext,
|
|
244
|
+
retryAfterMs,
|
|
245
|
+
});
|
|
39
246
|
}
|
|
40
247
|
if (statusCode === 401)
|
|
41
|
-
throw new
|
|
248
|
+
throw new AirtableApiError({
|
|
249
|
+
message: 'Incorrect API Key.',
|
|
250
|
+
statusCode,
|
|
251
|
+
retryable: false,
|
|
252
|
+
request: requestContext,
|
|
253
|
+
retryAfterMs,
|
|
254
|
+
});
|
|
42
255
|
else if (statusCode === 403)
|
|
43
|
-
throw new
|
|
256
|
+
throw new AirtableApiError({
|
|
257
|
+
message: 'Not authorized.',
|
|
258
|
+
statusCode,
|
|
259
|
+
retryable: false,
|
|
260
|
+
request: requestContext,
|
|
261
|
+
retryAfterMs,
|
|
262
|
+
});
|
|
44
263
|
else if (statusCode === 404)
|
|
45
|
-
throw new
|
|
264
|
+
throw new AirtableApiError({
|
|
265
|
+
message: 'Table or record not found.',
|
|
266
|
+
statusCode,
|
|
267
|
+
retryable: false,
|
|
268
|
+
request: requestContext,
|
|
269
|
+
retryAfterMs,
|
|
270
|
+
});
|
|
46
271
|
else if (statusCode === 413)
|
|
47
|
-
throw new
|
|
272
|
+
throw new AirtableApiError({
|
|
273
|
+
message: 'Request body is too large.',
|
|
274
|
+
statusCode,
|
|
275
|
+
retryable: false,
|
|
276
|
+
request: requestContext,
|
|
277
|
+
retryAfterMs,
|
|
278
|
+
});
|
|
48
279
|
else if (statusCode === 422) {
|
|
49
|
-
throw new
|
|
280
|
+
throw new AirtableApiError({
|
|
281
|
+
message: 'Operation cannot be processed. Do the field names match?',
|
|
282
|
+
statusCode,
|
|
283
|
+
retryable: false,
|
|
284
|
+
request: requestContext,
|
|
285
|
+
retryAfterMs,
|
|
286
|
+
});
|
|
50
287
|
}
|
|
51
288
|
else if (statusCode === 429) {
|
|
52
|
-
throw new
|
|
289
|
+
throw new AirtableApiError({
|
|
290
|
+
message: 'Too many requests to the Airtable server.',
|
|
291
|
+
statusCode,
|
|
292
|
+
retryable: true,
|
|
293
|
+
request: requestContext,
|
|
294
|
+
retryAfterMs,
|
|
295
|
+
});
|
|
53
296
|
}
|
|
54
297
|
else if (statusCode === 500)
|
|
55
|
-
throw new
|
|
298
|
+
throw new AirtableApiError({
|
|
299
|
+
message: 'Airtable server error.',
|
|
300
|
+
statusCode,
|
|
301
|
+
retryable: true,
|
|
302
|
+
request: requestContext,
|
|
303
|
+
retryAfterMs,
|
|
304
|
+
});
|
|
56
305
|
else if (statusCode === 503)
|
|
57
|
-
throw new
|
|
58
|
-
|
|
306
|
+
throw new AirtableApiError({
|
|
307
|
+
message: 'Airtable service unavailable.',
|
|
308
|
+
statusCode,
|
|
309
|
+
retryable: true,
|
|
310
|
+
request: requestContext,
|
|
311
|
+
retryAfterMs,
|
|
312
|
+
});
|
|
313
|
+
throw new AirtableApiError({
|
|
314
|
+
message: 'Unexpected error.',
|
|
315
|
+
statusCode,
|
|
316
|
+
retryable: false,
|
|
317
|
+
request: requestContext,
|
|
318
|
+
retryAfterMs,
|
|
319
|
+
});
|
|
59
320
|
}
|
|
60
321
|
async function apiRequest({ url, method, apiKey, body, }) {
|
|
61
322
|
return new Promise((resolve, reject) => {
|
|
@@ -82,6 +343,7 @@ async function apiRequest({ url, method, apiKey, body, }) {
|
|
|
82
343
|
data: JSON.parse(data),
|
|
83
344
|
statusCode: res.statusCode,
|
|
84
345
|
statusMessage: res.statusMessage,
|
|
346
|
+
headers: res.headers,
|
|
85
347
|
});
|
|
86
348
|
}
|
|
87
349
|
catch (error) {
|
|
@@ -98,15 +360,3 @@ async function apiRequest({ url, method, apiKey, body, }) {
|
|
|
98
360
|
req.end();
|
|
99
361
|
});
|
|
100
362
|
}
|
|
101
|
-
export async function getBaseSchema({ apiKey, baseId, }) {
|
|
102
|
-
const response = await fetch(`https://api.airtable.com/v0/meta/bases/${baseId}/tables`, {
|
|
103
|
-
method: 'GET',
|
|
104
|
-
headers: {
|
|
105
|
-
Authorization: `Bearer ${apiKey}`,
|
|
106
|
-
},
|
|
107
|
-
});
|
|
108
|
-
if (!response.ok) {
|
|
109
|
-
throw new Error(`Error fetching base schema: ${response.statusText}`);
|
|
110
|
-
}
|
|
111
|
-
return response.json();
|
|
112
|
-
}
|
package/lib/types/bases.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AirtableBaseSchema } from './metadata.js';
|
|
1
2
|
type BaseList = {
|
|
2
3
|
id: string;
|
|
3
4
|
name: string;
|
|
@@ -7,57 +8,24 @@ export type ListBasesResponse = {
|
|
|
7
8
|
bases: BaseList[];
|
|
8
9
|
offset?: string;
|
|
9
10
|
};
|
|
10
|
-
type
|
|
11
|
-
inverseLinkFieldId?: string;
|
|
12
|
-
isReversed?: boolean;
|
|
13
|
-
linkedTableId?: string;
|
|
14
|
-
prefersSingleRecordLink?: boolean;
|
|
15
|
-
};
|
|
16
|
-
type Field = {
|
|
17
|
-
description?: string;
|
|
18
|
-
id: string;
|
|
11
|
+
export type CreateBaseFieldConfig = {
|
|
19
12
|
name: string;
|
|
20
13
|
type: string;
|
|
21
|
-
options?: BasesFieldOptions;
|
|
22
|
-
};
|
|
23
|
-
type TableView = {
|
|
24
|
-
id: string;
|
|
25
|
-
name: string;
|
|
26
|
-
type: string;
|
|
27
|
-
};
|
|
28
|
-
type Table = {
|
|
29
14
|
description?: string;
|
|
30
|
-
|
|
31
|
-
id: string;
|
|
32
|
-
name: string;
|
|
33
|
-
primaryFieldId: string;
|
|
34
|
-
views: TableView[];
|
|
35
|
-
};
|
|
36
|
-
export type GetBaseSchemaResponse = {
|
|
37
|
-
tables: Table[];
|
|
38
|
-
};
|
|
39
|
-
type TableConfigFieldOptions = {
|
|
40
|
-
color?: string;
|
|
41
|
-
icon?: string;
|
|
15
|
+
options?: Record<string, unknown>;
|
|
42
16
|
};
|
|
43
|
-
type
|
|
44
|
-
description?: string;
|
|
17
|
+
export type CreateBaseTableConfig = {
|
|
45
18
|
name: string;
|
|
46
|
-
type: string;
|
|
47
|
-
options?: TableConfigFieldOptions;
|
|
48
|
-
};
|
|
49
|
-
type BasesTableConfig = {
|
|
50
19
|
description?: string;
|
|
51
|
-
fields:
|
|
52
|
-
name: string;
|
|
20
|
+
fields: CreateBaseFieldConfig[];
|
|
53
21
|
};
|
|
54
22
|
export type CreateBaseRequestBody = {
|
|
55
|
-
name: string;
|
|
56
|
-
tables: BasesTableConfig[];
|
|
57
23
|
workspaceId: string;
|
|
24
|
+
name: string;
|
|
25
|
+
tables: CreateBaseTableConfig[];
|
|
58
26
|
};
|
|
59
27
|
export type CreateBaseResponse = {
|
|
60
28
|
id: string;
|
|
61
|
-
tables:
|
|
29
|
+
tables: AirtableBaseSchema['tables'];
|
|
62
30
|
};
|
|
63
31
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-airtable-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Lightweight TypeScript-first Airtable client with metadata-based type generation.",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -16,14 +16,15 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"bin": {
|
|
19
|
-
"easy-airtable-api": "
|
|
19
|
+
"easy-airtable-api": "lib/cli.js"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"watch": "tsc --watch",
|
|
23
23
|
"build": "rm -r ./lib && tsc",
|
|
24
|
-
"
|
|
24
|
+
"prepublishOnly": "npm test && npm run build",
|
|
25
|
+
"release": "npm run prepublishOnly && npm pack && npm publish --ignore-scripts",
|
|
25
26
|
"lint": "eslint 'src/**/*.{ts,tsx}'",
|
|
26
|
-
"test": "npm run test:
|
|
27
|
+
"test": "npm run test:all",
|
|
27
28
|
"test:codegen": "vitest run tests/codegen.test.ts",
|
|
28
29
|
"test:integration": "RUN_AIRTABLE_INTEGRATION_TESTS=true vitest run tests/get-record.test.ts tests/get-records.test.ts tests/create-records.test.ts tests/update-records.test.ts tests/delete-records.test.ts",
|
|
29
30
|
"test:all": "vitest run"
|
|
@@ -33,7 +34,7 @@
|
|
|
33
34
|
],
|
|
34
35
|
"repository": {
|
|
35
36
|
"type": "git",
|
|
36
|
-
"url": "https://github.com/marcellocurto/easy-airtable-api.git"
|
|
37
|
+
"url": "git+https://github.com/marcellocurto/easy-airtable-api.git"
|
|
37
38
|
},
|
|
38
39
|
"homepage": "https://marcellocurto.com/code/easy-airtable-api",
|
|
39
40
|
"author": "Marcello Curto",
|