easy-airtable-api 0.1.0 → 0.2.0
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 +30 -0
- package/README.md +55 -1
- package/package.json +5 -2
- package/easy-airtable-api-0.1.0.tgz +0 -0
- package/lib/bases.d.ts +0 -18
- package/lib/bases.js +0 -40
- package/lib/cli.d.ts +0 -2
- package/lib/cli.js +0 -82
- package/lib/codegen/build.d.ts +0 -2
- package/lib/codegen/build.js +0 -25
- package/lib/codegen/emit-typescript.d.ts +0 -8
- package/lib/codegen/emit-typescript.js +0 -117
- package/lib/codegen/generate.d.ts +0 -2
- package/lib/codegen/generate.js +0 -23
- package/lib/codegen/identifiers.d.ts +0 -11
- package/lib/codegen/identifiers.js +0 -73
- package/lib/codegen/index.d.ts +0 -3
- package/lib/codegen/index.js +0 -2
- package/lib/codegen/load-schema.d.ts +0 -3
- package/lib/codegen/load-schema.js +0 -28
- package/lib/codegen/map-field.d.ts +0 -30
- package/lib/codegen/map-field.js +0 -296
- package/lib/codegen/normalize-schema.d.ts +0 -11
- package/lib/codegen/normalize-schema.js +0 -79
- package/lib/codegen/types.d.ts +0 -64
- package/lib/codegen/types.js +0 -1
- package/lib/codegen/utils.d.ts +0 -14
- package/lib/codegen/utils.js +0 -30
- package/lib/codegen/write-atomic.d.ts +0 -1
- package/lib/codegen/write-atomic.js +0 -11
- package/lib/index.d.ts +0 -8
- package/lib/index.js +0 -3
- package/lib/records.d.ts +0 -197
- package/lib/records.js +0 -300
- package/lib/requests.d.ts +0 -74
- package/lib/requests.js +0 -362
- package/lib/types/bases.d.ts +0 -31
- package/lib/types/bases.js +0 -1
- package/lib/types/fields.d.ts +0 -330
- package/lib/types/fields.js +0 -1
- package/lib/types/metadata.d.ts +0 -24
- package/lib/types/metadata.js +0 -1
- package/lib/types/records.d.ts +0 -130
- package/lib/types/records.js +0 -1
- package/lib/types/tables.d.ts +0 -99
- package/lib/types/tables.js +0 -1
- package/lib/utils.d.ts +0 -4
- package/lib/utils.js +0 -19
package/lib/requests.js
DELETED
|
@@ -1,362 +0,0 @@
|
|
|
1
|
-
import { request } from 'https';
|
|
2
|
-
import { URL } from 'url';
|
|
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) {
|
|
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;
|
|
134
|
-
if (!apiKey) {
|
|
135
|
-
throw new Error('API key is not set. Please provide a valid Airtable API key.');
|
|
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;
|
|
200
|
-
if (!baseId) {
|
|
201
|
-
throw new Error('Base ID is not set. Please provide a valid Airtable base ID.');
|
|
202
|
-
}
|
|
203
|
-
if (!tableId) {
|
|
204
|
-
throw new Error('Table ID/Name is not set. Please provide a valid Airtable table ID or name.');
|
|
205
|
-
}
|
|
206
|
-
return airtableApiRequest({
|
|
207
|
-
apiKey,
|
|
208
|
-
method,
|
|
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
|
-
},
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
function validateResponse(response, requestContext) {
|
|
222
|
-
const statusCode = response.statusCode;
|
|
223
|
-
const retryAfterMs = parseRetryAfterMs(response.headers?.['retry-after']);
|
|
224
|
-
if (statusCode !== undefined &&
|
|
225
|
-
statusCode >= 200 &&
|
|
226
|
-
statusCode < 300)
|
|
227
|
-
return;
|
|
228
|
-
const isAirtableError = (data) => {
|
|
229
|
-
return (data !== null &&
|
|
230
|
-
typeof data === 'object' &&
|
|
231
|
-
'error' in data &&
|
|
232
|
-
typeof data.error === 'object' &&
|
|
233
|
-
data.error !== null &&
|
|
234
|
-
'message' in data.error &&
|
|
235
|
-
typeof data.error.message === 'string');
|
|
236
|
-
};
|
|
237
|
-
if (response.data && isAirtableError(response.data)) {
|
|
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
|
-
});
|
|
246
|
-
}
|
|
247
|
-
if (statusCode === 401)
|
|
248
|
-
throw new AirtableApiError({
|
|
249
|
-
message: 'Incorrect API Key.',
|
|
250
|
-
statusCode,
|
|
251
|
-
retryable: false,
|
|
252
|
-
request: requestContext,
|
|
253
|
-
retryAfterMs,
|
|
254
|
-
});
|
|
255
|
-
else if (statusCode === 403)
|
|
256
|
-
throw new AirtableApiError({
|
|
257
|
-
message: 'Not authorized.',
|
|
258
|
-
statusCode,
|
|
259
|
-
retryable: false,
|
|
260
|
-
request: requestContext,
|
|
261
|
-
retryAfterMs,
|
|
262
|
-
});
|
|
263
|
-
else if (statusCode === 404)
|
|
264
|
-
throw new AirtableApiError({
|
|
265
|
-
message: 'Table or record not found.',
|
|
266
|
-
statusCode,
|
|
267
|
-
retryable: false,
|
|
268
|
-
request: requestContext,
|
|
269
|
-
retryAfterMs,
|
|
270
|
-
});
|
|
271
|
-
else if (statusCode === 413)
|
|
272
|
-
throw new AirtableApiError({
|
|
273
|
-
message: 'Request body is too large.',
|
|
274
|
-
statusCode,
|
|
275
|
-
retryable: false,
|
|
276
|
-
request: requestContext,
|
|
277
|
-
retryAfterMs,
|
|
278
|
-
});
|
|
279
|
-
else if (statusCode === 422) {
|
|
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
|
-
});
|
|
287
|
-
}
|
|
288
|
-
else if (statusCode === 429) {
|
|
289
|
-
throw new AirtableApiError({
|
|
290
|
-
message: 'Too many requests to the Airtable server.',
|
|
291
|
-
statusCode,
|
|
292
|
-
retryable: true,
|
|
293
|
-
request: requestContext,
|
|
294
|
-
retryAfterMs,
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
else if (statusCode === 500)
|
|
298
|
-
throw new AirtableApiError({
|
|
299
|
-
message: 'Airtable server error.',
|
|
300
|
-
statusCode,
|
|
301
|
-
retryable: true,
|
|
302
|
-
request: requestContext,
|
|
303
|
-
retryAfterMs,
|
|
304
|
-
});
|
|
305
|
-
else if (statusCode === 503)
|
|
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
|
-
});
|
|
320
|
-
}
|
|
321
|
-
async function apiRequest({ url, method, apiKey, body, }) {
|
|
322
|
-
return new Promise((resolve, reject) => {
|
|
323
|
-
const parsedUrl = new URL(url);
|
|
324
|
-
const { hostname, pathname, search } = parsedUrl;
|
|
325
|
-
const options = {
|
|
326
|
-
method,
|
|
327
|
-
hostname,
|
|
328
|
-
path: `${pathname}${search}`,
|
|
329
|
-
timeout: 300000,
|
|
330
|
-
headers: {
|
|
331
|
-
'Content-Type': 'application/json',
|
|
332
|
-
Authorization: `Bearer ${apiKey}`,
|
|
333
|
-
},
|
|
334
|
-
};
|
|
335
|
-
const req = request(options, (res) => {
|
|
336
|
-
let data = '';
|
|
337
|
-
res.on('data', (chunk) => {
|
|
338
|
-
data += chunk;
|
|
339
|
-
});
|
|
340
|
-
res.on('end', () => {
|
|
341
|
-
try {
|
|
342
|
-
resolve({
|
|
343
|
-
data: JSON.parse(data),
|
|
344
|
-
statusCode: res.statusCode,
|
|
345
|
-
statusMessage: res.statusMessage,
|
|
346
|
-
headers: res.headers,
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
catch (error) {
|
|
350
|
-
reject(new Error(`Failed to parse response as JSON: ${data}`));
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
});
|
|
354
|
-
req.on('error', (error) => {
|
|
355
|
-
reject(new Error(`airtableApi: ${error.message} (fn_${apiRequest.name})`));
|
|
356
|
-
});
|
|
357
|
-
if (body) {
|
|
358
|
-
req.write(JSON.stringify(body));
|
|
359
|
-
}
|
|
360
|
-
req.end();
|
|
361
|
-
});
|
|
362
|
-
}
|
package/lib/types/bases.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { AirtableBaseSchema } from './metadata.js';
|
|
2
|
-
type BaseList = {
|
|
3
|
-
id: string;
|
|
4
|
-
name: string;
|
|
5
|
-
permissionLevel: 'none' | 'read' | 'comment' | 'edit' | 'create';
|
|
6
|
-
};
|
|
7
|
-
export type ListBasesResponse = {
|
|
8
|
-
bases: BaseList[];
|
|
9
|
-
offset?: string;
|
|
10
|
-
};
|
|
11
|
-
export type CreateBaseFieldConfig = {
|
|
12
|
-
name: string;
|
|
13
|
-
type: string;
|
|
14
|
-
description?: string;
|
|
15
|
-
options?: Record<string, unknown>;
|
|
16
|
-
};
|
|
17
|
-
export type CreateBaseTableConfig = {
|
|
18
|
-
name: string;
|
|
19
|
-
description?: string;
|
|
20
|
-
fields: CreateBaseFieldConfig[];
|
|
21
|
-
};
|
|
22
|
-
export type CreateBaseRequestBody = {
|
|
23
|
-
workspaceId: string;
|
|
24
|
-
name: string;
|
|
25
|
-
tables: CreateBaseTableConfig[];
|
|
26
|
-
};
|
|
27
|
-
export type CreateBaseResponse = {
|
|
28
|
-
id: string;
|
|
29
|
-
tables: AirtableBaseSchema['tables'];
|
|
30
|
-
};
|
|
31
|
-
export {};
|
package/lib/types/bases.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|