From c7b74ca38d28accc52054ae1a28834388f90292c Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Tue, 1 Sep 2020 14:31:54 -0400 Subject: [PATCH] Add AirtableBase return type --- src/base.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/base.ts b/src/base.ts index 2898d4a4..b24f348a 100644 --- a/src/base.ts +++ b/src/base.ts @@ -18,12 +18,22 @@ const userAgent = `Airtable.js/${packageVersion}`; type BaseRequestOptions = { method?: string; path?: string; - qs?: any; - headers?: any; - body?: any; + qs?: Record; + headers?: Record; + body?: Record; _numAttempts?: number; }; +type BaseResponse = Response & {statusCode: Response['status']}; + +type AirtableBase = { + (tableName: string): Table; + _base: Base; + getId(): string; + makeRequest(options: BaseRequestOptions): Promise; + table(tableName: string): Table; +}; + class Base { readonly _airtable: Airtable; readonly _id: string; @@ -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}/${ @@ -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) { @@ -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); @@ -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; } }