-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(core): Result model * feat(core, commands): execute returns Result * feat(models): Builder model
- Loading branch information
Showing
10 changed files
with
161 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './result' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
/** | ||
* Any result | ||
*/ | ||
export type AnyResult = Result<unknown, unknown> | ||
|
||
/** | ||
* Result is a discriminated union type that represents either a success or a failure. | ||
*/ | ||
export class Result< | ||
TSuccess = void, | ||
TFail = string | ||
> implements Result<TSuccess, TFail> { | ||
private readonly _success: boolean; | ||
private readonly _payload: TSuccess | TFail | undefined | ||
|
||
/** | ||
* Initializes a new instance of the Result class. | ||
* @param success Indicates if the result is a success or a failure. | ||
* @param payload The payload of the result. | ||
*/ | ||
private constructor( | ||
success: boolean, | ||
payload: TSuccess | TFail | undefined, | ||
) { | ||
this._success = success | ||
this._payload = payload | ||
} | ||
|
||
/** | ||
* Creates a new result indicating a success with the specified payload. | ||
* @param payload The payload of the result. | ||
* @returns A new instance of the Result class. | ||
*/ | ||
public static ok<TSuccessPayload, TFailPayload>( | ||
payload?: TSuccessPayload | ||
): Result<TSuccessPayload, TFailPayload> { | ||
return new Result<TSuccessPayload, TFailPayload>(true, payload) | ||
} | ||
|
||
/** | ||
* Creates a new result indicating a failure with the specified payload. | ||
* @param payload The payload of the result. | ||
* @returns A new instance of the Result class. | ||
*/ | ||
public static fail<TSuccessPayload, TFailPayload>( | ||
payload?: TFailPayload | ||
): Result<TSuccessPayload, TFailPayload> { | ||
return new Result<TSuccessPayload, TFailPayload>(false, payload) | ||
} | ||
|
||
/** | ||
* Indicates if the result is a success. | ||
* @returns True if the result is a success, otherwise false. | ||
*/ | ||
get isSuccess(): boolean { | ||
return this._success | ||
} | ||
|
||
/** | ||
* Indicates if the result is a failure. | ||
* @returns True if the result is a failure, otherwise false. | ||
*/ | ||
get isFailure(): boolean { | ||
return !this._success | ||
} | ||
|
||
/** | ||
* Gets the payload of the result. | ||
* @returns The payload of the result. | ||
*/ | ||
get value(): TSuccess { | ||
return this._payload as TSuccess | ||
} | ||
|
||
/** | ||
* Gets the payload of the result. | ||
* @returns The payload of the result. | ||
*/ | ||
get error(): TFail { | ||
return this._payload as TFail | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Result } from '@lib/core' | ||
|
||
export abstract class Builder<TModel, TError> { | ||
public abstract build(): Result<TModel, TError> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './domain' | ||
export * from './commands' | ||
export * from './core' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Result } from '@lib/core' | ||
|
||
describe('Result', () => { | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* ok */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
describe('.ok()', () => { | ||
it('should create a new result indicating a success with the specified payload', () => { | ||
const result = Result.ok('success') | ||
expect(result.isSuccess).toBe(true) | ||
expect(result.isFailure).toBe(false) | ||
expect(result.value).toBe('success') | ||
}) | ||
|
||
it('should create a new result indicating a success with no payload', () => { | ||
const result = Result.ok() | ||
expect(result.isSuccess).toBe(true) | ||
expect(result.isFailure).toBe(false) | ||
expect(result.value).toBeUndefined() | ||
}) | ||
}) | ||
|
||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* fail */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
describe('.fail()', () => { | ||
it('should create a new result indicating a failure with the specified payload', () => { | ||
const result = Result.fail('failure') | ||
expect(result.isSuccess).toBe(false) | ||
expect(result.isFailure).toBe(true) | ||
expect(result.error).toBe('failure') | ||
}) | ||
|
||
it('should create a new result indicating a failure with no payload', () => { | ||
const result = Result.fail() | ||
expect(result.isSuccess).toBe(false) | ||
expect(result.isFailure).toBe(true) | ||
expect(result.error).toBeUndefined() | ||
}) | ||
}) | ||
}) |