easy-airtable-api 0.1.0 → 0.2.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +55 -1
  3. package/package.json +5 -2
  4. package/easy-airtable-api-0.1.0.tgz +0 -0
  5. package/lib/bases.d.ts +0 -18
  6. package/lib/bases.js +0 -40
  7. package/lib/cli.d.ts +0 -2
  8. package/lib/cli.js +0 -82
  9. package/lib/codegen/build.d.ts +0 -2
  10. package/lib/codegen/build.js +0 -25
  11. package/lib/codegen/emit-typescript.d.ts +0 -8
  12. package/lib/codegen/emit-typescript.js +0 -117
  13. package/lib/codegen/generate.d.ts +0 -2
  14. package/lib/codegen/generate.js +0 -23
  15. package/lib/codegen/identifiers.d.ts +0 -11
  16. package/lib/codegen/identifiers.js +0 -73
  17. package/lib/codegen/index.d.ts +0 -3
  18. package/lib/codegen/index.js +0 -2
  19. package/lib/codegen/load-schema.d.ts +0 -3
  20. package/lib/codegen/load-schema.js +0 -28
  21. package/lib/codegen/map-field.d.ts +0 -30
  22. package/lib/codegen/map-field.js +0 -296
  23. package/lib/codegen/normalize-schema.d.ts +0 -11
  24. package/lib/codegen/normalize-schema.js +0 -79
  25. package/lib/codegen/types.d.ts +0 -64
  26. package/lib/codegen/types.js +0 -1
  27. package/lib/codegen/utils.d.ts +0 -14
  28. package/lib/codegen/utils.js +0 -30
  29. package/lib/codegen/write-atomic.d.ts +0 -1
  30. package/lib/codegen/write-atomic.js +0 -11
  31. package/lib/index.d.ts +0 -8
  32. package/lib/index.js +0 -3
  33. package/lib/records.d.ts +0 -197
  34. package/lib/records.js +0 -300
  35. package/lib/requests.d.ts +0 -74
  36. package/lib/requests.js +0 -362
  37. package/lib/types/bases.d.ts +0 -31
  38. package/lib/types/bases.js +0 -1
  39. package/lib/types/fields.d.ts +0 -330
  40. package/lib/types/fields.js +0 -1
  41. package/lib/types/metadata.d.ts +0 -24
  42. package/lib/types/metadata.js +0 -1
  43. package/lib/types/records.d.ts +0 -130
  44. package/lib/types/records.js +0 -1
  45. package/lib/types/tables.d.ts +0 -99
  46. package/lib/types/tables.js +0 -1
  47. package/lib/utils.d.ts +0 -4
  48. package/lib/utils.js +0 -19
@@ -1,296 +0,0 @@
1
- import { getChoices, getRecordValue, stringifyUnion } from './utils.js';
2
- const FIELD_TYPE_ALIASES = {
3
- attachment: 'multipleAttachments',
4
- linkToAnotherRecord: 'multipleRecordLinks',
5
- multipleLookupValues: 'lookup',
6
- };
7
- const READONLY_FIELD_TYPES = new Set([
8
- 'aiText',
9
- 'autoNumber',
10
- 'button',
11
- 'count',
12
- 'createdBy',
13
- 'createdTime',
14
- 'formula',
15
- 'lastModifiedBy',
16
- 'lastModifiedTime',
17
- 'lookup',
18
- 'rollup',
19
- 'syncSource',
20
- ]);
21
- const COMPUTED_FIELD_TYPES = new Set([
22
- 'aiText',
23
- 'autoNumber',
24
- 'button',
25
- 'count',
26
- 'formula',
27
- 'lookup',
28
- 'rollup',
29
- 'syncSource',
30
- ]);
31
- function canonicalizeFieldType(type) {
32
- return FIELD_TYPE_ALIASES[type] ?? type;
33
- }
34
- function resolveUnknown(field, unknownFieldBehavior, reason) {
35
- const warning = reason
36
- ? `${field.name}: ${reason}`
37
- : `${field.name}: unsupported Airtable field type ${field.type}`;
38
- if (unknownFieldBehavior === 'error') {
39
- throw new Error(warning);
40
- }
41
- return {
42
- type: 'unknown',
43
- warning,
44
- collection: 'scalar',
45
- };
46
- }
47
- function resolveUnknownArray(field, unknownFieldBehavior, reason) {
48
- const resolved = resolveUnknown(field, unknownFieldBehavior, reason);
49
- return {
50
- type: 'unknown[]',
51
- warning: resolved.warning,
52
- collection: 'array',
53
- };
54
- }
55
- function buildEnumType(field, enumMode) {
56
- const choices = getChoices(field.options);
57
- if (!choices?.length)
58
- return undefined;
59
- const literals = stringifyUnion(choices.map((choice) => choice.name));
60
- if (enumMode === 'literal')
61
- return literals;
62
- if (enumMode === 'broad')
63
- return 'string';
64
- return `${literals} | (string & {})`;
65
- }
66
- function mapResultDescriptor(descriptor, field, enumMode, unknownFieldBehavior) {
67
- const resultType = getRecordValue(descriptor, 'type');
68
- if (!resultType) {
69
- return resolveUnknown(field, unknownFieldBehavior, 'metadata result type is missing');
70
- }
71
- const nestedField = {
72
- ...field,
73
- type: resultType,
74
- options: getRecordValue(descriptor, 'options'),
75
- };
76
- return mapFieldTypes({
77
- field: nestedField,
78
- enumMode,
79
- unknownFieldBehavior,
80
- }).read;
81
- }
82
- export function mapFieldTypes({ field, enumMode, unknownFieldBehavior, }) {
83
- const canonicalType = canonicalizeFieldType(field.type);
84
- const normalizedField = canonicalType === field.type ? field : { ...field, type: canonicalType };
85
- const readonly = READONLY_FIELD_TYPES.has(canonicalType);
86
- const computed = COMPUTED_FIELD_TYPES.has(canonicalType);
87
- const linkedTableId = getRecordValue(normalizedField.options, 'linkedTableId');
88
- switch (canonicalType) {
89
- case 'singleLineText':
90
- case 'multilineText':
91
- case 'richText':
92
- case 'email':
93
- case 'url':
94
- case 'phoneNumber':
95
- case 'date':
96
- case 'dateTime':
97
- return {
98
- read: { type: 'string', collection: 'scalar' },
99
- create: { type: 'string', collection: 'scalar' },
100
- update: { type: 'string', collection: 'scalar' },
101
- readonly,
102
- computed,
103
- };
104
- case 'aiText':
105
- return {
106
- read: { type: 'AICell', collection: 'scalar' },
107
- readonly,
108
- computed,
109
- };
110
- case 'number':
111
- case 'currency':
112
- case 'percent':
113
- case 'rating':
114
- case 'duration':
115
- return {
116
- read: { type: 'number', collection: 'scalar' },
117
- create: { type: 'number', collection: 'scalar' },
118
- update: { type: 'number', collection: 'scalar' },
119
- readonly,
120
- computed,
121
- };
122
- case 'checkbox':
123
- return {
124
- read: { type: 'boolean', collection: 'scalar' },
125
- create: { type: 'boolean', collection: 'scalar' },
126
- update: { type: 'boolean', collection: 'scalar' },
127
- readonly,
128
- computed,
129
- };
130
- case 'singleSelect': {
131
- const enumType = buildEnumType(normalizedField, enumMode) ?? 'string';
132
- return {
133
- read: { type: enumType, collection: 'scalar' },
134
- create: { type: enumType, collection: 'scalar' },
135
- update: { type: enumType, collection: 'scalar' },
136
- readonly,
137
- computed,
138
- enumTypeAlias: enumType,
139
- };
140
- }
141
- case 'multipleSelects': {
142
- const enumType = buildEnumType(normalizedField, enumMode) ?? 'string';
143
- return {
144
- read: { type: `Array<${enumType}>`, collection: 'array' },
145
- create: { type: `Array<${enumType}>`, collection: 'array' },
146
- update: { type: `Array<${enumType}>`, collection: 'array' },
147
- readonly,
148
- computed,
149
- enumTypeAlias: enumType,
150
- };
151
- }
152
- case 'multipleAttachments':
153
- return {
154
- read: { type: 'Attachment[]', collection: 'array' },
155
- create: { type: 'AttachmentWrite[]', collection: 'array' },
156
- update: { type: 'AttachmentWrite[]', collection: 'array' },
157
- readonly,
158
- computed,
159
- };
160
- case 'singleCollaborator':
161
- return {
162
- read: { type: 'Collaborator', collection: 'scalar' },
163
- create: { type: 'CollaboratorWrite', collection: 'scalar' },
164
- update: { type: 'CollaboratorWrite', collection: 'scalar' },
165
- readonly,
166
- computed,
167
- };
168
- case 'multipleCollaborators':
169
- return {
170
- read: { type: 'Collaborator[]', collection: 'array' },
171
- create: { type: 'CollaboratorWrite[]', collection: 'array' },
172
- update: { type: 'CollaboratorWrite[]', collection: 'array' },
173
- readonly,
174
- computed,
175
- };
176
- case 'multipleRecordLinks':
177
- return {
178
- read: { type: 'string[]', collection: 'array' },
179
- create: { type: 'string[]', collection: 'array' },
180
- update: { type: 'string[]', collection: 'array' },
181
- readonly,
182
- computed,
183
- linkedTableId,
184
- };
185
- case 'barcode':
186
- return {
187
- read: { type: 'BarcodeCell', collection: 'scalar' },
188
- create: { type: 'BarcodeWrite', collection: 'scalar' },
189
- update: { type: 'BarcodeWrite', collection: 'scalar' },
190
- readonly,
191
- computed,
192
- };
193
- case 'createdTime':
194
- case 'lastModifiedTime':
195
- return {
196
- read: { type: 'string', collection: 'scalar' },
197
- readonly,
198
- computed,
199
- };
200
- case 'createdBy':
201
- case 'lastModifiedBy':
202
- return {
203
- read: { type: 'Collaborator', collection: 'scalar' },
204
- readonly,
205
- computed,
206
- };
207
- case 'button':
208
- return {
209
- read: { type: 'ButtonCell', collection: 'scalar' },
210
- readonly,
211
- computed,
212
- };
213
- case 'count':
214
- case 'autoNumber':
215
- return {
216
- read: { type: 'number', collection: 'scalar' },
217
- readonly,
218
- computed,
219
- };
220
- case 'formula': {
221
- const result = mapResultDescriptor(getRecordValue(normalizedField.options, 'result'), normalizedField, enumMode, unknownFieldBehavior);
222
- return {
223
- read: result,
224
- readonly,
225
- computed,
226
- };
227
- }
228
- case 'rollup': {
229
- const result = getRecordValue(normalizedField.options, 'result');
230
- const mapped = result
231
- ? mapResultDescriptor(result, normalizedField, enumMode, unknownFieldBehavior)
232
- : resolveUnknown(normalizedField, unknownFieldBehavior, 'rollup result metadata is missing');
233
- return {
234
- read: mapped,
235
- readonly,
236
- computed,
237
- };
238
- }
239
- case 'lookup': {
240
- const result = getRecordValue(normalizedField.options, 'result');
241
- const mapped = result
242
- ? mapResultDescriptor(result, normalizedField, enumMode, unknownFieldBehavior)
243
- : resolveUnknownArray(normalizedField, unknownFieldBehavior, 'lookup result metadata is missing');
244
- return {
245
- read: mapped.collection === 'array'
246
- ? mapped
247
- : {
248
- ...mapped,
249
- type: mapped.type === 'unknown' ? 'unknown[]' : `Array<${mapped.type}>`,
250
- collection: 'array',
251
- },
252
- readonly,
253
- computed,
254
- };
255
- }
256
- default: {
257
- const unknown = resolveUnknown(normalizedField, unknownFieldBehavior);
258
- return {
259
- read: unknown,
260
- create: readonly ? undefined : unknown,
261
- update: readonly ? undefined : unknown,
262
- readonly,
263
- computed,
264
- };
265
- }
266
- }
267
- }
268
- export function mapFieldToNormalizedField({ field, enumMode, unknownFieldBehavior, tsKey, tsConstKey, tsTypeName, }) {
269
- const mapped = mapFieldTypes({
270
- field,
271
- enumMode,
272
- unknownFieldBehavior,
273
- });
274
- const warnings = [
275
- mapped.read.warning,
276
- mapped.create?.warning,
277
- mapped.update?.warning,
278
- ].filter((warning) => Boolean(warning));
279
- return {
280
- id: field.id,
281
- name: field.name,
282
- tsKey,
283
- tsConstKey,
284
- tsTypeName,
285
- airtableType: field.type,
286
- readonly: mapped.readonly,
287
- computed: mapped.computed,
288
- readType: mapped.read.type,
289
- createType: mapped.create?.type,
290
- updateType: mapped.update?.type,
291
- linkedTableId: mapped.linkedTableId,
292
- enumTypeAlias: mapped.enumTypeAlias,
293
- warnings,
294
- options: field.options,
295
- };
296
- }
@@ -1,11 +0,0 @@
1
- import type { AirtableBaseSchema } from '../types/metadata.js';
2
- import type { EnumMode, NormalizedBase, UnknownFieldBehavior } from './types.js';
3
- export declare function normalizeSchema({ schema, tableNameOrId, enumMode, unknownFieldBehavior, }: {
4
- schema: AirtableBaseSchema;
5
- tableNameOrId?: string | string[];
6
- enumMode: EnumMode;
7
- unknownFieldBehavior: UnknownFieldBehavior;
8
- }): {
9
- normalizedBase: NormalizedBase;
10
- warnings: string[];
11
- };
@@ -1,79 +0,0 @@
1
- import { makeUniqueConstKeys, makeUniquePascalNames, toPascalCase, toSafeConstKey, } from './identifiers.js';
2
- import { mapFieldToNormalizedField } from './map-field.js';
3
- import { compareByNameThenId, ensureArray } from './utils.js';
4
- function countBy(items, getKey) {
5
- const counts = new Map();
6
- items.forEach((item) => {
7
- const key = getKey(item);
8
- counts.set(key, (counts.get(key) ?? 0) + 1);
9
- });
10
- return counts;
11
- }
12
- export function normalizeSchema({ schema, tableNameOrId, enumMode, unknownFieldBehavior, }) {
13
- const filters = new Set(ensureArray(tableNameOrId));
14
- const selectedTables = [...schema.tables]
15
- .filter((table) => {
16
- if (!filters.size)
17
- return true;
18
- return filters.has(table.id) || filters.has(table.name);
19
- })
20
- .sort(compareByNameThenId);
21
- if (!selectedTables.length) {
22
- throw new Error('No Airtable tables matched the requested tableNameOrId filter.');
23
- }
24
- const warnings = [];
25
- const tableNames = makeUniquePascalNames(selectedTables);
26
- const tableConstKeys = makeUniqueConstKeys(selectedTables);
27
- const tableTypeNameCounts = countBy(selectedTables, (table) => toPascalCase(table.name));
28
- const tableConstKeyCounts = countBy(selectedTables, (table) => toSafeConstKey(table.name));
29
- const tables = selectedTables.map((table) => {
30
- const tsName = tableNames.get(table.id) ?? table.name;
31
- const tsConstKey = tableConstKeys.get(table.id) ?? table.name;
32
- if ((tableTypeNameCounts.get(toPascalCase(table.name)) ?? 0) > 1) {
33
- warnings.push(`Table name collision detected for ${table.name}. Generated type name ${tsName} includes a stable suffix.`);
34
- }
35
- if ((tableConstKeyCounts.get(toSafeConstKey(table.name)) ?? 0) > 1) {
36
- warnings.push(`Table const key collision detected for ${table.name}. Generated tables key ${tsConstKey} includes a stable suffix.`);
37
- }
38
- const fieldNames = makeUniquePascalNames(table.fields);
39
- const fieldConstKeys = makeUniqueConstKeys(table.fields);
40
- const exactFieldNameCounts = countBy(table.fields, (field) => field.name);
41
- const fieldTypeNameCounts = countBy(table.fields, (field) => toPascalCase(field.name));
42
- const fieldConstKeyCounts = countBy(table.fields, (field) => toSafeConstKey(field.name));
43
- const fields = table.fields.map((field) => {
44
- const fieldTsName = fieldNames.get(field.id) ?? field.name;
45
- const fieldTsConstKey = fieldConstKeys.get(field.id) ?? field.name;
46
- const normalizedField = mapFieldToNormalizedField({
47
- field,
48
- enumMode,
49
- unknownFieldBehavior,
50
- tsKey: field.name,
51
- tsConstKey: fieldTsConstKey,
52
- tsTypeName: `${tsName}${fieldTsName}`,
53
- });
54
- if ((exactFieldNameCounts.get(field.name) ?? 0) > 1) {
55
- warnings.push(`Duplicate field name detected for ${table.name}.${field.name}. Generated interfaces merge the colliding fields into a union.`);
56
- }
57
- if ((fieldTypeNameCounts.get(toPascalCase(field.name)) ?? 0) > 1) {
58
- warnings.push(`Field name collision detected for ${table.name}.${field.name}. Generated helper type name ${normalizedField.tsTypeName} includes a stable suffix.`);
59
- }
60
- if ((fieldConstKeyCounts.get(toSafeConstKey(field.name)) ?? 0) > 1) {
61
- warnings.push(`Field const key collision detected for ${table.name}.${field.name}. Generated fields key ${normalizedField.tsConstKey} includes a stable suffix.`);
62
- }
63
- warnings.push(...normalizedField.warnings.map((warning) => `${table.name}.${warning}`));
64
- return normalizedField;
65
- });
66
- return {
67
- id: table.id,
68
- name: table.name,
69
- tsName,
70
- tsConstKey,
71
- primaryFieldId: table.primaryFieldId,
72
- fields,
73
- };
74
- });
75
- return {
76
- normalizedBase: { tables },
77
- warnings: [...new Set(warnings)],
78
- };
79
- }
@@ -1,64 +0,0 @@
1
- import type { AirtableBaseSchema } from '../types/metadata.js';
2
- export type AirtableSchemaSource = {
3
- baseId: string;
4
- accessToken?: string;
5
- apiKey?: string;
6
- } | {
7
- schemaPath: string;
8
- } | {
9
- schema: AirtableBaseSchema;
10
- };
11
- export type EnumMode = 'literal' | 'hybrid' | 'broad';
12
- export type CreateRequiredMode = 'allOptional' | 'primaryField';
13
- export type UnknownFieldBehavior = 'unknown' | 'error';
14
- export type SchemaMode = 'read' | 'write' | 'full';
15
- export interface GenerateAirtableTypesOptions {
16
- source: AirtableSchemaSource;
17
- output?: string;
18
- tableNameOrId?: string | string[];
19
- enumMode?: EnumMode;
20
- createRequiredMode?: CreateRequiredMode;
21
- unknownFieldBehavior?: UnknownFieldBehavior;
22
- includeTableIds?: boolean;
23
- includeFieldIds?: boolean;
24
- schemaMode?: SchemaMode;
25
- format?: boolean;
26
- }
27
- export interface AirtableCodegenResult {
28
- content: string;
29
- tablesGenerated: number;
30
- warnings: string[];
31
- }
32
- export interface GenerateAirtableTypesResult {
33
- outputPath: string;
34
- tablesGenerated: number;
35
- warnings: string[];
36
- }
37
- export interface NormalizedBase {
38
- tables: NormalizedTable[];
39
- }
40
- export interface NormalizedTable {
41
- id: string;
42
- name: string;
43
- tsName: string;
44
- tsConstKey: string;
45
- primaryFieldId: string;
46
- fields: NormalizedField[];
47
- }
48
- export interface NormalizedField {
49
- id: string;
50
- name: string;
51
- tsKey: string;
52
- tsConstKey: string;
53
- tsTypeName: string;
54
- airtableType: string;
55
- readonly: boolean;
56
- computed: boolean;
57
- readType: string;
58
- createType?: string;
59
- updateType?: string;
60
- enumTypeAlias?: string;
61
- linkedTableId?: string;
62
- warnings: string[];
63
- options?: Record<string, unknown>;
64
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,14 +0,0 @@
1
- export declare function compareByNameThenId(left: {
2
- name: string;
3
- id: string;
4
- }, right: {
5
- name: string;
6
- id: string;
7
- }): number;
8
- export declare function isRecord(value: unknown): value is Record<string, unknown>;
9
- export declare function getRecordValue<T = unknown>(value: unknown, key: string): T | undefined;
10
- export declare function getChoices(options: unknown): {
11
- name: string;
12
- }[] | undefined;
13
- export declare function stringifyUnion(values: string[]): string;
14
- export declare function ensureArray<T>(value: T | T[] | undefined): T[];
@@ -1,30 +0,0 @@
1
- export function compareByNameThenId(left, right) {
2
- return left.name.localeCompare(right.name) || left.id.localeCompare(right.id);
3
- }
4
- export function isRecord(value) {
5
- return typeof value === 'object' && value !== null;
6
- }
7
- export function getRecordValue(value, key) {
8
- if (!isRecord(value) || !(key in value))
9
- return undefined;
10
- return value[key];
11
- }
12
- export function getChoices(options) {
13
- const choices = getRecordValue(options, 'choices');
14
- if (!choices)
15
- return undefined;
16
- const validChoices = choices
17
- .filter(isRecord)
18
- .map((choice) => getRecordValue(choice, 'name'))
19
- .filter((choice) => Boolean(choice))
20
- .map((name) => ({ name }));
21
- return validChoices.length ? validChoices : undefined;
22
- }
23
- export function stringifyUnion(values) {
24
- return values.map((value) => JSON.stringify(value)).join(' | ');
25
- }
26
- export function ensureArray(value) {
27
- if (value === undefined)
28
- return [];
29
- return Array.isArray(value) ? value : [value];
30
- }
@@ -1 +0,0 @@
1
- export declare function writeAtomic(outputPath: string, content: string): Promise<string>;
@@ -1,11 +0,0 @@
1
- import { mkdir, rename, writeFile } from 'node:fs/promises';
2
- import { dirname, resolve } from 'node:path';
3
- export async function writeAtomic(outputPath, content) {
4
- const resolvedPath = resolve(outputPath);
5
- const parentDirectory = dirname(resolvedPath);
6
- const temporaryPath = `${resolvedPath}.tmp-${process.pid}-${Date.now()}`;
7
- await mkdir(parentDirectory, { recursive: true });
8
- await writeFile(temporaryPath, content, 'utf8');
9
- await rename(temporaryPath, resolvedPath);
10
- return resolvedPath;
11
- }
package/lib/index.d.ts DELETED
@@ -1,8 +0,0 @@
1
- export { getRecord, getRecordsPage, iterateRecordsPages, getRecords, createRecord, createRecords, updateRecord, replaceRecord, updateRecords, replaceRecords, updateRecordsUpsert, deleteRecord, deleteRecords, } from './records.js';
2
- export { listBases, createBase, getBaseSchema } from './bases.js';
3
- export { airtableRequestRaw, AirtableApiError } from './requests.js';
4
- export type { AirtableRetryOptions } from './requests.js';
5
- export type { AirtableRecord, GeneratedRecordFieldsCompatibleGetRecordOptions, GeneratedRecordFieldsCompatibleGetRecordsOptions, } from './types/records.js';
6
- export type { CreateBaseFieldConfig, CreateBaseRequestBody, CreateBaseResponse, CreateBaseTableConfig, ListBasesResponse, } from './types/bases.js';
7
- export type { AirtableBaseSchema, AirtableFieldSchema, AirtableTableSchema, AirtableViewSchema, } from './types/metadata.js';
8
- export type { AICell, Attachment, AttachmentWrite, Barcode, BarcodeCell, BarcodeWrite, ButtonCell, ButtonFieldValue, Collaborator, CollaboratorWrite, } from './types/fields.js';
package/lib/index.js DELETED
@@ -1,3 +0,0 @@
1
- export { getRecord, getRecordsPage, iterateRecordsPages, getRecords, createRecord, createRecords, updateRecord, replaceRecord, updateRecords, replaceRecords, updateRecordsUpsert, deleteRecord, deleteRecords, } from './records.js';
2
- export { listBases, createBase, getBaseSchema } from './bases.js';
3
- export { airtableRequestRaw, AirtableApiError } from './requests.js';