diff --git a/example/update.js b/example/update.js new file mode 100644 index 00000000..1d9e2902 --- /dev/null +++ b/example/update.js @@ -0,0 +1,17 @@ +// file: example/update.js + +process.env.DEBUG = 'node-vault'; // switch on debug mode + +const vault = require('./../src/index')(); + +vault.update('secret/hello', { value: 'world', lease: '1s' }) + .then(() => vault.read('secret/hello')) + .catch((err) => console.error(err.message)); + +vault.update('secret/hello', { value: 'everyone', lease: '1s' }) + .then(() => vault.read('secret/hello')) + .catch((err) => console.error(err.message)); + +vault.update('secret/hello', { value: { 'foo': 'bar', 'world': 'everyone' } }) + .then(() => vault.read('secret/hello')) + .catch((err) => console.error(err.message)); diff --git a/index.d.ts b/index.d.ts index 8385001c..981580b9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -47,6 +47,7 @@ declare namespace NodeVault { read(path: string, requestOptions?: Option): Promise; list(path: string, requestOptions?: Option): Promise; delete(path: string, requestOptions?: Option): Promise; + update(path: string, data: any, requestOptions?: Option): Promise; generateFunction(name: string, conf: functionConf): void; diff --git a/package.json b/package.json index 57907e07..2d0d1ada 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "Sean Gallavan (https://github.com/seangallavan)", "seyfert (https://github.com/seyfert)", "Simon Boulet (https://github.com/siboulet)", + "skeletonz28 (https://github.com/skeletonz28)", "Tim Robinson (https://github.com/timjrobinson)", "Tom Vogel (https://github.com/tavogel)", "Trevor Robinson (https://github.com/trevorr)", diff --git a/src/index.js b/src/index.js index faccfead..d868d408 100644 --- a/src/index.js +++ b/src/index.js @@ -127,6 +127,16 @@ module.exports = (config = {}) => { return client.request(options); }; + client.update = (path, data, requestOptions) => { + debug('update %o to %s', data, path); + const options = { ...config.requestOptions, ...requestOptions }; + options.path = `/${path}`; + options.json = data; + options.method = 'PATCH'; + options.headers = { 'Content-Type': 'application/merge-patch+json' }; + return client.request(options); + }; + client.read = (path, requestOptions) => { debug(`read ${path}`); const options = { ...config.requestOptions, ...requestOptions }; diff --git a/test/unit.js b/test/unit.js index 51e07ef1..2ac9525b 100644 --- a/test/unit.js +++ b/test/unit.js @@ -217,6 +217,36 @@ describe('node-vault', () => { }); }); + describe('update(path, data, options)', () => { + it('should update data to path', (done) => { + const path = 'secret/hello'; + const data = { + value: 'everyone', + }; + const params = { + method: 'PATCH', + uri: getURI(path), + }; + vault.update(path, data) + .then(assertRequest(request, params, done)) + .catch(done); + }); + + it('should handle undefined options', (done) => { + const path = 'secret/hello'; + const data = { + value: 'everyone', + }; + const params = { + method: 'PATCH', + uri: getURI(path), + }; + vault.update(path, data) + .then(assertRequest(request, params, done)) + .catch(done); + }); + }); + describe('read(path, options)', () => { it('should read data from path', (done) => { const path = 'secret/hello';