From d4b4f42c2562d1b8168a305b93b7e0ff2107db66 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Fri, 23 Dec 2016 16:24:07 +0700 Subject: [PATCH] Swap factory function for wrapper function in reduce callback Looks simpler and saves a level of indentation --- src/index.js | 94 +++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/src/index.js b/src/index.js index 8171a0b..24bbc99 100644 --- a/src/index.js +++ b/src/index.js @@ -30,63 +30,61 @@ class Onionoo { // Return object containing endpoint methods return this.options.endpoints.reduce((onionoo, endpoint) => { - onionoo[endpoint] = this.createEndpointMethod(endpoint); + onionoo[endpoint] = options => this.get(endpoint, options); return onionoo; }, {}); } // Returns a function to make requests to a given endpoint - createEndpointMethod(endpoint) { - return options => { - // Build query string (don't encode ':' for search filters) - 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); - } + get(endpoint, options) { + // Build query string (don't encode ':' for search filters) + const qs = querystring.encode(options).replace(/%3A/g, ':'); - // If caching is enabled, check for url in cache - return this.options.cache.get(url) - .then(cachedResult => { - let options = {}; - - // 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'] - }; - } + // 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 + return this.options.cache.get(url) + .then(cachedResult => { + let options = {}; + + // 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 - return this.makeRequest(url, options) - .then(response => { - // If we get a 304, fill in the body - if (response.statusCode === 304) { - response.body = cachedResult.body; - } - - // If we get a 200 or 304, cache it - if ([200, 304].includes(response.statusCode)) { - this.options.cache.set(url, response); - } - - return response; - }); - }); - }; + // Make a request + return this.makeRequest(url, options) + .then(response => { + // If we get a 304, fill in the body + if (response.statusCode === 304) { + response.body = cachedResult.body; + } + + // If we get a 200 or 304, cache it + if ([200, 304].includes(response.statusCode)) { + this.options.cache.set(url, response); + } + + return response; + }); + }); } // Returns a promise for a request