Simple implementation of the Operation Result pattern
npm install @sefr/result- What does it do ?
- Compatibility
- Dependencies
- Installation
- How to use
- Incorrect usages
- Credits
- License
Provide a simple implementation of the Operation Result Pattern for TypeScript to avoid boilerplate code.
| TypeScript | EcmaScript |
|------------|------------|
| \>= 2.8.0 | \>= ES2015 |
This package is dependencies-free.
Nothing more than :
``shell`
npm i -S @sefr/result
ā Returning a success without content :
`typescript
import { Result } from "@sefr/result";
function doSomething(...args: Array
return Result.ok();
}
const toto: Result
if (toto.isFailure) {
// do something...
}
`
ā Returning a success with a string content :
`typescript
import { Result } from "@sefr/result";
function doSomething(...args: Array
return Result.ok("Operation successful !");
}
const toto: Result
if (toto.isFailure) {
// do something...
} else {
const titi = toto.value; // "Operation successful"
}
`
ā Returning a failure with some custom error :
`typescript
import { Result } from "@sefr/result";
class SomeCustomError extends Error {
constructor(public readonly someMoreInformation: Record
super(message);
}
}
function doSomething(...args: Array
return Result.failure(new SomeCustomError(
{ id: "5", someMoreInfo: "stuff & co..." },
"Oops! Something went wrong !"
));
}
const toto: Result
if (toto.isFailure) {
const titi = toto.value; // SomeCustomError
// do something...
}
`
ā Returning an error as successful operation is not allowed, TypeScript will not allow it :
`typescript
import { Result } from "@sefr/result";
function doSomething(...args: Array
return Result.ok(new Error("Operation successfull !"));
}
`
ā Returning a failure without any reason (understand Error), TypeScript will also fail on build :
`typescript
import { Result } from "@sefr/result";
function doSomething(...args: Array
return Result.failure();
}
``
+ Developed with the awesome TypeScript
+ Tested with Mocha and Chai
+ Code style enforced with ESLint and TypeScript-ESLint
This software is available under the MIT License. See the LICENSE file for more informations.
ā¬ļø Go back to top