airtable-ts-codegen 2.0.1 → 2.1.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/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
 
@@ -358,5 +358,6 @@ export declare const tablesMeta: {
358
358
  name: string;
359
359
  description?: never;
360
360
  })[];
361
+ views: never[];
361
362
  }[];
362
363
  };
@@ -64,5 +64,6 @@ exports.tablesMeta = {
64
64
  { type: 'autoNumber', id: 'fldGM8wIYk3G8WMiE', name: 'ID' },
65
65
  { type: 'barcode', id: 'fldMPs1eLH8X5bWeS', name: 'Barcode' },
66
66
  { type: 'button', id: 'fldAG3LTbJRwQVEvS', name: 'Button' }],
67
+ views: [],
67
68
  }],
68
69
  };
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;
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.0",
4
4
  "description": "Autogenerate TypeScript definitions for your Airtable base",
5
5
  "license": "MIT",
6
6
  "author": "Adam Jones (domdomegg)",