easy-airtable-api 0.0.5 → 0.0.7

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
@@ -16,6 +16,16 @@ npm install easy-airtable-api
16
16
 
17
17
  ## Changelog
18
18
 
19
+ ### 0.0.7
20
+
21
+ - Add all available updateRecords options.
22
+ - getRecords function gets all records automatically.
23
+
24
+ ### 0.0.6
25
+
26
+ - Add all available updateRecord options.
27
+ - Fix path to index.d.ts.
28
+
19
29
  ### 0.0.5
20
30
 
21
31
  - Drop CJS build and only support ESM for now.
@@ -39,4 +49,4 @@ npm install easy-airtable-api
39
49
 
40
50
  ### 0.0.1
41
51
 
42
- - Basic project setup - no real functionality.
52
+ - Basic project setup - no real functionality.
package/lib/requests.d.ts CHANGED
@@ -11,12 +11,17 @@ export declare function getRecords<Fields>({ apiKey, baseId, tableId, options, }
11
11
  tableId: string;
12
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 CHANGED
@@ -10,24 +10,50 @@ export async function getRecord({ apiKey, baseId, tableId, recordId, }) {
10
10
  });
11
11
  }
12
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;
13
+ validateGetRecordsOptions(options);
14
+ let records = [];
15
+ let currentOffset;
16
+ do {
17
+ const requestBody = currentOffset
18
+ ? { ...options, offset: currentOffset }
19
+ : options;
20
+ const response = await airtableRequest({
21
+ apiKey,
22
+ baseId,
23
+ tableId,
24
+ endpoint: '/listRecords',
25
+ method: 'POST',
26
+ body: requestBody,
27
+ });
28
+ records = records.concat(response.records);
29
+ currentOffset = response.offset;
30
+ } while (currentOffset);
31
+ return records;
32
+ }
33
+ function validateGetRecordsOptions(options) {
34
+ if (!options)
35
+ return;
36
+ if (options.cellFormat === 'string') {
37
+ if (!options.timeZone || !options.userLocale) {
38
+ throw new Error('The timeZone and userLocale parameters are required when using string as the cellFormat.');
39
+ }
40
+ }
41
+ if (!options.maxRecords) {
42
+ options.maxRecords = 100;
43
+ }
22
44
  }
23
- export async function updateRecord({ apiKey, baseId, tableId, recordId, fields, }) {
45
+ export async function updateRecord({ apiKey, baseId, tableId, recordId, fields, options, }) {
24
46
  return await airtableRequest({
25
47
  apiKey,
26
48
  baseId,
27
49
  tableId,
28
50
  endpoint: `/${recordId}`,
29
- method: 'PATCH',
30
- body: { fields },
51
+ method: options?.overwriteFieldsNotSpecified === true ? 'PUT' : 'PATCH',
52
+ body: {
53
+ fields,
54
+ typecast: options?.typecast === true ? true : false,
55
+ returnFieldsByFieldId: options?.returnFieldsByFieldId === true ? true : false,
56
+ },
31
57
  });
32
58
  }
33
59
  export async function updateRecords({ apiKey, baseId, tableId, records, options, }) {
@@ -92,21 +118,23 @@ function validateResponse(response) {
92
118
  if (statusCode === 200)
93
119
  return;
94
120
  if (statusCode === 401)
95
- throw new Error('Wrong API Key.');
121
+ throw new Error('Incorrect API Key.');
96
122
  else if (statusCode === 403)
97
123
  throw new Error('Not authorized.');
98
124
  else if (statusCode === 404)
99
- throw new Error('Table or Record not found.');
125
+ throw new Error('Table or record not found.');
100
126
  else if (statusCode === 413)
101
- throw new Error('Request body is too large');
102
- else if (statusCode === 422)
103
- throw new Error('Operation cannot be processed');
104
- else if (statusCode === 429)
105
- throw new Error('Too many request to Airtable server.');
127
+ throw new Error('Request body is too large.');
128
+ else if (statusCode === 422) {
129
+ throw new Error('Operation cannot be processed. Do the field names match?');
130
+ }
131
+ else if (statusCode === 429) {
132
+ throw new Error('Too many requests to the Airtable server.');
133
+ }
106
134
  else if (statusCode === 500)
107
- throw new Error('Airtable Server Error');
135
+ throw new Error('Airtable server error.');
108
136
  else if (statusCode === 503)
109
- throw new Error('Airtable Service Unabailanle');
137
+ throw new Error('Airtable service unavailable.');
110
138
  throw new Error('Unexpected error.');
111
139
  }
112
140
  async function apiRequest({ url, method, apiKey, body, }) {
@@ -8,7 +8,6 @@ type DefaultFieldsType = {
8
8
  };
9
9
  type BaseId = string;
10
10
  type TableIdOrName = string;
11
- type Timezone = string;
12
11
  type SortDirection = 'asc' | 'desc';
13
12
  type CellFormat = 'json' | 'string';
14
13
  type RecordMetadataOptions = 'commentCount';
@@ -17,8 +16,8 @@ interface SortObject {
17
16
  direction?: SortDirection;
18
17
  }
19
18
  export interface GetRecordsQueryParameters {
20
- timeZone?: Timezone;
21
- userLocale?: string;
19
+ timeZone?: Timezones;
20
+ userLocale?: UserLocales;
22
21
  pageSize?: number;
23
22
  maxRecords?: number;
24
23
  offset?: string;
@@ -128,4 +127,6 @@ export interface SingleDeleteRecordResponse {
128
127
  id: string;
129
128
  deleted: true;
130
129
  }
130
+ export type Timezones = 'Africa/Abidjan' | 'Africa/Accra' | 'Africa/Algiers' | 'Africa/Bissau' | 'Africa/Cairo' | 'Africa/Casablanca' | 'Africa/Ceuta' | 'Africa/El_Aaiun' | 'Africa/Johannesburg' | 'Africa/Khartoum' | 'Africa/Lagos' | 'Africa/Maputo' | 'Africa/Monrovia' | 'Africa/Nairobi' | 'Africa/Ndjamena' | 'Africa/Tripoli' | 'Africa/Tunis' | 'Africa/Windhoek' | 'America/Adak' | 'America/Anchorage' | 'America/Araguaina' | 'America/Argentina/Buenos_Aires' | 'America/Argentina/Catamarca' | 'America/Argentina/Cordoba' | 'America/Argentina/Jujuy' | 'America/Argentina/La_Rioja' | 'America/Argentina/Mendoza' | 'America/Argentina/Rio_Gallegos' | 'America/Argentina/Salta' | 'America/Argentina/San_Juan' | 'America/Argentina/San_Luis' | 'America/Argentina/Tucuman' | 'America/Argentina/Ushuaia' | 'America/Asuncion' | 'America/Atikokan' | 'America/Bahia' | 'America/Bahia_Banderas' | 'America/Barbados' | 'America/Belem' | 'America/Belize' | 'America/Blanc-Sablon' | 'America/Boa_Vista' | 'America/Bogota' | 'America/Boise' | 'America/Cambridge_Bay' | 'America/Campo_Grande' | 'America/Cancun' | 'America/Caracas' | 'America/Cayenne' | 'America/Chicago' | 'America/Chihuahua' | 'America/Costa_Rica' | 'America/Creston' | 'America/Cuiaba' | 'America/Curacao' | 'America/Danmarkshavn' | 'America/Dawson' | 'America/Dawson_Creek' | 'America/Denver' | 'America/Detroit' | 'America/Edmonton' | 'America/Eirunepe' | 'America/El_Salvador' | 'America/Fort_Nelson' | 'America/Fortaleza' | 'America/Glace_Bay' | 'America/Godthab' | 'America/Goose_Bay' | 'America/Grand_Turk' | 'America/Guatemala' | 'America/Guayaquil' | 'America/Guyana' | 'America/Halifax' | 'America/Havana' | 'America/Hermosillo' | 'America/Indiana/Indianapolis' | 'America/Indiana/Knox' | 'America/Indiana/Marengo' | 'America/Indiana/Petersburg' | 'America/Indiana/Tell_City' | 'America/Indiana/Vevay' | 'America/Indiana/Vincennes' | 'America/Indiana/Winamac' | 'America/Inuvik' | 'America/Iqaluit' | 'America/Jamaica' | 'America/Juneau' | 'America/Kentucky/Louisville' | 'America/Kentucky/Monticello' | 'America/La_Paz' | 'America/Lima' | 'America/Los_Angeles' | 'America/Maceio' | 'America/Managua' | 'America/Manaus' | 'America/Martinique' | 'America/Matamoros' | 'America/Mazatlan' | 'America/Menominee' | 'America/Merida' | 'America/Metlakatla' | 'America/Mexico_City' | 'America/Miquelon' | 'America/Moncton' | 'America/Monterrey' | 'America/Montevideo' | 'America/Nassau' | 'America/New_York' | 'America/Nipigon' | 'America/Nome' | 'America/Noronha' | 'America/North_Dakota/Beulah' | 'America/North_Dakota/Center' | 'America/North_Dakota/New_Salem' | 'America/Ojinaga' | 'America/Panama' | 'America/Pangnirtung' | 'America/Paramaribo' | 'America/Phoenix' | 'America/Port-au-Prince' | 'America/Port_of_Spain' | 'America/Porto_Velho' | 'America/Puerto_Rico' | 'America/Rainy_River' | 'America/Rankin_Inlet' | 'America/Recife' | 'America/Regina' | 'America/Resolute' | 'America/Rio_Branco' | 'America/Santarem' | 'America/Santiago' | 'America/Santo_Domingo' | 'America/Sao_Paulo' | 'America/Scoresbysund' | 'America/Sitka' | 'America/St_Johns' | 'America/Swift_Current' | 'America/Tegucigalpa' | 'America/Thule' | 'America/Thunder_Bay' | 'America/Tijuana' | 'America/Toronto' | 'America/Vancouver' | 'America/Whitehorse' | 'America/Winnipeg' | 'America/Yakutat' | 'America/Yellowknife' | 'Antarctica/Casey' | 'Antarctica/Davis' | 'Antarctica/DumontDUrville' | 'Antarctica/Macquarie' | 'Antarctica/Mawson' | 'Antarctica/Palmer' | 'Antarctica/Rothera' | 'Antarctica/Syowa' | 'Antarctica/Troll' | 'Antarctica/Vostok' | 'Asia/Almaty' | 'Asia/Amman' | 'Asia/Anadyr' | 'Asia/Aqtau' | 'Asia/Aqtobe' | 'Asia/Ashgabat' | 'Asia/Baghdad' | 'Asia/Baku' | 'Asia/Bangkok' | 'Asia/Barnaul' | 'Asia/Beirut' | 'Asia/Bishkek' | 'Asia/Brunei' | 'Asia/Chita' | 'Asia/Choibalsan' | 'Asia/Colombo' | 'Asia/Damascus' | 'Asia/Dhaka' | 'Asia/Dili' | 'Asia/Dubai' | 'Asia/Dushanbe' | 'Asia/Gaza' | 'Asia/Hebron' | 'Asia/Ho_Chi_Minh' | 'Asia/Hong_Kong' | 'Asia/Hovd' | 'Asia/Irkutsk' | 'Asia/Jakarta' | 'Asia/Jayapura' | 'Asia/Jerusalem' | 'Asia/Kabul' | 'Asia/Kamchatka' | 'Asia/Karachi' | 'Asia/Kathmandu' | 'Asia/Khandyga' | 'Asia/Kolkata' | 'Asia/Krasnoyarsk' | 'Asia/Kuala_Lumpur' | 'Asia/Kuching' | 'Asia/Macau' | 'Asia/Magadan' | 'Asia/Makassar' | 'Asia/Manila' | 'Asia/Nicosia' | 'Asia/Novokuznetsk' | 'Asia/Novosibirsk' | 'Asia/Omsk' | 'Asia/Oral' | 'Asia/Pontianak' | 'Asia/Pyongyang' | 'Asia/Qatar' | 'Asia/Qyzylorda' | 'Asia/Rangoon' | 'Asia/Riyadh' | 'Asia/Sakhalin' | 'Asia/Samarkand' | 'Asia/Seoul' | 'Asia/Shanghai' | 'Asia/Singapore' | 'Asia/Srednekolymsk' | 'Asia/Taipei' | 'Asia/Tashkent' | 'Asia/Tbilisi' | 'Asia/Tehran' | 'Asia/Thimphu' | 'Asia/Tokyo' | 'Asia/Tomsk' | 'Asia/Ulaanbaatar' | 'Asia/Urumqi' | 'Asia/Ust-Nera' | 'Asia/Vladivostok' | 'Asia/Yakutsk' | 'Asia/Yekaterinburg' | 'Asia/Yerevan' | 'Atlantic/Azores' | 'Atlantic/Bermuda' | 'Atlantic/Canary' | 'Atlantic/Cape_Verde' | 'Atlantic/Faroe' | 'Atlantic/Madeira' | 'Atlantic/Reykjavik' | 'Atlantic/South_Georgia' | 'Atlantic/Stanley' | 'Australia/Adelaide' | 'Australia/Brisbane' | 'Australia/Broken_Hill' | 'Australia/Currie' | 'Australia/Darwin' | 'Australia/Eucla' | 'Australia/Hobart' | 'Australia/Lindeman' | 'Australia/Lord_Howe' | 'Australia/Melbourne' | 'Australia/Perth' | 'Australia/Sydney' | 'GMT' | 'Europe/Amsterdam' | 'Europe/Andorra' | 'Europe/Astrakhan' | 'Europe/Athens' | 'Europe/Belgrade' | 'Europe/Berlin' | 'Europe/Brussels' | 'Europe/Bucharest' | 'Europe/Budapest' | 'Europe/Chisinau' | 'Europe/Copenhagen' | 'Europe/Dublin' | 'Europe/Gibraltar' | 'Europe/Helsinki' | 'Europe/Istanbul' | 'Europe/Kaliningrad' | 'Europe/Kiev' | 'Europe/Kirov' | 'Europe/Lisbon' | 'Europe/London' | 'Europe/Luxembourg' | 'Europe/Madrid' | 'Europe/Malta' | 'Europe/Minsk' | 'Europe/Monaco' | 'Europe/Moscow' | 'Europe/Oslo' | 'Europe/Paris' | 'Europe/Prague' | 'Europe/Riga' | 'Europe/Rome' | 'Europe/Samara' | 'Europe/Simferopol' | 'Europe/Sofia' | 'Europe/Stockholm' | 'Europe/Tallinn' | 'Europe/Tirane' | 'Europe/Ulyanovsk' | 'Europe/Uzhgorod' | 'Europe/Vienna' | 'Europe/Vilnius' | 'Europe/Volgograd' | 'Europe/Warsaw' | 'Europe/Zaporozhye' | 'Europe/Zurich' | 'Indian/Chagos' | 'Indian/Christmas' | 'Indian/Cocos' | 'Indian/Kerguelen' | 'Indian/Mahe' | 'Indian/Maldives' | 'Indian/Mauritius' | 'Indian/Reunion' | 'Pacific/Apia' | 'Pacific/Auckland' | 'Pacific/Bougainville' | 'Pacific/Chatham' | 'Pacific/Chuuk' | 'Pacific/Easter' | 'Pacific/Efate' | 'Pacific/Enderbury' | 'Pacific/Fakaofo' | 'Pacific/Fiji' | 'Pacific/Funafuti' | 'Pacific/Galapagos' | 'Pacific/Gambier' | 'Pacific/Guadalcanal' | 'Pacific/Guam' | 'Pacific/Honolulu' | 'Pacific/Kiritimati' | 'Pacific/Kosrae' | 'Pacific/Kwajalein' | 'Pacific/Majuro' | 'Pacific/Marquesas' | 'Pacific/Nauru' | 'Pacific/Niue' | 'Pacific/Norfolk' | 'Pacific/Noumea' | 'Pacific/Pago_Pago' | 'Pacific/Palau' | 'Pacific/Pitcairn' | 'Pacific/Pohnpei' | 'Pacific/Port_Moresby' | 'Pacific/Rarotonga' | 'Pacific/Tahiti' | 'Pacific/Tarawa' | 'Pacific/Tongatapu' | 'Pacific/Wake' | 'Pacific/Wallis';
131
+ type UserLocales = 'af' | 'ar-ma' | 'ar-sa' | 'ar-tn' | 'ar' | 'az' | 'be' | 'bg' | 'bn' | 'bo' | 'br' | 'bs' | 'ca' | 'cs' | 'cv' | 'cy' | 'da' | 'de-at' | 'de' | 'el' | 'en-au' | 'en-ca' | 'en-gb' | 'en-ie' | 'en-nz' | 'eo' | 'es' | 'et' | 'eu' | 'fa' | 'fi' | 'fo' | 'fr-ca' | 'fr-ch' | 'fr' | 'fy' | 'gl' | 'he' | 'hi' | 'hr' | 'hu' | 'hy-am' | 'id' | 'is' | 'it' | 'ja' | 'jv' | 'ka' | 'km' | 'ko' | 'lb' | 'lt' | 'lv' | 'me' | 'mk' | 'ml' | 'mr' | 'ms' | 'my' | 'nb' | 'ne' | 'nl' | 'nn' | 'pl' | 'pt-br' | 'pt' | 'ro' | 'ru' | 'si' | 'sk' | 'sl' | 'sq' | 'sr-cyrl' | 'sr' | 'sv' | 'ta' | 'th' | 'tl-ph' | 'tr' | 'tzl' | 'tzm-latn' | 'tzm' | 'uk' | 'uz' | 'vi' | 'zh-cn' | 'zh-tw';
131
132
  export {};
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "easy-airtable-api",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "description": "An easy way to connect to Airtable via their API.",
6
6
  "main": "lib/index.js",
7
- "types": "lib/types/index.d.ts",
7
+ "types": "lib/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
10
  "import": "./lib/index.js"
@@ -15,7 +15,8 @@
15
15
  "build": "rm -r ./lib && tsc",
16
16
  "publish": "npm publish",
17
17
  "lint": "eslint 'src/**/*.{ts,tsx}'",
18
- "test": "vitest"
18
+ "test": "vitest",
19
+ "testGetRecords": "vitest get-records.test.ts"
19
20
  },
20
21
  "keywords": [
21
22
  "airtable"