sufa
v1.0.7
Published
Result
Downloads
22
Maintainers
Readme
Result
Result model with model factory and match on success & on failure callbacks.
Features
- Data in success;
- Error in failure;
- Match with onSuccess & onFailure callbacks;
- Factory.
Usage
Prepare
Create model factory
import { ResultModelFactory, ResultModel } from 'sufa';
const resultModelFactory = new ResultModelFactory({
Model: ResultModel
});
Import types
/**
* @import { IResultModelFactory, IResultModel } from 'sufa'
*/
Commands
Result factory
Ok
const okResult = resultModelFactory.ok();
const okDataResult = resultModelFactory.ok('data');
Fail
const error = new Error('Message');
result.fail(error);
Result
isSuccess
const isSuccess = result.success;
isFailure
const isFailure = result.failure;
getData
const data = result.getData();
getError
const error = result.getError();
match
result.match({
onSuccess: () => {},
onFailure: () => {}
});