easy-airtable-api 0.0.1 → 0.0.3
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 +31 -1
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +5 -0
- package/lib/cjs/requests.d.ts +46 -0
- package/lib/cjs/requests.js +186 -0
- package/lib/cjs/types/bases.d.ts +63 -0
- package/lib/cjs/types/bases.js +2 -0
- package/lib/cjs/types/fields.d.ts +328 -0
- package/lib/cjs/types/fields.js +2 -0
- package/lib/cjs/types/records.d.ts +131 -0
- package/lib/cjs/types/records.js +2 -0
- package/lib/cjs/types/tables.d.ts +99 -0
- package/lib/cjs/types/tables.js +2 -0
- package/lib/cjs/utils.d.ts +4 -0
- package/lib/cjs/utils.js +32 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/requests.d.ts +46 -0
- package/lib/esm/requests.js +177 -0
- package/lib/esm/types/bases.d.ts +63 -0
- package/lib/esm/types/bases.js +1 -0
- package/lib/esm/types/fields.d.ts +328 -0
- package/lib/esm/types/fields.js +1 -0
- package/lib/esm/types/records.d.ts +131 -0
- package/lib/esm/types/records.js +1 -0
- package/lib/esm/types/tables.d.ts +99 -0
- package/lib/esm/types/tables.js +1 -0
- package/lib/esm/utils.d.ts +4 -0
- package/lib/esm/utils.js +26 -0
- package/package.json +46 -7
- package/{tsconfig.json → tsconfig.cjs.json} +8 -2
- package/tsconfig.esm.json +21 -0
- package/lib/main.d.ts +0 -0
- package/lib/main.js +0 -1
- package/src/main.ts +0 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
export type AirtableRecord<Fields = DefaultFieldsType> = {
|
|
2
|
+
id: string;
|
|
3
|
+
createdTime: string;
|
|
4
|
+
fields: Fields;
|
|
5
|
+
};
|
|
6
|
+
type DefaultFieldsType = {
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
};
|
|
9
|
+
type BaseId = string;
|
|
10
|
+
type TableIdOrName = string;
|
|
11
|
+
type Timezone = string;
|
|
12
|
+
type SortDirection = 'asc' | 'desc';
|
|
13
|
+
type CellFormat = 'json' | 'string';
|
|
14
|
+
type RecordMetadataOptions = 'commentCount';
|
|
15
|
+
interface SortObject {
|
|
16
|
+
field: string;
|
|
17
|
+
direction?: SortDirection;
|
|
18
|
+
}
|
|
19
|
+
export interface GetRecordsQueryParameters {
|
|
20
|
+
timeZone?: Timezone;
|
|
21
|
+
userLocale?: string;
|
|
22
|
+
pageSize?: number;
|
|
23
|
+
maxRecords?: number;
|
|
24
|
+
offset?: string;
|
|
25
|
+
view?: string;
|
|
26
|
+
sort?: SortObject[];
|
|
27
|
+
filterByFormula?: string;
|
|
28
|
+
cellFormat?: CellFormat;
|
|
29
|
+
fields?: string[];
|
|
30
|
+
returnFieldsByFieldId?: boolean;
|
|
31
|
+
recordMetadata?: RecordMetadataOptions[];
|
|
32
|
+
}
|
|
33
|
+
type RecordField = {
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
};
|
|
36
|
+
export interface AirtableBaseRecord {
|
|
37
|
+
id: string;
|
|
38
|
+
createdTime: string;
|
|
39
|
+
fields: RecordField;
|
|
40
|
+
}
|
|
41
|
+
export interface ListRecordsResponse {
|
|
42
|
+
offset?: string;
|
|
43
|
+
records: AirtableBaseRecord[];
|
|
44
|
+
}
|
|
45
|
+
export interface GetRecordQueryParameters {
|
|
46
|
+
cellFormat?: CellFormat;
|
|
47
|
+
returnFieldsByFieldId?: boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface GetRecordPathParameters {
|
|
50
|
+
baseId: BaseId;
|
|
51
|
+
tableIdOrName: TableIdOrName;
|
|
52
|
+
recordId: string;
|
|
53
|
+
}
|
|
54
|
+
export interface UpdatePathParameters {
|
|
55
|
+
baseId: BaseId;
|
|
56
|
+
tableIdOrName: TableIdOrName;
|
|
57
|
+
}
|
|
58
|
+
export interface PerformUpsert {
|
|
59
|
+
fieldsToMergeOn: string[];
|
|
60
|
+
}
|
|
61
|
+
export interface UpdateRecordsRequestBody {
|
|
62
|
+
performUpsert?: PerformUpsert;
|
|
63
|
+
returnFieldsByFieldId?: boolean;
|
|
64
|
+
typecast?: boolean;
|
|
65
|
+
records: AirtableBaseRecord[];
|
|
66
|
+
}
|
|
67
|
+
export interface UpsertResponse {
|
|
68
|
+
createdRecords: string[];
|
|
69
|
+
updatedRecords: string[];
|
|
70
|
+
records: AirtableBaseRecord[];
|
|
71
|
+
}
|
|
72
|
+
export interface UpdateRecordPathParameters {
|
|
73
|
+
baseId: BaseId;
|
|
74
|
+
tableIdOrName: TableIdOrName;
|
|
75
|
+
recordId: string;
|
|
76
|
+
}
|
|
77
|
+
export interface UpdateRecordRequestBody {
|
|
78
|
+
typecast?: boolean;
|
|
79
|
+
fields: RecordField;
|
|
80
|
+
}
|
|
81
|
+
export interface UpdateRecordResponse {
|
|
82
|
+
id: string;
|
|
83
|
+
createdTime: string;
|
|
84
|
+
fields: RecordField;
|
|
85
|
+
}
|
|
86
|
+
export interface CreateRecordsPathParameters {
|
|
87
|
+
baseId: BaseId;
|
|
88
|
+
tableIdOrName: TableIdOrName;
|
|
89
|
+
}
|
|
90
|
+
export interface CreateRecordsRequestBody {
|
|
91
|
+
fields?: RecordField;
|
|
92
|
+
records?: AirtableBaseRecord[];
|
|
93
|
+
returnFieldsByFieldId?: boolean;
|
|
94
|
+
typecast?: boolean;
|
|
95
|
+
}
|
|
96
|
+
export type UpdateRecordsRequestOptions = {
|
|
97
|
+
typecast?: boolean;
|
|
98
|
+
returnFieldsByFieldId?: boolean;
|
|
99
|
+
};
|
|
100
|
+
export interface CreateRecordResponse {
|
|
101
|
+
id: string;
|
|
102
|
+
createdTime: string;
|
|
103
|
+
fields: RecordField;
|
|
104
|
+
}
|
|
105
|
+
export interface CreateRecordsResponse {
|
|
106
|
+
records: CreateRecordResponse[];
|
|
107
|
+
}
|
|
108
|
+
export interface DeleteRecordsPathParameters {
|
|
109
|
+
baseId: BaseId;
|
|
110
|
+
tableIdOrName: TableIdOrName;
|
|
111
|
+
}
|
|
112
|
+
export interface DeleteRecordsQueryParameters {
|
|
113
|
+
records: string[];
|
|
114
|
+
}
|
|
115
|
+
export interface DeleteRecordResponse {
|
|
116
|
+
id: string;
|
|
117
|
+
deleted: true;
|
|
118
|
+
}
|
|
119
|
+
export interface DeleteRecordsResponse {
|
|
120
|
+
records: DeleteRecordResponse[];
|
|
121
|
+
}
|
|
122
|
+
export interface DeleteRecordPathParameters {
|
|
123
|
+
baseId: BaseId;
|
|
124
|
+
tableIdOrName: TableIdOrName;
|
|
125
|
+
recordId: string;
|
|
126
|
+
}
|
|
127
|
+
export interface SingleDeleteRecordResponse {
|
|
128
|
+
id: string;
|
|
129
|
+
deleted: true;
|
|
130
|
+
}
|
|
131
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export type UpserptRecords = {
|
|
2
|
+
records: {
|
|
3
|
+
id: string;
|
|
4
|
+
createdTime: string;
|
|
5
|
+
fields: unknown;
|
|
6
|
+
}[];
|
|
7
|
+
updatedRecords: string[];
|
|
8
|
+
createdRecords: string[];
|
|
9
|
+
};
|
|
10
|
+
export interface UpdateRecordsBody<Fields> {
|
|
11
|
+
typecast: boolean;
|
|
12
|
+
records: UpdateRecords<Fields>;
|
|
13
|
+
}
|
|
14
|
+
export interface UpdateRecordsBodyUpsert<Fields> extends UpdateRecordsBody<Fields> {
|
|
15
|
+
performUpsert: {
|
|
16
|
+
fieldsToMergeOn: FieldsToMergeOn;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export type BaseId = string;
|
|
20
|
+
export type TableId = string;
|
|
21
|
+
export type FieldsToMergeOn = string[];
|
|
22
|
+
export type ApiRequest = {
|
|
23
|
+
url: string;
|
|
24
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
25
|
+
apiKey: string;
|
|
26
|
+
body?: RequestBody;
|
|
27
|
+
};
|
|
28
|
+
export type RequestMethods = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
29
|
+
export interface RequestMethodProps {
|
|
30
|
+
url: string;
|
|
31
|
+
apiKey: string;
|
|
32
|
+
body?: RequestBody;
|
|
33
|
+
}
|
|
34
|
+
export type RequestBody = unknown;
|
|
35
|
+
export interface AirtableRecordRequest {
|
|
36
|
+
baseId: string;
|
|
37
|
+
tableId: string;
|
|
38
|
+
atId?: string;
|
|
39
|
+
body: RequestBody;
|
|
40
|
+
}
|
|
41
|
+
export interface UpdateAirtableRecordRequest {
|
|
42
|
+
atId?: string;
|
|
43
|
+
baseId: string;
|
|
44
|
+
tableId: string;
|
|
45
|
+
body?: RequestBody;
|
|
46
|
+
}
|
|
47
|
+
export interface RequestBodyWebsiteInfos {
|
|
48
|
+
fields: unknown;
|
|
49
|
+
}
|
|
50
|
+
export type UpdateRecord<Fields> = {
|
|
51
|
+
fields: Fields;
|
|
52
|
+
};
|
|
53
|
+
export type UpdateRecords<Fields> = UpdateRecord<Fields>[];
|
|
54
|
+
type FieldType = 'singleLineText' | 'checkbox';
|
|
55
|
+
type TableFieldOptions = {
|
|
56
|
+
color?: string;
|
|
57
|
+
icon?: string;
|
|
58
|
+
};
|
|
59
|
+
type FieldConfig = {
|
|
60
|
+
id?: string;
|
|
61
|
+
name: string;
|
|
62
|
+
description?: string;
|
|
63
|
+
type: FieldType;
|
|
64
|
+
options?: TableFieldOptions;
|
|
65
|
+
};
|
|
66
|
+
export type BaseTableDetails = {
|
|
67
|
+
name: string;
|
|
68
|
+
description?: string;
|
|
69
|
+
};
|
|
70
|
+
export type TableConfig = {
|
|
71
|
+
name: string;
|
|
72
|
+
description?: string;
|
|
73
|
+
fields: FieldConfig[];
|
|
74
|
+
};
|
|
75
|
+
type ViewType = 'grid' | 'form' | 'calendar' | 'gallery' | 'kanban' | 'timeline' | 'block';
|
|
76
|
+
export type View = {
|
|
77
|
+
id: string;
|
|
78
|
+
name: string;
|
|
79
|
+
type: ViewType;
|
|
80
|
+
visibleFieldIds?: string[];
|
|
81
|
+
};
|
|
82
|
+
export type TableModel = {
|
|
83
|
+
id: string;
|
|
84
|
+
primaryFieldId: string;
|
|
85
|
+
name: string;
|
|
86
|
+
description?: string;
|
|
87
|
+
fields: FieldConfig[];
|
|
88
|
+
views: View[];
|
|
89
|
+
};
|
|
90
|
+
export type UpdateTableRequestBody = {
|
|
91
|
+
name?: string;
|
|
92
|
+
description?: string;
|
|
93
|
+
};
|
|
94
|
+
export type CreateTableRequestBody = {
|
|
95
|
+
name: string;
|
|
96
|
+
description?: string;
|
|
97
|
+
fields: FieldConfig[];
|
|
98
|
+
};
|
|
99
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { UpdateRecords } from './types/fields';
|
|
2
|
+
export declare const getArrayInChunks: (records: UpdateRecords) => Promise<import("./types/fields").UpdateRecord[][]>;
|
|
3
|
+
export declare const getErrorMessage: (error: Error | unknown) => string;
|
|
4
|
+
export declare const delay: (ms?: number) => Promise<unknown>;
|
package/lib/cjs/utils.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.delay = exports.getErrorMessage = exports.getArrayInChunks = void 0;
|
|
13
|
+
const getArrayInChunks = (records) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const chunks = [];
|
|
15
|
+
const chunkSize = 10;
|
|
16
|
+
for (let i = 0; i < records.length; i += chunkSize) {
|
|
17
|
+
const chunk = records.slice(i, i + chunkSize);
|
|
18
|
+
chunks.push(chunk);
|
|
19
|
+
}
|
|
20
|
+
return chunks;
|
|
21
|
+
});
|
|
22
|
+
exports.getArrayInChunks = getArrayInChunks;
|
|
23
|
+
const getErrorMessage = (error) => {
|
|
24
|
+
if (error instanceof Error)
|
|
25
|
+
return error.message;
|
|
26
|
+
return String(error);
|
|
27
|
+
};
|
|
28
|
+
exports.getErrorMessage = getErrorMessage;
|
|
29
|
+
const delay = (ms = 0) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
return yield new Promise((resolve) => setTimeout(resolve, ms));
|
|
31
|
+
});
|
|
32
|
+
exports.delay = delay;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getRecord } from './requests';
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getRecord } from './requests';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { AirtableRecord, GetRecordsQueryParameters, UpdateRecordsRequestOptions } from './types/records';
|
|
2
|
+
export declare function getRecord<Fields>({ apiKey, baseId, tableId, recordId, }: {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
baseId: string;
|
|
5
|
+
tableId: string;
|
|
6
|
+
recordId: string;
|
|
7
|
+
}): Promise<AirtableRecord<Fields>>;
|
|
8
|
+
export declare function getRecords<Fields>({ apiKey, baseId, tableId, options, }: {
|
|
9
|
+
apiKey: string;
|
|
10
|
+
baseId: string;
|
|
11
|
+
tableId: string;
|
|
12
|
+
options: GetRecordsQueryParameters;
|
|
13
|
+
}): Promise<AirtableRecord<Fields>[]>;
|
|
14
|
+
export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, }: {
|
|
15
|
+
apiKey: string;
|
|
16
|
+
baseId: string;
|
|
17
|
+
tableId: string;
|
|
18
|
+
recordId: string;
|
|
19
|
+
fields: object;
|
|
20
|
+
}): Promise<AirtableRecord<Fields>>;
|
|
21
|
+
export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
22
|
+
apiKey: string;
|
|
23
|
+
baseId: string;
|
|
24
|
+
tableId: string;
|
|
25
|
+
records: {
|
|
26
|
+
id: string;
|
|
27
|
+
fields: object;
|
|
28
|
+
}[];
|
|
29
|
+
options?: UpdateRecordsRequestOptions;
|
|
30
|
+
}): Promise<AirtableRecord<Fields>[]>;
|
|
31
|
+
export declare function replaceRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, }: {
|
|
32
|
+
apiKey: string;
|
|
33
|
+
baseId: string;
|
|
34
|
+
tableId: string;
|
|
35
|
+
recordId: string;
|
|
36
|
+
fields: object;
|
|
37
|
+
}): Promise<AirtableRecord<Fields>>;
|
|
38
|
+
export declare function replaceMultipleRecords<Fields>({ apiKey, baseId, tableId, records, }: {
|
|
39
|
+
apiKey: string;
|
|
40
|
+
baseId: string;
|
|
41
|
+
tableId: string;
|
|
42
|
+
records: {
|
|
43
|
+
id: string;
|
|
44
|
+
fields: object;
|
|
45
|
+
}[];
|
|
46
|
+
}): Promise<AirtableRecord<Fields>[]>;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { request } from 'https';
|
|
11
|
+
import { URL } from 'url';
|
|
12
|
+
export function getRecord({ apiKey, baseId, tableId, recordId, }) {
|
|
13
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
+
return airtableRequest({
|
|
15
|
+
apiKey,
|
|
16
|
+
baseId,
|
|
17
|
+
tableId,
|
|
18
|
+
endpoint: `/${recordId}`,
|
|
19
|
+
method: 'GET',
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export function getRecords({ apiKey, baseId, tableId, options, }) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
return airtableRequest({
|
|
26
|
+
apiKey,
|
|
27
|
+
baseId,
|
|
28
|
+
tableId,
|
|
29
|
+
endpoint: '/listRecords',
|
|
30
|
+
method: 'POST',
|
|
31
|
+
body: options,
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
export function updateRecord({ apiKey, baseId, tableId, recordId, fields, }) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
return airtableRequest({
|
|
38
|
+
apiKey,
|
|
39
|
+
baseId,
|
|
40
|
+
tableId,
|
|
41
|
+
endpoint: `/${recordId}`,
|
|
42
|
+
method: 'PATCH',
|
|
43
|
+
body: { fields },
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export function updateRecords({ apiKey, baseId, tableId, records, options, }) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
50
|
+
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
51
|
+
}
|
|
52
|
+
return airtableRequest({
|
|
53
|
+
apiKey,
|
|
54
|
+
baseId,
|
|
55
|
+
tableId,
|
|
56
|
+
endpoint: '/',
|
|
57
|
+
method: 'PATCH',
|
|
58
|
+
body: Object.assign({ records }, options),
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export function replaceRecord({ apiKey, baseId, tableId, recordId, fields, }) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
return airtableRequest({
|
|
65
|
+
apiKey,
|
|
66
|
+
baseId,
|
|
67
|
+
tableId,
|
|
68
|
+
endpoint: `/${recordId}`,
|
|
69
|
+
method: 'PUT',
|
|
70
|
+
body: { fields },
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
export function replaceMultipleRecords({ apiKey, baseId, tableId, records, }) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
77
|
+
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to replace.');
|
|
78
|
+
}
|
|
79
|
+
return airtableRequest({
|
|
80
|
+
apiKey,
|
|
81
|
+
baseId,
|
|
82
|
+
tableId,
|
|
83
|
+
endpoint: '/',
|
|
84
|
+
method: 'PUT',
|
|
85
|
+
body: { records },
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function airtableRequest(request) {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
const { apiKey, baseId, tableId, endpoint, method, body, apiURL } = request;
|
|
92
|
+
const url = apiURL || 'https://api.airtable.com/v0';
|
|
93
|
+
if (!apiKey) {
|
|
94
|
+
throw new Error('API key is not set. Please provide a valid Airtable API key.');
|
|
95
|
+
}
|
|
96
|
+
if (!baseId) {
|
|
97
|
+
throw new Error('Base ID is not set. Please provide a valid Airtable base ID.');
|
|
98
|
+
}
|
|
99
|
+
if (!tableId) {
|
|
100
|
+
throw new Error('Table ID/Name is not set. Please provide a valid Airtable table ID or name.');
|
|
101
|
+
}
|
|
102
|
+
const response = yield apiRequest({
|
|
103
|
+
url: `${url}/${baseId}/${tableId}${endpoint}`,
|
|
104
|
+
apiKey,
|
|
105
|
+
method,
|
|
106
|
+
body,
|
|
107
|
+
});
|
|
108
|
+
validateResponse(response);
|
|
109
|
+
return response.data;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function validateResponse(response) {
|
|
113
|
+
const statusCode = response.statusCode;
|
|
114
|
+
console.log(response);
|
|
115
|
+
if (statusCode === 200)
|
|
116
|
+
return;
|
|
117
|
+
if (statusCode === 401)
|
|
118
|
+
throw new Error('Wrong API Key.');
|
|
119
|
+
else if (statusCode === 403)
|
|
120
|
+
throw new Error('NOT_AUTHORIZED');
|
|
121
|
+
else if (statusCode === 404)
|
|
122
|
+
throw new Error('NOT_FOUND');
|
|
123
|
+
else if (statusCode === 413)
|
|
124
|
+
throw new Error('Request body is too large');
|
|
125
|
+
else if (statusCode === 422)
|
|
126
|
+
throw new Error('Operation cannot be processed');
|
|
127
|
+
else if (statusCode === 429)
|
|
128
|
+
throw new Error('TOO_MANY_REQUESTS');
|
|
129
|
+
else if (statusCode === 500)
|
|
130
|
+
throw new Error('SERVER_ERROR');
|
|
131
|
+
else if (statusCode === 503)
|
|
132
|
+
throw new Error('SERVICE_UNAVAILABLE');
|
|
133
|
+
throw new Error('UNEXPECTED_ERROR');
|
|
134
|
+
}
|
|
135
|
+
function apiRequest({ url, method, apiKey, body, }) {
|
|
136
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
137
|
+
return new Promise((resolve, reject) => {
|
|
138
|
+
const parsedUrl = new URL(url);
|
|
139
|
+
const { hostname, pathname, search } = parsedUrl;
|
|
140
|
+
const options = {
|
|
141
|
+
method,
|
|
142
|
+
hostname,
|
|
143
|
+
path: `${pathname}${search}`,
|
|
144
|
+
timeout: 300000,
|
|
145
|
+
headers: {
|
|
146
|
+
'Content-Type': 'application/json',
|
|
147
|
+
Authorization: `Bearer ${apiKey}`,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
const req = request(options, (res) => {
|
|
151
|
+
let data = '';
|
|
152
|
+
res.on('data', (chunk) => {
|
|
153
|
+
data += chunk;
|
|
154
|
+
});
|
|
155
|
+
res.on('end', () => {
|
|
156
|
+
try {
|
|
157
|
+
resolve({
|
|
158
|
+
data: JSON.parse(data),
|
|
159
|
+
statusCode: res.statusCode,
|
|
160
|
+
statusMessage: res.statusMessage,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
reject(new Error(`Failed to parse response as JSON: ${data}`));
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
req.on('error', (error) => {
|
|
169
|
+
reject(new Error(`airtableApi: ${error.message} (fn_${apiRequest.name})`));
|
|
170
|
+
});
|
|
171
|
+
if (body) {
|
|
172
|
+
req.write(JSON.stringify(body));
|
|
173
|
+
}
|
|
174
|
+
req.end();
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
type BaseList = {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
permissionLevel: 'none' | 'read' | 'comment' | 'edit' | 'create';
|
|
5
|
+
};
|
|
6
|
+
export type ListBasesResponse = {
|
|
7
|
+
bases: BaseList[];
|
|
8
|
+
offset?: string;
|
|
9
|
+
};
|
|
10
|
+
type BasesFieldOptions = {
|
|
11
|
+
inverseLinkFieldId?: string;
|
|
12
|
+
isReversed?: boolean;
|
|
13
|
+
linkedTableId?: string;
|
|
14
|
+
prefersSingleRecordLink?: boolean;
|
|
15
|
+
};
|
|
16
|
+
type Field = {
|
|
17
|
+
description?: string;
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
type: string;
|
|
21
|
+
options?: BasesFieldOptions;
|
|
22
|
+
};
|
|
23
|
+
type TableView = {
|
|
24
|
+
id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
type: string;
|
|
27
|
+
};
|
|
28
|
+
type Table = {
|
|
29
|
+
description?: string;
|
|
30
|
+
fields: Field[];
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
primaryFieldId: string;
|
|
34
|
+
views: TableView[];
|
|
35
|
+
};
|
|
36
|
+
export type GetBaseSchemaResponse = {
|
|
37
|
+
tables: Table[];
|
|
38
|
+
};
|
|
39
|
+
type TableConfigFieldOptions = {
|
|
40
|
+
color?: string;
|
|
41
|
+
icon?: string;
|
|
42
|
+
};
|
|
43
|
+
type TableConfigField = {
|
|
44
|
+
description?: string;
|
|
45
|
+
name: string;
|
|
46
|
+
type: string;
|
|
47
|
+
options?: TableConfigFieldOptions;
|
|
48
|
+
};
|
|
49
|
+
type BasesTableConfig = {
|
|
50
|
+
description?: string;
|
|
51
|
+
fields: TableConfigField[];
|
|
52
|
+
name: string;
|
|
53
|
+
};
|
|
54
|
+
export type CreateBaseRequestBody = {
|
|
55
|
+
name: string;
|
|
56
|
+
tables: BasesTableConfig[];
|
|
57
|
+
workspaceId: string;
|
|
58
|
+
};
|
|
59
|
+
export type CreateBaseResponse = {
|
|
60
|
+
id: string;
|
|
61
|
+
tables: Table[];
|
|
62
|
+
};
|
|
63
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|