easy-airtable-api 0.0.11 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  This is intended as a more accessible and user-friendly alternative to the official Airtable Node.js library.
4
4
 
5
- My focus has been on delivering an enhanced experience by utilizing types and simplifying the process of retrieving and modifying records through some helpful functions.
5
+ Our focus has been on delivering an enhanced experience by utilizing types and simplifying the process of retrieving and modifying records through some helpful functions.
6
6
 
7
- Currently, this project is in its early stages and requires significant development. Nonetheless, most methods to retrieve and update records have already been implemented.
7
+ Currently, this project is in its early stages and requires significant development. Nonetheless, most methods for retrieving and updating records have already been implemented.
8
8
 
9
9
  ## Install
10
10
 
@@ -60,6 +60,15 @@ const records = await getRecords<Fields>({
60
60
 
61
61
  ## Changelog
62
62
 
63
+ ### 0.0.13
64
+
65
+ - Added generateTypeScriptDefinitions function to generate TypeScript definitions for Airtable tables.
66
+
67
+ ### 0.0.12
68
+
69
+ - Reverted making apiKey, baseId, tableId default to undefined
70
+ - Added option to customize delay between requests via options.requestInterval
71
+
63
72
  ### 0.0.11
64
73
 
65
74
  - Make apiKey, baseId, tableId default to undefined #12
package/lib/requests.d.ts CHANGED
@@ -1,20 +1,20 @@
1
1
  import { AirtableRecord, DeleteRecordsResponse, GetRecordsQueryParameters } from './types/records';
2
2
  export declare function getRecord<Fields>({ apiKey, baseId, tableId, recordId, }: {
3
- apiKey?: string;
4
- baseId?: string;
5
- tableId?: string;
3
+ apiKey: string;
4
+ baseId: string;
5
+ tableId: string;
6
6
  recordId: string;
7
7
  }): Promise<AirtableRecord<Fields>>;
8
8
  export declare function getRecords<Fields>({ apiKey, baseId, tableId, options, }: {
9
- apiKey?: string;
10
- baseId?: string;
11
- tableId?: string;
9
+ apiKey: string;
10
+ baseId: string;
11
+ tableId: string;
12
12
  options?: GetRecordsQueryParameters;
13
13
  }): Promise<AirtableRecord<Fields>[]>;
14
14
  export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, options, }: {
15
- apiKey?: string;
16
- baseId?: string;
17
- tableId?: string;
15
+ apiKey: string;
16
+ baseId: string;
17
+ tableId: string;
18
18
  recordId: string;
19
19
  fields: Fields;
20
20
  options?: {
@@ -24,9 +24,9 @@ export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId
24
24
  };
25
25
  }): Promise<AirtableRecord<Fields>>;
26
26
  export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records, options, }: {
27
- apiKey?: string;
28
- baseId?: string;
29
- tableId?: string;
27
+ apiKey: string;
28
+ baseId: string;
29
+ tableId: string;
30
30
  records: {
31
31
  id: string;
32
32
  fields: Fields;
@@ -35,14 +35,15 @@ export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records
35
35
  typecast?: boolean;
36
36
  returnFieldsByFieldId?: boolean;
37
37
  overwriteFieldsNotSpecified?: boolean;
38
+ requestInterval?: number;
38
39
  };
39
40
  }): Promise<{
40
41
  records: AirtableRecord<Fields>[];
41
42
  }>;
42
43
  export declare function updateRecordsUpsert<Fields>({ apiKey, baseId, tableId, records, options, }: {
43
- apiKey?: string;
44
- baseId?: string;
45
- tableId?: string;
44
+ apiKey: string;
45
+ baseId: string;
46
+ tableId: string;
46
47
  records: {
47
48
  id?: string;
48
49
  fields: Fields;
@@ -52,15 +53,44 @@ export declare function updateRecordsUpsert<Fields>({ apiKey, baseId, tableId, r
52
53
  typecast?: boolean;
53
54
  returnFieldsByFieldId?: boolean;
54
55
  overwriteFieldsNotSpecified?: boolean;
56
+ requestInterval?: number;
55
57
  };
56
58
  }): Promise<{
57
59
  createdRecords: string[];
58
60
  updatedRecords: string[];
59
61
  records: AirtableRecord<Fields>[];
60
62
  }>;
61
- export declare function deleteRecords({ apiKey, baseId, tableId, recordIds, }: {
62
- apiKey?: string;
63
- baseId?: string;
64
- tableId?: string;
63
+ export declare function deleteRecords({ apiKey, baseId, tableId, recordIds, options, }: {
64
+ apiKey: string;
65
+ baseId: string;
66
+ tableId: string;
65
67
  recordIds: string[];
68
+ options?: {
69
+ requestInterval?: number;
70
+ };
66
71
  }): Promise<DeleteRecordsResponse>;
72
+ type AirtableField = {
73
+ id: string;
74
+ name: string;
75
+ type: string;
76
+ options?: object;
77
+ };
78
+ type AirtableTable = {
79
+ id: string;
80
+ name: string;
81
+ primaryFieldId: string;
82
+ fields: AirtableField[];
83
+ };
84
+ type AirtableBaseSchema = {
85
+ tables: AirtableTable[];
86
+ };
87
+ export declare function getBaseSchema({ apiKey, baseId, }: {
88
+ apiKey: string;
89
+ baseId: string;
90
+ }): Promise<AirtableBaseSchema>;
91
+ export declare function generateTypeScriptDefinitions({ apiKey, baseId, tableNameOrId, }: {
92
+ apiKey: string;
93
+ baseId: string;
94
+ tableNameOrId: string;
95
+ }): Promise<string>;
96
+ export {};
package/lib/requests.js CHANGED
@@ -29,7 +29,7 @@ export async function getRecords({ apiKey, baseId, tableId, options, }) {
29
29
  records = records.concat(response.records);
30
30
  currentOffset = response.offset;
31
31
  if (currentOffset)
32
- await delay(500);
32
+ await delay(options?.requestInterval ?? 500);
33
33
  } while (currentOffset);
34
34
  return records;
35
35
  }
@@ -83,7 +83,7 @@ export async function updateRecords({ apiKey, baseId, tableId, records, options,
83
83
  },
84
84
  });
85
85
  combinedResults = combinedResults.concat(result.records);
86
- await delay(500);
86
+ await delay(options?.requestInterval ?? 500);
87
87
  }
88
88
  return { records: combinedResults };
89
89
  }
@@ -120,7 +120,7 @@ export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, op
120
120
  allCreatedRecords = allCreatedRecords.concat(result.createdRecords);
121
121
  allUpdatedRecords = allUpdatedRecords.concat(result.updatedRecords);
122
122
  allRecords = allRecords.concat(result.records);
123
- await delay(500);
123
+ await delay(options?.requestInterval ?? 500);
124
124
  }
125
125
  return {
126
126
  createdRecords: allCreatedRecords,
@@ -128,7 +128,7 @@ export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, op
128
128
  records: allRecords,
129
129
  };
130
130
  }
131
- export async function deleteRecords({ apiKey, baseId, tableId, recordIds, }) {
131
+ export async function deleteRecords({ apiKey, baseId, tableId, recordIds, options, }) {
132
132
  if (!Array.isArray(recordIds) || recordIds.length === 0) {
133
133
  throw new Error('The record ids array is empty or not provided. Please provide a non-empty array of record ids to delete the records.');
134
134
  }
@@ -148,7 +148,7 @@ export async function deleteRecords({ apiKey, baseId, tableId, recordIds, }) {
148
148
  method: 'DELETE',
149
149
  });
150
150
  combinedResults = combinedResults.concat(result.records);
151
- await delay(500);
151
+ await delay(options?.requestInterval ?? 500);
152
152
  }
153
153
  return { records: combinedResults };
154
154
  }
@@ -238,3 +238,77 @@ async function apiRequest({ url, method, apiKey, body, }) {
238
238
  req.end();
239
239
  });
240
240
  }
241
+ export async function getBaseSchema({ apiKey, baseId, }) {
242
+ const response = await fetch(`https://api.airtable.com/v0/meta/bases/${baseId}/tables`, {
243
+ method: 'GET',
244
+ headers: {
245
+ Authorization: `Bearer ${apiKey}`,
246
+ },
247
+ });
248
+ if (!response.ok) {
249
+ throw new Error(`Error fetching base schema: ${response.statusText}`);
250
+ }
251
+ return response.json();
252
+ }
253
+ export async function generateTypeScriptDefinitions({ apiKey, baseId, tableNameOrId, }) {
254
+ const schema = await getBaseSchema({ apiKey, baseId });
255
+ const table = schema.tables.find((t) => t.id === tableNameOrId || t.name === tableNameOrId);
256
+ if (!table) {
257
+ throw new Error(`Table with name or ID "${tableNameOrId}" not found.`);
258
+ }
259
+ let typeDefinitions = `type ${table.name}Fields = {\n`;
260
+ table.fields.forEach((field) => {
261
+ typeDefinitions += ` ${field.name}?: ${mapAirtableTypeToTypeScript(field.type)};\n`;
262
+ });
263
+ typeDefinitions += '};\n';
264
+ return typeDefinitions;
265
+ }
266
+ function mapAirtableTypeToTypeScript(airtableType) {
267
+ switch (airtableType) {
268
+ case 'aiText':
269
+ case 'singleLineText':
270
+ case 'email':
271
+ case 'url':
272
+ case 'phoneNumber':
273
+ case 'multilineText':
274
+ case 'richText':
275
+ case 'button':
276
+ case 'barcode':
277
+ case 'createdTime':
278
+ case 'lastModifiedTime':
279
+ return 'string';
280
+ case 'number':
281
+ case 'currency':
282
+ case 'percent':
283
+ case 'rating':
284
+ case 'duration':
285
+ case 'autoNumber':
286
+ case 'count':
287
+ return 'number';
288
+ case 'checkbox':
289
+ return 'boolean';
290
+ case 'date':
291
+ case 'dateTime':
292
+ return 'Date';
293
+ case 'singleSelect':
294
+ case 'multipleSelects':
295
+ return 'string[]';
296
+ case 'attachment':
297
+ return 'any[]';
298
+ case 'collaborator':
299
+ case 'multipleCollaborators':
300
+ return '{ id: string; email?: string; name?: string; permissionLevel?: string; profilePicUrl?: string; }[]';
301
+ case 'linkToAnotherRecord':
302
+ case 'multipleRecordLinks':
303
+ return 'string[]';
304
+ case 'lookup':
305
+ case 'rollup':
306
+ return 'any';
307
+ case 'formula':
308
+ return 'string | number | boolean | any[]';
309
+ case 'syncSource':
310
+ return 'string';
311
+ default:
312
+ return 'any';
313
+ }
314
+ }
@@ -28,6 +28,7 @@ export interface GetRecordsQueryParameters {
28
28
  fields?: string[];
29
29
  returnFieldsByFieldId?: boolean;
30
30
  recordMetadata?: RecordMetadataOptions[];
31
+ requestInterval?: number;
31
32
  }
32
33
  type RecordField = {
33
34
  [key: string]: unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-airtable-api",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "type": "module",
5
5
  "description": "An easy way to connect to Airtable via their API.",
6
6
  "main": "lib/index.js",