xen-service
Advanced tools
Comparing version 0.1.5 to 0.1.6
{ | ||
"name": "xen-service", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Simple and flexible microservice library", | ||
@@ -5,0 +5,0 @@ "main": "source/XenService.js", |
const { isFSA } = require('flux-standard-action'); | ||
const { toObservable, toResponseAction } = require('./helpers.js'); | ||
const { | ||
toObservable, | ||
toResponseAction, | ||
toErrorAction | ||
} = require('./helpers.js'); | ||
@@ -22,9 +26,16 @@ function defaultDispatch(state) { | ||
return toObservable( | ||
state.procedures[action.type]( | ||
action, | ||
defaultDispatch(state), | ||
state.models | ||
) | ||
).map(toResponseAction(action)); | ||
return toObservable(state.procedures[action.type]) | ||
.flatMap(procedure => toObservable( | ||
procedure( | ||
action, | ||
defaultDispatch(state), | ||
state.models | ||
) | ||
)) | ||
.map(toResponseAction(action)) | ||
.catch(error => toObservable( | ||
toErrorAction(action)({ | ||
message: error.message | ||
}) | ||
)); | ||
}; | ||
@@ -31,0 +42,0 @@ } |
@@ -29,2 +29,14 @@ const { Observable } = require('rxjs/Observable'); | ||
const toErrorAction = requestAction => anything => { | ||
return Object.assign({}, requestAction, { | ||
meta: Object.assign({}, requestAction.meta, { | ||
protocol: 'response' | ||
}), | ||
error: true, | ||
payload: isFSA(anything) | ||
? anything.payload | ||
: anything | ||
}); | ||
}; | ||
function fetchAction(endpoint, action) { | ||
@@ -37,7 +49,3 @@ const responsePromise = fetch(endpoint, { | ||
.then(response => response.json()) | ||
.catch(() => { | ||
/* eslint no-debugger: 1 */ | ||
// !TODO: add call toErrorAction | ||
debugger; | ||
}); | ||
.catch(error => toErrorAction(action)({ message: error.message })); | ||
return toObservable(responsePromise); | ||
@@ -55,2 +63,3 @@ } | ||
toResponseAction, | ||
toErrorAction, | ||
toObservable, | ||
@@ -57,0 +66,0 @@ fetchAction, |
@@ -5,2 +5,3 @@ const { isFSA } = require('flux-standard-action'); | ||
toResponseAction, | ||
toErrorAction, | ||
fetchAction: fetchActionHelper | ||
@@ -100,9 +101,16 @@ } = require('../source/helpers.js'); | ||
// Execute local procedures | ||
return toObservable( | ||
state.procedures[action.type]( | ||
action, | ||
interserviceDispatchWithScopedState, | ||
state.models | ||
) | ||
).map(toResponseAction(action)); | ||
return toObservable(state.procedures[action.type]) | ||
.flatMap(procedure => toObservable( | ||
procedure( | ||
action, | ||
interserviceDispatchWithScopedState, | ||
state.models | ||
) | ||
)) | ||
.map(toResponseAction(action)) | ||
.catch(error => toObservable( | ||
toErrorAction(action)({ | ||
message: error.message | ||
}) | ||
)); | ||
} else { | ||
@@ -109,0 +117,0 @@ if ( |
@@ -17,1 +17,2 @@ require('rxjs/add/observable/combineLatest'); | ||
require('rxjs/add/operator/zip'); | ||
require('rxjs/add/operator/catch'); |
@@ -64,2 +64,18 @@ /* eslint-env mocha */ | ||
it('should respond to errors with an error action', done => { | ||
const dispatch = defaultDispatch({ | ||
procedures: { | ||
test: () => { throw new Error('Headcrab'); } | ||
}, | ||
models: {} | ||
}); | ||
dispatch({ type: 'test' }).subscribe(responseAction => { | ||
expect(isFSA(responseAction)).to.be.true; | ||
expect(responseAction.error).to.be.true; | ||
expect(responseAction.payload).to.eql({ message: 'Headcrab' }); | ||
expect(responseAction.meta.protocol).to.equal('response'); | ||
done(); | ||
}); | ||
}); | ||
it('should wrap procedure result in Observable', () => { | ||
@@ -66,0 +82,0 @@ const dispatch = defaultDispatch({ |
@@ -5,3 +5,4 @@ /* eslint-env mocha */ | ||
xdescribe('toResponseAction', () => {}); | ||
xdescribe('toErrorAction', () => {}); | ||
xdescribe('fetchAction', () => {}); | ||
xdescribe('devLog', () => {}); |
@@ -146,2 +146,19 @@ /* eslint-env mocha */ | ||
it('should respond to error with an error action', done => { | ||
const dispatch = interserviceDispatchWithDispatchMap({ | ||
name: 'TestService', | ||
procedures: { | ||
test: () => { throw new Error('Zombine'); } | ||
}, | ||
models: {} | ||
}, testDispatchMap); | ||
dispatch({ type: 'test' }, true).subscribe(responseAction => { | ||
expect(isFSA(responseAction)).to.be.true; | ||
expect(responseAction.error).to.be.true; | ||
expect(responseAction.payload).to.eql({ message: 'Zombine' }); | ||
expect(responseAction.meta.protocol).to.equal('response'); | ||
done(); | ||
}); | ||
}); | ||
it('should wrap procedure result in Observable', () => { | ||
@@ -148,0 +165,0 @@ const dispatch = interserviceDispatchWithDispatchMap({ |
58751
1693