Skip to content

Commit

Permalink
expose set function through api
Browse files Browse the repository at this point in the history
  • Loading branch information
lcalvy committed Sep 26, 2016
1 parent 430190f commit 3330e19
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
26 changes: 24 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import createDebug from 'debug';

const debug = createDebug('squirrel');

const createAPI = store => {
const createAPI = (store, client, options = {cwd: '/'}) => {
const getBy = (index, key) => {
debug(`getBy: ${index} => ${key}`);
return store('indexes').then(getOr(null, [index, key, 'value']));
Expand All @@ -28,6 +28,12 @@ const createAPI = store => {
return store('node').then(_get(path));
};

const set = (_path, value) => {
_path = path.join(options.cwd, _path);
debug(`set => ${_path}`);
return _set(_path, value);
};

const _get = curry((_path, node) => {
if (!node) return null;
if (path.relative(node.key, _path) === '') return node;
Expand All @@ -37,10 +43,26 @@ const createAPI = store => {
}, node.nodes));
});

const _set = (_path, value) => {
return new Promise(function(resolve, reject) {
if (!value) {
return resolve(value);
}
client.set(_path, value, (err, res) => {
if (err) {
return reject(err);
}

return resolve(res);
});
});
};

return {
getBy,
getAll,
get
get,
set
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const createSquirrel = options => {
save(node$),
indexBuilder
);
const api = createAPI(store);
const api = createAPI(store, client, options);

return {
...api,
Expand Down
29 changes: 27 additions & 2 deletions src/test/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';

import createAPI from '../api';

import Etcd from './fixtures/node-etcd';
const node = {
key: '/',
dir: true,
Expand Down Expand Up @@ -85,13 +85,17 @@ const store = {
};

const getStore = key => Promise.resolve(store[key]);
const api = createAPI(getStore);
const client = new Etcd();
const api = createAPI(getStore, client);
const clientErrored = new Etcd({err: new Error('boom')});
const apiErrored = createAPI(getStore, clientErrored);

test('should create API', t => {
t.truthy(api);
t.truthy(api.get);
t.truthy(api.getBy);
t.truthy(api.getAll);
t.truthy(api.set);
});

test('should get keys of simple index', async t => {
Expand Down Expand Up @@ -174,3 +178,24 @@ test('should get null if any node matches this path', async t => {
null
);
});

test('should set nothing if value is null', async t => {
t.deepEqual(
await api.set('/nope', null),
null
);
});

test('should set value if value setted', async t => {
t.deepEqual(
await api.set('/foo', {foo: 'baz'}),
{foo: 'baz'}
);
});

test('should failed if error occured', async t => {
t.throws(
apiErrored.set('/foo', {foo: 'baz'}),
/boom/
);
});
14 changes: 14 additions & 0 deletions src/test/fixtures/node-etcd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

function Etcd(options = {}) {
this.options = options;
}

Etcd.prototype.set = function set(path, value, callback) {
if (this.options.err) {
return callback(this.options.err);
}
callback(null, value);
};

module.exports = Etcd;

0 comments on commit 3330e19

Please sign in to comment.