airtable-ts-codegen 2.0.1 → 2.1.1

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
@@ -12,6 +12,16 @@ AIRTABLE_API_KEY=pat1234.abcd AIRTABLE_BASE_ID=app1234 npx airtable-ts-codegen
12
12
 
13
13
  This will output a file `app1234.ts` that exports all the table definitions
14
14
 
15
+ ### Generate from a specific view
16
+
17
+ You can also generate TypeScript definitions based on a specific view, which will only include the fields visible in those views:
18
+
19
+ ```sh
20
+ AIRTABLE_API_KEY=pat1234.abcd AIRTABLE_BASE_ID=app1234 AIRTABLE_VIEW_IDS=viw1234,viw5678 npx airtable-ts-codegen
21
+ ```
22
+
23
+ This will output a file `app1234.ts` that exports table definitions with only the fields visible in the specified views.
24
+
15
25
  <details>
16
26
  <summary>Example generated file</summary>
17
27
 
@@ -35,6 +35,8 @@ export declare const tablesMeta: {
35
35
  isValid?: never;
36
36
  formula?: never;
37
37
  result?: never;
38
+ recordLinkFieldId?: never;
39
+ fieldIdInLinkedTable?: never;
38
40
  };
39
41
  id: string;
40
42
  name: string;
@@ -58,6 +60,8 @@ export declare const tablesMeta: {
58
60
  isValid?: never;
59
61
  formula?: never;
60
62
  result?: never;
63
+ recordLinkFieldId?: never;
64
+ fieldIdInLinkedTable?: never;
61
65
  };
62
66
  id: string;
63
67
  name: string;
@@ -85,6 +89,8 @@ export declare const tablesMeta: {
85
89
  isValid?: never;
86
90
  formula?: never;
87
91
  result?: never;
92
+ recordLinkFieldId?: never;
93
+ fieldIdInLinkedTable?: never;
88
94
  };
89
95
  id: string;
90
96
  name: string;
@@ -108,6 +114,8 @@ export declare const tablesMeta: {
108
114
  isValid?: never;
109
115
  formula?: never;
110
116
  result?: never;
117
+ recordLinkFieldId?: never;
118
+ fieldIdInLinkedTable?: never;
111
119
  };
112
120
  id: string;
113
121
  name: string;
@@ -134,6 +142,8 @@ export declare const tablesMeta: {
134
142
  isValid?: never;
135
143
  formula?: never;
136
144
  result?: never;
145
+ recordLinkFieldId?: never;
146
+ fieldIdInLinkedTable?: never;
137
147
  };
138
148
  id: string;
139
149
  name: string;
@@ -163,6 +173,8 @@ export declare const tablesMeta: {
163
173
  isValid?: never;
164
174
  formula?: never;
165
175
  result?: never;
176
+ recordLinkFieldId?: never;
177
+ fieldIdInLinkedTable?: never;
166
178
  };
167
179
  id: string;
168
180
  name: string;
@@ -186,6 +198,8 @@ export declare const tablesMeta: {
186
198
  isValid?: never;
187
199
  formula?: never;
188
200
  result?: never;
201
+ recordLinkFieldId?: never;
202
+ fieldIdInLinkedTable?: never;
189
203
  };
190
204
  id: string;
191
205
  name: string;
@@ -209,6 +223,8 @@ export declare const tablesMeta: {
209
223
  isValid?: never;
210
224
  formula?: never;
211
225
  result?: never;
226
+ recordLinkFieldId?: never;
227
+ fieldIdInLinkedTable?: never;
212
228
  };
213
229
  id: string;
214
230
  name: string;
@@ -232,6 +248,8 @@ export declare const tablesMeta: {
232
248
  isValid?: never;
233
249
  formula?: never;
234
250
  result?: never;
251
+ recordLinkFieldId?: never;
252
+ fieldIdInLinkedTable?: never;
235
253
  };
236
254
  id: string;
237
255
  name: string;
@@ -255,6 +273,8 @@ export declare const tablesMeta: {
255
273
  isValid?: never;
256
274
  formula?: never;
257
275
  result?: never;
276
+ recordLinkFieldId?: never;
277
+ fieldIdInLinkedTable?: never;
258
278
  };
259
279
  id: string;
260
280
  name: string;
@@ -281,6 +301,8 @@ export declare const tablesMeta: {
281
301
  symbol?: never;
282
302
  durationFormat?: never;
283
303
  max?: never;
304
+ recordLinkFieldId?: never;
305
+ fieldIdInLinkedTable?: never;
284
306
  };
285
307
  id: string;
286
308
  name: string;
@@ -300,6 +322,8 @@ export declare const tablesMeta: {
300
322
  format: string;
301
323
  };
302
324
  timeZone: string;
325
+ precision?: never;
326
+ choices?: never;
303
327
  };
304
328
  };
305
329
  choices?: never;
@@ -317,6 +341,8 @@ export declare const tablesMeta: {
317
341
  max?: never;
318
342
  isValid?: never;
319
343
  formula?: never;
344
+ recordLinkFieldId?: never;
345
+ fieldIdInLinkedTable?: never;
320
346
  };
321
347
  id: string;
322
348
  name: string;
@@ -338,6 +364,8 @@ export declare const tablesMeta: {
338
364
  format: string;
339
365
  };
340
366
  timeZone: string;
367
+ precision?: never;
368
+ choices?: never;
341
369
  };
342
370
  };
343
371
  choices?: never;
@@ -353,10 +381,109 @@ export declare const tablesMeta: {
353
381
  durationFormat?: never;
354
382
  max?: never;
355
383
  formula?: never;
384
+ recordLinkFieldId?: never;
385
+ fieldIdInLinkedTable?: never;
386
+ };
387
+ id: string;
388
+ name: string;
389
+ description?: never;
390
+ } | {
391
+ type: string;
392
+ options: {
393
+ isValid: boolean;
394
+ recordLinkFieldId: string;
395
+ fieldIdInLinkedTable: string;
396
+ result: {
397
+ type: string;
398
+ options?: never;
399
+ };
400
+ choices?: never;
401
+ isReversed?: never;
402
+ referencedFieldIds?: never;
403
+ prompt?: never;
404
+ icon?: never;
405
+ color?: never;
406
+ dateFormat?: never;
407
+ timeFormat?: never;
408
+ timeZone?: never;
409
+ precision?: never;
410
+ symbol?: never;
411
+ durationFormat?: never;
412
+ max?: never;
413
+ formula?: never;
414
+ };
415
+ id: string;
416
+ name: string;
417
+ description?: never;
418
+ } | {
419
+ type: string;
420
+ options: {
421
+ isValid: boolean;
422
+ recordLinkFieldId: string;
423
+ fieldIdInLinkedTable: string;
424
+ result: {
425
+ type: string;
426
+ options: {
427
+ precision: number;
428
+ dateFormat?: never;
429
+ timeFormat?: never;
430
+ timeZone?: never;
431
+ choices?: never;
432
+ };
433
+ };
434
+ choices?: never;
435
+ isReversed?: never;
436
+ referencedFieldIds?: never;
437
+ prompt?: never;
438
+ icon?: never;
439
+ color?: never;
440
+ dateFormat?: never;
441
+ timeFormat?: never;
442
+ timeZone?: never;
443
+ precision?: never;
444
+ symbol?: never;
445
+ durationFormat?: never;
446
+ max?: never;
447
+ formula?: never;
448
+ };
449
+ id: string;
450
+ name: string;
451
+ description?: never;
452
+ } | {
453
+ type: string;
454
+ options: {
455
+ isValid: boolean;
456
+ recordLinkFieldId: string;
457
+ fieldIdInLinkedTable: string;
458
+ result: {
459
+ type: string;
460
+ options: {
461
+ choices: never[];
462
+ dateFormat?: never;
463
+ timeFormat?: never;
464
+ timeZone?: never;
465
+ precision?: never;
466
+ };
467
+ };
468
+ choices?: never;
469
+ isReversed?: never;
470
+ referencedFieldIds?: never;
471
+ prompt?: never;
472
+ icon?: never;
473
+ color?: never;
474
+ dateFormat?: never;
475
+ timeFormat?: never;
476
+ timeZone?: never;
477
+ precision?: never;
478
+ symbol?: never;
479
+ durationFormat?: never;
480
+ max?: never;
481
+ formula?: never;
356
482
  };
357
483
  id: string;
358
484
  name: string;
359
485
  description?: never;
360
486
  })[];
487
+ views: never[];
361
488
  }[];
362
489
  };
@@ -63,6 +63,22 @@ exports.tablesMeta = {
63
63
  { type: 'createdBy', id: 'fldDcRoVGJXIhVSAi', name: 'Created By' },
64
64
  { type: 'autoNumber', id: 'fldGM8wIYk3G8WMiE', name: 'ID' },
65
65
  { type: 'barcode', id: 'fldMPs1eLH8X5bWeS', name: 'Barcode' },
66
- { type: 'button', id: 'fldAG3LTbJRwQVEvS', name: 'Button' }],
66
+ { type: 'button', id: 'fldAG3LTbJRwQVEvS', name: 'Button' },
67
+ {
68
+ type: 'multipleLookupValues', options: {
69
+ isValid: true, recordLinkFieldId: 'fldLinked', fieldIdInLinkedTable: 'fldName', result: { type: 'singleLineText' },
70
+ }, id: 'fldLookupText', name: 'Lookup Text',
71
+ },
72
+ {
73
+ type: 'multipleLookupValues', options: {
74
+ isValid: true, recordLinkFieldId: 'fldLinked', fieldIdInLinkedTable: 'fldNumber', result: { type: 'number', options: { precision: 0 } },
75
+ }, id: 'fldLookupNumber', name: 'Lookup Number',
76
+ },
77
+ {
78
+ type: 'multipleLookupValues', options: {
79
+ isValid: true, recordLinkFieldId: 'fldLinked', fieldIdInLinkedTable: 'fldTags', result: { type: 'multipleSelects', options: { choices: [] } },
80
+ }, id: 'fldLookupTags', name: 'Lookup Tags',
81
+ }],
82
+ views: [],
67
83
  }],
68
84
  };
package/dist/cli.js CHANGED
@@ -12,8 +12,16 @@ const baseId = process.env.AIRTABLE_BASE_ID;
12
12
  if (!baseId) {
13
13
  throw new Error('No Airtable base id set. Make sure the AIRTABLE_BASE_ID environment variable is set.');
14
14
  }
15
- (0, _1.main)({ apiKey, baseId }).then((result) => {
16
- (0, fs_1.writeFileSync)(`${(0, escapeIdentifier_1.escapeIdentifier)(baseId)}.ts`, result);
15
+ const viewIds = process.env.AIRTABLE_VIEW_IDS;
16
+ const config = { apiKey, baseId, ...(viewIds && { viewIds: viewIds.split(',') }) };
17
+ const generateCode = async () => {
18
+ console.log(`Generating TypeScript definitions for base ${baseId}${viewIds ? ` with views ${viewIds}` : ''}...`);
19
+ return (0, _1.main)(config);
20
+ };
21
+ generateCode().then((result) => {
22
+ const filename = `${(0, escapeIdentifier_1.escapeIdentifier)(baseId)}.ts`;
23
+ (0, fs_1.writeFileSync)(filename, result);
24
+ console.log(`Generated ${filename}`);
17
25
  }).catch((err) => {
18
26
  console.error(err);
19
27
  process.exit(1);
@@ -6,12 +6,19 @@ export type FieldSchema = {
6
6
  description?: string;
7
7
  options?: object;
8
8
  };
9
+ export type ViewSchema = {
10
+ id: string;
11
+ name: string;
12
+ type: 'grid' | 'form' | 'calendar' | 'gallery' | 'kanban' | 'timeline' | 'block';
13
+ visibleFieldIds?: string[];
14
+ };
9
15
  export type BaseSchema = {
10
16
  id: string;
11
17
  name: string;
12
18
  primaryFieldId?: string;
13
19
  description?: string;
14
20
  fields: FieldSchema[];
21
+ views: ViewSchema[];
15
22
  }[];
16
23
  /**
17
24
  * Get the schemas from the cache or Airtable API for the tables in the given base.
@@ -14,6 +14,9 @@ const getBaseSchema = async (baseId, options) => {
14
14
  const res = await (0, axios_1.default)({
15
15
  baseURL: options.endpointUrl ?? 'https://api.airtable.com',
16
16
  url: `/v0/meta/bases/${baseId}/tables`,
17
+ params: {
18
+ include: ['visibleFieldIds'],
19
+ },
17
20
  ...(options.requestTimeout ? { timeout: options.requestTimeout } : {}),
18
21
  headers: {
19
22
  Authorization: `Bearer ${options.apiKey}`,
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export type Config = {
2
2
  apiKey: string;
3
3
  baseId: string;
4
+ viewIds?: string[];
4
5
  endpointUrl?: string;
5
6
  requestTimeout?: number;
6
7
  customHeaders?: Record<string, string | number | boolean>;
package/dist/index.js CHANGED
@@ -6,15 +6,17 @@ const getBaseSchema_1 = require("./getBaseSchema");
6
6
  const escapeString_1 = require("./escape/escapeString");
7
7
  const escapeIdentifier_1 = require("./escape/escapeIdentifier");
8
8
  const jsTypeForAirtableType_1 = require("./jsTypeForAirtableType");
9
+ const view_1 = require("./view");
9
10
  // This generates a single typescript file containing all table definitions for a given base.
10
11
  const main = async (config) => {
11
12
  const baseSchema = await (0, getBaseSchema_1.getBaseSchema)(config.baseId, config);
13
+ const filteredBaseSchema = await (0, view_1.filterBaseSchemaByView)(baseSchema, config);
12
14
  return [
13
15
  '/* DO NOT EDIT: this file was automatically generated by airtable-ts-codegen */',
14
16
  '/* eslint-disable */',
15
17
  'import type { Item, Table } from \'airtable-ts\';',
16
18
  '',
17
- baseSchema.map((tableSchema) => generateCode(config, tableSchema)).join('\n\n'),
19
+ filteredBaseSchema.map((tableSchema) => generateCode(config, tableSchema)).join('\n\n'),
18
20
  ].join('\n');
19
21
  };
20
22
  exports.main = main;
@@ -46,8 +46,26 @@ const jsTypeForAirtableType = (field) => {
46
46
  return 'number'; // Unix timestamp in seconds
47
47
  case 'checkbox':
48
48
  return 'boolean';
49
- case 'lookup':
50
49
  case 'multipleLookupValues':
50
+ if (field.options
51
+ && 'result' in field.options
52
+ && typeof field.options.result === 'object'
53
+ && field.options.result !== null) {
54
+ const innerType = (0, exports.jsTypeForAirtableType)(field.options.result);
55
+ if (innerType === null) {
56
+ return null;
57
+ }
58
+ // multipleLookupValues always returns an array
59
+ // Strip nullability from inner type since the array itself represents empty state
60
+ const baseType = innerType.replace(/ \| null$/, '').replace(/^null \| /, '');
61
+ // If inner type is already an array, Airtable flattens lookup results
62
+ if (baseType.endsWith('[]')) {
63
+ return baseType;
64
+ }
65
+ return `${baseType}[]`;
66
+ }
67
+ throw new Error(`Invalid ${field.type} field (no options.result): ${field.id}`);
68
+ case 'lookup':
51
69
  case 'rollup':
52
70
  case 'formula':
53
71
  if (field.options
package/dist/view.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { Config } from './index';
2
+ import { type BaseSchema } from './getBaseSchema';
3
+ /**
4
+ * Filter base schema based on view IDs.
5
+ * If viewIds are specified:
6
+ * - Only include tables that have at least one matching view
7
+ * - For grid views with visibleFieldIds, filter fields to only visible ones
8
+ * - Throw error if any view ID doesn't match any table
9
+ */
10
+ export declare const filterBaseSchemaByView: (baseSchema: BaseSchema, config: Config) => Promise<BaseSchema>;
package/dist/view.js ADDED
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filterBaseSchemaByView = void 0;
4
+ /**
5
+ * Find a view by ID in the base schema
6
+ */
7
+ const findViewInBaseSchema = (baseSchema, viewId) => {
8
+ for (const table of baseSchema) {
9
+ const view = table.views.find((v) => v.id === viewId);
10
+ if (view) {
11
+ return { table, view };
12
+ }
13
+ }
14
+ return null;
15
+ };
16
+ /**
17
+ * Filter base schema based on view IDs.
18
+ * If viewIds are specified:
19
+ * - Only include tables that have at least one matching view
20
+ * - For grid views with visibleFieldIds, filter fields to only visible ones
21
+ * - Throw error if any view ID doesn't match any table
22
+ */
23
+ const filterBaseSchemaByView = async (baseSchema, config) => {
24
+ // If no viewIds are provided, return the original schema
25
+ if (!config.viewIds) {
26
+ return baseSchema;
27
+ }
28
+ const matchedTableIds = new Set();
29
+ const viewToTableMap = new Map();
30
+ // First pass: find all views and their tables, validate all views exist
31
+ for (const viewId of config.viewIds) {
32
+ const result = findViewInBaseSchema(baseSchema, viewId);
33
+ if (!result) {
34
+ throw new Error(`View "${viewId}" not found in any table. Please check the view ID is correct.`);
35
+ }
36
+ viewToTableMap.set(viewId, result);
37
+ matchedTableIds.add(result.table.id);
38
+ }
39
+ // Second pass: filter tables and fields
40
+ const filteredTables = [];
41
+ for (const table of baseSchema) {
42
+ // Only include tables that have at least one matching view
43
+ if (!matchedTableIds.has(table.id)) {
44
+ continue;
45
+ }
46
+ // Find all matching views for this table
47
+ const matchingViews = config.viewIds
48
+ .map((viewId) => viewToTableMap.get(viewId))
49
+ .filter((result) => result?.table.id === table.id)
50
+ .map((result) => result.view);
51
+ // Check if any of the matching views are grid views with visibleFieldIds
52
+ const gridViewsWithVisibleFields = matchingViews.filter((view) => view.type === 'grid' && view.visibleFieldIds && view.visibleFieldIds.length > 0);
53
+ let filteredTable = table;
54
+ // If we have grid views with visible field restrictions, apply field filtering
55
+ if (gridViewsWithVisibleFields.length > 0) {
56
+ // Collect all visible field IDs from all matching grid views
57
+ const allVisibleFieldIds = new Set();
58
+ for (const view of gridViewsWithVisibleFields) {
59
+ if (view.visibleFieldIds) {
60
+ view.visibleFieldIds.forEach((fieldId) => allVisibleFieldIds.add(fieldId));
61
+ }
62
+ }
63
+ // Filter fields to only include those visible in at least one grid view
64
+ filteredTable = {
65
+ ...table,
66
+ fields: table.fields.filter((field) => allVisibleFieldIds.has(field.id)),
67
+ };
68
+ }
69
+ filteredTables.push(filteredTable);
70
+ }
71
+ return filteredTables;
72
+ };
73
+ exports.filterBaseSchemaByView = filterBaseSchemaByView;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "airtable-ts-codegen",
3
- "version": "2.0.1",
3
+ "version": "2.1.1",
4
4
  "description": "Autogenerate TypeScript definitions for your Airtable base",
5
5
  "license": "MIT",
6
6
  "author": "Adam Jones (domdomegg)",