easy-airtable-api 0.0.4 → 0.0.6
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 +10 -1
- package/lib/{types/index.d.ts → index.d.ts} +1 -1
- package/lib/{esm/index.js → index.js} +1 -1
- package/lib/{types/requests.d.ts → requests.d.ts} +7 -2
- package/lib/requests.js +158 -0
- package/lib/utils.d.ts +4 -0
- package/lib/utils.js +17 -0
- package/package.json +6 -8
- package/lib/cjs/index.js +0 -10
- package/lib/cjs/requests.js +0 -186
- package/lib/cjs/types/bases.js +0 -2
- package/lib/cjs/types/fields.js +0 -2
- package/lib/cjs/types/records.js +0 -2
- package/lib/cjs/types/tables.js +0 -2
- package/lib/cjs/utils.js +0 -32
- package/lib/esm/requests.js +0 -177
- package/lib/esm/utils.js +0 -26
- package/lib/types/utils.d.ts +0 -4
- package/tsconfig.cjs.json +0 -23
- package/tsconfig.esm.json +0 -23
- /package/lib/types/{types/bases.d.ts → bases.d.ts} +0 -0
- /package/lib/{esm/types → types}/bases.js +0 -0
- /package/lib/types/{types/fields.d.ts → fields.d.ts} +0 -0
- /package/lib/{esm/types → types}/fields.js +0 -0
- /package/lib/types/{types/records.d.ts → records.d.ts} +0 -0
- /package/lib/{esm/types → types}/records.js +0 -0
- /package/lib/types/{types/tables.d.ts → tables.d.ts} +0 -0
- /package/lib/{esm/types → types}/tables.js +0 -0
package/README.md
CHANGED
|
@@ -16,9 +16,18 @@ npm install easy-airtable-api
|
|
|
16
16
|
|
|
17
17
|
## Changelog
|
|
18
18
|
|
|
19
|
+
### 0.0.6
|
|
20
|
+
|
|
21
|
+
- Add all available updateRecord options.
|
|
22
|
+
- Fix path to index.d.ts.
|
|
23
|
+
|
|
24
|
+
### 0.0.5
|
|
25
|
+
|
|
26
|
+
- Drop CJS build and only support ESM for now.
|
|
27
|
+
|
|
19
28
|
### 0.0.4
|
|
20
29
|
|
|
21
|
-
- Export all available methods
|
|
30
|
+
- Export all available methods.
|
|
22
31
|
- Restucture lib folder to not have duplicate types.
|
|
23
32
|
|
|
24
33
|
### 0.0.3
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { getRecord, getRecords, updateRecord, updateRecords, replaceRecord, replaceRecords, } from './requests';
|
|
1
|
+
export { getRecord, getRecords, updateRecord, updateRecords, replaceRecord, replaceRecords, } from './requests.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { getRecord, getRecords, updateRecord, updateRecords, replaceRecord, replaceRecords, } from './requests';
|
|
1
|
+
export { getRecord, getRecords, updateRecord, updateRecords, replaceRecord, replaceRecords, } from './requests.js';
|
|
@@ -9,14 +9,19 @@ export declare function getRecords<Fields>({ apiKey, baseId, tableId, options, }
|
|
|
9
9
|
apiKey: string;
|
|
10
10
|
baseId: string;
|
|
11
11
|
tableId: string;
|
|
12
|
-
options
|
|
12
|
+
options?: GetRecordsQueryParameters;
|
|
13
13
|
}): Promise<AirtableRecord<Fields>[]>;
|
|
14
|
-
export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, }: {
|
|
14
|
+
export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, options, }: {
|
|
15
15
|
apiKey: string;
|
|
16
16
|
baseId: string;
|
|
17
17
|
tableId: string;
|
|
18
18
|
recordId: string;
|
|
19
19
|
fields: object;
|
|
20
|
+
options?: {
|
|
21
|
+
typecast?: boolean;
|
|
22
|
+
returnFieldsByFieldId?: boolean;
|
|
23
|
+
overwriteFieldsNotSpecified?: boolean;
|
|
24
|
+
};
|
|
20
25
|
}): Promise<AirtableRecord<Fields>>;
|
|
21
26
|
export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records, options, }: {
|
|
22
27
|
apiKey: string;
|
package/lib/requests.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { request } from 'https';
|
|
2
|
+
import { URL } from 'url';
|
|
3
|
+
export async function getRecord({ apiKey, baseId, tableId, recordId, }) {
|
|
4
|
+
return await airtableRequest({
|
|
5
|
+
apiKey,
|
|
6
|
+
baseId,
|
|
7
|
+
tableId,
|
|
8
|
+
endpoint: `/${recordId}`,
|
|
9
|
+
method: 'GET',
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export async function getRecords({ apiKey, baseId, tableId, options, }) {
|
|
13
|
+
const response = await airtableRequest({
|
|
14
|
+
apiKey,
|
|
15
|
+
baseId,
|
|
16
|
+
tableId,
|
|
17
|
+
endpoint: '/listRecords',
|
|
18
|
+
method: 'POST',
|
|
19
|
+
body: options,
|
|
20
|
+
});
|
|
21
|
+
return response.records;
|
|
22
|
+
}
|
|
23
|
+
export async function updateRecord({ apiKey, baseId, tableId, recordId, fields, options, }) {
|
|
24
|
+
return await airtableRequest({
|
|
25
|
+
apiKey,
|
|
26
|
+
baseId,
|
|
27
|
+
tableId,
|
|
28
|
+
endpoint: `/${recordId}`,
|
|
29
|
+
method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
|
|
30
|
+
body: {
|
|
31
|
+
fields,
|
|
32
|
+
typecast: options?.typecast === true ? true : false,
|
|
33
|
+
returnFieldsByFieldId: options?.returnFieldsByFieldId === true ? true : false,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
export async function updateRecords({ apiKey, baseId, tableId, records, options, }) {
|
|
38
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
39
|
+
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
40
|
+
}
|
|
41
|
+
return await airtableRequest({
|
|
42
|
+
apiKey,
|
|
43
|
+
baseId,
|
|
44
|
+
tableId,
|
|
45
|
+
endpoint: '/',
|
|
46
|
+
method: 'PATCH',
|
|
47
|
+
body: { records, ...options },
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export async function replaceRecord({ apiKey, baseId, tableId, recordId, fields, }) {
|
|
51
|
+
return await airtableRequest({
|
|
52
|
+
apiKey,
|
|
53
|
+
baseId,
|
|
54
|
+
tableId,
|
|
55
|
+
endpoint: `/${recordId}`,
|
|
56
|
+
method: 'PUT',
|
|
57
|
+
body: { fields },
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
export async function replaceRecords({ apiKey, baseId, tableId, records, }) {
|
|
61
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
62
|
+
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to replace.');
|
|
63
|
+
}
|
|
64
|
+
return await airtableRequest({
|
|
65
|
+
apiKey,
|
|
66
|
+
baseId,
|
|
67
|
+
tableId,
|
|
68
|
+
endpoint: '/',
|
|
69
|
+
method: 'PUT',
|
|
70
|
+
body: { records },
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async function airtableRequest(request) {
|
|
74
|
+
const { apiKey, baseId, tableId, endpoint, method, body, apiURL } = request;
|
|
75
|
+
const url = apiURL || 'https://api.airtable.com/v0';
|
|
76
|
+
if (!apiKey) {
|
|
77
|
+
throw new Error('API key is not set. Please provide a valid Airtable API key.');
|
|
78
|
+
}
|
|
79
|
+
if (!baseId) {
|
|
80
|
+
throw new Error('Base ID is not set. Please provide a valid Airtable base ID.');
|
|
81
|
+
}
|
|
82
|
+
if (!tableId) {
|
|
83
|
+
throw new Error('Table ID/Name is not set. Please provide a valid Airtable table ID or name.');
|
|
84
|
+
}
|
|
85
|
+
const response = await apiRequest({
|
|
86
|
+
url: `${url}/${baseId}/${tableId}${endpoint}`,
|
|
87
|
+
apiKey,
|
|
88
|
+
method,
|
|
89
|
+
body,
|
|
90
|
+
});
|
|
91
|
+
validateResponse(response);
|
|
92
|
+
return response.data;
|
|
93
|
+
}
|
|
94
|
+
function validateResponse(response) {
|
|
95
|
+
const statusCode = response.statusCode;
|
|
96
|
+
if (statusCode === 200)
|
|
97
|
+
return;
|
|
98
|
+
if (statusCode === 401)
|
|
99
|
+
throw new Error('Wrong API Key.');
|
|
100
|
+
else if (statusCode === 403)
|
|
101
|
+
throw new Error('Not authorized.');
|
|
102
|
+
else if (statusCode === 404)
|
|
103
|
+
throw new Error('Table or Record not found.');
|
|
104
|
+
else if (statusCode === 413)
|
|
105
|
+
throw new Error('Request body is too large');
|
|
106
|
+
else if (statusCode === 422) {
|
|
107
|
+
throw new Error('Operation cannot be processed. Do the field names match?');
|
|
108
|
+
}
|
|
109
|
+
else if (statusCode === 429) {
|
|
110
|
+
throw new Error('Too many request to Airtable server.');
|
|
111
|
+
}
|
|
112
|
+
else if (statusCode === 500)
|
|
113
|
+
throw new Error('Airtable Server Error');
|
|
114
|
+
else if (statusCode === 503)
|
|
115
|
+
throw new Error('Airtable Service Unabailanle');
|
|
116
|
+
throw new Error('Unexpected error.');
|
|
117
|
+
}
|
|
118
|
+
async function apiRequest({ url, method, apiKey, body, }) {
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
const parsedUrl = new URL(url);
|
|
121
|
+
const { hostname, pathname, search } = parsedUrl;
|
|
122
|
+
const options = {
|
|
123
|
+
method,
|
|
124
|
+
hostname,
|
|
125
|
+
path: `${pathname}${search}`,
|
|
126
|
+
timeout: 300000,
|
|
127
|
+
headers: {
|
|
128
|
+
'Content-Type': 'application/json',
|
|
129
|
+
Authorization: `Bearer ${apiKey}`,
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
const req = request(options, (res) => {
|
|
133
|
+
let data = '';
|
|
134
|
+
res.on('data', (chunk) => {
|
|
135
|
+
data += chunk;
|
|
136
|
+
});
|
|
137
|
+
res.on('end', () => {
|
|
138
|
+
try {
|
|
139
|
+
resolve({
|
|
140
|
+
data: JSON.parse(data),
|
|
141
|
+
statusCode: res.statusCode,
|
|
142
|
+
statusMessage: res.statusMessage,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
reject(new Error(`Failed to parse response as JSON: ${data}`));
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
req.on('error', (error) => {
|
|
151
|
+
reject(new Error(`airtableApi: ${error.message} (fn_${apiRequest.name})`));
|
|
152
|
+
});
|
|
153
|
+
if (body) {
|
|
154
|
+
req.write(JSON.stringify(body));
|
|
155
|
+
}
|
|
156
|
+
req.end();
|
|
157
|
+
});
|
|
158
|
+
}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { UpdateRecords } from './types/fields';
|
|
2
|
+
export declare function getArrayInChunks(records: UpdateRecords): Promise<import("./types/fields").UpdateRecord[][]>;
|
|
3
|
+
export declare function getErrorMessage(error: Error | unknown): string;
|
|
4
|
+
export declare function delay(ms?: number): Promise<unknown>;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export async function getArrayInChunks(records) {
|
|
2
|
+
const chunks = [];
|
|
3
|
+
const chunkSize = 10;
|
|
4
|
+
for (let i = 0; i < records.length; i += chunkSize) {
|
|
5
|
+
const chunk = records.slice(i, i + chunkSize);
|
|
6
|
+
chunks.push(chunk);
|
|
7
|
+
}
|
|
8
|
+
return chunks;
|
|
9
|
+
}
|
|
10
|
+
export function getErrorMessage(error) {
|
|
11
|
+
if (error instanceof Error)
|
|
12
|
+
return error.message;
|
|
13
|
+
return String(error);
|
|
14
|
+
}
|
|
15
|
+
export async function delay(ms = 0) {
|
|
16
|
+
return await new Promise((resolve) => setTimeout(resolve, ms));
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-airtable-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "An easy way to connect to Airtable via their API.",
|
|
5
|
-
"main": "lib/
|
|
6
|
-
"
|
|
7
|
-
"types": "lib/types/index.d.ts",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"
|
|
11
|
-
"import": "./lib/esm/index.js",
|
|
12
|
-
"types": "./lib/types/index.d.ts"
|
|
10
|
+
"import": "./lib/index.js"
|
|
13
11
|
}
|
|
14
12
|
},
|
|
15
13
|
"scripts": {
|
|
16
14
|
"watch": "tsc --watch",
|
|
17
|
-
"build": "rm -r ./lib && tsc
|
|
15
|
+
"build": "rm -r ./lib && tsc",
|
|
18
16
|
"publish": "npm publish",
|
|
19
17
|
"lint": "eslint 'src/**/*.{ts,tsx}'",
|
|
20
18
|
"test": "vitest"
|
package/lib/cjs/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.replaceRecords = exports.replaceRecord = exports.updateRecords = exports.updateRecord = exports.getRecords = exports.getRecord = void 0;
|
|
4
|
-
var requests_1 = require("./requests");
|
|
5
|
-
Object.defineProperty(exports, "getRecord", { enumerable: true, get: function () { return requests_1.getRecord; } });
|
|
6
|
-
Object.defineProperty(exports, "getRecords", { enumerable: true, get: function () { return requests_1.getRecords; } });
|
|
7
|
-
Object.defineProperty(exports, "updateRecord", { enumerable: true, get: function () { return requests_1.updateRecord; } });
|
|
8
|
-
Object.defineProperty(exports, "updateRecords", { enumerable: true, get: function () { return requests_1.updateRecords; } });
|
|
9
|
-
Object.defineProperty(exports, "replaceRecord", { enumerable: true, get: function () { return requests_1.replaceRecord; } });
|
|
10
|
-
Object.defineProperty(exports, "replaceRecords", { enumerable: true, get: function () { return requests_1.replaceRecords; } });
|
package/lib/cjs/requests.js
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
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.replaceRecords = exports.replaceRecord = exports.updateRecords = exports.updateRecord = exports.getRecords = exports.getRecord = void 0;
|
|
13
|
-
const https_1 = require("https");
|
|
14
|
-
const url_1 = require("url");
|
|
15
|
-
function getRecord(_a) {
|
|
16
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, recordId, }) {
|
|
17
|
-
return airtableRequest({
|
|
18
|
-
apiKey,
|
|
19
|
-
baseId,
|
|
20
|
-
tableId,
|
|
21
|
-
endpoint: `/${recordId}`,
|
|
22
|
-
method: 'GET',
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
exports.getRecord = getRecord;
|
|
27
|
-
function getRecords(_a) {
|
|
28
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, options, }) {
|
|
29
|
-
return airtableRequest({
|
|
30
|
-
apiKey,
|
|
31
|
-
baseId,
|
|
32
|
-
tableId,
|
|
33
|
-
endpoint: '/listRecords',
|
|
34
|
-
method: 'POST',
|
|
35
|
-
body: options,
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
exports.getRecords = getRecords;
|
|
40
|
-
function updateRecord(_a) {
|
|
41
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, recordId, fields, }) {
|
|
42
|
-
return airtableRequest({
|
|
43
|
-
apiKey,
|
|
44
|
-
baseId,
|
|
45
|
-
tableId,
|
|
46
|
-
endpoint: `/${recordId}`,
|
|
47
|
-
method: 'PATCH',
|
|
48
|
-
body: { fields },
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
exports.updateRecord = updateRecord;
|
|
53
|
-
function updateRecords(_a) {
|
|
54
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, records, options, }) {
|
|
55
|
-
if (!Array.isArray(records) || records.length === 0) {
|
|
56
|
-
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
|
|
57
|
-
}
|
|
58
|
-
return airtableRequest({
|
|
59
|
-
apiKey,
|
|
60
|
-
baseId,
|
|
61
|
-
tableId,
|
|
62
|
-
endpoint: '/',
|
|
63
|
-
method: 'PATCH',
|
|
64
|
-
body: Object.assign({ records }, options),
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
exports.updateRecords = updateRecords;
|
|
69
|
-
function replaceRecord(_a) {
|
|
70
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, recordId, fields, }) {
|
|
71
|
-
return airtableRequest({
|
|
72
|
-
apiKey,
|
|
73
|
-
baseId,
|
|
74
|
-
tableId,
|
|
75
|
-
endpoint: `/${recordId}`,
|
|
76
|
-
method: 'PUT',
|
|
77
|
-
body: { fields },
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
exports.replaceRecord = replaceRecord;
|
|
82
|
-
function replaceRecords(_a) {
|
|
83
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, records, }) {
|
|
84
|
-
if (!Array.isArray(records) || records.length === 0) {
|
|
85
|
-
throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to replace.');
|
|
86
|
-
}
|
|
87
|
-
return airtableRequest({
|
|
88
|
-
apiKey,
|
|
89
|
-
baseId,
|
|
90
|
-
tableId,
|
|
91
|
-
endpoint: '/',
|
|
92
|
-
method: 'PUT',
|
|
93
|
-
body: { records },
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
exports.replaceRecords = replaceRecords;
|
|
98
|
-
function airtableRequest(request) {
|
|
99
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
-
const { apiKey, baseId, tableId, endpoint, method, body, apiURL } = request;
|
|
101
|
-
const url = apiURL || 'https://api.airtable.com/v0';
|
|
102
|
-
if (!apiKey) {
|
|
103
|
-
throw new Error('API key is not set. Please provide a valid Airtable API key.');
|
|
104
|
-
}
|
|
105
|
-
if (!baseId) {
|
|
106
|
-
throw new Error('Base ID is not set. Please provide a valid Airtable base ID.');
|
|
107
|
-
}
|
|
108
|
-
if (!tableId) {
|
|
109
|
-
throw new Error('Table ID/Name is not set. Please provide a valid Airtable table ID or name.');
|
|
110
|
-
}
|
|
111
|
-
const response = yield apiRequest({
|
|
112
|
-
url: `${url}/${baseId}/${tableId}${endpoint}`,
|
|
113
|
-
apiKey,
|
|
114
|
-
method,
|
|
115
|
-
body,
|
|
116
|
-
});
|
|
117
|
-
validateResponse(response);
|
|
118
|
-
return response.data;
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
function validateResponse(response) {
|
|
122
|
-
const statusCode = response.statusCode;
|
|
123
|
-
console.log(response);
|
|
124
|
-
if (statusCode === 200)
|
|
125
|
-
return;
|
|
126
|
-
if (statusCode === 401)
|
|
127
|
-
throw new Error('Wrong API Key.');
|
|
128
|
-
else if (statusCode === 403)
|
|
129
|
-
throw new Error('NOT_AUTHORIZED');
|
|
130
|
-
else if (statusCode === 404)
|
|
131
|
-
throw new Error('NOT_FOUND');
|
|
132
|
-
else if (statusCode === 413)
|
|
133
|
-
throw new Error('Request body is too large');
|
|
134
|
-
else if (statusCode === 422)
|
|
135
|
-
throw new Error('Operation cannot be processed');
|
|
136
|
-
else if (statusCode === 429)
|
|
137
|
-
throw new Error('TOO_MANY_REQUESTS');
|
|
138
|
-
else if (statusCode === 500)
|
|
139
|
-
throw new Error('SERVER_ERROR');
|
|
140
|
-
else if (statusCode === 503)
|
|
141
|
-
throw new Error('SERVICE_UNAVAILABLE');
|
|
142
|
-
throw new Error('UNEXPECTED_ERROR');
|
|
143
|
-
}
|
|
144
|
-
function apiRequest(_a) {
|
|
145
|
-
return __awaiter(this, arguments, void 0, function* ({ url, method, apiKey, body, }) {
|
|
146
|
-
return new Promise((resolve, reject) => {
|
|
147
|
-
const parsedUrl = new url_1.URL(url);
|
|
148
|
-
const { hostname, pathname, search } = parsedUrl;
|
|
149
|
-
const options = {
|
|
150
|
-
method,
|
|
151
|
-
hostname,
|
|
152
|
-
path: `${pathname}${search}`,
|
|
153
|
-
timeout: 300000,
|
|
154
|
-
headers: {
|
|
155
|
-
'Content-Type': 'application/json',
|
|
156
|
-
Authorization: `Bearer ${apiKey}`,
|
|
157
|
-
},
|
|
158
|
-
};
|
|
159
|
-
const req = (0, https_1.request)(options, (res) => {
|
|
160
|
-
let data = '';
|
|
161
|
-
res.on('data', (chunk) => {
|
|
162
|
-
data += chunk;
|
|
163
|
-
});
|
|
164
|
-
res.on('end', () => {
|
|
165
|
-
try {
|
|
166
|
-
resolve({
|
|
167
|
-
data: JSON.parse(data),
|
|
168
|
-
statusCode: res.statusCode,
|
|
169
|
-
statusMessage: res.statusMessage,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
catch (error) {
|
|
173
|
-
reject(new Error(`Failed to parse response as JSON: ${data}`));
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
req.on('error', (error) => {
|
|
178
|
-
reject(new Error(`airtableApi: ${error.message} (fn_${apiRequest.name})`));
|
|
179
|
-
});
|
|
180
|
-
if (body) {
|
|
181
|
-
req.write(JSON.stringify(body));
|
|
182
|
-
}
|
|
183
|
-
req.end();
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
}
|
package/lib/cjs/types/bases.js
DELETED
package/lib/cjs/types/fields.js
DELETED
package/lib/cjs/types/records.js
DELETED
package/lib/cjs/types/tables.js
DELETED
package/lib/cjs/utils.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
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 = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (ms = 0) {
|
|
30
|
-
return yield new Promise((resolve) => setTimeout(resolve, ms));
|
|
31
|
-
});
|
|
32
|
-
exports.delay = delay;
|
package/lib/esm/requests.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
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(_a) {
|
|
13
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, recordId, }) {
|
|
14
|
-
return airtableRequest({
|
|
15
|
-
apiKey,
|
|
16
|
-
baseId,
|
|
17
|
-
tableId,
|
|
18
|
-
endpoint: `/${recordId}`,
|
|
19
|
-
method: 'GET',
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
export function getRecords(_a) {
|
|
24
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, options, }) {
|
|
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(_a) {
|
|
36
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, recordId, fields, }) {
|
|
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(_a) {
|
|
48
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, records, options, }) {
|
|
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(_a) {
|
|
63
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, recordId, fields, }) {
|
|
64
|
-
return airtableRequest({
|
|
65
|
-
apiKey,
|
|
66
|
-
baseId,
|
|
67
|
-
tableId,
|
|
68
|
-
endpoint: `/${recordId}`,
|
|
69
|
-
method: 'PUT',
|
|
70
|
-
body: { fields },
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
export function replaceRecords(_a) {
|
|
75
|
-
return __awaiter(this, arguments, void 0, function* ({ apiKey, baseId, tableId, records, }) {
|
|
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(_a) {
|
|
136
|
-
return __awaiter(this, arguments, void 0, function* ({ url, method, apiKey, body, }) {
|
|
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
|
-
}
|
package/lib/esm/utils.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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
|
-
export const getArrayInChunks = (records) => __awaiter(void 0, void 0, void 0, function* () {
|
|
11
|
-
const chunks = [];
|
|
12
|
-
const chunkSize = 10;
|
|
13
|
-
for (let i = 0; i < records.length; i += chunkSize) {
|
|
14
|
-
const chunk = records.slice(i, i + chunkSize);
|
|
15
|
-
chunks.push(chunk);
|
|
16
|
-
}
|
|
17
|
-
return chunks;
|
|
18
|
-
});
|
|
19
|
-
export const getErrorMessage = (error) => {
|
|
20
|
-
if (error instanceof Error)
|
|
21
|
-
return error.message;
|
|
22
|
-
return String(error);
|
|
23
|
-
};
|
|
24
|
-
export const delay = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (ms = 0) {
|
|
25
|
-
return yield new Promise((resolve) => setTimeout(resolve, ms));
|
|
26
|
-
});
|
package/lib/types/utils.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
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/tsconfig.cjs.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es6",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "./lib/cjs",
|
|
8
|
-
"declarationDir": "./lib/types",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"skipLibCheck": false,
|
|
11
|
-
"esModuleInterop": true,
|
|
12
|
-
"types": [
|
|
13
|
-
"node"
|
|
14
|
-
]
|
|
15
|
-
},
|
|
16
|
-
"include": [
|
|
17
|
-
"src/**/*.ts"
|
|
18
|
-
],
|
|
19
|
-
"exclude": [
|
|
20
|
-
"lib",
|
|
21
|
-
"node_modules"
|
|
22
|
-
]
|
|
23
|
-
}
|
package/tsconfig.esm.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es6",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "./lib/esm",
|
|
8
|
-
"declarationDir": "./lib/types",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"skipLibCheck": false,
|
|
11
|
-
"esModuleInterop": true,
|
|
12
|
-
"types": [
|
|
13
|
-
"node"
|
|
14
|
-
],
|
|
15
|
-
},
|
|
16
|
-
"include": [
|
|
17
|
-
"src/**/*.ts"
|
|
18
|
-
],
|
|
19
|
-
"exclude": [
|
|
20
|
-
"lib",
|
|
21
|
-
"node_modules"
|
|
22
|
-
]
|
|
23
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|