diff --git a/src/api.js b/src/api.js index 60b18ec..0e13f5f 100644 --- a/src/api.js +++ b/src/api.js @@ -22,7 +22,7 @@ function setupCache (config = {}) { // Execute request against local cache let res = await request(reqConfig, req) - let next = res.next + const next = res.next // Response is not function, something was in cache, return it if (!isFunction(next)) return next @@ -39,7 +39,7 @@ function setupCache (config = {}) { if (networkError) { // Check if we should attempt reading stale cache data - let readOnError = isFunction(reqConfig.readOnError) + const readOnError = isFunction(reqConfig.readOnError) ? reqConfig.readOnError(networkError, req) : reqConfig.readOnError diff --git a/src/redis.js b/src/redis.js index d2d1b0e..586ab55 100644 --- a/src/redis.js +++ b/src/redis.js @@ -24,6 +24,7 @@ class RedisStore { this.hlenAsync = promisify(client.hlen).bind(client) this.hgetallAsync = promisify(client.hgetall).bind(client) } + async getItem (key) { const item = (await this.hgetAsync(this.HASH_KEY, key)) || null diff --git a/test/spec/cache.spec.js b/test/spec/cache.spec.js index 0770184..dc72362 100644 --- a/test/spec/cache.spec.js +++ b/test/spec/cache.spec.js @@ -43,7 +43,7 @@ describe('Cache store', () => { const storedData = JSON.parse(store.store.test) - assert.equal(storedData.expires, expires) + assert.strictEqual(storedData.expires, expires) assert.ok(storedData.data.data.youhou) store.setItem = async () => { @@ -52,7 +52,7 @@ describe('Cache store', () => { cacheResult = await cache.write(config, req, res) - assert.equal(cacheResult, false) + assert.strictEqual(cacheResult, false) }) it('Should clear cache if a store error occurs and clearOnError option is activated', async () => { @@ -68,11 +68,11 @@ describe('Cache store', () => { cacheResult = await cache.write(config, req, res) - assert.equal(cacheResult, false) + assert.strictEqual(cacheResult, false) const length = await store.length() - assert.equal(length, 0) + assert.strictEqual(length, 0) }) it('Should throw when unable to clear cache after a store error occurs', async () => { @@ -91,14 +91,14 @@ describe('Cache store', () => { cacheResult = await cache.write(config, req, res) - assert.equal(cacheResult, false) + assert.strictEqual(cacheResult, false) }) it('Should read from cache', async () => { try { await cache.read(config, req) } catch (err) { - assert.equal(err.reason, 'cache-miss') + assert.strictEqual(err.reason, 'cache-miss') } await cache.write(config, req, res) @@ -106,7 +106,7 @@ describe('Cache store', () => { try { await cache.read(config, req) } catch (err) { - assert.equal(err.reason, 'cache-stale') + assert.strictEqual(err.reason, 'cache-stale') } config.expires = Date.now() + (15 * 60 * 1000) // Add 15min to cache expiry date @@ -124,23 +124,23 @@ describe('Cache store', () => { try { await cache.read(config, req) } catch (err) { - assert.equal(err.reason, 'cache-miss') + assert.strictEqual(err.reason, 'cache-miss') } }) it('Should generate a cache key', () => { const expected = function key () {} - assert.deepEqual(cache.key({ key: expected }), expected) + assert.deepStrictEqual(cache.key({ key: expected }), expected) let cacheKey = cache.key({ key: 'my-key' }) assert.ok(isFunction(cacheKey)) - assert.equal(cacheKey({ url: 'https://httpbin.org/' }), 'my-key/https://httpbin.org/') + assert.strictEqual(cacheKey({ url: 'https://httpbin.org/' }), 'my-key/https://httpbin.org/') cacheKey = cache.key({}) assert.ok(isFunction(cacheKey)) - assert.equal(cacheKey({ url: 'https://httpbin.org/' }), 'https://httpbin.org/') + assert.strictEqual(cacheKey({ url: 'https://httpbin.org/' }), 'https://httpbin.org/') }) }) diff --git a/test/spec/config.spec.js b/test/spec/config.spec.js index 61563e2..4524e49 100644 --- a/test/spec/config.spec.js +++ b/test/spec/config.spec.js @@ -29,17 +29,17 @@ describe('Per request config', () => { } it('Should merge per-request keys in a new object', () => { - let requestConfig = { maxAge: 1000 } + const requestConfig = { maxAge: 1000 } fakeRequest.cache = requestConfig - let mergedConfig = config.mergeRequestConfig(globalConfig, fakeRequest) + const mergedConfig = config.mergeRequestConfig(globalConfig, fakeRequest) assert.notStrictEqual(globalConfig, mergedConfig) assert.notStrictEqual(requestConfig, mergedConfig) }) it('Should merge the permitted keys', () => { - let requestConfig = { + const requestConfig = { maxAge: 10, key: 'myKey', exclude: { @@ -51,28 +51,28 @@ describe('Per request config', () => { } fakeRequest.cache = requestConfig - let mergedConfig = config.mergeRequestConfig(globalConfig, fakeRequest) + const mergedConfig = config.mergeRequestConfig(globalConfig, fakeRequest) - assert.equal(mergedConfig.maxAge, requestConfig.maxAge) - assert.equal(mergedConfig.key, requestConfig.key) - assert.deepEqual(mergedConfig.exclude, { + assert.strictEqual(mergedConfig.maxAge, requestConfig.maxAge) + assert.strictEqual(mergedConfig.key, requestConfig.key) + assert.deepStrictEqual(mergedConfig.exclude, { ...globalConfig.exclude, ...requestConfig.exclude }) - assert.equal(mergedConfig.clearOnStale, requestConfig.clearOnStale) - assert.equal(mergedConfig.clearOnError, requestConfig.clearOnError) + assert.strictEqual(mergedConfig.clearOnStale, requestConfig.clearOnStale) + assert.strictEqual(mergedConfig.clearOnError, requestConfig.clearOnError) assert.strictEqual(mergedConfig.debug, requestConfig.debug) - assert.notEqual(mergedConfig.maxAge, globalConfig.maxAge) - assert.notEqual(mergedConfig.key, globalConfig.key) - assert.notEqual(mergedConfig.exclude, globalConfig.exclude) - assert.notEqual(mergedConfig.clearOnStale, globalConfig.clearOnStale) - assert.notEqual(mergedConfig.clearOnError, globalConfig.clearOnError) - assert.notEqual(mergedConfig.debug, globalConfig.debug) + assert.notStrictEqual(mergedConfig.maxAge, globalConfig.maxAge) + assert.notStrictEqual(mergedConfig.key, globalConfig.key) + assert.notStrictEqual(mergedConfig.exclude, globalConfig.exclude) + assert.notStrictEqual(mergedConfig.clearOnStale, globalConfig.clearOnStale) + assert.notStrictEqual(mergedConfig.clearOnError, globalConfig.clearOnError) + assert.notStrictEqual(mergedConfig.debug, globalConfig.debug) }) it('Should not merge the disallowed keys', () => { - let requestConfig = { + const requestConfig = { limit: true, store: 'abc', adapter: 'whoops', @@ -81,22 +81,22 @@ describe('Per request config', () => { } fakeRequest.cache = requestConfig - let mergedConfig = config.mergeRequestConfig(globalConfig, fakeRequest) + const mergedConfig = config.mergeRequestConfig(globalConfig, fakeRequest) - assert.equal(mergedConfig.limit, globalConfig.limit) - assert.equal(mergedConfig.store, globalConfig.store) - assert.equal(mergedConfig.adapter, globalConfig.adapter) - assert.equal(mergedConfig.uuid, globalConfig.key(fakeRequest)) - assert.equal(mergedConfig.acceptStale, globalConfig.acceptStale) + assert.strictEqual(mergedConfig.limit, globalConfig.limit) + assert.strictEqual(mergedConfig.store, globalConfig.store) + assert.strictEqual(mergedConfig.adapter, globalConfig.adapter) + assert.strictEqual(mergedConfig.uuid, globalConfig.key(fakeRequest)) + assert.strictEqual(mergedConfig.acceptStale, globalConfig.acceptStale) }) it('Should transform the debug key when true', () => { - let requestConfig = { + const requestConfig = { debug: true } fakeRequest.cache = requestConfig - let mergedConfig = config.mergeRequestConfig(globalConfig, fakeRequest) + const mergedConfig = config.mergeRequestConfig(globalConfig, fakeRequest) assert.ok(typeof mergedConfig.debug === 'function') }) diff --git a/test/spec/exclude.spec.js b/test/spec/exclude.spec.js index 785aae1..7a27982 100644 --- a/test/spec/exclude.spec.js +++ b/test/spec/exclude.spec.js @@ -12,7 +12,7 @@ describe('Cache exclusion', () => { it('Should not exclude if not configured', () => { const config = { debug } - assert.equal(exclude(config, { url }), false) + assert.strictEqual(exclude(config, { url }), false) }) it('Should exclude requests with query parameters with config.exclude.query=true', () => { @@ -52,7 +52,7 @@ describe('Cache exclusion', () => { } assert.ok(exclude(config, { url })) - assert.equal(exclude(config, { url: 'https://some-rest.api/invoices' }), false) + assert.strictEqual(exclude(config, { url: 'https://some-rest.api/invoices' }), false) }) it('Should exclude filtered requests', () => { @@ -64,6 +64,6 @@ describe('Cache exclusion', () => { } assert.ok(exclude(config, { url, params: { shouldExclude: true } })) - assert.equal(exclude(config, { url }), false) + assert.strictEqual(exclude(config, { url }), false) }) }) diff --git a/test/spec/index.spec.js b/test/spec/index.spec.js index 1b4ae3a..5e5005e 100644 --- a/test/spec/index.spec.js +++ b/test/spec/index.spec.js @@ -42,12 +42,12 @@ describe('Integration', function () { let response = await api(definition) - assert.equal(response.status, 200) + assert.strictEqual(response.status, 200) assert.ok(isObject(response.data)) response = await api(definition) - assert.equal(response.status, 200) + assert.strictEqual(response.status, 200) assert.ok(isObject(response.data)) assert.ok(response.request.fromCache) }) @@ -60,11 +60,11 @@ describe('Integration', function () { try { await api({ url: 'https://httpbin.org/status/404' }) } catch (err) { - assert.equal(err.response.status, 404) + assert.strictEqual(err.response.status, 404) const length = await api.cache.length() - assert.equal(length, 0) + assert.strictEqual(length, 0) } }) @@ -78,15 +78,15 @@ describe('Integration', function () { let res = await api({ url }) let length - assert.equal(res.status, 200) + assert.strictEqual(res.status, 200) length = await api.cache.length() - assert.equal(length, 1) + assert.strictEqual(length, 1) res = await api({ url, method: 'OPTIONS' }) length = await api.cache.length() - assert.equal(length, 0) + assert.strictEqual(length, 0) }) it('Should cache GET requests with params', async function () { @@ -114,7 +114,7 @@ describe('Integration', function () { let response = await api2(definition) - assert.equal(response.status, 200) + assert.strictEqual(response.status, 200) assert.ok(isObject(response.data)) assert.ok(has(response.data.args, 'userId')) assert.ok(!response.request.fromCache) @@ -132,28 +132,28 @@ describe('Integration', function () { assert.ok(response.request.fromCache) }) - it("Should not cache requests with same url but different baseURL", async () => { + it('Should not cache requests with same url but different baseURL', async () => { const api = setup() // https://httpbin.org/anything - const response = await api.get("anything", { - baseURL: "https://httpbin.org/", + const response = await api.get('anything', { + baseURL: 'https://httpbin.org/', cache: { maxAge: 15 * 1000 } }) - assert.notEqual(response.request.fromCache, true) + assert.notStrictEqual(response.request.fromCache, true) // https://httpbin.org/anything/anything - const response2 = await api.get("anything", { - baseURL: "https://httpbin.org/anything/", + const response2 = await api.get('anything', { + baseURL: 'https://httpbin.org/anything/', cache: { maxAge: 15 * 1000 } }) - assert.notEqual(response2.request.fromCache, true) + assert.notStrictEqual(response2.request.fromCache, true) }) it('Should cache GET requests with params even though URLSearchParams does not exist', async () => { @@ -174,7 +174,7 @@ describe('Integration', function () { let response = await api(definition) - assert.equal(response.status, 200) + assert.strictEqual(response.status, 200) assert.ok(has(response.data.args, 'userId')) assert.ok(!response.request.fromCache) @@ -211,13 +211,13 @@ describe('Integration', function () { const responses = await send() responses.forEach(response => { - assert.equal(response.status, 200) + assert.strictEqual(response.status, 200) assert.ok(isObject(response.data)) }) const length = await api3.cache.length() - assert.equal(length, config.cache.limit) + assert.strictEqual(length, config.cache.limit) }) it('Should exclude paths', async function () { @@ -246,7 +246,7 @@ describe('Integration', function () { const length = await api4.cache.length() - assert.equal(length, 0) + assert.strictEqual(length, 0) }) it('Should activate debugging mode or take a debug function', () => { @@ -263,14 +263,14 @@ describe('Integration', function () { }) assert.ok(isFunction(cache.config.debug)) - assert.equal(cache.config.debug('test'), 'test') + assert.strictEqual(cache.config.debug('test'), 'test') }) it('Should take an optional store', () => { const store = new MemoryStore() const cache = setupCache({ store }) - assert.deepEqual(cache.store, store) + assert.deepStrictEqual(cache.store, store) }) it('Should be able to transform response whether it comes from network or cache', async function () { @@ -292,28 +292,28 @@ describe('Integration', function () { } }) - let response = await request() - let response2 = await request() - let response3 = await request() + const response = await request() + const response2 = await request() + const response3 = await request() assert.ok(!response.request.fromCache) - assert.equal(response.data, 'foobar') + assert.strictEqual(response.data, 'foobar') assert.ok(response2.request.fromCache) - assert.equal(response2.data, 'foobar') + assert.strictEqual(response2.data, 'foobar') assert.ok(response3.request.fromCache) - assert.equal(response3.data, 'foobar') + assert.strictEqual(response3.data, 'foobar') // assert.doesNotThrow(async () => { // response = await request() // assert.ok(response.request.fromCache) - // assert.equal(response.data, 'foobar') + // assert.strictEqual(response.data, 'foobar') // assert.doesNotThrow(async () => { // response = await request() // assert.ok(response.request.fromCache) - // assert.equal(response.data, 'foobar') + // assert.strictEqual(response.data, 'foobar') // }) // }) }) @@ -364,13 +364,13 @@ describe('Integration', function () { method: 'get' }) - assert.equal(response.status, 200) + assert.strictEqual(response.status, 200) assert.ok(isObject(response.data)) const { data } = await store.getItem(url) - assert.equal(data.status, 200) - assert.equal(data.data.url, url) + assert.strictEqual(data.status, 200) + assert.strictEqual(data.data.url, url) }) it('Should be able to set caching options per request', async function () { @@ -433,26 +433,26 @@ describe('Integration', function () { const item = await api.cache.getItem('https://httpbin.org/cache/2345') - assert.equal(item.expires, 12345000) + assert.strictEqual(item.expires, 12345000) MockDate.reset() }) - it("when no cache-control header", async () => { + it('when no cache-control header', async () => { MockDate.set(10000000) const api = setup({ - baseURL: "https://httpbin.org", + baseURL: 'https://httpbin.org', cache: { readHeaders: true } }) - const response = await api.get("/cache") + const response = await api.get('/cache') assert.ok(!response.request.fromCache) - const item = await api.cache.getItem("https://httpbin.org/cache") + const item = await api.cache.getItem('https://httpbin.org/cache') - assert.equal(item.expires, 10000000) + assert.strictEqual(item.expires, 10000000) MockDate.reset() }) @@ -501,7 +501,7 @@ describe('Integration', function () { const item = await api.cache.getItem(baseURL + route) - assert.equal(item.expires, new Date(dateInThePast).getTime()) + assert.strictEqual(item.expires, new Date(dateInThePast).getTime()) response = await api.get(route) diff --git a/test/spec/limit.spec.js b/test/spec/limit.spec.js index d19b883..5827a15 100644 --- a/test/spec/limit.spec.js +++ b/test/spec/limit.spec.js @@ -30,13 +30,13 @@ describe('Limit', () => { let length = await store.length() - assert.equal(length, 2) + assert.strictEqual(length, 2) await limit(config) length = await store.length() - assert.equal(length, 1) + assert.strictEqual(length, 1) }) }) }) diff --git a/test/spec/memory.spec.js b/test/spec/memory.spec.js index 0f98453..981fd0e 100644 --- a/test/spec/memory.spec.js +++ b/test/spec/memory.spec.js @@ -19,7 +19,7 @@ describe('Memory store', () => { const value = await store.getItem('foo') - assert.equal(value, expected) + assert.strictEqual(value, expected) }) it('setItem(): Should set an item', async () => { @@ -27,7 +27,7 @@ describe('Memory store', () => { await store.setItem('foo', expected) - assert.equal(store.store.foo, JSON.stringify(expected)) + assert.strictEqual(store.store.foo, JSON.stringify(expected)) }) it('removeItem(): Should remove an item', async () => { @@ -35,7 +35,7 @@ describe('Memory store', () => { await store.removeItem('foo') - assert.equal(store.store.foo, undefined) + assert.strictEqual(store.store.foo, undefined) }) it('clear(): Should clear all set values', async () => { @@ -44,8 +44,8 @@ describe('Memory store', () => { await store.clear() - assert.equal(store.store.foo, undefined) - assert.equal(store.store.hello, undefined) + assert.strictEqual(store.store.foo, undefined) + assert.strictEqual(store.store.hello, undefined) assert.ok(isEmpty(store.store)) }) @@ -61,6 +61,6 @@ describe('Memory store', () => { const storedData = await store.getItem('key') - assert.notEqual(data.key, storedData.key) + assert.notStrictEqual(data.key, storedData.key) }) }) diff --git a/test/spec/redis.spec.js b/test/spec/redis.spec.js index 307685d..ae8c17f 100644 --- a/test/spec/redis.spec.js +++ b/test/spec/redis.spec.js @@ -21,7 +21,7 @@ describe('Redis store', () => { it('Should accept custom HASH_KEY', async () => { const expected = 'customHash' store = new RedisStore(client, expected) - assert.equal(store.HASH_KEY, expected) + assert.strictEqual(store.HASH_KEY, expected) }) it('getItem(): Should retrieve an item', async () => { @@ -31,7 +31,7 @@ describe('Redis store', () => { const value = await store.getItem('foo') - assert.equal(value, expected) + assert.strictEqual(value, expected) }) it('setItem(): Should set an item', async () => { @@ -41,7 +41,7 @@ describe('Redis store', () => { const value = await store.getItem('foo') - assert.equal(value, expected) + assert.strictEqual(value, expected) }) it('removeItem(): Should remove an item', async () => { @@ -50,7 +50,8 @@ describe('Redis store', () => { await store.removeItem('foo') const value = await store.getItem('foo') - assert.equal(value, undefined) + + assert.strictEqual(value, null) }) it('clear(): Should clear all set values', async () => { @@ -61,7 +62,7 @@ describe('Redis store', () => { const length = await store.length() - assert.equal(length, 0) + assert.strictEqual(length, 0) }) it('Should serialize stored data to prevent modification by reference', async () => { @@ -75,6 +76,6 @@ describe('Redis store', () => { const storedData = await store.getItem('key') - assert.notEqual(data.key, storedData.key) + assert.notStrictEqual(data.key, storedData.key) }) }) diff --git a/test/spec/request.spec.js b/test/spec/request.spec.js index 2571b6b..d067aa2 100644 --- a/test/spec/request.spec.js +++ b/test/spec/request.spec.js @@ -70,7 +70,7 @@ describe('Request', () => { const length = await store.length() - assert.equal(length, 0) + assert.strictEqual(length, 0) }) it('Should clear based on new invalidate function', async () => { @@ -92,7 +92,7 @@ describe('Request', () => { const length = await store.length() - assert.equal(length, 1) + assert.strictEqual(length, 1) }) // Helpers diff --git a/test/spec/serialize.spec.js b/test/spec/serialize.spec.js index 1aedc67..b98836a 100644 --- a/test/spec/serialize.spec.js +++ b/test/spec/serialize.spec.js @@ -15,12 +15,12 @@ describe('Serialize', () => { const expected = { data: { youhou: true } } it('Should serialize response object', () => { - assert.deepEqual(serialize(config, req, res), expected) + assert.deepStrictEqual(serialize(config, req, res), expected) }) it('Should parse response.data if in JSON format', () => { res.data = '{ "youhou": true }' - assert.deepEqual(serialize(config, req, res), expected) + assert.deepStrictEqual(serialize(config, req, res), expected) }) }) diff --git a/test/spec/utilities.spec.js b/test/spec/utilities.spec.js index ecb685b..b12a0b3 100644 --- a/test/spec/utilities.spec.js +++ b/test/spec/utilities.spec.js @@ -35,21 +35,21 @@ describe('Utilities', () => { describe('getTag', () => { it('Should identify null ', () => { - assert.equal(getTag(null), '[object Null]') + assert.strictEqual(getTag(null), '[object Null]') }) it('Should identify undefined ', () => { - assert.equal(getTag(undefined), '[object Undefined]') + assert.strictEqual(getTag(undefined), '[object Undefined]') }) it('Should identify strings ', () => { - assert.equal(getTag('foo'), '[object String]') + assert.strictEqual(getTag('foo'), '[object String]') }) it('Should identify all function types ', () => { - assert.equal(getTag(functions[0]), '[object Function]') - assert.equal(getTag(functions[1]), '[object GeneratorFunction]') - assert.equal(getTag(functions[2]), '[object AsyncFunction]') + assert.strictEqual(getTag(functions[0]), '[object Function]') + assert.strictEqual(getTag(functions[1]), '[object GeneratorFunction]') + assert.strictEqual(getTag(functions[2]), '[object AsyncFunction]') }) }) @@ -88,14 +88,14 @@ describe('Utilities', () => { }) assert.ok(called) assert.ok(mapped instanceof Array) - assert.equal(mapped[0], 'n=10') - assert.equal(mapped[1], 'a=15') + assert.strictEqual(mapped[0], 'n=10') + assert.strictEqual(mapped[1], 'a=15') }) it('Should return empty Array for non-object values', () => { const mapFn = (v, k) => `${k}=${v}` - notObjects.forEach(value => assert.deepEqual(mapObject(value, mapFn), [])) - functions.forEach(value => assert.deepEqual(mapObject(value, mapFn), [])) + notObjects.forEach(value => assert.deepStrictEqual(mapObject(value, mapFn), [])) + functions.forEach(value => assert.deepStrictEqual(mapObject(value, mapFn), [])) }) }) }) diff --git a/webpack.config.js b/webpack.config.js index 2cde165..f419345 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -7,13 +7,13 @@ const cwd = process.cwd() // Base filename and version variable to store what kind of version we'll be generating let filename = 'cache[version].js' -let version = [''] +const version = [''] // Start with empty list of plugins and externals and an undefined devtool const plugins = [ // new BundleAnalyzerPlugin() ] -let externals = {} +const externals = {} let mode = 'development' let target = 'web'