Skip to content

Commit

Permalink
Fix lint issues and add lint-fix script
Browse files Browse the repository at this point in the history
  • Loading branch information
lisathesecond committed Mar 14, 2018
1 parent cb31dce commit 75c0fc6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 81 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"scripts": {
"clean": "rimraf dist",
"lint": "standard src/**/*.js test/**/*.js | snazzy",
"lint-fix": "standard --fix src/**/*.js test/**/*.js | snazzy",
"build": "npm run clean && webpack && NODE_ENV=production webpack && NODE_BUNDLED=please webpack && NODE_ENV=production NODE_BUNDLED=please webpack",
"test": "NODE_ENV=test karma start karma.conf.js",
"watch": "NODE_WATCH=true npm test"
Expand Down
48 changes: 24 additions & 24 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import axios from 'axios';
import merge from 'lodash/merge';
import omit from 'lodash/omit';
import axios from 'axios'
import merge from 'lodash/merge'
import omit from 'lodash/omit'

import MemoryStore from './memory';
import { key } from './cache';
import MemoryStore from './memory'
import { key } from './cache'

const noop = () => {};
const debug = (...args) => console.log('[axios-cache-adapter]', ...args);
const noop = () => {}
const debug = (...args) => console.log('[axios-cache-adapter]', ...args)

const defaults = {
// Default settings when solely creating the cache adapter with setupCache.
Expand All @@ -32,35 +32,35 @@ const defaults = {
maxAge: 15 * 60 * 1000
}
}
};
}

// List of disallowed in the per-request config.
const disallowedPerRequestKeys = ['limit', 'store', 'adapter'];
const disallowedPerRequestKeys = ['limit', 'store', 'adapter']

/**
* Make a global config object.
*
* @param {Object} [override={}] Optional config override.
* @return {Object}
*/
const makeConfig = function(override = {}) {
let config = merge({}, defaults.cache, override);
const makeConfig = function (override = {}) {
let config = merge({}, defaults.cache, override)

// Create a cache key method
config.key = key(config);
config.key = key(config)

// If debug mode is on, create a simple logger method
if (config.debug !== false) {
config.debug = typeof config.debug === 'function' ? config.debug : debug;
config.debug = typeof config.debug === 'function' ? config.debug : debug
} else {
config.debug = noop;
config.debug = noop
}

// Create an in memory store if none was given
if (!config.store) config.store = new MemoryStore();
if (!config.store) config.store = new MemoryStore()

return config;
};
return config
}

/**
* Merge the per-request config in another config.
Expand All @@ -74,15 +74,15 @@ const makeConfig = function(override = {}) {
* @param {Object} [requestConfig={}] The per-request config.
* @return {Object}
*/
const mergeRequestConfig = function(config, requestConfig = {}) {
let mergedConfig = merge({}, config, omit(requestConfig, disallowedPerRequestKeys));
const mergeRequestConfig = function (config, requestConfig = {}) {
let mergedConfig = merge({}, config, omit(requestConfig, disallowedPerRequestKeys))

if (mergedConfig.debug === true) {
mergedConfig.debug = debug;
mergedConfig.debug = debug
}

return mergedConfig;
};
return mergedConfig
}

export { defaults, makeConfig, mergeRequestConfig };
export default { defaults, makeConfig, mergeRequestConfig };
export { defaults, makeConfig, mergeRequestConfig }
export default { defaults, makeConfig, mergeRequestConfig }
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import { defaults, makeConfig, mergeRequestConfig } from './config'
*/
function setupCache (config = {}) {
// Extend default configuration
config = makeConfig(config);
config = makeConfig(config)

// Axios adapter. Receives the axios request configuration as only parameter
async function adapter (req) {

// Merge the per-request config with the instance config.
const reqConfig = mergeRequestConfig(config, req.cache);
const reqConfig = mergeRequestConfig(config, req.cache)

// Execute request against local cache
let res = await request(reqConfig, req)
Expand Down
97 changes: 43 additions & 54 deletions test/spec/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* globals describe it beforeEach */
/* globals describe it */

import assert from 'assert';
import isEqual from 'lodash/isEqual';
import assert from 'assert'
import isEqual from 'lodash/isEqual'

import config from 'src/config';
import config from 'src/config'

describe('Per request config', () => {
const debug = () => {};
// const debug = () => {}
// const debug = (...args) => { console.log(...args) }

const globalConfig = {
Expand All @@ -23,31 +23,20 @@ describe('Per request config', () => {
clearOnStale: true,
clearOnError: true,
debug: false
};
const requestConfig = {
maxAge: 10,
limit: true,
store: 'abc',
key: 'myKey',
exclude: true,
adapter: 'whoops',
clearOnStale: false,
clearOnError: false,
debug: true
};
}

it('Should merge per-request keys in a new object', () => {
let requestConfig = { maxAge: 1000 };
let mergedConfig = config.mergeRequestConfig(globalConfig, requestConfig);
assert.notStrictEqual(globalConfig, mergedConfig);
assert.notStrictEqual(requestConfig, mergedConfig);
});
let requestConfig = { maxAge: 1000 }
let mergedConfig = config.mergeRequestConfig(globalConfig, requestConfig)
assert.notStrictEqual(globalConfig, mergedConfig)
assert.notStrictEqual(requestConfig, mergedConfig)
})

it('Should accept an optional per-request config', () => {
let mergedConfig = config.mergeRequestConfig(globalConfig);
assert.notStrictEqual(globalConfig, mergedConfig);
assert.ok(isEqual(globalConfig, mergedConfig));
});
let mergedConfig = config.mergeRequestConfig(globalConfig)
assert.notStrictEqual(globalConfig, mergedConfig)
assert.ok(isEqual(globalConfig, mergedConfig))
})

it('Should merge the permitted keys', () => {
let requestConfig = {
Expand All @@ -56,41 +45,41 @@ describe('Per request config', () => {
exclude: true,
clearOnStale: false,
clearOnError: false,
debug: () => {},
};
let mergedConfig = config.mergeRequestConfig(globalConfig, requestConfig);
assert.equal(mergedConfig.maxAge, requestConfig.maxAge);
assert.equal(mergedConfig.key, requestConfig.key);
assert.equal(mergedConfig.exclude, requestConfig.exclude);
assert.equal(mergedConfig.clearOnStale, requestConfig.clearOnStale);
assert.equal(mergedConfig.clearOnError, requestConfig.clearOnError);
assert.strictEqual(mergedConfig.debug, requestConfig.debug);
debug: () => {}
}
let mergedConfig = config.mergeRequestConfig(globalConfig, requestConfig)
assert.equal(mergedConfig.maxAge, requestConfig.maxAge)
assert.equal(mergedConfig.key, requestConfig.key)
assert.equal(mergedConfig.exclude, requestConfig.exclude)
assert.equal(mergedConfig.clearOnStale, requestConfig.clearOnStale)
assert.equal(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.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)
})

it('Should not merge the disallowed keys', () => {
let requestConfig = {
limit: true,
store: 'abc',
adapter: 'whoops'
};
let mergedConfig = config.mergeRequestConfig(globalConfig, requestConfig);
assert.equal(mergedConfig.limit, globalConfig.limit);
assert.equal(mergedConfig.store, globalConfig.store);
assert.equal(mergedConfig.adapter, globalConfig.adapter);
});
}
let mergedConfig = config.mergeRequestConfig(globalConfig, requestConfig)
assert.equal(mergedConfig.limit, globalConfig.limit)
assert.equal(mergedConfig.store, globalConfig.store)
assert.equal(mergedConfig.adapter, globalConfig.adapter)
})

it('Should transform the debug key when true', () => {
let requestConfig = {
debug: true,
};
let mergedConfig = config.mergeRequestConfig(globalConfig, requestConfig);
assert.ok(typeof mergedConfig.debug === 'function');
});
});
debug: true
}
let mergedConfig = config.mergeRequestConfig(globalConfig, requestConfig)
assert.ok(typeof mergedConfig.debug === 'function')
})
})

0 comments on commit 75c0fc6

Please sign in to comment.