airtable-ts-codegen 2.1.1 → 2.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.
- package/README.md +12 -0
- package/dist/cli.js +5 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -2
- package/dist/jsTypeForAirtableType.d.ts +5 -1
- package/dist/jsTypeForAirtableType.js +4 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,6 +22,18 @@ AIRTABLE_API_KEY=pat1234.abcd AIRTABLE_BASE_ID=app1234 AIRTABLE_VIEW_IDS=viw1234
|
|
|
22
22
|
|
|
23
23
|
This will output a file `app1234.ts` that exports table definitions with only the fields visible in the specified views.
|
|
24
24
|
|
|
25
|
+
### Attachment type (unstable)
|
|
26
|
+
|
|
27
|
+
By default, `multipleAttachments` fields are typed as `string[]` (array of URLs). To get full attachment metadata (id, filename, size, type, etc.), set `UNSTABLE_AIRTABLE_ATTACHMENT_TYPE=Attachment`:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
AIRTABLE_API_KEY=pat1234.abcd AIRTABLE_BASE_ID=app1234 UNSTABLE_AIRTABLE_ATTACHMENT_TYPE=Attachment npx airtable-ts-codegen
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
This will generate `Attachment[]` instead of `string[]` for attachment fields, giving you access to the full attachment object with properties like `id`, `url`, `filename`, `size`, `type`, `width`, `height`, and `thumbnails`.
|
|
34
|
+
|
|
35
|
+
> **Note:** This option is unstable and may change or be removed in future versions.
|
|
36
|
+
|
|
25
37
|
<details>
|
|
26
38
|
<summary>Example generated file</summary>
|
|
27
39
|
|
package/dist/cli.js
CHANGED
|
@@ -13,7 +13,11 @@ 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
15
|
const viewIds = process.env.AIRTABLE_VIEW_IDS;
|
|
16
|
-
const
|
|
16
|
+
const unstableAttachmentTypeEnv = process.env.UNSTABLE_AIRTABLE_ATTACHMENT_TYPE;
|
|
17
|
+
const unstable_attachmentType = unstableAttachmentTypeEnv === 'Attachment' ? 'Attachment' : 'string';
|
|
18
|
+
const config = {
|
|
19
|
+
apiKey, baseId, ...(viewIds && { viewIds: viewIds.split(',') }), unstable_attachmentType,
|
|
20
|
+
};
|
|
17
21
|
const generateCode = async () => {
|
|
18
22
|
console.log(`Generating TypeScript definitions for base ${baseId}${viewIds ? ` with views ${viewIds}` : ''}...`);
|
|
19
23
|
return (0, _1.main)(config);
|
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,13 @@ export type Config = {
|
|
|
5
5
|
endpointUrl?: string;
|
|
6
6
|
requestTimeout?: number;
|
|
7
7
|
customHeaders?: Record<string, string | number | boolean>;
|
|
8
|
+
/**
|
|
9
|
+
* Type to use for multipleAttachments fields.
|
|
10
|
+
* - 'string': generates `string[]` (array of URLs only)
|
|
11
|
+
* - 'Attachment': generates `Attachment[]` (full metadata including id, filename, size, etc.)
|
|
12
|
+
* @default 'string'
|
|
13
|
+
* @unstable This option may change or be removed in future versions.
|
|
14
|
+
*/
|
|
15
|
+
unstable_attachmentType?: 'string' | 'Attachment';
|
|
8
16
|
};
|
|
9
17
|
export declare const main: (config: Config) => Promise<string>;
|
package/dist/index.js
CHANGED
|
@@ -11,10 +11,15 @@ const view_1 = require("./view");
|
|
|
11
11
|
const main = async (config) => {
|
|
12
12
|
const baseSchema = await (0, getBaseSchema_1.getBaseSchema)(config.baseId, config);
|
|
13
13
|
const filteredBaseSchema = await (0, view_1.filterBaseSchemaByView)(baseSchema, config);
|
|
14
|
+
// Determine if we need to import Attachment type
|
|
15
|
+
const useAttachmentType = config.unstable_attachmentType === 'Attachment';
|
|
16
|
+
const importTypes = useAttachmentType
|
|
17
|
+
? 'import type { Attachment, Item, Table } from \'airtable-ts\';'
|
|
18
|
+
: 'import type { Item, Table } from \'airtable-ts\';';
|
|
14
19
|
return [
|
|
15
20
|
'/* DO NOT EDIT: this file was automatically generated by airtable-ts-codegen */',
|
|
16
21
|
'/* eslint-disable */',
|
|
17
|
-
|
|
22
|
+
importTypes,
|
|
18
23
|
'',
|
|
19
24
|
filteredBaseSchema.map((tableSchema) => generateCode(config, tableSchema)).join('\n\n'),
|
|
20
25
|
].join('\n');
|
|
@@ -45,11 +50,12 @@ const generateCode = (config, tableSchema) => {
|
|
|
45
50
|
const itemNameRaw = (0, escapeIdentifier_1.escapeIdentifier)((0, recase_1.recase)(null, 'pascal', tableSchema.name));
|
|
46
51
|
const itemName = /.s$/.test(itemNameRaw) ? itemNameRaw.slice(0, itemNameRaw.length - 1) : itemNameRaw;
|
|
47
52
|
const tableName = (0, escapeIdentifier_1.escapeIdentifier)(`${(0, recase_1.recase)(null, 'camel', tableSchema.name)}Table`);
|
|
53
|
+
const jsTypeOptions = { attachmentType: config.unstable_attachmentType };
|
|
48
54
|
const fields = tableSchema.fields.map((f) => ({
|
|
49
55
|
...f,
|
|
50
56
|
originalName: f.name,
|
|
51
57
|
jsName: (0, escapeIdentifier_1.escapeIdentifier)((0, recase_1.recase)(null, 'camel', (0, escapeIdentifier_1.escapeIdentifier)(f.name))),
|
|
52
|
-
jsType: (0, jsTypeForAirtableType_1.jsTypeForAirtableType)(f),
|
|
58
|
+
jsType: (0, jsTypeForAirtableType_1.jsTypeForAirtableType)(f, jsTypeOptions),
|
|
53
59
|
}));
|
|
54
60
|
return `export interface ${itemName} extends Item {
|
|
55
61
|
id: string,${fields.map(generateInterfaceEntry).join('')}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { type FieldSchema } from './getBaseSchema';
|
|
2
|
+
export type JsTypeOptions = {
|
|
3
|
+
/** Type to use for multipleAttachments fields */
|
|
4
|
+
attachmentType?: 'string' | 'Attachment' | undefined;
|
|
5
|
+
};
|
|
2
6
|
/**
|
|
3
7
|
* Returns the corresponding Typescript type for the given Airtable field type.
|
|
4
8
|
*
|
|
5
9
|
* Unsupported fields return `null` and will be filtered out by the caller.
|
|
6
10
|
*/
|
|
7
|
-
export declare const jsTypeForAirtableType: (field: FieldSchema) => string | null;
|
|
11
|
+
export declare const jsTypeForAirtableType: (field: FieldSchema, options?: JsTypeOptions) => string | null;
|
|
@@ -6,7 +6,7 @@ exports.jsTypeForAirtableType = void 0;
|
|
|
6
6
|
*
|
|
7
7
|
* Unsupported fields return `null` and will be filtered out by the caller.
|
|
8
8
|
*/
|
|
9
|
-
const jsTypeForAirtableType = (field) => {
|
|
9
|
+
const jsTypeForAirtableType = (field, options = {}) => {
|
|
10
10
|
switch (field.type) {
|
|
11
11
|
case 'url':
|
|
12
12
|
case 'email':
|
|
@@ -25,6 +25,7 @@ const jsTypeForAirtableType = (field) => {
|
|
|
25
25
|
case 'createdBy':
|
|
26
26
|
return 'string';
|
|
27
27
|
case 'multipleAttachments':
|
|
28
|
+
return options.attachmentType === 'Attachment' ? 'Attachment[]' : 'string[]';
|
|
28
29
|
case 'multipleCollaborators':
|
|
29
30
|
case 'multipleRecordLinks':
|
|
30
31
|
case 'multipleSelects':
|
|
@@ -51,7 +52,7 @@ const jsTypeForAirtableType = (field) => {
|
|
|
51
52
|
&& 'result' in field.options
|
|
52
53
|
&& typeof field.options.result === 'object'
|
|
53
54
|
&& field.options.result !== null) {
|
|
54
|
-
const innerType = (0, exports.jsTypeForAirtableType)(field.options.result);
|
|
55
|
+
const innerType = (0, exports.jsTypeForAirtableType)(field.options.result, options);
|
|
55
56
|
if (innerType === null) {
|
|
56
57
|
return null;
|
|
57
58
|
}
|
|
@@ -72,7 +73,7 @@ const jsTypeForAirtableType = (field) => {
|
|
|
72
73
|
&& 'result' in field.options
|
|
73
74
|
&& typeof field.options.result === 'object'
|
|
74
75
|
&& field.options.result !== null) {
|
|
75
|
-
const innerType = (0, exports.jsTypeForAirtableType)(field.options.result);
|
|
76
|
+
const innerType = (0, exports.jsTypeForAirtableType)(field.options.result, options);
|
|
76
77
|
if (innerType === null) {
|
|
77
78
|
return null;
|
|
78
79
|
}
|