Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated requests with node-fetch #191

Merged
merged 19 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ To install airtable.js in a node project:

npm install airtable

Airtable.js should work with Node 10 and above.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe is compatible with instead of should work? So the wording is a bit more confidence inspiring 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.


### Browser

Expand All @@ -33,6 +34,8 @@ Edit `test/test_files/index.html` - put your `BASE_ID` and `API_KEY` (Be careful

Then open http://localhost:8000/ in your browser.

Airtable.js should work with browsers supported by Airtable App.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is compatible with and Airtable web app

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed as well.

See the [techincal requirements](https://support.airtable.com/hc/en-us/articles/217990018) for more details.

# Configuration

Expand Down
11 changes: 11 additions & 0 deletions lib/abort-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// istanbul ignore file
if (typeof window === 'undefined') {
module.exports = require('abort-controller');
} else {
if ('signal' in new Request('')) {
module.exports = window.AbortController;
} else {
var polyfill = require('abortcontroller-polyfill/dist/cjs-ponyfill');
module.exports = polyfill.AbortController;
}
}
108 changes: 63 additions & 45 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var forEach = require('lodash/forEach');
var get = require('lodash/get');
var assign = require('lodash/assign');
var isPlainObject = require('lodash/isPlainObject');
var fetch = require('./fetch');
var AbortController = require('./abort-controller');

// This will become require('xhr') in the browser.
var request = require('request');

var objectToQueryParamString = require('./object_to_query_param_string');
var AirtableError = require('./airtable_error');
var Table = require('./table');
var HttpHeaders = require('./http_headers');
Expand All @@ -34,59 +34,76 @@ Base.prototype.makeRequest = function(options) {

var method = get(options, 'method', 'GET').toUpperCase();

var url =
this._airtable._endpointUrl +
'/v' +
this._airtable._apiVersionMajor +
'/' +
this._id +
get(options, 'path', '/') +
'?' +
objectToQueryParamString(get(options, 'qs', {}));

var controller = new AbortController();

var requestOptions = {
method: method,
url:
this._airtable._endpointUrl +
'/v' +
this._airtable._apiVersionMajor +
'/' +
this._id +
get(options, 'path', '/'),
qs: get(options, 'qs', {}),
headers: this._getRequestHeaders(get(options, 'headers', {})),
json: true,
timeout: this._airtable.requestTimeout,
signal: controller.signal,
};

if ('body' in options && _canRequestMethodIncludeBody(method)) {
requestOptions.body = options.body;
requestOptions.body = JSON.stringify(options.body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im assuming the JSON.stringify was previously handled by the request library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was.

}

var timeout = setTimeout(function() {
controller.abort();
}, this._airtable.requestTimeout);

return new Promise(function(resolve, reject) {
request(requestOptions, function(err, response, body) {
if (!err && response.statusCode === 429 && !that._airtable._noRetryIfRateLimited) {
var numAttempts = get(options, '_numAttempts', 0);
var backoffDelayMs = exponentialBackoffWithJitter(numAttempts);
setTimeout(function() {
var newOptions = assign({}, options, {
_numAttempts: numAttempts + 1,
});
that.makeRequest(newOptions)
.then(resolve)
.catch(reject);
}, backoffDelayMs);
return;
}

if (err) {
fetch(url, requestOptions)
.then(function(resp) {
clearTimeout(timeout);
resp.statusCode = resp.status;
if (resp.status === 429 && !that._airtable._noRetryIfRateLimited) {
var numAttempts = get(options, '_numAttempts', 0);
var backoffDelayMs = exponentialBackoffWithJitter(numAttempts);
setTimeout(function() {
var newOptions = assign({}, options, {
_numAttempts: numAttempts + 1,
});
that.makeRequest(newOptions)
.then(resolve)
.catch(reject);
}, backoffDelayMs);
} else {
resp.json()
.then(function(body) {
var err =
that._checkStatusForError(resp.status, body) ||
_getErrorForNonObjectBody(resp.status, body);

if (err) {
reject(err);
} else {
resolve({
statusCode: resp.status,
headers: resp.headers,
body: body,
});
}
})
.catch(function() {
var err = _getErrorForNonObjectBody(resp.status);
reject(err);
});
}
})
.catch(function(err) {
clearTimeout(timeout);
err = new AirtableError('CONNECTION_ERROR', err.message, null);
} else {
err =
that._checkStatusForError(response.statusCode, body) ||
_getErrorForNonObjectBody(response.statusCode, body);
}

if (err) {
reject(err);
return;
}

resolve({
statusCode: response.statusCode,
headers: response.headers,
body: body,
});
});
});
};

Expand All @@ -100,6 +117,7 @@ Base.prototype._getRequestHeaders = function(headers) {

result.set('Authorization', 'Bearer ' + this._airtable._apiKey);
result.set('User-Agent', userAgent);
result.set('Content-Type', 'application/json');
forEach(headers, function(headerValue, headerKey) {
result.set(headerKey, headerValue);
});
Expand Down
4 changes: 4 additions & 0 deletions lib/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var fetch = require('node-fetch');

// istanbul ignore next
module.exports = typeof window === 'undefined' ? fetch : window.fetch;
54 changes: 31 additions & 23 deletions lib/run_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
var exponentialBackoffWithJitter = require('./exponential_backoff_with_jitter');
var objectToQueryParamString = require('./object_to_query_param_string');
var packageVersion = require('./package_version');

// This will become require('xhr') in the browser.
var request = require('request');
var fetch = require('./fetch');
var AbortController = require('./abort-controller');

var userAgent = 'Airtable.js/' + packageVersion;

Expand All @@ -24,6 +23,7 @@ function runAction(base, method, path, queryParams, bodyData, callback, numAttem
authorization: 'Bearer ' + base._airtable._apiKey,
'x-api-version': base._airtable._apiVersion,
'x-airtable-application-id': base.getId(),
'content-type': 'application/json',
};
var isBrowser = typeof window !== 'undefined';
// Some browsers do not allow overriding the user agent.
Expand All @@ -34,35 +34,43 @@ function runAction(base, method, path, queryParams, bodyData, callback, numAttem
headers['User-Agent'] = userAgent;
}

var controller = new AbortController();
var options = {
method: method.toUpperCase(),
url: url,
json: true,
timeout: base._airtable.requestTimeout,
headers: headers,
signal: controller.signal,
};

if (bodyData !== null) {
options.body = bodyData;
options.body = JSON.stringify(bodyData);
}

request(options, function(error, resp, body) {
if (error) {
callback(error, resp, body);
return;
}

if (resp.statusCode === 429 && !base._airtable._noRetryIfRateLimited) {
var backoffDelayMs = exponentialBackoffWithJitter(numAttempts);
setTimeout(function() {
runAction(base, method, path, queryParams, bodyData, callback, numAttempts + 1);
}, backoffDelayMs);
return;
}
var timeout = setTimeout(function() {
controller.abort();
}, base._airtable.requestTimeout);

error = base._checkStatusForError(resp.statusCode, body);
callback(error, resp, body);
});
fetch(url, options)
.then(function(resp) {
clearTimeout(timeout);
if (resp.status === 429 && !base._airtable._noRetryIfRateLimited) {
var backoffDelayMs = exponentialBackoffWithJitter(numAttempts);
setTimeout(function() {
runAction(base, method, path, queryParams, bodyData, callback, numAttempts + 1);
}, backoffDelayMs);
} else {
resp.json()
.then(function(body) {
var error = base._checkStatusForError(resp.status, body);
resp.statusCode = resp.status;
callback(error, resp, body);
})
.catch(callback);
}
})
.catch(function(error) {
clearTimeout(timeout);
callback(error);
});
}

module.exports = runAction;
Loading