easy-airtable-api 0.0.15 → 0.0.16

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 ADDED
@@ -0,0 +1,79 @@
1
+ # Changelog
2
+
3
+ ## 0.0.16
4
+
5
+ - Improved type generation to support more Airtable field types.
6
+
7
+ ## 0.0.15
8
+
9
+ - Type generation wraps type keys in quotations
10
+ - Fix various type generation issues
11
+ - added createRecords and createRecord methods
12
+
13
+ ## 0.0.14
14
+
15
+ - Exported generateTypeScriptDefinitions function.
16
+
17
+ ## 0.0.13
18
+
19
+ - Added generateTypeScriptDefinitions function to generate TypeScript definitions for Airtable tables.
20
+
21
+ ## 0.0.12
22
+
23
+ - Reverted making apiKey, baseId, tableId default to undefined
24
+ - Added option to customize delay between requests via options.requestInterval
25
+
26
+ ## 0.0.11
27
+
28
+ - Make apiKey, baseId, tableId default to undefined #12
29
+ - Missing .js in utils import #13
30
+
31
+ ## 0.0.10
32
+
33
+ - Fixed deleteRecords implementation errors.
34
+
35
+ ## 0.0.9
36
+
37
+ - Implemented the deleteRecords method.
38
+
39
+ ## 0.0.8
40
+
41
+ - Implemented the updateRecords method.
42
+ - Implemented the updateRecordsUpsert method.
43
+ - Added a delay when executing many requests in succession.
44
+ - Fixed the updateRecord and updateRecords methods to return correct field types.
45
+
46
+ ## 0.0.7
47
+
48
+ - Added all available options for updateRecords.
49
+ - Modified the getRecords function to automatically retrieve all records.
50
+
51
+ ## 0.0.6
52
+
53
+ - Added all available options for updateRecord.
54
+ - Corrected the path to index.d.ts.
55
+
56
+ ## 0.0.5
57
+
58
+ - Discontinued CJS build, supporting only ESM for now.
59
+
60
+ ## 0.0.4
61
+
62
+ - Exported all available methods.
63
+ - Restructured the lib folder to eliminate duplicate types.
64
+
65
+ ## 0.0.3
66
+
67
+ - Supported both ESM & CJS builds.
68
+ - Enhanced server error handling.
69
+ - Introduced generic field types.
70
+
71
+ ## 0.0.2
72
+
73
+ - Developed basic request functions to retrieve or update records.
74
+ - Added type definitions for requests and fields.
75
+ - Conducted basic tests to verify the core functionality of the library.
76
+
77
+ ## 0.0.1
78
+
79
+ - Initial project setup with no real functionality implemented.
package/README.md CHANGED
@@ -1,14 +1,19 @@
1
1
  # Easy Airtable API
2
2
 
3
- I created this as a simpler alternative to the official Airtable Node.js library.
3
+ Easy Airtable API is a lightweight, TypeScript-first Airtable client for reading, creating, updating, upserting, and deleting records.
4
4
 
5
- This library makes it easier to work with Airtable by providing TypeScript support and straightforward functions for reading and writing records.
5
+ It is built to stay practical:
6
6
 
7
- While this is still an early project and needs more work, you can already use it to fetch and update records in your Airtable bases.
7
+ - small runtime API
8
+ - typed CRUD helpers
9
+ - code generation from Airtable metadata
10
+ - a better fit for app code than a large SDK surface
8
11
 
9
- I'm already using it in production for many projects.
12
+ The intended workflow is simple:
10
13
 
11
- It also supports generating TypeScript definitions for your Airtable tables which is super useful.
14
+ 1. generate types from your Airtable base
15
+ 2. import the generated types in your app
16
+ 3. use the runtime helpers with those types
12
17
 
13
18
  ## Install
14
19
 
@@ -20,193 +25,279 @@ npm install easy-airtable-api
20
25
  bun add easy-airtable-api
21
26
  ```
22
27
 
23
- ## How to Use
28
+ ## Recommended project setup
24
29
 
25
- ### Get a Single Record
30
+ A good setup is to generate Airtable types once and commit the generated file.
26
31
 
27
- ```ts
28
- import { getRecord } from 'easy-airtable-api';
32
+ ### Example file structure
29
33
 
30
- type Fields = {
31
- Name?: string;
32
- Notes?: string;
33
- Status?: string;
34
- };
35
-
36
- const record = await getRecord<Fields>({
37
- apiKey: 'apiKey',
38
- baseId: 'baseId',
39
- tableNameOrId: 'tableNameOrId',
40
- recordId: 'recordId',
41
- });
34
+ ```txt
35
+ .
36
+ ├── scripts/
37
+ │ └── generate-airtable-types.ts
38
+ ├── src/
39
+ │ ├── airtable.generated.ts
40
+ │ └── airtable.ts
41
+ ├── .env
42
+ └── package.json
42
43
  ```
43
44
 
44
- ### Get Multiple Records
45
+ ### Example environment variables
45
46
 
46
- ```ts
47
- import { getRecords } from 'easy-airtable-api';
48
-
49
- type Fields = {
50
- Name?: string;
51
- Notes?: string;
52
- Status?: string;
53
- };
54
-
55
- const records = await getRecords<Fields>({
56
- apiKey: 'apiKey',
57
- baseId: 'baseId',
58
- tableNameOrId: 'tableNameOrId',
59
- options: {
60
- maxRecords: 500,
61
- },
62
- });
47
+ ```bash
48
+ AIRTABLE_ACCESS_TOKEN=pat_xxx
49
+ AIRTABLE_BASE_ID=app_xxx
50
+ AIRTABLE_PROJECTS_TABLE_ID=tbl_xxx
63
51
  ```
64
52
 
65
- ### Update a Single Record
53
+ ### Example `package.json` scripts
54
+
55
+ ```json
56
+ {
57
+ "scripts": {
58
+ "airtable:types": "tsx scripts/generate-airtable-types.ts"
59
+ }
60
+ }
61
+ ```
62
+
63
+ ## 1) Generate types into your project
64
+
65
+ Create `scripts/generate-airtable-types.ts`:
66
66
 
67
67
  ```ts
68
- import { updateRecord } from 'easy-airtable-api';
69
-
70
- type Fields = {
71
- Name?: string;
72
- Status?: string;
73
- };
74
-
75
- const record = await updateRecord<Fields>({
76
- apiKey: 'apiKey',
77
- baseId: 'baseId',
78
- tableNameOrId: 'tableNameOrId',
79
- recordId: 'recordId',
80
- options: {
81
- typecast: true,
82
- },
83
- fields: {
84
- Name: 'New Name',
85
- Status: 'Active',
68
+ import { generateAirtableTypes } from 'easy-airtable-api/codegen';
69
+
70
+ await generateAirtableTypes({
71
+ source: {
72
+ baseId: process.env.AIRTABLE_BASE_ID!,
73
+ accessToken: process.env.AIRTABLE_ACCESS_TOKEN!,
86
74
  },
75
+ output: './src/airtable.generated.ts',
76
+ enumMode: 'hybrid',
87
77
  });
88
78
  ```
89
79
 
90
- ### Update Multiple Records
80
+ Then run:
91
81
 
92
- ```ts
93
- import { updateRecords } from 'easy-airtable-api';
82
+ ```bash
83
+ npm run airtable:types
84
+ ```
94
85
 
95
- type Fields = {
96
- Name?: string;
97
- Status?: string;
98
- };
86
+ ## 2) Use the generated types in your app
99
87
 
100
- const records = await updateRecords<Fields>({
101
- apiKey: 'apiKey',
102
- baseId: 'baseId',
103
- tableNameOrId: 'tableNameOrId',
104
- records: [
105
- {
106
- id: 'recordId1',
107
- fields: {
108
- Name: 'New Name 1',
109
- Status: 'Active',
110
- }
88
+ Create `src/airtable.ts`:
89
+
90
+ ```ts
91
+ import { createRecord, getRecords, updateRecord } from 'easy-airtable-api';
92
+ import type {
93
+ ProjectsCreateFields,
94
+ ProjectsRecordFields,
95
+ } from './airtable.generated';
96
+
97
+ const apiKey = process.env.AIRTABLE_ACCESS_TOKEN!;
98
+ const baseId = process.env.AIRTABLE_BASE_ID!;
99
+ const tableId = process.env.AIRTABLE_PROJECTS_TABLE_ID!;
100
+
101
+ export async function listProjects() {
102
+ return getRecords<ProjectsRecordFields>({
103
+ apiKey,
104
+ baseId,
105
+ tableId,
106
+ options: {
107
+ maxRecords: 100,
111
108
  },
112
- {
113
- id: 'recordId2',
114
- fields: {
115
- Name: 'New Name 2',
116
- Status: 'Inactive',
117
- }
118
- }
119
- ],
120
- options: {
121
- typecast: true,
122
- }
123
- });
109
+ });
110
+ }
111
+
112
+ export async function createProject(fields: ProjectsCreateFields) {
113
+ return createRecord<ProjectsCreateFields>({
114
+ apiKey,
115
+ baseId,
116
+ tableId,
117
+ fields,
118
+ options: {
119
+ typecast: true,
120
+ },
121
+ });
122
+ }
123
+
124
+ export async function markProjectInProgress(recordId: string) {
125
+ return updateRecord<ProjectsCreateFields>({
126
+ apiKey,
127
+ baseId,
128
+ tableId,
129
+ recordId,
130
+ fields: {
131
+ Status: 'In Progress',
132
+ },
133
+ });
134
+ }
124
135
  ```
125
136
 
126
- ### Generate TypeScript Definitions
137
+ ## Codegen
127
138
 
128
- ```ts
129
- import { generateTypeScriptDefinitions } from 'easy-airtable-api';
139
+ ### `generateAirtableTypes()`
130
140
 
131
- const types = await generateTypeScriptDefinitions({
132
- apiKey: 'apiKey',
133
- baseId: 'baseId',
134
- tableNameOrId: 'tableNameOrId',
135
- });
141
+ ```ts
142
+ import { generateAirtableTypes } from 'easy-airtable-api/codegen';
136
143
  ```
137
144
 
138
- ## Changelog
139
-
140
- ### 0.0.15
145
+ Supported schema sources:
141
146
 
142
- - Type generation wraps type keys in quotations
143
- - Fix various type generation issues
144
- - added createRecords and createRecord methods
147
+ - Airtable metadata API via `baseId` + `accessToken` / `apiKey`
148
+ - local schema file via `schemaPath`
149
+ - in-memory schema object via `schema`
145
150
 
146
- ### 0.0.14
151
+ Example using a local schema file:
147
152
 
148
- - Exported generateTypeScriptDefinitions function.
153
+ ```ts
154
+ await generateAirtableTypes({
155
+ source: {
156
+ schemaPath: './airtable-schema.json',
157
+ },
158
+ output: './src/airtable.generated.ts',
159
+ });
160
+ ```
149
161
 
150
- ### 0.0.13
162
+ ### Enum typing modes
151
163
 
152
- - Added generateTypeScriptDefinitions function to generate TypeScript definitions for Airtable tables.
164
+ Choice-like fields such as single select and multiple select support 3 modes:
153
165
 
154
- ### 0.0.12
166
+ - `literal` — exact Airtable option values only
167
+ - `hybrid` — exact Airtable values plus a string fallback
168
+ - `broad` — generic `string` / `string[]`
155
169
 
156
- - Reverted making apiKey, baseId, tableId default to undefined
157
- - Added option to customize delay between requests via options.requestInterval
170
+ `hybrid` is the default and usually the best fit for real Airtable projects because it preserves autocomplete while tolerating schema drift.
158
171
 
159
- ### 0.0.11
172
+ ### Useful codegen options
160
173
 
161
- - Make apiKey, baseId, tableId default to undefined #12
162
- - Missing .js in utils import #13
174
+ ```ts
175
+ await generateAirtableTypes({
176
+ source: {
177
+ baseId: process.env.AIRTABLE_BASE_ID!,
178
+ accessToken: process.env.AIRTABLE_ACCESS_TOKEN!,
179
+ },
180
+ output: './src/airtable.generated.ts',
181
+ tableNameOrId: ['Projects', 'Tasks'],
182
+ enumMode: 'hybrid',
183
+ createRequiredMode: 'allOptional',
184
+ unknownFieldBehavior: 'unknown',
185
+ includeTableIds: true,
186
+ includeFieldIds: true,
187
+ schemaMode: 'full',
188
+ });
189
+ ```
163
190
 
164
- ### 0.0.10
191
+ ## Runtime examples
165
192
 
166
- - Fixed deleteRecords implementation errors.
193
+ ### Get a single record
167
194
 
168
- ### 0.0.9
195
+ ```ts
196
+ import { getRecord } from 'easy-airtable-api';
197
+ import type { ProjectsRecordFields } from './airtable.generated';
169
198
 
170
- - Implemented the deleteRecords method.
199
+ const record = await getRecord<ProjectsRecordFields>({
200
+ apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
201
+ baseId: process.env.AIRTABLE_BASE_ID!,
202
+ tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
203
+ recordId: 'rec123',
204
+ });
205
+ ```
171
206
 
172
- ### 0.0.8
207
+ ### Create multiple records
173
208
 
174
- - Implemented the updateRecords method.
175
- - Implemented the updateRecordsUpsert method.
176
- - Added a delay when executing many requests in succession.
177
- - Fixed the updateRecord and updateRecords methods to return correct field types.
209
+ ```ts
210
+ import { createRecords } from 'easy-airtable-api';
211
+ import type { ProjectsCreateFields } from './airtable.generated';
178
212
 
179
- ### 0.0.7
213
+ const result = await createRecords<ProjectsCreateFields>({
214
+ apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
215
+ baseId: process.env.AIRTABLE_BASE_ID!,
216
+ tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
217
+ records: [
218
+ {
219
+ fields: {
220
+ Name: 'Project A',
221
+ Status: 'Todo',
222
+ },
223
+ },
224
+ {
225
+ fields: {
226
+ Name: 'Project B',
227
+ Status: 'In Progress',
228
+ },
229
+ },
230
+ ],
231
+ options: {
232
+ typecast: true,
233
+ },
234
+ });
235
+ ```
180
236
 
181
- - Added all available options for updateRecords.
182
- - Modified the getRecords function to automatically retrieve all records.
237
+ ### Upsert records
183
238
 
184
- ### 0.0.6
239
+ ```ts
240
+ import { updateRecordsUpsert } from 'easy-airtable-api';
241
+ import type { ProjectsCreateFields } from './airtable.generated';
185
242
 
186
- - Added all available options for updateRecord.
187
- - Corrected the path to index.d.ts.
243
+ const result = await updateRecordsUpsert<ProjectsCreateFields>({
244
+ apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
245
+ baseId: process.env.AIRTABLE_BASE_ID!,
246
+ tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
247
+ records: [
248
+ {
249
+ fields: {
250
+ Name: 'Project A',
251
+ Status: 'Done',
252
+ },
253
+ },
254
+ ],
255
+ options: {
256
+ fieldsToMergeOn: ['Name'],
257
+ typecast: true,
258
+ },
259
+ });
260
+ ```
188
261
 
189
- ### 0.0.5
262
+ ### Delete records
190
263
 
191
- - Discontinued CJS build, supporting only ESM for now.
264
+ ```ts
265
+ import { deleteRecords } from 'easy-airtable-api';
192
266
 
193
- ### 0.0.4
267
+ await deleteRecords({
268
+ apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
269
+ baseId: process.env.AIRTABLE_BASE_ID!,
270
+ tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
271
+ recordIds: ['rec123', 'rec456'],
272
+ });
273
+ ```
194
274
 
195
- - Exported all available methods.
196
- - Restructured the lib folder to eliminate duplicate types.
275
+ ## Exports
197
276
 
198
- ### 0.0.3
277
+ Runtime package:
199
278
 
200
- - Supported both ESM & CJS builds.
201
- - Enhanced server error handling.
202
- - Introduced generic field types.
279
+ ```ts
280
+ import {
281
+ createRecord,
282
+ createRecords,
283
+ deleteRecords,
284
+ getRecord,
285
+ getRecords,
286
+ updateRecord,
287
+ updateRecords,
288
+ updateRecordsUpsert,
289
+ } from 'easy-airtable-api';
290
+ ```
203
291
 
204
- ### 0.0.2
292
+ Codegen package:
205
293
 
206
- - Developed basic request functions to retrieve or update records.
207
- - Added type definitions for requests and fields.
208
- - Conducted basic tests to verify the core functionality of the library.
294
+ ```ts
295
+ import {
296
+ buildAirtableTypes,
297
+ generateAirtableTypes,
298
+ } from 'easy-airtable-api/codegen';
299
+ ```
209
300
 
210
- ### 0.0.1
301
+ ## Changelog
211
302
 
212
- - Initial project setup with no real functionality implemented.
303
+ See [CHANGELOG.md](./CHANGELOG.md).
package/lib/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/lib/cli.js ADDED
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env node
2
+ import { generateAirtableTypes } from './codegen/generate.js';
3
+ function readFlag(args, flag) {
4
+ const index = args.indexOf(flag);
5
+ if (index === -1)
6
+ return undefined;
7
+ return args[index + 1];
8
+ }
9
+ function readMultiFlag(args, flag) {
10
+ const values = [];
11
+ args.forEach((argument, index) => {
12
+ if (argument === flag && args[index + 1]) {
13
+ values.push(args[index + 1]);
14
+ }
15
+ });
16
+ return values;
17
+ }
18
+ function hasFlag(args, flag) {
19
+ return args.includes(flag);
20
+ }
21
+ function printHelp() {
22
+ console.log(`easy-airtable-api generate [options]
23
+
24
+ Options:
25
+ --baseId <id>
26
+ --accessToken <token>
27
+ --apiKey <token>
28
+ --schemaPath <path>
29
+ --output <path>
30
+ --table <nameOrId> Repeatable
31
+ --enumMode <mode> literal | hybrid | broad
32
+ --schemaMode <mode> read | write | full
33
+ --createRequiredMode <mode> allOptional | primaryField
34
+ --unknownFieldBehavior <mode> unknown | error
35
+ --noTableIds
36
+ --noFieldIds
37
+ --help
38
+ `);
39
+ }
40
+ async function main() {
41
+ const args = process.argv.slice(2);
42
+ const command = args[0];
43
+ if (!command || command === '--help' || command === '-h') {
44
+ printHelp();
45
+ return;
46
+ }
47
+ if (command !== 'generate') {
48
+ throw new Error(`Unknown command: ${command}`);
49
+ }
50
+ const tables = readMultiFlag(args, '--table');
51
+ const schemaPath = readFlag(args, '--schemaPath');
52
+ const baseId = readFlag(args, '--baseId');
53
+ const accessToken = readFlag(args, '--accessToken');
54
+ const apiKey = readFlag(args, '--apiKey');
55
+ const source = schemaPath
56
+ ? { schemaPath }
57
+ : {
58
+ baseId: baseId ?? '',
59
+ accessToken,
60
+ apiKey,
61
+ };
62
+ const result = await generateAirtableTypes({
63
+ source,
64
+ output: readFlag(args, '--output'),
65
+ tableNameOrId: tables.length ? tables : undefined,
66
+ enumMode: readFlag(args, '--enumMode'),
67
+ schemaMode: readFlag(args, '--schemaMode'),
68
+ createRequiredMode: readFlag(args, '--createRequiredMode'),
69
+ unknownFieldBehavior: readFlag(args, '--unknownFieldBehavior'),
70
+ includeTableIds: !hasFlag(args, '--noTableIds'),
71
+ includeFieldIds: !hasFlag(args, '--noFieldIds'),
72
+ });
73
+ console.log(`Generated ${result.tablesGenerated} table(s) at ${result.outputPath}`);
74
+ if (result.warnings.length) {
75
+ console.warn(`Warnings:\n- ${result.warnings.join('\n- ')}`);
76
+ }
77
+ }
78
+ main().catch((error) => {
79
+ const message = error instanceof Error ? error.message : String(error);
80
+ console.error(message);
81
+ process.exit(1);
82
+ });
@@ -0,0 +1,2 @@
1
+ import type { AirtableCodegenResult, GenerateAirtableTypesOptions } from './types.js';
2
+ export declare function buildAirtableTypes(options: Omit<GenerateAirtableTypesOptions, 'output'>): Promise<AirtableCodegenResult>;
@@ -0,0 +1,25 @@
1
+ import { emitTypeScript } from './emit-typescript.js';
2
+ import { loadSchema } from './load-schema.js';
3
+ import { normalizeSchema } from './normalize-schema.js';
4
+ export async function buildAirtableTypes(options) {
5
+ const { source, tableNameOrId, enumMode = 'hybrid', createRequiredMode = 'allOptional', unknownFieldBehavior = 'unknown', includeTableIds = true, includeFieldIds = true, schemaMode = 'full', } = options;
6
+ const schema = await loadSchema(source);
7
+ const { normalizedBase, warnings } = normalizeSchema({
8
+ schema,
9
+ tableNameOrId,
10
+ enumMode,
11
+ unknownFieldBehavior,
12
+ });
13
+ const content = emitTypeScript({
14
+ normalizedBase,
15
+ includeTableIds,
16
+ includeFieldIds,
17
+ schemaMode,
18
+ createRequiredMode,
19
+ });
20
+ return {
21
+ content,
22
+ tablesGenerated: normalizedBase.tables.length,
23
+ warnings,
24
+ };
25
+ }
@@ -0,0 +1,8 @@
1
+ import type { CreateRequiredMode, NormalizedBase, SchemaMode } from './types.js';
2
+ export declare function emitTypeScript({ normalizedBase, includeTableIds, includeFieldIds, schemaMode, createRequiredMode, }: {
3
+ normalizedBase: NormalizedBase;
4
+ includeTableIds: boolean;
5
+ includeFieldIds: boolean;
6
+ schemaMode: SchemaMode;
7
+ createRequiredMode: CreateRequiredMode;
8
+ }): string;