ok

Return success

When processing is successful

import { Result } from 'types-ddd';

return Result.ok<T, F>(/* ... resource type T */);
circle-info

T: first generic type refer to the value to return in success case.

F: second one refer to the error type to return in failure case. By default is string

The resource accepts a generic type. It can be a primary type or a more dynamic type.

return Result.ok<string>("Hello World");

It do not throw error and return an object with attributes and result status

const result = Result.ok<string>("simple string");

console.log(result)

> Result {
    isSuccess: true, 
    isFailure: false, 
    error: null, 
    _value: "simple string",
    statusCodeNumber: 200,
    statusCode: "OK"
}

The returned value must be accessed by your key to get a result

How to confirm that the return is really successful

How to return an interface

If you declared the return as an interface and returned a value other than the one defined. You will have an error

Result also accpet statusCode optionally, if you want to provide domain status for infra.

circle-info

By default

success result has status "OK" = 200

failure result has "UNPROCESSABLE_ENTITY" = 422

You also can return a void using Result.success instead Result.ok. Let's see a real use case

It is possible to define an internationalization error message

Last updated