easy-airtable-api 0.0.16 → 0.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/CHANGELOG.md +16 -0
- package/README.md +224 -1
- package/easy-airtable-api-0.1.0.tgz +0 -0
- package/lib/bases.d.ts +18 -0
- package/lib/bases.js +40 -0
- package/lib/codegen/emit-typescript.js +43 -30
- package/lib/codegen/identifiers.d.ts +4 -0
- package/lib/codegen/identifiers.js +24 -2
- package/lib/codegen/load-schema.js +1 -1
- package/lib/codegen/map-field.d.ts +12 -13
- package/lib/codegen/map-field.js +74 -56
- package/lib/codegen/normalize-schema.js +31 -13
- package/lib/codegen/types.d.ts +2 -0
- package/lib/index.d.ts +8 -3
- package/lib/index.js +3 -1
- package/lib/records.d.ts +110 -9
- package/lib/records.js +111 -22
- package/lib/requests.d.ts +64 -5
- package/lib/requests.js +280 -30
- package/lib/types/bases.d.ts +8 -40
- package/lib/types/fields.d.ts +12 -10
- package/lib/types/records.d.ts +8 -0
- package/package.json +7 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
- Exported a broader canonical public field/value type surface, including shared types used by generated code such as `AICell`, `BarcodeCell`, `BarcodeWrite`, `ButtonCell`, and `CollaboratorWrite`.
|
|
6
|
+
- Improved code generation to normalize legacy Airtable metadata variants, normalize Unicode identifiers, and emit collision-safe table/field constants and interface properties.
|
|
7
|
+
- Fixed generated typing for `aiText`, lookup/rollup nested collection results, duplicate field names, and response-mode guardrails for field-name-keyed generated record types.
|
|
8
|
+
- Expanded codegen fixture coverage and compile/type-level tests for aliases, Unicode, collisions, generated output safety, and response-mode compatibility.
|
|
9
|
+
|
|
10
|
+
## 0.0.17
|
|
11
|
+
|
|
12
|
+
- Reworked Airtable type generation around the new `easy-airtable-api/codegen` workflow.
|
|
13
|
+
- Added public metadata/base helpers: `listBases`, `getBaseSchema`, and verified `createBase`.
|
|
14
|
+
- Added `airtableRequestRaw` as a public escape hatch for unsupported Airtable endpoints.
|
|
15
|
+
- Hardened the shared request layer with structured `AirtableApiError`, retries, `Retry-After`, jittered backoff, safer encoding, and configurable retry options.
|
|
16
|
+
- Added record API parity helpers including `deleteRecord`, `replaceRecord`, `replaceRecords`, `getRecord(..., options)`, `getRecordsPage`, and `iterateRecordsPages`.
|
|
17
|
+
- Expanded offline and opt-in integration test coverage for retries, batching, validation, metadata, codegen, and non-mutation guarantees.
|
|
18
|
+
|
|
3
19
|
## 0.0.16
|
|
4
20
|
|
|
5
21
|
- Improved type generation to support more Airtable field types.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Easy Airtable API
|
|
2
2
|
|
|
3
|
-
Easy Airtable API is a lightweight, TypeScript-first Airtable client for
|
|
3
|
+
Easy Airtable API is a lightweight, TypeScript-first Airtable client for records, metadata/base APIs, code generation, and raw Airtable requests.
|
|
4
4
|
|
|
5
5
|
It is built to stay practical:
|
|
6
6
|
|
|
@@ -48,6 +48,7 @@ A good setup is to generate Airtable types once and commit the generated file.
|
|
|
48
48
|
AIRTABLE_ACCESS_TOKEN=pat_xxx
|
|
49
49
|
AIRTABLE_BASE_ID=app_xxx
|
|
50
50
|
AIRTABLE_PROJECTS_TABLE_ID=tbl_xxx
|
|
51
|
+
AIRTABLE_WORKSPACE_ID=wsp_xxx
|
|
51
52
|
```
|
|
52
53
|
|
|
53
54
|
### Example `package.json` scripts
|
|
@@ -188,6 +189,145 @@ await generateAirtableTypes({
|
|
|
188
189
|
});
|
|
189
190
|
```
|
|
190
191
|
|
|
192
|
+
### Generated type assumptions
|
|
193
|
+
|
|
194
|
+
Generated read types target Airtable's default JSON response mode:
|
|
195
|
+
|
|
196
|
+
- `cellFormat: 'json'`
|
|
197
|
+
- `returnFieldsByFieldId: false`
|
|
198
|
+
|
|
199
|
+
If you switch to `cellFormat: 'string'` or `returnFieldsByFieldId: true`, the response shape is no longer compatible with field-name-keyed generated interfaces. The read helpers now include type-level guardrails for this path, and incompatible calls fall back to untyped field maps unless you stay on the default JSON mode.
|
|
200
|
+
|
|
201
|
+
### Shared field/value types
|
|
202
|
+
|
|
203
|
+
Generated files import shared canonical field/value types from the package root instead of emitting duplicate local interfaces. You can import the same types in application code:
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
import type {
|
|
207
|
+
AICell,
|
|
208
|
+
Attachment,
|
|
209
|
+
AttachmentWrite,
|
|
210
|
+
BarcodeCell,
|
|
211
|
+
BarcodeWrite,
|
|
212
|
+
ButtonCell,
|
|
213
|
+
Collaborator,
|
|
214
|
+
CollaboratorWrite,
|
|
215
|
+
} from 'easy-airtable-api';
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Examples:
|
|
219
|
+
|
|
220
|
+
- `aiText` read values use `AICell`
|
|
221
|
+
- attachment reads use `Attachment[]`, writes use `AttachmentWrite[]`
|
|
222
|
+
- collaborator reads use `Collaborator` / `Collaborator[]`, writes use `CollaboratorWrite` / `CollaboratorWrite[]`
|
|
223
|
+
- barcode reads use `BarcodeCell`, writes use `BarcodeWrite`
|
|
224
|
+
- button fields are readonly and use `ButtonCell`
|
|
225
|
+
|
|
226
|
+
These shared types make generated output and application code agree on one canonical public surface. They do not, by themselves, guarantee that every structured write shape has been exhaustively live-verified against Airtable in every edge case.
|
|
227
|
+
|
|
228
|
+
## Metadata/base APIs
|
|
229
|
+
|
|
230
|
+
### List accessible bases
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import { listBases } from 'easy-airtable-api';
|
|
234
|
+
|
|
235
|
+
const result = await listBases({
|
|
236
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
for (const base of result.bases) {
|
|
240
|
+
console.log(base.id, base.name, base.permissionLevel);
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Fetch a base schema directly
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
import { getBaseSchema } from 'easy-airtable-api';
|
|
248
|
+
|
|
249
|
+
const schema = await getBaseSchema({
|
|
250
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
251
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
console.log(schema.tables.map((table) => table.name));
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
This is the same metadata path used by the code generator when `source` includes a `baseId` and token.
|
|
258
|
+
|
|
259
|
+
### Create a base
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
import { createBase } from 'easy-airtable-api';
|
|
263
|
+
|
|
264
|
+
const created = await createBase({
|
|
265
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
266
|
+
body: {
|
|
267
|
+
workspaceId: process.env.AIRTABLE_WORKSPACE_ID!,
|
|
268
|
+
name: 'Apartment Hunting',
|
|
269
|
+
tables: [
|
|
270
|
+
{
|
|
271
|
+
name: 'Apartments',
|
|
272
|
+
fields: [
|
|
273
|
+
{ name: 'Name', type: 'singleLineText' },
|
|
274
|
+
{
|
|
275
|
+
name: 'Visited',
|
|
276
|
+
type: 'checkbox',
|
|
277
|
+
options: { color: 'greenBright', icon: 'check' },
|
|
278
|
+
},
|
|
279
|
+
],
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
console.log(created.id);
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Per Airtable's metadata API documentation, creating a base requires a token authorized for `schema.bases:write`, and the caller must be able to create bases in the target workspace (documented as workspace creator permissions).
|
|
289
|
+
|
|
290
|
+
## Raw Airtable request escape hatch
|
|
291
|
+
|
|
292
|
+
Use `airtableRequestRaw()` when you need an Airtable endpoint that this library does not wrap yet, but still want the shared auth, retry, encoding, and structured error behavior.
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
import { airtableRequestRaw } from 'easy-airtable-api';
|
|
296
|
+
|
|
297
|
+
const schema = await airtableRequestRaw({
|
|
298
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
299
|
+
method: 'GET',
|
|
300
|
+
path: '/v0/meta/bases/app123/tables',
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
It accepts either Airtable-style `/v0/...` paths or paths relative to the API root used internally.
|
|
305
|
+
|
|
306
|
+
## Retry configuration
|
|
307
|
+
|
|
308
|
+
All runtime helpers accept an optional top-level `retry` object for tuning retry behavior without leaving the library's shared request layer.
|
|
309
|
+
|
|
310
|
+
```ts
|
|
311
|
+
import { getRecords } from 'easy-airtable-api';
|
|
312
|
+
|
|
313
|
+
const records = await getRecords({
|
|
314
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
315
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
316
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
317
|
+
retry: {
|
|
318
|
+
maxRetries: 2,
|
|
319
|
+
baseDelayMs: 250,
|
|
320
|
+
maxDelayMs: 1000,
|
|
321
|
+
retryOn429: true,
|
|
322
|
+
retryOn5xx: true,
|
|
323
|
+
retryOnNetworkErrors: true,
|
|
324
|
+
useJitter: false,
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
The same `retry` option is supported by metadata helpers such as `listBases()` / `getBaseSchema()` and by `airtableRequestRaw()`.
|
|
330
|
+
|
|
191
331
|
## Runtime examples
|
|
192
332
|
|
|
193
333
|
### Get a single record
|
|
@@ -201,9 +341,46 @@ const record = await getRecord<ProjectsRecordFields>({
|
|
|
201
341
|
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
202
342
|
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
203
343
|
recordId: 'rec123',
|
|
344
|
+
options: {
|
|
345
|
+
returnFieldsByFieldId: false,
|
|
346
|
+
},
|
|
204
347
|
});
|
|
205
348
|
```
|
|
206
349
|
|
|
350
|
+
### Get one page of records
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
import { getRecordsPage } from 'easy-airtable-api';
|
|
354
|
+
import type { ProjectsRecordFields } from './airtable.generated';
|
|
355
|
+
|
|
356
|
+
const page = await getRecordsPage<ProjectsRecordFields>({
|
|
357
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
358
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
359
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
360
|
+
options: {
|
|
361
|
+
pageSize: 25,
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### Iterate through record pages
|
|
367
|
+
|
|
368
|
+
```ts
|
|
369
|
+
import { iterateRecordsPages } from 'easy-airtable-api';
|
|
370
|
+
import type { ProjectsRecordFields } from './airtable.generated';
|
|
371
|
+
|
|
372
|
+
for await (const page of iterateRecordsPages<ProjectsRecordFields>({
|
|
373
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
374
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
375
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
376
|
+
options: {
|
|
377
|
+
pageSize: 100,
|
|
378
|
+
},
|
|
379
|
+
})) {
|
|
380
|
+
console.log(page.records.length, page.offset);
|
|
381
|
+
}
|
|
382
|
+
```
|
|
383
|
+
|
|
207
384
|
### Create multiple records
|
|
208
385
|
|
|
209
386
|
```ts
|
|
@@ -234,6 +411,24 @@ const result = await createRecords<ProjectsCreateFields>({
|
|
|
234
411
|
});
|
|
235
412
|
```
|
|
236
413
|
|
|
414
|
+
### Replace a single record
|
|
415
|
+
|
|
416
|
+
```ts
|
|
417
|
+
import { replaceRecord } from 'easy-airtable-api';
|
|
418
|
+
import type { ProjectsCreateFields } from './airtable.generated';
|
|
419
|
+
|
|
420
|
+
const result = await replaceRecord<ProjectsCreateFields>({
|
|
421
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
422
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
423
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
424
|
+
recordId: 'rec123',
|
|
425
|
+
fields: {
|
|
426
|
+
Name: 'Project A',
|
|
427
|
+
Status: 'Done',
|
|
428
|
+
},
|
|
429
|
+
});
|
|
430
|
+
```
|
|
431
|
+
|
|
237
432
|
### Upsert records
|
|
238
433
|
|
|
239
434
|
```ts
|
|
@@ -259,6 +454,19 @@ const result = await updateRecordsUpsert<ProjectsCreateFields>({
|
|
|
259
454
|
});
|
|
260
455
|
```
|
|
261
456
|
|
|
457
|
+
### Delete a single record
|
|
458
|
+
|
|
459
|
+
```ts
|
|
460
|
+
import { deleteRecord } from 'easy-airtable-api';
|
|
461
|
+
|
|
462
|
+
await deleteRecord({
|
|
463
|
+
apiKey: process.env.AIRTABLE_ACCESS_TOKEN!,
|
|
464
|
+
baseId: process.env.AIRTABLE_BASE_ID!,
|
|
465
|
+
tableId: process.env.AIRTABLE_PROJECTS_TABLE_ID!,
|
|
466
|
+
recordId: 'rec123',
|
|
467
|
+
});
|
|
468
|
+
```
|
|
469
|
+
|
|
262
470
|
### Delete records
|
|
263
471
|
|
|
264
472
|
```ts
|
|
@@ -278,11 +486,20 @@ Runtime package:
|
|
|
278
486
|
|
|
279
487
|
```ts
|
|
280
488
|
import {
|
|
489
|
+
airtableRequestRaw,
|
|
490
|
+
createBase,
|
|
281
491
|
createRecord,
|
|
282
492
|
createRecords,
|
|
493
|
+
deleteRecord,
|
|
283
494
|
deleteRecords,
|
|
495
|
+
getBaseSchema,
|
|
284
496
|
getRecord,
|
|
285
497
|
getRecords,
|
|
498
|
+
getRecordsPage,
|
|
499
|
+
iterateRecordsPages,
|
|
500
|
+
listBases,
|
|
501
|
+
replaceRecord,
|
|
502
|
+
replaceRecords,
|
|
286
503
|
updateRecord,
|
|
287
504
|
updateRecords,
|
|
288
505
|
updateRecordsUpsert,
|
|
@@ -298,6 +515,12 @@ import {
|
|
|
298
515
|
} from 'easy-airtable-api/codegen';
|
|
299
516
|
```
|
|
300
517
|
|
|
518
|
+
## Testing
|
|
519
|
+
|
|
520
|
+
Codegen coverage lives in focused fixtures under `tests/fixtures/` and runs as part of the normal Vitest suite.
|
|
521
|
+
|
|
522
|
+
The fixture matrix also includes compile checks for generated output and type-level guardrail tests for response modes that are incompatible with field-name-keyed generated types.
|
|
523
|
+
|
|
301
524
|
## Changelog
|
|
302
525
|
|
|
303
526
|
See [CHANGELOG.md](./CHANGELOG.md).
|
|
Binary file
|
package/lib/bases.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AirtableRetryOptions } from './requests.js';
|
|
2
|
+
import type { CreateBaseRequestBody, CreateBaseResponse, ListBasesResponse } from './types/bases.js';
|
|
3
|
+
import type { AirtableBaseSchema } from './types/metadata.js';
|
|
4
|
+
export declare function listBases({ apiKey, offset, retry, }: {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
offset?: string;
|
|
7
|
+
retry?: AirtableRetryOptions;
|
|
8
|
+
}): Promise<ListBasesResponse>;
|
|
9
|
+
export declare function createBase({ apiKey, body, retry, }: {
|
|
10
|
+
apiKey: string;
|
|
11
|
+
body: CreateBaseRequestBody;
|
|
12
|
+
retry?: AirtableRetryOptions;
|
|
13
|
+
}): Promise<CreateBaseResponse>;
|
|
14
|
+
export declare function getBaseSchema({ apiKey, baseId, retry, }: {
|
|
15
|
+
apiKey: string;
|
|
16
|
+
baseId: string;
|
|
17
|
+
retry?: AirtableRetryOptions;
|
|
18
|
+
}): Promise<AirtableBaseSchema>;
|
package/lib/bases.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { airtableApiRequest } from './requests.js';
|
|
2
|
+
export async function listBases({ apiKey, offset, retry, }) {
|
|
3
|
+
return airtableApiRequest({
|
|
4
|
+
apiKey,
|
|
5
|
+
method: 'GET',
|
|
6
|
+
path: '/meta/bases',
|
|
7
|
+
query: { offset },
|
|
8
|
+
retry,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export async function createBase({ apiKey, body, retry, }) {
|
|
12
|
+
return airtableApiRequest({
|
|
13
|
+
apiKey,
|
|
14
|
+
method: 'POST',
|
|
15
|
+
path: '/meta/bases',
|
|
16
|
+
body,
|
|
17
|
+
retry,
|
|
18
|
+
requestContext: {
|
|
19
|
+
method: 'POST',
|
|
20
|
+
path: '/meta/bases',
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export async function getBaseSchema({ apiKey, baseId, retry, }) {
|
|
25
|
+
if (!baseId) {
|
|
26
|
+
throw new Error('Base ID is not set. Please provide a valid Airtable base ID.');
|
|
27
|
+
}
|
|
28
|
+
const path = `/meta/bases/${encodeURIComponent(baseId)}/tables`;
|
|
29
|
+
return airtableApiRequest({
|
|
30
|
+
apiKey,
|
|
31
|
+
method: 'GET',
|
|
32
|
+
path,
|
|
33
|
+
retry,
|
|
34
|
+
requestContext: {
|
|
35
|
+
method: 'GET',
|
|
36
|
+
baseId,
|
|
37
|
+
path,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
@@ -1,26 +1,48 @@
|
|
|
1
|
-
import { quotePropertyKey
|
|
1
|
+
import { quotePropertyKey } from './identifiers.js';
|
|
2
2
|
function isPrimaryCreateField(field, primaryFieldId, createRequiredMode) {
|
|
3
3
|
return (createRequiredMode === 'primaryField' &&
|
|
4
4
|
field.id === primaryFieldId &&
|
|
5
5
|
Boolean(field.createType));
|
|
6
6
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
function getFieldTypeForKind(field, kind) {
|
|
8
|
+
if (kind === 'read')
|
|
9
|
+
return field.readType;
|
|
10
|
+
if (kind === 'create')
|
|
11
|
+
return field.createType;
|
|
12
|
+
return field.updateType;
|
|
13
|
+
}
|
|
14
|
+
function groupFieldsByPropertyKey(fields, kind) {
|
|
15
|
+
const groups = new Map();
|
|
16
|
+
fields.forEach((field) => {
|
|
17
|
+
const type = getFieldTypeForKind(field, kind);
|
|
18
|
+
if (!type)
|
|
19
|
+
return;
|
|
20
|
+
const existing = groups.get(field.tsKey);
|
|
21
|
+
if (existing) {
|
|
22
|
+
existing.push(field);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
groups.set(field.tsKey, [field]);
|
|
14
26
|
});
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
27
|
+
return [...groups.entries()].map(([key, groupedFields]) => ({
|
|
28
|
+
key,
|
|
29
|
+
fields: groupedFields,
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
function unionTypes(types) {
|
|
33
|
+
const distinctTypes = [...new Set(types)];
|
|
34
|
+
return distinctTypes.join(' | ');
|
|
35
|
+
}
|
|
36
|
+
function emitInterface({ name, fields, kind, primaryFieldId, createRequiredMode, }) {
|
|
37
|
+
const groupedFields = groupFieldsByPropertyKey(fields, kind);
|
|
38
|
+
const lines = groupedFields.map(({ key, fields: grouped }) => {
|
|
39
|
+
const propertyTypes = grouped
|
|
40
|
+
.map((field) => getFieldTypeForKind(field, kind))
|
|
41
|
+
.filter((type) => Boolean(type));
|
|
42
|
+
const propertyType = unionTypes(propertyTypes);
|
|
21
43
|
const isRequired = kind === 'create' &&
|
|
22
|
-
isPrimaryCreateField(field, primaryFieldId, createRequiredMode);
|
|
23
|
-
return ` ${quotePropertyKey(
|
|
44
|
+
grouped.some((field) => isPrimaryCreateField(field, primaryFieldId, createRequiredMode));
|
|
45
|
+
return ` ${quotePropertyKey(key)}${isRequired ? '' : '?'}: ${propertyType};`;
|
|
24
46
|
});
|
|
25
47
|
if (!lines.length) {
|
|
26
48
|
return `export interface ${name} {}`;
|
|
@@ -33,13 +55,13 @@ function emitEnumAliases(table) {
|
|
|
33
55
|
.map((field) => `export type ${field.tsTypeName} = ${field.enumTypeAlias};`);
|
|
34
56
|
}
|
|
35
57
|
function emitTableIds(normalizedBase) {
|
|
36
|
-
const lines = normalizedBase.tables.map((table) => ` ${
|
|
58
|
+
const lines = normalizedBase.tables.map((table) => ` ${table.tsConstKey}: ${JSON.stringify(table.id)},`);
|
|
37
59
|
return ['export const tables = {', ...lines, '} as const;'].join('\n');
|
|
38
60
|
}
|
|
39
61
|
function emitFieldIds(normalizedBase) {
|
|
40
62
|
const lines = normalizedBase.tables.map((table) => {
|
|
41
|
-
const fieldLines = table.fields.map((field) => ` ${
|
|
42
|
-
return [` ${
|
|
63
|
+
const fieldLines = table.fields.map((field) => ` ${field.tsConstKey}: ${JSON.stringify(field.id)},`);
|
|
64
|
+
return [` ${table.tsConstKey}: {`, ...fieldLines, ' },'].join('\n');
|
|
43
65
|
});
|
|
44
66
|
return ['export const fields = {', ...lines, '} as const;'].join('\n');
|
|
45
67
|
}
|
|
@@ -47,18 +69,9 @@ export function emitTypeScript({ normalizedBase, includeTableIds, includeFieldId
|
|
|
47
69
|
const sections = [
|
|
48
70
|
'/* eslint-disable */',
|
|
49
71
|
'// Generated by easy-airtable-api. Do not edit manually.',
|
|
72
|
+
"// Generated record-field types assume Airtable's default response mode: cellFormat: 'json' and returnFieldsByFieldId: false.",
|
|
50
73
|
'',
|
|
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
|
-
'}',
|
|
74
|
+
"import type { AICell, AirtableRecord, Attachment, AttachmentWrite, BarcodeCell, BarcodeWrite, ButtonCell, Collaborator, CollaboratorWrite } from 'easy-airtable-api';",
|
|
62
75
|
];
|
|
63
76
|
if (includeTableIds) {
|
|
64
77
|
sections.push('', emitTableIds(normalizedBase));
|
|
@@ -4,4 +4,8 @@ export declare function makeUniquePascalNames<T extends {
|
|
|
4
4
|
id: string;
|
|
5
5
|
name: string;
|
|
6
6
|
}>(items: T[]): Map<string, string>;
|
|
7
|
+
export declare function makeUniqueConstKeys<T extends {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
}>(items: T[]): Map<string, string>;
|
|
7
11
|
export declare function quotePropertyKey(value: string): string;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
const NON_IDENTIFIER = /[^a-zA-Z0-9_$]+/g;
|
|
2
2
|
const MULTIPLE_UNDERSCORES = /_+/g;
|
|
3
|
+
const COMBINING_MARKS = /[\u0300-\u036f]/g;
|
|
4
|
+
function normalizeIdentifierInput(value) {
|
|
5
|
+
return value.normalize('NFKD').replace(COMBINING_MARKS, '');
|
|
6
|
+
}
|
|
3
7
|
function suffixFromId(id) {
|
|
4
8
|
return id.replace(/[^a-zA-Z0-9]/g, '').slice(-6) || 'Id';
|
|
5
9
|
}
|
|
6
10
|
export function toPascalCase(value) {
|
|
7
|
-
const normalized = value
|
|
11
|
+
const normalized = normalizeIdentifierInput(value)
|
|
8
12
|
.replace(NON_IDENTIFIER, ' ')
|
|
9
13
|
.trim()
|
|
10
14
|
.split(/\s+/)
|
|
@@ -18,7 +22,7 @@ export function toPascalCase(value) {
|
|
|
18
22
|
return normalized;
|
|
19
23
|
}
|
|
20
24
|
export function toSafeConstKey(value) {
|
|
21
|
-
const normalized = value
|
|
25
|
+
const normalized = normalizeIdentifierInput(value)
|
|
22
26
|
.replace(NON_IDENTIFIER, '_')
|
|
23
27
|
.replace(MULTIPLE_UNDERSCORES, '_')
|
|
24
28
|
.replace(/^_+|_+$/g, '');
|
|
@@ -46,6 +50,24 @@ export function makeUniquePascalNames(items) {
|
|
|
46
50
|
});
|
|
47
51
|
return result;
|
|
48
52
|
}
|
|
53
|
+
export function makeUniqueConstKeys(items) {
|
|
54
|
+
const result = new Map();
|
|
55
|
+
const counts = new Map();
|
|
56
|
+
items.forEach((item) => {
|
|
57
|
+
const base = toSafeConstKey(item.name);
|
|
58
|
+
const count = counts.get(base) ?? 0;
|
|
59
|
+
counts.set(base, count + 1);
|
|
60
|
+
});
|
|
61
|
+
items.forEach((item) => {
|
|
62
|
+
const base = toSafeConstKey(item.name);
|
|
63
|
+
if ((counts.get(base) ?? 0) === 1) {
|
|
64
|
+
result.set(item.id, base);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
result.set(item.id, `${base}_${suffixFromId(item.id)}`);
|
|
68
|
+
});
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
49
71
|
export function quotePropertyKey(value) {
|
|
50
72
|
return JSON.stringify(value);
|
|
51
73
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { resolve } from 'node:path';
|
|
3
|
-
import { getBaseSchema } from '../
|
|
3
|
+
import { getBaseSchema } from '../bases.js';
|
|
4
4
|
import { isRecord } from './utils.js';
|
|
5
5
|
function isAirtableBaseSchema(value) {
|
|
6
6
|
if (!isRecord(value))
|
|
@@ -1,31 +1,30 @@
|
|
|
1
1
|
import type { AirtableFieldSchema } from '../types/metadata.js';
|
|
2
2
|
import type { EnumMode, NormalizedField, UnknownFieldBehavior } from './types.js';
|
|
3
|
+
type CollectionKind = 'scalar' | 'array';
|
|
4
|
+
type MappedFieldType = {
|
|
5
|
+
type: string;
|
|
6
|
+
warning?: string;
|
|
7
|
+
collection: CollectionKind;
|
|
8
|
+
};
|
|
3
9
|
export declare function mapFieldTypes({ field, enumMode, unknownFieldBehavior, }: {
|
|
4
10
|
field: AirtableFieldSchema;
|
|
5
11
|
enumMode: EnumMode;
|
|
6
12
|
unknownFieldBehavior: UnknownFieldBehavior;
|
|
7
13
|
}): {
|
|
8
|
-
read:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
create?: {
|
|
13
|
-
type: string;
|
|
14
|
-
warning?: string;
|
|
15
|
-
};
|
|
16
|
-
update?: {
|
|
17
|
-
type: string;
|
|
18
|
-
warning?: string;
|
|
19
|
-
};
|
|
14
|
+
read: MappedFieldType;
|
|
15
|
+
create?: MappedFieldType;
|
|
16
|
+
update?: MappedFieldType;
|
|
20
17
|
readonly: boolean;
|
|
21
18
|
computed: boolean;
|
|
22
19
|
linkedTableId?: string;
|
|
23
20
|
enumTypeAlias?: string;
|
|
24
21
|
};
|
|
25
|
-
export declare function mapFieldToNormalizedField({ field, enumMode, unknownFieldBehavior, tsKey, tsTypeName, }: {
|
|
22
|
+
export declare function mapFieldToNormalizedField({ field, enumMode, unknownFieldBehavior, tsKey, tsConstKey, tsTypeName, }: {
|
|
26
23
|
field: AirtableFieldSchema;
|
|
27
24
|
enumMode: EnumMode;
|
|
28
25
|
unknownFieldBehavior: UnknownFieldBehavior;
|
|
29
26
|
tsKey: string;
|
|
27
|
+
tsConstKey: string;
|
|
30
28
|
tsTypeName: string;
|
|
31
29
|
}): NormalizedField;
|
|
30
|
+
export {};
|