Skip to content

Commit

Permalink
add initial dist/ to version control
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcarraway committed May 8, 2024
1 parent 491d101 commit 05f0101
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 13 deletions.
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

65 changes: 65 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export type AirtableClientOpts = {
/** A string of at least 10 characters. */
apiKey: string;
/** A string of at least 10 characters beginning with "app". */
baseId: string;
/**
* Optionally set the base URL (without a trailing slash).
* Defaults to "https://api.airtable.com/v0".
*/
baseUrl?: string;
};
export type AirtableResponse = {
data: unknown;
ok: boolean;
status: number;
statusText: string;
};
export type FieldsObj = Record<string, boolean | Date | null | number | string | undefined>;
export type CreateRecordOpts = {
fields: FieldsObj;
tableIdOrName: string;
};
export type GetRecordOpts = {
/** A string of at least 10 characters beginning with "rec". */
recordId: string;
tableIdOrName: string;
};
export type UpdateRecordOpts = {
fields: FieldsObj;
/**
* A PATCH request (the default) will only update the fields you specify,
* leaving the rest as they were.
*
* A PUT request will perform a destructive update and clear all unspecified cell values.
*/
method?: `PATCH` | `PUT`;
/** A string of at least 10 characters beginning with "rec". */
recordId: string;
tableIdOrName: string;
};
/**
* @see https://github.com/ensembleblock/airtable
*/
export declare class AirtableClient {
private baseId;
private baseUrl;
private headers;
constructor({ apiKey, baseId, baseUrl }: AirtableClientOpts);
/**
* Create a record.
* @see https://airtable.com/developers/web/api/create-records
*/
createRecord({ fields, tableIdOrName, }: CreateRecordOpts): Promise<AirtableResponse>;
/**
* Retrieve a single record using an Airtable `recordId`.
* Any "empty" fields (e.g. "", [], or false) in the record will not be returned.
* @see https://airtable.com/developers/web/api/get-record
*/
getRecord({ recordId, tableIdOrName, }: GetRecordOpts): Promise<AirtableResponse>;
/**
* Updates a single record.
* @see https://airtable.com/developers/web/api/update-record
*/
updateRecord({ fields, method, recordId, tableIdOrName, }: UpdateRecordOpts): Promise<AirtableResponse>;
}
102 changes: 102 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @see https://github.com/ensembleblock/airtable
*/
export class AirtableClient {
baseId = null;
baseUrl = `https://api.airtable.com/v0`;
headers = {};
constructor({ apiKey, baseId, baseUrl }) {
if (typeof apiKey !== `string` || apiKey.length < 10) {
throw new TypeError(`AirtableClient expected 'apiKey' to be string of at least 10 characters`);
}
if (typeof baseId !== `string` ||
baseId.length < 10 ||
!baseId.startsWith(`app`)) {
throw new TypeError(`AirtableClient expected 'baseId' to be string of at least 10 characters starting with 'app'`);
}
this.headers = {
Authorization: `Bearer ${apiKey}`,
'Content-Type': `application/json`,
};
this.baseId = baseId;
if (baseUrl) {
if (typeof baseUrl !== `string` || baseUrl.endsWith(`/`)) {
throw new TypeError(`AirtableClient expected 'baseUrl' to be a string without a trailing slash`);
}
this.baseUrl = baseUrl;
}
}
/**
* Create a record.
* @see https://airtable.com/developers/web/api/create-records
*/
async createRecord({ fields, tableIdOrName, }) {
if (!fields || typeof fields !== `object` || Array.isArray(fields)) {
throw new TypeError(`Airtable createRecord expected 'fields' to be a plain object`);
}
if (!tableIdOrName || typeof tableIdOrName !== `string`) {
throw new TypeError(`Airtable createRecord expected 'tableIdOrName' to be a non-empty string`);
}
const createRecordUrl = `${this.baseUrl}/${this.baseId}/${tableIdOrName}`;
const body = JSON.stringify({ fields });
const res = await fetch(createRecordUrl, {
body,
headers: this.headers,
method: `POST`,
});
const data = await res.json();
return { data, ok: res.ok, status: res.status, statusText: res.statusText };
}
/**
* Retrieve a single record using an Airtable `recordId`.
* Any "empty" fields (e.g. "", [], or false) in the record will not be returned.
* @see https://airtable.com/developers/web/api/get-record
*/
async getRecord({ recordId, tableIdOrName, }) {
if (typeof recordId !== `string` ||
recordId.length < 10 ||
!recordId.startsWith(`rec`)) {
throw new TypeError(`Airtable getRecord expected 'recordId' to be string of at least 10 characters starting with 'rec'`);
}
if (!tableIdOrName || typeof tableIdOrName !== `string`) {
throw new TypeError(`Airtable getRecord expected 'tableIdOrName' to be a non-empty string`);
}
const getRecordUrl = `${this.baseUrl}/${this.baseId}/${tableIdOrName}/${recordId}`;
const res = await fetch(getRecordUrl, {
headers: this.headers,
method: `GET`,
});
const data = await res.json();
return { data, ok: res.ok, status: res.status, statusText: res.statusText };
}
/**
* Updates a single record.
* @see https://airtable.com/developers/web/api/update-record
*/
async updateRecord({ fields, method = `PATCH`, recordId, tableIdOrName, }) {
if (!fields || typeof fields !== `object` || Array.isArray(fields)) {
throw new TypeError(`Airtable updateRecord expected 'fields' to be a plain object`);
}
if (typeof method !== `string` ||
![`PATCH`, `PUT`].includes(method.toUpperCase())) {
throw new TypeError(`Airtable updateRecord expected 'method' to be 'PATCH' or 'PUT'`);
}
if (typeof recordId !== `string` ||
recordId.length < 10 ||
!recordId.startsWith(`rec`)) {
throw new TypeError(`Airtable updateRecord expected 'recordId' to be string of at least 10 characters starting with 'rec'`);
}
if (!tableIdOrName || typeof tableIdOrName !== `string`) {
throw new TypeError(`Airtable updateRecord expected 'tableIdOrName' to be a non-empty string`);
}
const updateRecordUrl = `${this.baseUrl}/${this.baseId}/${tableIdOrName}/${recordId}`;
const body = JSON.stringify({ fields });
const res = await fetch(updateRecordUrl, {
body,
headers: this.headers,
method: method.toUpperCase(),
});
const data = await res.json();
return { data, ok: res.ok, status: res.status, statusText: res.statusText };
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"access": "public"
},
"scripts": {
"build": "tsc --project tsconfig.dist.json",
"build": "tsc",
"clean": "rimraf dist node_modules",
"clean:build": "rimraf dist",
"dev": "tsc --watch",
Expand Down
10 changes: 0 additions & 10 deletions tsconfig.dist.json

This file was deleted.

2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"outDir": "dist",
"pretty": true,
"resolveJsonModule": false,
"rootDir": "./src",
"skipLibCheck": true,
"sourceMap": false,
"strict": true,
"stripInternal": true,
"target": "ES2022",
Expand Down

0 comments on commit 05f0101

Please sign in to comment.