Browse Source

Swap factory function for wrapper function in reduce callback

Looks simpler and saves a level of indentation
pull/15/head
Luke Childs 8 years ago
parent
commit
d4b4f42c25
  1. 94
      src/index.js

94
src/index.js

@ -30,63 +30,61 @@ class Onionoo {
// Return object containing endpoint methods // Return object containing endpoint methods
return this.options.endpoints.reduce((onionoo, endpoint) => { return this.options.endpoints.reduce((onionoo, endpoint) => {
onionoo[endpoint] = this.createEndpointMethod(endpoint); onionoo[endpoint] = options => this.get(endpoint, options);
return onionoo; return onionoo;
}, {}); }, {});
} }
// Returns a function to make requests to a given endpoint // Returns a function to make requests to a given endpoint
createEndpointMethod(endpoint) { get(endpoint, options) {
return options => { // Build query string (don't encode ':' for search filters)
// Build query string (don't encode ':' for search filters) const qs = querystring.encode(options).replace(/%3A/g, ':');
const qs = querystring.encode(options).replace(/%3A/g, ':');
// Build url
let url = `${this.options.baseUrl}/${endpoint}`;
url += qs ? `?${qs}` : '';
// If caching is disabled, just make the request
if (!this.options.cache) {
return this.makeRequest(url);
}
// If caching is enabled, check for url in cache // Build url
return this.options.cache.get(url) let url = `${this.options.baseUrl}/${endpoint}`;
.then(cachedResult => { url += qs ? `?${qs}` : '';
let options = {};
// If caching is disabled, just make the request
// If we have it cached if (!this.options.cache) {
if (cachedResult) { return this.makeRequest(url);
// Return the cached entry if it's still valid }
if (!expired(cachedResult.headers)) {
return cachedResult; // If caching is enabled, check for url in cache
return this.options.cache.get(url)
// If it's stale, add last-modified date to headers .then(cachedResult => {
} else if (cachedResult.headers['last-modified']) { let options = {};
options.headers = {
'if-modified-since': cachedResult.headers['last-modified'] // If we have it cached
}; if (cachedResult) {
} // Return the cached entry if it's still valid
if (!expired(cachedResult.headers)) {
return cachedResult;
// If it's stale, add last-modified date to headers
} else if (cachedResult.headers['last-modified']) {
options.headers = {
'if-modified-since': cachedResult.headers['last-modified']
};
} }
}
// Make a request // Make a request
return this.makeRequest(url, options) return this.makeRequest(url, options)
.then(response => { .then(response => {
// If we get a 304, fill in the body // If we get a 304, fill in the body
if (response.statusCode === 304) { if (response.statusCode === 304) {
response.body = cachedResult.body; response.body = cachedResult.body;
} }
// If we get a 200 or 304, cache it // If we get a 200 or 304, cache it
if ([200, 304].includes(response.statusCode)) { if ([200, 304].includes(response.statusCode)) {
this.options.cache.set(url, response); this.options.cache.set(url, response);
} }
return response; return response;
}); });
}); });
};
} }
// Returns a promise for a request // Returns a promise for a request

Loading…
Cancel
Save