Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/igorbezsmertnyi/result-object
Function or method result structure serializer
https://github.com/igorbezsmertnyi/result-object
Last synced: 5 days ago
JSON representation
Function or method result structure serializer
- Host: GitHub
- URL: https://github.com/igorbezsmertnyi/result-object
- Owner: igorbezsmertnyi
- License: mit
- Created: 2022-12-14T20:02:47.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-07T10:07:39.000Z (11 months ago)
- Last Synced: 2024-10-12T08:29:58.560Z (about 1 month ago)
- Language: TypeScript
- Size: 144 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ResultObject
In JavaScript, errors and failures are typically implemented with exceptions. In some situations, however, exceptions may not be the best choice. The `ResultObject` contains a build in methods that transform the result to common structure. The `ResultObject` object are wrappers for a successful, or failed results of the functions or methods.
### Object Sturecture
```js
// SuccessОbject{
"ok": true,
"data": {
// ...some data
},
"error": undefined,
}// FailedОbject
{
"ok": false,
"data": {
// ...some data
},
"error": Error // Error object
}
```## Installation
```shell
$ npm install @ib-code/result-object
$ yarn add @ib-code/result-object
```## Usage
```ts
import { Result, ResultObject } from 'result-object'const someFunctions = (): ResultObject => {
try {
const numebr = getNumber() // example functionreturn Result.success(numebr)
} catch (error) {
return Result.failed(error, "Additional data")
}
}
```## Author
- [github/igorbezsmertnyi](https://github.com/igorbezsmertnyi)
## License
Copyright (c) 2022 Igor Bezsmertnyi, contributors. Released under the MIT license