easy-airtable-api 0.0.14 → 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 +79 -0
- package/README.md +252 -74
- package/lib/cli.d.ts +2 -0
- package/lib/cli.js +82 -0
- package/lib/codegen/build.d.ts +2 -0
- package/lib/codegen/build.js +25 -0
- package/lib/codegen/emit-typescript.d.ts +8 -0
- package/lib/codegen/emit-typescript.js +104 -0
- package/lib/codegen/generate.d.ts +2 -0
- package/lib/codegen/generate.js +23 -0
- package/lib/codegen/identifiers.d.ts +7 -0
- package/lib/codegen/identifiers.js +51 -0
- package/lib/codegen/index.d.ts +3 -0
- package/lib/codegen/index.js +2 -0
- package/lib/codegen/load-schema.d.ts +3 -0
- package/lib/codegen/load-schema.js +28 -0
- package/lib/codegen/map-field.d.ts +31 -0
- package/lib/codegen/map-field.js +278 -0
- package/lib/codegen/normalize-schema.d.ts +11 -0
- package/lib/codegen/normalize-schema.js +61 -0
- package/lib/codegen/types.d.ts +62 -0
- package/lib/codegen/types.js +1 -0
- package/lib/codegen/utils.d.ts +14 -0
- package/lib/codegen/utils.js +30 -0
- package/lib/codegen/write-atomic.d.ts +1 -0
- package/lib/codegen/write-atomic.js +11 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.js +1 -1
- package/lib/records.d.ts +96 -0
- package/lib/records.js +211 -0
- package/lib/requests.d.ts +8 -89
- package/lib/requests.js +13 -215
- package/lib/types/fields.d.ts +8 -8
- package/lib/types/metadata.d.ts +24 -0
- package/lib/types/metadata.js +1 -0
- package/lib/types/records.d.ts +1 -4
- package/lib/utils.d.ts +2 -2
- package/lib/utils.js +2 -0
- package/package.json +14 -6
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,10 +1,19 @@
|
|
|
1
1
|
# Easy Airtable API
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Easy Airtable API is a lightweight, TypeScript-first Airtable client for reading, creating, updating, upserting, and deleting records.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
It is built to stay practical:
|
|
6
6
|
|
|
7
|
-
|
|
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
|
|
11
|
+
|
|
12
|
+
The intended workflow is simple:
|
|
13
|
+
|
|
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
|
|
8
17
|
|
|
9
18
|
## Install
|
|
10
19
|
|
|
@@ -16,110 +25,279 @@ npm install easy-airtable-api
|
|
|
16
25
|
bun add easy-airtable-api
|
|
17
26
|
```
|
|
18
27
|
|
|
19
|
-
##
|
|
28
|
+
## Recommended project setup
|
|
20
29
|
|
|
21
|
-
|
|
30
|
+
A good setup is to generate Airtable types once and commit the generated file.
|
|
22
31
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
baseId: "baseId",
|
|
35
|
-
tableId: "tableId",
|
|
36
|
-
recordId: "recordId",
|
|
37
|
-
});
|
|
32
|
+
### Example file structure
|
|
33
|
+
|
|
34
|
+
```txt
|
|
35
|
+
.
|
|
36
|
+
├── scripts/
|
|
37
|
+
│ └── generate-airtable-types.ts
|
|
38
|
+
├── src/
|
|
39
|
+
│ ├── airtable.generated.ts
|
|
40
|
+
│ └── airtable.ts
|
|
41
|
+
├── .env
|
|
42
|
+
└── package.json
|
|
38
43
|
```
|
|
39
44
|
|
|
40
|
-
###
|
|
45
|
+
### Example environment variables
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
AIRTABLE_ACCESS_TOKEN=pat_xxx
|
|
49
|
+
AIRTABLE_BASE_ID=app_xxx
|
|
50
|
+
AIRTABLE_PROJECTS_TABLE_ID=tbl_xxx
|
|
51
|
+
```
|
|
52
|
+
|
|
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`:
|
|
41
66
|
|
|
42
67
|
```ts
|
|
43
|
-
import {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const records = await getRecords<Fields>({
|
|
52
|
-
apiKey: "apiKey",
|
|
53
|
-
baseId: "baseId",
|
|
54
|
-
tableId: "tableId",
|
|
55
|
-
options: {
|
|
56
|
-
maxRecords: 500,
|
|
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!,
|
|
57
74
|
},
|
|
75
|
+
output: './src/airtable.generated.ts',
|
|
76
|
+
enumMode: 'hybrid',
|
|
58
77
|
});
|
|
59
78
|
```
|
|
60
79
|
|
|
61
|
-
|
|
80
|
+
Then run:
|
|
62
81
|
|
|
63
|
-
|
|
82
|
+
```bash
|
|
83
|
+
npm run airtable:types
|
|
84
|
+
```
|
|
64
85
|
|
|
65
|
-
|
|
86
|
+
## 2) Use the generated types in your app
|
|
66
87
|
|
|
67
|
-
|
|
88
|
+
Create `src/airtable.ts`:
|
|
68
89
|
|
|
69
|
-
|
|
70
|
-
|
|
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,
|
|
108
|
+
},
|
|
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
|
+
}
|
|
135
|
+
```
|
|
71
136
|
|
|
72
|
-
|
|
137
|
+
## Codegen
|
|
73
138
|
|
|
74
|
-
|
|
75
|
-
- Missing .js in utils import #13
|
|
139
|
+
### `generateAirtableTypes()`
|
|
76
140
|
|
|
77
|
-
|
|
141
|
+
```ts
|
|
142
|
+
import { generateAirtableTypes } from 'easy-airtable-api/codegen';
|
|
143
|
+
```
|
|
78
144
|
|
|
79
|
-
|
|
145
|
+
Supported schema sources:
|
|
80
146
|
|
|
81
|
-
|
|
147
|
+
- Airtable metadata API via `baseId` + `accessToken` / `apiKey`
|
|
148
|
+
- local schema file via `schemaPath`
|
|
149
|
+
- in-memory schema object via `schema`
|
|
82
150
|
|
|
83
|
-
|
|
151
|
+
Example using a local schema file:
|
|
84
152
|
|
|
85
|
-
|
|
153
|
+
```ts
|
|
154
|
+
await generateAirtableTypes({
|
|
155
|
+
source: {
|
|
156
|
+
schemaPath: './airtable-schema.json',
|
|
157
|
+
},
|
|
158
|
+
output: './src/airtable.generated.ts',
|
|
159
|
+
});
|
|
160
|
+
```
|
|
86
161
|
|
|
87
|
-
|
|
88
|
-
- Implemented the updateRecordsUpsert method.
|
|
89
|
-
- Added a delay when executing many requests in succession.
|
|
90
|
-
- Fixed the updateRecord and updateRecords methods to return correct field types.
|
|
162
|
+
### Enum typing modes
|
|
91
163
|
|
|
92
|
-
|
|
164
|
+
Choice-like fields such as single select and multiple select support 3 modes:
|
|
93
165
|
|
|
94
|
-
-
|
|
95
|
-
-
|
|
166
|
+
- `literal` — exact Airtable option values only
|
|
167
|
+
- `hybrid` — exact Airtable values plus a string fallback
|
|
168
|
+
- `broad` — generic `string` / `string[]`
|
|
96
169
|
|
|
97
|
-
|
|
170
|
+
`hybrid` is the default and usually the best fit for real Airtable projects because it preserves autocomplete while tolerating schema drift.
|
|
98
171
|
|
|
99
|
-
|
|
100
|
-
- Corrected the path to index.d.ts.
|
|
172
|
+
### Useful codegen options
|
|
101
173
|
|
|
102
|
-
|
|
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
|
+
```
|
|
103
190
|
|
|
104
|
-
|
|
191
|
+
## Runtime examples
|
|
105
192
|
|
|
106
|
-
###
|
|
193
|
+
### Get a single record
|
|
107
194
|
|
|
108
|
-
|
|
109
|
-
|
|
195
|
+
```ts
|
|
196
|
+
import { getRecord } from 'easy-airtable-api';
|
|
197
|
+
import type { ProjectsRecordFields } from './airtable.generated';
|
|
198
|
+
|
|
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
|
+
```
|
|
110
206
|
|
|
111
|
-
###
|
|
207
|
+
### Create multiple records
|
|
112
208
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
209
|
+
```ts
|
|
210
|
+
import { createRecords } from 'easy-airtable-api';
|
|
211
|
+
import type { ProjectsCreateFields } from './airtable.generated';
|
|
212
|
+
|
|
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
|
+
```
|
|
116
236
|
|
|
117
|
-
###
|
|
237
|
+
### Upsert records
|
|
118
238
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
239
|
+
```ts
|
|
240
|
+
import { updateRecordsUpsert } from 'easy-airtable-api';
|
|
241
|
+
import type { ProjectsCreateFields } from './airtable.generated';
|
|
242
|
+
|
|
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
|
+
```
|
|
261
|
+
|
|
262
|
+
### Delete records
|
|
122
263
|
|
|
123
|
-
|
|
264
|
+
```ts
|
|
265
|
+
import { deleteRecords } from 'easy-airtable-api';
|
|
266
|
+
|
|
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
|
+
```
|
|
274
|
+
|
|
275
|
+
## Exports
|
|
276
|
+
|
|
277
|
+
Runtime package:
|
|
278
|
+
|
|
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
|
+
```
|
|
291
|
+
|
|
292
|
+
Codegen package:
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
import {
|
|
296
|
+
buildAirtableTypes,
|
|
297
|
+
generateAirtableTypes,
|
|
298
|
+
} from 'easy-airtable-api/codegen';
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## Changelog
|
|
124
302
|
|
|
125
|
-
|
|
303
|
+
See [CHANGELOG.md](./CHANGELOG.md).
|
package/lib/cli.d.ts
ADDED
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,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;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { quotePropertyKey, toSafeConstKey } from './identifiers.js';
|
|
2
|
+
function isPrimaryCreateField(field, primaryFieldId, createRequiredMode) {
|
|
3
|
+
return (createRequiredMode === 'primaryField' &&
|
|
4
|
+
field.id === primaryFieldId &&
|
|
5
|
+
Boolean(field.createType));
|
|
6
|
+
}
|
|
7
|
+
function emitInterface({ name, fields, kind, primaryFieldId, createRequiredMode, }) {
|
|
8
|
+
const writableFields = fields.filter((field) => {
|
|
9
|
+
if (kind === 'read')
|
|
10
|
+
return true;
|
|
11
|
+
if (kind === 'create')
|
|
12
|
+
return Boolean(field.createType);
|
|
13
|
+
return Boolean(field.updateType);
|
|
14
|
+
});
|
|
15
|
+
const lines = writableFields.map((field) => {
|
|
16
|
+
const type = kind === 'read'
|
|
17
|
+
? field.readType
|
|
18
|
+
: kind === 'create'
|
|
19
|
+
? field.createType
|
|
20
|
+
: field.updateType;
|
|
21
|
+
const isRequired = kind === 'create' &&
|
|
22
|
+
isPrimaryCreateField(field, primaryFieldId, createRequiredMode);
|
|
23
|
+
return ` ${quotePropertyKey(field.tsKey)}${isRequired ? '' : '?'}: ${type};`;
|
|
24
|
+
});
|
|
25
|
+
if (!lines.length) {
|
|
26
|
+
return `export interface ${name} {}`;
|
|
27
|
+
}
|
|
28
|
+
return [`export interface ${name} {`, ...lines, `}`].join('\n');
|
|
29
|
+
}
|
|
30
|
+
function emitEnumAliases(table) {
|
|
31
|
+
return table.fields
|
|
32
|
+
.filter((field) => Boolean(field.enumTypeAlias))
|
|
33
|
+
.map((field) => `export type ${field.tsTypeName} = ${field.enumTypeAlias};`);
|
|
34
|
+
}
|
|
35
|
+
function emitTableIds(normalizedBase) {
|
|
36
|
+
const lines = normalizedBase.tables.map((table) => ` ${toSafeConstKey(table.tsName)}: ${JSON.stringify(table.id)},`);
|
|
37
|
+
return ['export const tables = {', ...lines, '} as const;'].join('\n');
|
|
38
|
+
}
|
|
39
|
+
function emitFieldIds(normalizedBase) {
|
|
40
|
+
const lines = normalizedBase.tables.map((table) => {
|
|
41
|
+
const fieldLines = table.fields.map((field) => ` ${toSafeConstKey(field.tsKey)}: ${JSON.stringify(field.id)},`);
|
|
42
|
+
return [` ${toSafeConstKey(table.tsName)}: {`, ...fieldLines, ' },'].join('\n');
|
|
43
|
+
});
|
|
44
|
+
return ['export const fields = {', ...lines, '} as const;'].join('\n');
|
|
45
|
+
}
|
|
46
|
+
export function emitTypeScript({ normalizedBase, includeTableIds, includeFieldIds, schemaMode, createRequiredMode, }) {
|
|
47
|
+
const sections = [
|
|
48
|
+
'/* eslint-disable */',
|
|
49
|
+
'// Generated by easy-airtable-api. Do not edit manually.',
|
|
50
|
+
'',
|
|
51
|
+
"import type { AirtableRecord, Attachment, AttachmentWrite, Collaborator } from 'easy-airtable-api';",
|
|
52
|
+
'',
|
|
53
|
+
'export interface Barcode {',
|
|
54
|
+
' text: string;',
|
|
55
|
+
' type?: string;',
|
|
56
|
+
'}',
|
|
57
|
+
'',
|
|
58
|
+
'export interface ButtonFieldValue {',
|
|
59
|
+
' label: string;',
|
|
60
|
+
' url: string | null;',
|
|
61
|
+
'}',
|
|
62
|
+
];
|
|
63
|
+
if (includeTableIds) {
|
|
64
|
+
sections.push('', emitTableIds(normalizedBase));
|
|
65
|
+
}
|
|
66
|
+
if (includeFieldIds) {
|
|
67
|
+
sections.push('', emitFieldIds(normalizedBase));
|
|
68
|
+
}
|
|
69
|
+
normalizedBase.tables.forEach((table) => {
|
|
70
|
+
sections.push('');
|
|
71
|
+
sections.push(`// ${table.name} (${table.id})`);
|
|
72
|
+
const enumAliases = emitEnumAliases(table);
|
|
73
|
+
if (enumAliases.length) {
|
|
74
|
+
sections.push(...enumAliases, '');
|
|
75
|
+
}
|
|
76
|
+
if (schemaMode === 'read' || schemaMode === 'full') {
|
|
77
|
+
sections.push(emitInterface({
|
|
78
|
+
name: `${table.tsName}RecordFields`,
|
|
79
|
+
fields: table.fields,
|
|
80
|
+
kind: 'read',
|
|
81
|
+
primaryFieldId: table.primaryFieldId,
|
|
82
|
+
createRequiredMode,
|
|
83
|
+
}));
|
|
84
|
+
sections.push(`export type ${table.tsName}Record = AirtableRecord<${table.tsName}RecordFields>;`);
|
|
85
|
+
}
|
|
86
|
+
if (schemaMode === 'write' || schemaMode === 'full') {
|
|
87
|
+
sections.push(emitInterface({
|
|
88
|
+
name: `${table.tsName}CreateFields`,
|
|
89
|
+
fields: table.fields,
|
|
90
|
+
kind: 'create',
|
|
91
|
+
primaryFieldId: table.primaryFieldId,
|
|
92
|
+
createRequiredMode,
|
|
93
|
+
}));
|
|
94
|
+
sections.push(emitInterface({
|
|
95
|
+
name: `${table.tsName}UpdateFields`,
|
|
96
|
+
fields: table.fields,
|
|
97
|
+
kind: 'update',
|
|
98
|
+
primaryFieldId: table.primaryFieldId,
|
|
99
|
+
createRequiredMode,
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return `${sections.join('\n')}\n`;
|
|
104
|
+
}
|