easy-airtable-api 0.0.7 → 0.0.8

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 CHANGED
@@ -1,52 +1,99 @@
1
1
  # Easy Airtable API
2
2
 
3
- This is meant as an easier/better-to-use alternative to the official Airtable node app.
3
+ This is intended as a more accessible and user-friendly alternative to the official Airtable Node.js library.
4
4
 
5
- My focus is on delivering a better experience using types and making it easier to get and modify records with some helper functions.
5
+ My focus has been on delivering an enhanced experience by utilizing types and simplifying the process of retrieving and modifying records through some helpful functions.
6
6
 
7
- Currently, this project is super early and still needs a lot of work. However, most methods have been implemented to at least get and update records.
8
-
9
- More to come soon.
7
+ Currently, this project is in its early stages and requires significant development. Nonetheless, most methods to retrieve and update records have already been implemented.
10
8
 
11
9
  ## Install
12
10
 
13
- ```
11
+ ```bash
14
12
  npm install easy-airtable-api
15
13
  ```
16
14
 
15
+ ```bash
16
+ bun add easy-airtable-api
17
+ ```
18
+
19
+ ## How to Use
20
+
21
+ ### Get a Single Record
22
+
23
+ ```ts
24
+ type Fields = {
25
+ Name?: string;
26
+ Notes?: string;
27
+ Status?: string;
28
+ };
29
+
30
+ const record = await getRecord<Fields>({
31
+ apiKey: 'apiKey',
32
+ baseId: 'baseId',
33
+ tableId: 'tableId',
34
+ recordId: 'recordId',
35
+ });
36
+ ```
37
+
38
+ ### Get Multiple Records
39
+
40
+ ```ts
41
+ type Fields = {
42
+ Name?: string;
43
+ Notes?: string;
44
+ Status?: string;
45
+ };
46
+
47
+ const records = await getRecords<Fields>({
48
+ apiKey: 'apiKey',
49
+ baseId: 'baseId',
50
+ tableId: 'tableId',
51
+ options: {
52
+ maxRecords: 500,
53
+ },
54
+ });
55
+ ```
56
+
17
57
  ## Changelog
18
58
 
59
+ ### 0.0.8
60
+
61
+ - Implemented the updateRecords method.
62
+ - Implemented the updateRecordsUpsert method.
63
+ - Added a delay when executing many requests in succession.
64
+ - Fixed the updateRecord and updateRecords methods to return correct field types.
65
+
19
66
  ### 0.0.7
20
67
 
21
- - Add all available updateRecords options.
22
- - getRecords function gets all records automatically.
68
+ - Added all available options for updateRecords.
69
+ - Modified the getRecords function to automatically retrieve all records.
23
70
 
24
71
  ### 0.0.6
25
72
 
26
- - Add all available updateRecord options.
27
- - Fix path to index.d.ts.
73
+ - Added all available options for updateRecord.
74
+ - Corrected the path to index.d.ts.
28
75
 
29
76
  ### 0.0.5
30
77
 
31
- - Drop CJS build and only support ESM for now.
78
+ - Discontinued CJS build, supporting only ESM for now.
32
79
 
33
80
  ### 0.0.4
34
81
 
35
- - Export all available methods.
36
- - Restucture lib folder to not have duplicate types.
82
+ - Exported all available methods.
83
+ - Restructured the lib folder to eliminate duplicate types.
37
84
 
38
85
  ### 0.0.3
39
86
 
40
- - ESM & CJS builds.
41
- - Server error handling.
42
- - Generic field types.
87
+ - Supported both ESM & CJS builds.
88
+ - Enhanced server error handling.
89
+ - Introduced generic field types.
43
90
 
44
91
  ### 0.0.2
45
92
 
46
- - Basic request functions to get or update records.
93
+ - Developed basic request functions to retrieve or update records.
47
94
  - Added type definitions for requests and fields.
48
- - Basic tests to check the core functionality of the library.
95
+ - Conducted basic tests to verify the core functionality of the library.
49
96
 
50
97
  ### 0.0.1
51
98
 
52
- - Basic project setup - no real functionality.
99
+ - Initial project setup with no real functionality implemented.
package/lib/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { getRecord, getRecords, updateRecord, updateRecords, replaceRecord, replaceRecords, } from './requests.js';
1
+ export { getRecord, getRecords, updateRecord, updateRecords, updateRecordsUpsert, } from './requests.js';
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- export { getRecord, getRecords, updateRecord, updateRecords, replaceRecord, replaceRecords, } from './requests.js';
1
+ export { getRecord, getRecords, updateRecord, updateRecords, updateRecordsUpsert, } from './requests.js';
package/lib/requests.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AirtableRecord, GetRecordsQueryParameters, UpdateRecordsRequestOptions } from './types/records';
1
+ import { AirtableRecord, GetRecordsQueryParameters } from './types/records';
2
2
  export declare function getRecord<Fields>({ apiKey, baseId, tableId, recordId, }: {
3
3
  apiKey: string;
4
4
  baseId: string;
@@ -16,7 +16,7 @@ export declare function updateRecord<Fields>({ apiKey, baseId, tableId, recordId
16
16
  baseId: string;
17
17
  tableId: string;
18
18
  recordId: string;
19
- fields: object;
19
+ fields: Fields;
20
20
  options?: {
21
21
  typecast?: boolean;
22
22
  returnFieldsByFieldId?: boolean;
@@ -29,23 +29,32 @@ export declare function updateRecords<Fields>({ apiKey, baseId, tableId, records
29
29
  tableId: string;
30
30
  records: {
31
31
  id: string;
32
- fields: object;
32
+ fields: Fields;
33
33
  }[];
34
- options?: UpdateRecordsRequestOptions;
35
- }): Promise<AirtableRecord<Fields>[]>;
36
- export declare function replaceRecord<Fields>({ apiKey, baseId, tableId, recordId, fields, }: {
37
- apiKey: string;
38
- baseId: string;
39
- tableId: string;
40
- recordId: string;
41
- fields: object;
42
- }): Promise<AirtableRecord<Fields>>;
43
- export declare function replaceRecords<Fields>({ apiKey, baseId, tableId, records, }: {
34
+ options?: {
35
+ typecast?: boolean;
36
+ returnFieldsByFieldId?: boolean;
37
+ overwriteFieldsNotSpecified?: boolean;
38
+ };
39
+ }): Promise<{
40
+ records: AirtableRecord<Fields>[];
41
+ }>;
42
+ export declare function updateRecordsUpsert<Fields>({ apiKey, baseId, tableId, records, options, }: {
44
43
  apiKey: string;
45
44
  baseId: string;
46
45
  tableId: string;
47
46
  records: {
48
- id: string;
49
- fields: object;
47
+ id?: string;
48
+ fields: Fields;
50
49
  }[];
51
- }): Promise<AirtableRecord<Fields>[]>;
50
+ options?: {
51
+ fieldsToMergeOn: string[];
52
+ typecast?: boolean;
53
+ returnFieldsByFieldId?: boolean;
54
+ overwriteFieldsNotSpecified?: boolean;
55
+ };
56
+ }): Promise<{
57
+ createdRecords: string[];
58
+ updatedRecords: string[];
59
+ records: AirtableRecord<Fields>[];
60
+ }>;
package/lib/requests.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { request } from 'https';
2
2
  import { URL } from 'url';
3
+ import { delay } from './utils';
3
4
  export async function getRecord({ apiKey, baseId, tableId, recordId, }) {
4
5
  return await airtableRequest({
5
6
  apiKey,
@@ -27,6 +28,8 @@ export async function getRecords({ apiKey, baseId, tableId, options, }) {
27
28
  });
28
29
  records = records.concat(response.records);
29
30
  currentOffset = response.offset;
31
+ if (currentOffset)
32
+ await delay(500);
30
33
  } while (currentOffset);
31
34
  return records;
32
35
  }
@@ -60,37 +63,70 @@ export async function updateRecords({ apiKey, baseId, tableId, records, options,
60
63
  if (!Array.isArray(records) || records.length === 0) {
61
64
  throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
62
65
  }
63
- return await airtableRequest({
64
- apiKey,
65
- baseId,
66
- tableId,
67
- endpoint: '/',
68
- method: 'PATCH',
69
- body: { records, ...options },
70
- });
71
- }
72
- export async function replaceRecord({ apiKey, baseId, tableId, recordId, fields, }) {
73
- return await airtableRequest({
74
- apiKey,
75
- baseId,
76
- tableId,
77
- endpoint: `/${recordId}`,
78
- method: 'PUT',
79
- body: { fields },
80
- });
66
+ const chunkSize = 10;
67
+ const chunks = [];
68
+ for (let i = 0; i < records.length; i += chunkSize) {
69
+ chunks.push(records.slice(i, i + chunkSize));
70
+ }
71
+ let combinedResults = [];
72
+ for (const chunk of chunks) {
73
+ const result = await airtableRequest({
74
+ apiKey,
75
+ baseId,
76
+ tableId,
77
+ endpoint: '/',
78
+ method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
79
+ body: {
80
+ records: chunk,
81
+ typecast: options?.typecast ?? false,
82
+ returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
83
+ },
84
+ });
85
+ combinedResults = combinedResults.concat(result.records);
86
+ await delay(500);
87
+ }
88
+ return { records: combinedResults };
81
89
  }
82
- export async function replaceRecords({ apiKey, baseId, tableId, records, }) {
90
+ export async function updateRecordsUpsert({ apiKey, baseId, tableId, records, options, }) {
83
91
  if (!Array.isArray(records) || records.length === 0) {
84
- throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to replace.');
92
+ throw new Error('The records array is empty or not provided. Please provide a non-empty array of records to update.');
85
93
  }
86
- return await airtableRequest({
87
- apiKey,
88
- baseId,
89
- tableId,
90
- endpoint: '/',
91
- method: 'PUT',
92
- body: { records },
93
- });
94
+ if (!Array.isArray(options?.fieldsToMergeOn) ||
95
+ options?.fieldsToMergeOn.length === 0) {
96
+ throw new Error('fieldsToMergeOn must be an array of strings.');
97
+ }
98
+ const chunkSize = 10;
99
+ const chunks = [];
100
+ for (let i = 0; i < records.length; i += chunkSize) {
101
+ chunks.push(records.slice(i, i + chunkSize));
102
+ }
103
+ let allCreatedRecords = [];
104
+ let allUpdatedRecords = [];
105
+ let allRecords = [];
106
+ for (const chunk of chunks) {
107
+ const result = await airtableRequest({
108
+ apiKey,
109
+ baseId,
110
+ tableId,
111
+ endpoint: '/',
112
+ method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
113
+ body: {
114
+ records: chunk,
115
+ typecast: options?.typecast ?? false,
116
+ returnFieldsByFieldId: options?.returnFieldsByFieldId ?? false,
117
+ performUpsert: { fieldsToMergeOn: options.fieldsToMergeOn },
118
+ },
119
+ });
120
+ allCreatedRecords = allCreatedRecords.concat(result.createdRecords);
121
+ allUpdatedRecords = allUpdatedRecords.concat(result.updatedRecords);
122
+ allRecords = allRecords.concat(result.records);
123
+ await delay(500);
124
+ }
125
+ return {
126
+ createdRecords: allCreatedRecords,
127
+ updatedRecords: allUpdatedRecords,
128
+ records: allRecords,
129
+ };
94
130
  }
95
131
  async function airtableRequest(request) {
96
132
  const { apiKey, baseId, tableId, endpoint, method, body, apiURL } = request;
@@ -4,7 +4,7 @@ export type AirtableRecord<Fields = DefaultFieldsType> = {
4
4
  fields: Fields;
5
5
  };
6
6
  type DefaultFieldsType = {
7
- [key: string]: unknown;
7
+ [key: string]: any;
8
8
  };
9
9
  type BaseId = string;
10
10
  type TableIdOrName = string;
@@ -92,10 +92,6 @@ export interface CreateRecordsRequestBody {
92
92
  returnFieldsByFieldId?: boolean;
93
93
  typecast?: boolean;
94
94
  }
95
- export type UpdateRecordsRequestOptions = {
96
- typecast?: boolean;
97
- returnFieldsByFieldId?: boolean;
98
- };
99
95
  export interface CreateRecordResponse {
100
96
  id: string;
101
97
  createdTime: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-airtable-api",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "description": "An easy way to connect to Airtable via their API.",
6
6
  "main": "lib/index.js",
@@ -16,7 +16,8 @@
16
16
  "publish": "npm publish",
17
17
  "lint": "eslint 'src/**/*.{ts,tsx}'",
18
18
  "test": "vitest",
19
- "testGetRecords": "vitest get-records.test.ts"
19
+ "testGetRecords": "vitest get-records.test.ts",
20
+ "testUpdateRecords": "vitest update-records.test.ts"
20
21
  },
21
22
  "keywords": [
22
23
  "airtable"