flux-standard-action-json
v1.1.0
Published
Flux standard action JSON serialization
Downloads
28
Maintainers
Readme
Flux Standard Action JSON serialization
Flux Standard Action JSON serialization utilities.
npm install --save flux-standard-action-json
Usage
import fsaJSON from 'flux-standard-action-json';
stringify(action, ?options)
Returns a JSON string if action
is FSA compliant. Otherwise, throws an error.
NOTE: Symbol
action types should have a key. For example:
fsaJSON.stringify({type: Symbol()});
// throws an error
fsaJSON.stringify({type: Symbol('ACTION_TYPE')});
// returns '{"type":"Symbol(ACTION_TYPE)"}'
?options
error
: (Object) Error serialization options. Applies only ifpayload
is anError
object. We depend on Errio so the same options apply.
Example:
fsaJSON.stringify({
type: 'ACTION_TYPE',
error: true,
new Error('Where?'))
}, {error: {stack: true}});
// returns '{"type":"ACTION_TYPE","error":true,"payload":{"name":"Error","message":"Where?","stack":"<a full error stack>"}}'
parse(json, ?options)
Returns an FSA compliant action parsed from a JSON string,
parsing Symbol
type and Error
payload where applies.
Examples:
fsaJSON.parse('{}');
// throws an error
fsaJSON.parse('{"type":"Symbol(ACTION_TYPE)"}');
// returns {type: Symbol.for('ACTION_TYPE')}
fsaJSON.parse('{"type":"TYPE","error":true,"payload":"Invalid something"}');
// returns {type: 'TYPE', error: true, payload: new Error('Invalid something')}
NOTE: Symbol
action types should have a key. For example:
fsaJSON.parse('{"type":"Symbol()"}');
// throws an error
?options
error
: (Object) Error deserialization options. Applies only ifpayload
is anError
object. We depend on Errio so the same options apply.