Skip to content

Commit d2e8d75

Browse files
author
Michael A. Plikk
committed
Add support for modifying data with a callback function
1 parent 2721844 commit d2e8d75

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module.exports = {
99
const data = {version: options.version};
1010
options.pre = options.pre || [];
1111

12+
const cb = typeof options.callback === 'function' ? options.callback : (data) => data;
13+
1214
if (!options.revisionFile) {
1315
return done(data);
1416
}
@@ -30,7 +32,7 @@ module.exports = {
3032
config: {
3133
auth: false,
3234
handler() {
33-
return data;
35+
return cb(data);
3436
},
3537
pre: options.pre,
3638
description: 'Show status',

test/index.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,34 @@ describe('Status Route Plugin', () => {
3131
res.statusCode.should.equal(200);
3232
res.result.should.have.property('version', '1.0.0');
3333
});
34+
35+
it('should support callback to modify data', async () => {
36+
const srv = new Hapi.Server({debug: {request: '*'}});
37+
await srv.register({
38+
plugin,
39+
options: {
40+
version: '1.0.0',
41+
callback: (data) => {
42+
data.count = n;
43+
return data;
44+
}
45+
}
46+
});
47+
let n = 1;
48+
const options = {
49+
method: 'GET',
50+
url: '/status'
51+
};
52+
53+
let res = await srv.inject(options);
54+
res.statusCode.should.equal(200);
55+
res.result.should.have.property('version', '1.0.0');
56+
res.result.should.have.property('count', 1);
57+
58+
n = 2;
59+
res = await srv.inject(options);
60+
res.statusCode.should.equal(200);
61+
res.result.should.have.property('version', '1.0.0');
62+
res.result.should.have.property('count', 2);
63+
});
3464
});

0 commit comments

Comments
 (0)