Skip to content

Commit

Permalink
Add AirtableBase return type
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Sep 1, 2020
1 parent ce60e6d commit c7b74ca
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ const userAgent = `Airtable.js/${packageVersion}`;
type BaseRequestOptions = {
method?: string;
path?: string;
qs?: any;
headers?: any;
body?: any;
qs?: Record<string, any>;
headers?: Record<string, any>;
body?: Record<string, any>;
_numAttempts?: number;
};

type BaseResponse = Response & {statusCode: Response['status']};

type AirtableBase = {
(tableName: string): Table;
_base: Base;
getId(): string;
makeRequest(options: BaseRequestOptions): Promise<BaseResponse>;
table(tableName: string): Table;
};

class Base {
readonly _airtable: Airtable;
readonly _id: string;
Expand All @@ -37,9 +47,7 @@ class Base {
return new Table(this, null, tableName);
}

makeRequest(options: BaseRequestOptions) {
options = options || {};

makeRequest(options: BaseRequestOptions = {}) {
const method = get(options, 'method', 'GET').toUpperCase();

const url = `${this._airtable._endpointUrl}/v${this._airtable._apiVersionMajor}/${
Expand All @@ -64,7 +72,7 @@ class Base {

return new Promise((resolve, reject) => {
fetch(url, requestOptions)
.then((resp: Response & {statusCode: Response['status']}) => {
.then((resp: BaseResponse) => {
clearTimeout(timeout);
resp.statusCode = resp.status;
if (resp.status === 429 && !this._airtable._noRetryIfRateLimited) {
Expand Down Expand Up @@ -205,7 +213,7 @@ class Base {
return this._id;
}

static createFunctor(airtable: Airtable, baseId: string) {
static createFunctor(airtable: Airtable, baseId: string): AirtableBase {
const base = new Base(airtable, baseId);
const baseFn = tableName => {
return base.doCall(tableName);
Expand All @@ -214,8 +222,7 @@ class Base {
baseFn[baseMethod] = base[baseMethod].bind(base);
});
baseFn._base = base;
baseFn.tables = base['tables'];
return baseFn;
return baseFn as AirtableBase;
}
}

Expand Down

0 comments on commit c7b74ca

Please sign in to comment.