easy-airtable-api 0.0.15 → 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 +88 -0
- package/README.md +402 -130
- 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/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 +8 -2
- package/lib/index.js +3 -2
- package/lib/records.d.ts +75 -9
- package/lib/records.js +110 -21
- package/lib/requests.d.ts +65 -21
- package/lib/requests.js +281 -31
- package/lib/types/bases.d.ts +8 -40
- 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 -1
- package/lib/utils.d.ts +2 -2
- package/package.json +17 -9
- package/lib/generate-types.d.ts +0 -5
- package/lib/generate-types.js +0 -78
package/lib/records.js
CHANGED
|
@@ -1,37 +1,64 @@
|
|
|
1
|
-
import { airtableRequest } from './requests';
|
|
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,4 +1,67 @@
|
|
|
1
|
-
import { RequestMethods } from './types/tables';
|
|
1
|
+
import { RequestMethods } from './types/tables.js';
|
|
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>;
|
|
2
65
|
export declare function airtableRequest<T>(request: {
|
|
3
66
|
apiKey: string;
|
|
4
67
|
baseId: string;
|
|
@@ -7,24 +70,5 @@ export declare function airtableRequest<T>(request: {
|
|
|
7
70
|
method: RequestMethods;
|
|
8
71
|
body?: object;
|
|
9
72
|
apiURL?: string;
|
|
73
|
+
retry?: AirtableRetryOptions;
|
|
10
74
|
}): Promise<T>;
|
|
11
|
-
type AirtableField = {
|
|
12
|
-
id: string;
|
|
13
|
-
name: string;
|
|
14
|
-
type: string;
|
|
15
|
-
options?: object;
|
|
16
|
-
};
|
|
17
|
-
type AirtableTable = {
|
|
18
|
-
id: string;
|
|
19
|
-
name: string;
|
|
20
|
-
primaryFieldId: string;
|
|
21
|
-
fields: AirtableField[];
|
|
22
|
-
};
|
|
23
|
-
type AirtableBaseSchema = {
|
|
24
|
-
tables: AirtableTable[];
|
|
25
|
-
};
|
|
26
|
-
export declare function getBaseSchema({ apiKey, baseId, }: {
|
|
27
|
-
apiKey: string;
|
|
28
|
-
baseId: string;
|
|
29
|
-
}): Promise<AirtableBaseSchema>;
|
|
30
|
-
export {};
|
package/lib/requests.js
CHANGED
|
@@ -1,32 +1,232 @@
|
|
|
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
|
-
return (data &&
|
|
229
|
+
return (data !== null &&
|
|
30
230
|
typeof data === 'object' &&
|
|
31
231
|
'error' in data &&
|
|
32
232
|
typeof data.error === 'object' &&
|
|
@@ -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
|
-
}
|