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 +79 -0
- package/README.md +233 -142
- 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 +2 -1
- package/lib/index.js +0 -1
- package/lib/records.d.ts +1 -1
- package/lib/records.js +1 -1
- package/lib/requests.d.ts +2 -17
- package/lib/requests.js +1 -1
- 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 -1
- package/lib/utils.d.ts +2 -2
- package/package.json +14 -7
- package/lib/generate-types.d.ts +0 -5
- package/lib/generate-types.js +0 -78
|
@@ -0,0 +1,14 @@
|
|
|
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[];
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function writeAtomic(outputPath: string, content: string): Promise<string>;
|
|
@@ -0,0 +1,11 @@
|
|
|
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
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { getRecord, getRecords, createRecord, createRecords, updateRecord, updateRecords, updateRecordsUpsert, deleteRecords, } from './records.js';
|
|
2
|
-
export {
|
|
2
|
+
export type { AirtableRecord } from './types/records.js';
|
|
3
|
+
export type { Attachment, AttachmentWrite, Collaborator, } from './types/fields.js';
|
package/lib/index.js
CHANGED
package/lib/records.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AirtableRecord, DeleteRecordsResponse, GetRecordsQueryParameters } from './types/records';
|
|
1
|
+
import { AirtableRecord, DeleteRecordsResponse, GetRecordsQueryParameters } from './types/records.js';
|
|
2
2
|
export declare function getRecord<Fields>({ apiKey, baseId, tableId, recordId, }: {
|
|
3
3
|
apiKey: string;
|
|
4
4
|
baseId: string;
|
package/lib/records.js
CHANGED
package/lib/requests.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { RequestMethods } from './types/tables';
|
|
1
|
+
import { RequestMethods } from './types/tables.js';
|
|
2
|
+
import type { AirtableBaseSchema } from './types/metadata.js';
|
|
2
3
|
export declare function airtableRequest<T>(request: {
|
|
3
4
|
apiKey: string;
|
|
4
5
|
baseId: string;
|
|
@@ -8,23 +9,7 @@ export declare function airtableRequest<T>(request: {
|
|
|
8
9
|
body?: object;
|
|
9
10
|
apiURL?: string;
|
|
10
11
|
}): Promise<T>;
|
|
11
|
-
type AirtableField = {
|
|
12
|
-
id: string;
|
|
13
|
-
name: string;
|
|
14
|
-
type: string;
|
|
15
|
-
options?: object;
|
|
16
|
-
};
|
|
17
|
-
type AirtableTable = {
|
|
18
|
-
id: string;
|
|
19
|
-
name: string;
|
|
20
|
-
primaryFieldId: string;
|
|
21
|
-
fields: AirtableField[];
|
|
22
|
-
};
|
|
23
|
-
type AirtableBaseSchema = {
|
|
24
|
-
tables: AirtableTable[];
|
|
25
|
-
};
|
|
26
12
|
export declare function getBaseSchema({ apiKey, baseId, }: {
|
|
27
13
|
apiKey: string;
|
|
28
14
|
baseId: string;
|
|
29
15
|
}): Promise<AirtableBaseSchema>;
|
|
30
|
-
export {};
|
package/lib/requests.js
CHANGED
package/lib/types/fields.d.ts
CHANGED
|
@@ -62,7 +62,7 @@ export interface CollaboratorCell {
|
|
|
62
62
|
}
|
|
63
63
|
export interface SingleCollaboratorFieldTypeAndOptions {
|
|
64
64
|
type: 'singleCollaborator';
|
|
65
|
-
options:
|
|
65
|
+
options: Record<string, never>;
|
|
66
66
|
}
|
|
67
67
|
export type CountCell = number;
|
|
68
68
|
export interface CountFieldTypeAndOptions {
|
|
@@ -80,7 +80,7 @@ export type CreatedTimeCell = string;
|
|
|
80
80
|
export interface CreatedTimeFieldTypeAndOptions {
|
|
81
81
|
type: 'createdTime';
|
|
82
82
|
options: {
|
|
83
|
-
result?:
|
|
83
|
+
result?: unknown;
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
86
|
export type CurrencyCell = number;
|
|
@@ -133,7 +133,7 @@ export interface FormulaFieldTypeAndOptions {
|
|
|
133
133
|
formula: string;
|
|
134
134
|
isValid: boolean;
|
|
135
135
|
referencedFieldIds: string[] | null;
|
|
136
|
-
result:
|
|
136
|
+
result: unknown;
|
|
137
137
|
};
|
|
138
138
|
}
|
|
139
139
|
export type LastModifiedByCell = {
|
|
@@ -151,7 +151,7 @@ export interface LastModifiedTimeFieldTypeAndOptions {
|
|
|
151
151
|
options: {
|
|
152
152
|
isValid: boolean;
|
|
153
153
|
referencedFieldIds: string[] | null;
|
|
154
|
-
result:
|
|
154
|
+
result: unknown;
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
157
|
export type LinkToAnotherRecordCellV1 = string[];
|
|
@@ -173,10 +173,10 @@ export type LongTextCell = string;
|
|
|
173
173
|
export interface LongTextFieldTypeAndOptions {
|
|
174
174
|
type: 'multilineText';
|
|
175
175
|
}
|
|
176
|
-
export type LookupCellV1 = (number | string | boolean |
|
|
176
|
+
export type LookupCellV1 = (number | string | boolean | unknown)[];
|
|
177
177
|
export type LookupCellV2 = {
|
|
178
178
|
valuesByLinkedRecordId: {
|
|
179
|
-
[key: string]:
|
|
179
|
+
[key: string]: unknown[];
|
|
180
180
|
};
|
|
181
181
|
linkedRecordIds: string[];
|
|
182
182
|
};
|
|
@@ -186,7 +186,7 @@ export interface LookupFieldTypeAndOptions {
|
|
|
186
186
|
fieldIdInLinkedTable: string | null;
|
|
187
187
|
isValid: boolean;
|
|
188
188
|
recordLinkFieldId: string | null;
|
|
189
|
-
result:
|
|
189
|
+
result: unknown;
|
|
190
190
|
};
|
|
191
191
|
}
|
|
192
192
|
export type Collaborator = {
|
|
@@ -215,7 +215,7 @@ export type RatingOptions = {
|
|
|
215
215
|
export type RollupOptions = {
|
|
216
216
|
fieldIdInLinkedTable?: string;
|
|
217
217
|
recordLinkFieldId?: string;
|
|
218
|
-
result?:
|
|
218
|
+
result?: unknown;
|
|
219
219
|
isValid?: boolean;
|
|
220
220
|
referencedFieldIds?: string[];
|
|
221
221
|
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type AirtableFieldOptions = Record<string, unknown>;
|
|
2
|
+
export interface AirtableFieldSchema {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
type: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
options?: AirtableFieldOptions;
|
|
8
|
+
}
|
|
9
|
+
export interface AirtableViewSchema {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
type: string;
|
|
13
|
+
}
|
|
14
|
+
export interface AirtableTableSchema {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
primaryFieldId: string;
|
|
19
|
+
fields: AirtableFieldSchema[];
|
|
20
|
+
views?: AirtableViewSchema[];
|
|
21
|
+
}
|
|
22
|
+
export interface AirtableBaseSchema {
|
|
23
|
+
tables: AirtableTableSchema[];
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/types/records.d.ts
CHANGED
package/lib/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { UpdateRecords } from './types/fields';
|
|
2
|
-
export declare function getArrayInChunks(records: UpdateRecords): Promise<import("./types/fields").UpdateRecord[][]>;
|
|
1
|
+
import { UpdateRecords } from './types/fields.js';
|
|
2
|
+
export declare function getArrayInChunks(records: UpdateRecords): Promise<import("./types/fields.js").UpdateRecord[][]>;
|
|
3
3
|
export declare function getErrorMessage(error: Error | unknown): string;
|
|
4
4
|
export declare function delay(ms?: number): Promise<unknown>;
|
package/package.json
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-airtable-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "Lightweight TypeScript-first Airtable client with metadata-based type generation.",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
10
11
|
"import": "./lib/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./codegen": {
|
|
14
|
+
"types": "./lib/codegen/index.d.ts",
|
|
15
|
+
"import": "./lib/codegen/index.js"
|
|
11
16
|
}
|
|
12
17
|
},
|
|
18
|
+
"bin": {
|
|
19
|
+
"easy-airtable-api": "./lib/cli.js"
|
|
20
|
+
},
|
|
13
21
|
"scripts": {
|
|
14
22
|
"watch": "tsc --watch",
|
|
15
23
|
"build": "rm -r ./lib && tsc",
|
|
16
24
|
"publish": "npm publish",
|
|
17
25
|
"lint": "eslint 'src/**/*.{ts,tsx}'",
|
|
18
|
-
"test": "
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"testDeleteRecords": "vitest delete-records.test.ts"
|
|
26
|
+
"test": "npm run test:codegen",
|
|
27
|
+
"test:codegen": "vitest run tests/codegen.test.ts",
|
|
28
|
+
"test:integration": "RUN_AIRTABLE_INTEGRATION_TESTS=true vitest run tests/get-record.test.ts tests/get-records.test.ts tests/create-records.test.ts tests/update-records.test.ts tests/delete-records.test.ts",
|
|
29
|
+
"test:all": "vitest run"
|
|
23
30
|
},
|
|
24
31
|
"keywords": [
|
|
25
32
|
"airtable"
|
package/lib/generate-types.d.ts
DELETED
package/lib/generate-types.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { getBaseSchema } from './requests';
|
|
2
|
-
export async function generateTypeScriptDefinitions({ apiKey, baseId, tableNameOrId, }) {
|
|
3
|
-
const schema = await getBaseSchema({ apiKey, baseId });
|
|
4
|
-
const table = schema.tables.find((t) => t.id === tableNameOrId || t.name === tableNameOrId);
|
|
5
|
-
if (!table) {
|
|
6
|
-
throw new Error(`Table with name or ID "${tableNameOrId}" not found.`);
|
|
7
|
-
}
|
|
8
|
-
let typeDefinitions = `type ${table.name}Fields = {\n`;
|
|
9
|
-
table.fields.forEach((field) => {
|
|
10
|
-
let fieldName;
|
|
11
|
-
if (field.name.includes('"')) {
|
|
12
|
-
if (field.name.includes("'")) {
|
|
13
|
-
if (field.name.includes('`')) {
|
|
14
|
-
fieldName = field.name;
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
fieldName = `\`${field.name}\``;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
fieldName = `'${field.name}'`;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
fieldName = `"${field.name}"`;
|
|
26
|
-
}
|
|
27
|
-
const typeInfo = mapAirtableTypeToTypeScript(field.type);
|
|
28
|
-
const fieldType = typeInfo.readonly
|
|
29
|
-
? `readonly ${typeInfo.type}`
|
|
30
|
-
: typeInfo.type;
|
|
31
|
-
typeDefinitions += ` ${fieldName}?: ${fieldType};\n`;
|
|
32
|
-
});
|
|
33
|
-
typeDefinitions += '};\n';
|
|
34
|
-
return typeDefinitions;
|
|
35
|
-
}
|
|
36
|
-
function mapAirtableTypeToTypeScript(airtableType) {
|
|
37
|
-
switch (airtableType) {
|
|
38
|
-
case 'date':
|
|
39
|
-
case 'dateTime':
|
|
40
|
-
return { type: 'string', readonly: false };
|
|
41
|
-
case 'createdTime':
|
|
42
|
-
case 'lastModifiedTime':
|
|
43
|
-
return { type: 'string', readonly: true };
|
|
44
|
-
case 'number':
|
|
45
|
-
case 'currency':
|
|
46
|
-
case 'percent':
|
|
47
|
-
case 'rating':
|
|
48
|
-
case 'duration':
|
|
49
|
-
case 'autoNumber':
|
|
50
|
-
case 'count':
|
|
51
|
-
return { type: 'number', readonly: false };
|
|
52
|
-
case 'checkbox':
|
|
53
|
-
return { type: 'boolean', readonly: false };
|
|
54
|
-
case 'singleSelect':
|
|
55
|
-
case 'multipleSelects':
|
|
56
|
-
return { type: 'string[]', readonly: false };
|
|
57
|
-
case 'attachment':
|
|
58
|
-
return { type: 'any[]', readonly: false };
|
|
59
|
-
case 'collaborator':
|
|
60
|
-
case 'multipleCollaborators':
|
|
61
|
-
return {
|
|
62
|
-
type: '{ id: string; email?: string; name?: string; permissionLevel?: string; profilePicUrl?: string; }[]',
|
|
63
|
-
readonly: false,
|
|
64
|
-
};
|
|
65
|
-
case 'linkToAnotherRecord':
|
|
66
|
-
case 'multipleRecordLinks':
|
|
67
|
-
return { type: 'string[]', readonly: false };
|
|
68
|
-
case 'lookup':
|
|
69
|
-
case 'rollup':
|
|
70
|
-
return { type: 'any', readonly: false };
|
|
71
|
-
case 'formula':
|
|
72
|
-
return { type: 'string | number | boolean | any[]', readonly: true };
|
|
73
|
-
case 'syncSource':
|
|
74
|
-
return { type: 'string', readonly: false };
|
|
75
|
-
default:
|
|
76
|
-
return { type: 'any', readonly: false };
|
|
77
|
-
}
|
|
78
|
-
}
|