easy-airtable-api 0.0.2 → 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.
Files changed (40) hide show
  1. package/README.md +6 -0
  2. package/lib/cjs/index.d.ts +1 -0
  3. package/lib/cjs/index.js +5 -0
  4. package/lib/cjs/requests.d.ts +46 -0
  5. package/lib/cjs/requests.js +186 -0
  6. package/lib/{types → cjs/types}/bases.d.ts +5 -4
  7. package/lib/{types → cjs/types}/records.d.ts +8 -0
  8. package/lib/esm/index.d.ts +1 -0
  9. package/lib/esm/index.js +1 -0
  10. package/lib/esm/requests.d.ts +46 -0
  11. package/lib/esm/requests.js +177 -0
  12. package/lib/esm/types/bases.d.ts +63 -0
  13. package/lib/esm/types/bases.js +1 -0
  14. package/lib/esm/types/fields.d.ts +328 -0
  15. package/lib/esm/types/fields.js +1 -0
  16. package/lib/esm/types/records.d.ts +131 -0
  17. package/lib/esm/types/records.js +1 -0
  18. package/lib/esm/types/tables.d.ts +99 -0
  19. package/lib/esm/types/tables.js +1 -0
  20. package/lib/esm/utils.d.ts +4 -0
  21. package/lib/esm/utils.js +26 -0
  22. package/package.json +15 -7
  23. package/tsconfig.cjs.json +21 -0
  24. package/tsconfig.esm.json +21 -0
  25. package/lib/main.d.ts +0 -31
  26. package/lib/main.js +0 -131
  27. package/lib/records.d.ts +0 -11
  28. package/lib/records.js +0 -46
  29. package/lib/requests.d.ts +0 -2
  30. package/lib/requests.js +0 -52
  31. package/lib/types/bases.js +0 -1
  32. package/lib/types.d.ts +0 -98
  33. /package/lib/{types.js → cjs/types/bases.js} +0 -0
  34. /package/lib/{types → cjs/types}/fields.d.ts +0 -0
  35. /package/lib/{types → cjs/types}/fields.js +0 -0
  36. /package/lib/{types → cjs/types}/records.js +0 -0
  37. /package/lib/{types → cjs/types}/tables.d.ts +0 -0
  38. /package/lib/{types → cjs/types}/tables.js +0 -0
  39. /package/lib/{utils.d.ts → cjs/utils.d.ts} +0 -0
  40. /package/lib/{utils.js → cjs/utils.js} +0 -0
package/lib/main.d.ts DELETED
@@ -1,31 +0,0 @@
1
- import { GetRecordsQueryParameters, UpdateRecordsRequestOptions } from './types/records';
2
- type AirtableRecord = {
3
- id: string;
4
- fields: {
5
- [key: string]: unknown;
6
- };
7
- };
8
- export default class Airtable {
9
- private apiKey;
10
- private baseId;
11
- private tableId;
12
- private apiURL;
13
- url(url: string): this;
14
- auth(key: string): this;
15
- base(baseId: string): this;
16
- table(tableId: string): this;
17
- private request;
18
- getRecord(recordId: string): Promise<AirtableRecord>;
19
- getRecords(options: GetRecordsQueryParameters): Promise<AirtableRecord[]>;
20
- updateRecord(recordId: string, fields: object): Promise<AirtableRecord>;
21
- updateRecords(records: {
22
- id: string;
23
- fields: object;
24
- }[], options?: UpdateRecordsRequestOptions): Promise<AirtableRecord[]>;
25
- replaceRecord(recordId: string, fields: object): Promise<AirtableRecord>;
26
- replaceMultipleRecords(records: {
27
- id: string;
28
- fields: object;
29
- }[]): Promise<AirtableRecord[]>;
30
- }
31
- export {};
package/lib/main.js DELETED
@@ -1,131 +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
- const requests_1 = require("./requests");
13
- class Airtable {
14
- constructor() {
15
- this.apiKey = undefined;
16
- this.baseId = undefined;
17
- this.tableId = undefined;
18
- this.apiURL = 'https://api.airtable.com/v0';
19
- }
20
- url(url) {
21
- if (typeof url === 'string') {
22
- this.apiURL = url;
23
- }
24
- else {
25
- throw new Error('API URL must be a string');
26
- }
27
- return this;
28
- }
29
- auth(key) {
30
- if (typeof key === 'string') {
31
- this.apiKey = key;
32
- }
33
- else {
34
- throw new Error('API Key must be a string');
35
- }
36
- return this;
37
- }
38
- base(baseId) {
39
- if (typeof baseId === 'string') {
40
- this.baseId = baseId;
41
- }
42
- else {
43
- throw new Error('baseId must be a string');
44
- }
45
- return this;
46
- }
47
- table(tableId) {
48
- if (typeof tableId === 'string') {
49
- this.tableId = tableId;
50
- }
51
- else {
52
- throw new Error('tableId/tableName must be a string');
53
- }
54
- return this;
55
- }
56
- request({ endpoint, method, body, }) {
57
- return __awaiter(this, void 0, void 0, function* () {
58
- if (!this.apiKey) {
59
- throw new Error('API Key must be set before making requests.');
60
- }
61
- if (!this.baseId) {
62
- throw new Error('Base ID must be set before making requests.');
63
- }
64
- if (!this.tableId) {
65
- throw new Error('Table ID/Name must be set before making requests.');
66
- }
67
- return yield (0, requests_1.apiRequest)({
68
- url: `${this.apiURL}/${this.baseId}/${this.tableId}${endpoint}`,
69
- apiKey: this.apiKey,
70
- method: method,
71
- body,
72
- });
73
- });
74
- }
75
- getRecord(recordId) {
76
- return __awaiter(this, void 0, void 0, function* () {
77
- return this.request({
78
- endpoint: `/${recordId}`,
79
- method: 'GET',
80
- });
81
- });
82
- }
83
- getRecords(options) {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- return this.request({
86
- endpoint: '/listRecords',
87
- method: 'POST',
88
- body: options,
89
- });
90
- });
91
- }
92
- updateRecord(recordId, fields) {
93
- return __awaiter(this, void 0, void 0, function* () {
94
- return this.request({
95
- endpoint: `/${recordId}`,
96
- method: 'PATCH',
97
- body: { fields },
98
- });
99
- });
100
- }
101
- updateRecords(records, options) {
102
- return __awaiter(this, void 0, void 0, function* () {
103
- if (!Array.isArray(records)) {
104
- throw new Error('records to update must be an array.');
105
- }
106
- if (records.length === 0) {
107
- throw new Error('records to update array must have at least one item.');
108
- }
109
- return this.request({
110
- endpoint: '/',
111
- method: 'PATCH',
112
- body: Object.assign({ records }, options),
113
- });
114
- });
115
- }
116
- replaceRecord(recordId, fields) {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- return this.request({
119
- endpoint: `/${recordId}`,
120
- method: 'PUT',
121
- body: { fields },
122
- });
123
- });
124
- }
125
- replaceMultipleRecords(records) {
126
- return __awaiter(this, void 0, void 0, function* () {
127
- return this.request({ endpoint: '/', method: 'PUT', body: { records } });
128
- });
129
- }
130
- }
131
- exports.default = Airtable;
package/lib/records.d.ts DELETED
@@ -1,11 +0,0 @@
1
- export declare const getAirtableRecords: ({ recordId, baseId, tableName, options, }: {
2
- recordId?: string | string[] | undefined;
3
- baseId: string;
4
- tableName: string;
5
- options?: string | undefined;
6
- }) => Promise<any>;
7
- export declare const getAllAirtableRecords: ({ baseId, tableName, options, }: {
8
- baseId: string;
9
- tableName: string;
10
- options: string;
11
- }) => Promise<unknown[]>;
package/lib/records.js DELETED
@@ -1,46 +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.getAllAirtableRecords = exports.getAirtableRecords = void 0;
13
- const getAirtableRecords = ({ recordId, baseId, tableName, options, }) => __awaiter(void 0, void 0, void 0, function* () {
14
- const url = `https://api.airtable.com/v0/${baseId}/${tableName}/${recordId ? recordId : ''}?cellFormat=json${options ? options : ''}`;
15
- try {
16
- return yield getRequest({ url });
17
- }
18
- catch (error) {
19
- console.log(error);
20
- }
21
- });
22
- exports.getAirtableRecords = getAirtableRecords;
23
- const getAllAirtableRecords = ({ baseId, tableName, options, }) => __awaiter(void 0, void 0, void 0, function* () {
24
- var _a, _b;
25
- let records = [];
26
- const request = yield (0, exports.getAirtableRecords)({ baseId, tableName, options });
27
- if ((_a = request === null || request === void 0 ? void 0 : request.error) === null || _a === void 0 ? void 0 : _a.message)
28
- throw new Error((_b = request === null || request === void 0 ? void 0 : request.error) === null || _b === void 0 ? void 0 : _b.message);
29
- const requestRecords = request.records;
30
- records = [...records, ...requestRecords];
31
- let paginationToken = request === null || request === void 0 ? void 0 : request.offset;
32
- if (paginationToken) {
33
- do {
34
- const request = yield (0, exports.getAirtableRecords)({
35
- baseId,
36
- tableName,
37
- options: `${options}&offset=${paginationToken}`,
38
- });
39
- const requestRecords = request === null || request === void 0 ? void 0 : request.records;
40
- records = [...records, ...requestRecords];
41
- paginationToken = request === null || request === void 0 ? void 0 : request.offset;
42
- } while (paginationToken);
43
- }
44
- return records;
45
- });
46
- exports.getAllAirtableRecords = getAllAirtableRecords;
package/lib/requests.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { ApiRequest } from './types/tables';
2
- export declare const apiRequest: <T>({ url, method, apiKey, body, }: ApiRequest) => Promise<T>;
package/lib/requests.js DELETED
@@ -1,52 +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.apiRequest = void 0;
13
- const https_1 = require("https");
14
- const url_1 = require("url");
15
- const apiRequest = ({ url, method, apiKey, body, }) => __awaiter(void 0, void 0, void 0, function* () {
16
- return new Promise((resolve, reject) => {
17
- const parsedUrl = new url_1.URL(url);
18
- const { hostname, pathname, search } = parsedUrl;
19
- const options = {
20
- method,
21
- hostname,
22
- path: `${pathname}${search}`,
23
- timeout: 300000,
24
- headers: {
25
- 'Content-Type': 'application/json',
26
- Authorization: `Bearer ${apiKey}`,
27
- },
28
- };
29
- const req = (0, https_1.request)(options, (res) => {
30
- let data = '';
31
- res.on('data', (chunk) => {
32
- data += chunk;
33
- });
34
- res.on('end', () => {
35
- try {
36
- resolve(JSON.parse(data));
37
- }
38
- catch (error) {
39
- reject(new Error(`Failed to parse response as JSON: ${data}`));
40
- }
41
- });
42
- });
43
- req.on('error', (error) => {
44
- reject(new Error(`airtableApi: ${error.message} (fn_${exports.apiRequest.name})`));
45
- });
46
- if (body) {
47
- req.write(JSON.stringify(body));
48
- }
49
- req.end();
50
- });
51
- });
52
- exports.apiRequest = apiRequest;
@@ -1 +0,0 @@
1
- "use strict";
package/lib/types.d.ts DELETED
@@ -1,98 +0,0 @@
1
- export type SingleLineText = string | null;
2
- export type MultilineText = string | null;
3
- export type RichText = string;
4
- export type SingleSelect = string;
5
- export type MultipleSelects = string[];
6
- export type MultipleLookupValues = string[];
7
- export type Checkbox = boolean;
8
- export type AirtableUrl = string | null;
9
- export type AirtableEmail = string;
10
- export type AirtableInteger = number | null;
11
- export type Count = number;
12
- export type Duration = number;
13
- export type Formula = string | string[] | number;
14
- export type MultipleRecordLinks = string[];
15
- export type MultipleAttachments = Attachment[];
16
- export type FormulaSingleReturn = string | number;
17
- export type Rollup = string | number;
18
- export type AutoNumber = number;
19
- export type AirtableButton = {
20
- label: string;
21
- url: string;
22
- };
23
- export type LastModifiedTime = string;
24
- export type CreatedTime = string;
25
- export type DateTime = string;
26
- export type PhoneNumber = string;
27
- export type Currency = number;
28
- export type Percent = number;
29
- export interface Attachment {
30
- id: string;
31
- url: string;
32
- filename: string;
33
- size: number;
34
- type: string;
35
- thumbnails?: {
36
- small: Thumbnail;
37
- large: Thumbnail;
38
- full: Thumbnail;
39
- };
40
- }
41
- export interface Thumbnail {
42
- url: string;
43
- width: number;
44
- height: number;
45
- }
46
- export type UpserptRecords = {
47
- records: {
48
- id: string;
49
- createdTime: string;
50
- fields: unknown;
51
- }[];
52
- updatedRecords: string[];
53
- createdRecords: string[];
54
- };
55
- export interface UpdateRecordsBody {
56
- typecast: boolean;
57
- records: UpdateRecords;
58
- }
59
- export interface UpdateRecordsBodyUpsert extends UpdateRecordsBody {
60
- performUpsert: {
61
- fieldsToMergeOn: FieldsToMergeOn;
62
- };
63
- }
64
- export type BaseId = string;
65
- export type TableId = string;
66
- export type FieldsToMergeOn = string[];
67
- export type ApiRequest = {
68
- method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
69
- url: string;
70
- apiKey: string;
71
- body?: RequestBody;
72
- };
73
- export type RequestMethods = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
74
- export interface RequestMethodProps {
75
- url: string;
76
- apiKey: string;
77
- body?: RequestBody;
78
- }
79
- export type RequestBody = unknown;
80
- export interface AirtableRecordRequest {
81
- baseId: string;
82
- tableId: string;
83
- atId?: string;
84
- body: RequestBody;
85
- }
86
- export interface UpdateAirtableRecordRequest {
87
- atId?: string;
88
- baseId: string;
89
- tableId: string;
90
- body?: RequestBody;
91
- }
92
- export interface RequestBodyWebsiteInfos {
93
- fields: unknown;
94
- }
95
- export type UpdateRecord = {
96
- fields: unknown;
97
- };
98
- export type UpdateRecords = UpdateRecord[];
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes