Skip to content

Commit

Permalink
Merge pull request #65 from CoorpAcademy/expose-api-set-rework
Browse files Browse the repository at this point in the history
Expose api set rework
  • Loading branch information
Loïc Calvy committed Sep 26, 2016
2 parents 3330e19 + e7194ea commit 8d5a358
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 47 deletions.
22 changes: 3 additions & 19 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
startsWith,
curry
} from 'lodash/fp';
import {set$} from './util/etcd';
import createDebug from 'debug';

const debug = createDebug('squirrel');

const createAPI = (store, client, options = {cwd: '/'}) => {
const createAPI = (store, client) => {
const getBy = (index, key) => {
debug(`getBy: ${index} => ${key}`);
return store('indexes').then(getOr(null, [index, key, 'value']));
Expand All @@ -29,9 +30,7 @@ const createAPI = (store, client, options = {cwd: '/'}) => {
};

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

const _get = curry((_path, node) => {
Expand All @@ -43,21 +42,6 @@ const createAPI = (store, client, options = {cwd: '/'}) => {
}, 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,
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, client, options);
const api = createAPI(store, client);

return {
...api,
Expand Down
30 changes: 17 additions & 13 deletions src/test/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import createEtcdMock from '../util/test/helpers/etcd';

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

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

test('should create API', t => {
t.truthy(api);
Expand Down Expand Up @@ -179,23 +176,30 @@ test('should get null if any node matches this path', async t => {
);
});

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 => {
const client = createEtcdMock({
set: [{
assert: (key, value) => {
t.deepEqual(key, '/foo');
t.deepEqual(value, {foo: 'baz'});
},
values: [null, {foo: 'baz'}, null]
}]
});
const api = createAPI(getStore, client);
t.deepEqual(
await api.set('/foo', {foo: 'baz'}),
{foo: 'baz'}
);
});

test('should failed if error occured', async t => {
const client = createEtcdMock({
set: [[new Error('boom'), null, null]]
});
const api = createAPI(getStore, client);
t.throws(
apiErrored.set('/foo', {foo: 'baz'}),
api.set('/foo', {foo: 'baz'}),
/boom/
);
});
14 changes: 0 additions & 14 deletions src/test/fixtures/node-etcd.js

This file was deleted.

0 comments on commit 8d5a358

Please sign in to comment.