Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit 8f06d97

Browse files
committed
work over default options management
I've adjusted the hooks to always consider the options from feathers config as well. The unit test got an empty app dummy because the hooks now always require a valid app.
1 parent a4a6e31 commit 8f06d97

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

src/hooks/cache.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
/**
22
* After hook - generates a cache object that is needed
33
* for the redis hook and the express middelware.
4-
* @todo add default value in config file
54
*/
6-
const defaults = {};
5+
const defaults = {
6+
defaultDuration: 3600 * 24
7+
};
78

89
export function cache(options) { // eslint-disable-line no-unused-vars
9-
options = Object.assign({}, defaults, options);
10-
1110
return function (hook) {
11+
const cacheOptions = hook.app.get('redisCache');
12+
13+
options = Object.assign({}, defaults, cacheOptions, options);
14+
1215
if (!hook.result.hasOwnProperty('cache')) {
1316
let cache = {};
1417

@@ -21,7 +24,7 @@ export function cache(options) { // eslint-disable-line no-unused-vars
2124

2225
cache = Object.assign({}, cache, {
2326
cached: false,
24-
duration: options.duration || 3600 * 24
27+
duration: options.duration || options.defaultDuration
2528
});
2629

2730
hook.result.cache = cache;
@@ -31,8 +34,6 @@ export function cache(options) { // eslint-disable-line no-unused-vars
3134
};
3235

3336
export function removeCacheInformation(options) { // eslint-disable-line no-unused-vars
34-
options = Object.assign({}, defaults, options);
35-
3637
return function (hook) {
3738
if (hook.result.hasOwnProperty('cache')) {
3839
delete hook.result.cache;

src/hooks/redis.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import moment from 'moment';
22
import chalk from 'chalk';
33
import { parsePath } from './helpers/path';
44

5-
const defaults = {};
5+
const defaults = {
6+
env: 'production',
7+
defaultDuration: 3600 * 24
8+
};
69

710
export function before(options) { // eslint-disable-line no-unused-vars
8-
options = Object.assign({}, defaults, options);
9-
1011
return function (hook) {
12+
const cacheOptions = hook.app.get('redisCache');
13+
14+
options = Object.assign({}, defaults, cacheOptions, options);
15+
1116
return new Promise(resolve => {
1217
const client = hook.app.get('redisClient');
13-
const cacheOptions = hook.app.get('redisCache');
14-
const env = cacheOptions.env || 'production';
15-
const path = parsePath(hook, cacheOptions);
18+
const path = parsePath(hook, options);
1619

1720
client.get(path, (err, reply) => {
1821
if (err !== null) resolve(hook);
@@ -24,7 +27,7 @@ export function before(options) { // eslint-disable-line no-unused-vars
2427
resolve(hook);
2528

2629
/* istanbul ignore next */
27-
if (env !== 'test') {
30+
if (options.env !== 'test') {
2831
console.log(`${chalk.cyan('[redis]')} returning cached value for ${chalk.green(path)}.`);
2932
console.log(`> Expires on ${duration}.`);
3033
}
@@ -37,17 +40,16 @@ export function before(options) { // eslint-disable-line no-unused-vars
3740
};
3841

3942
export function after(options) { // eslint-disable-line no-unused-vars
40-
options = Object.assign({}, defaults, options);
41-
4243
return function (hook) {
44+
const cacheOptions = hook.app.get('redisCache');
45+
46+
options = Object.assign({}, defaults, cacheOptions, options);
47+
4348
return new Promise(resolve => {
4449
if (!hook.result.cache.cached) {
45-
const cacheOptions = hook.app.get('redisCache');
46-
const env = cacheOptions.env || 'production';
47-
const cachingDefault = cacheOptions.defaultDuration ? cacheOptions.defaultDuration : 3600 * 24;
48-
const duration = hook.result.cache.duration || cachingDefault;
50+
const duration = hook.result.cache.duration || options.defaultDuration;
4951
const client = hook.app.get('redisClient');
50-
const path = parsePath(hook, cacheOptions);
52+
const path = parsePath(hook, options);
5153

5254
// adding a cache object
5355
Object.assign(hook.result.cache, {
@@ -66,7 +68,7 @@ export function after(options) { // eslint-disable-line no-unused-vars
6668
}
6769

6870
/* istanbul ignore next */
69-
if (env !== 'test') {
71+
if (options.env !== 'test') {
7072
console.log(`${chalk.cyan('[redis]')} added ${chalk.green(path)} to the cache.`);
7173
console.log(`> Expires in ${moment.duration(duration, 'seconds').humanize()}.`);
7274
}

test/cache.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { expect } from 'chai';
22
import { hookCache } from '../src';
33

4+
const app = {
5+
get: () => ({})
6+
};
7+
48
describe('Cache Hook', () => {
59
it('adds a cache object', () => {
610
const hook = hookCache();
711
const mock = {
12+
app: app,
813
params: { query: ''},
914
path: 'test-route',
1015
result: {
@@ -29,6 +34,7 @@ describe('Cache Hook', () => {
2934
it('does not modify the existing cache object', () => {
3035
const hook = hookCache();
3136
const mock = {
37+
app: app,
3238
params: { query: ''},
3339
path: 'test-route',
3440
result: {
@@ -52,6 +58,7 @@ describe('Cache Hook', () => {
5258
it('wraps arrays', () => {
5359
const hook = hookCache();
5460
const mock = {
61+
app: app,
5562
params: { query: ''},
5663
path: 'test-route-array',
5764
result: [

0 commit comments

Comments
 (0)