Skip to content

Commit 990b505

Browse files
feature: this commit introduces result pattern
1 parent 09303fd commit 990b505

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1+
import { AxiosInstance } from "axios";
2+
import { Result } from "../patterns/result";
3+
import { Error } from "../patterns/error";
4+
15
import { ProfileCreationScheme } from "../transport/profiles/profile-creation-scheme";
6+
import { ProfileScheme } from "../transport/profiles/profile-scheme";
27

38
export class ProfilesClient {
4-
public static async createOwner(parameters: ProfileCreationScheme): Promise<void> {
9+
private readonly httpClient: AxiosInstance;
10+
11+
public constructor(httpClient: AxiosInstance) {
12+
this.httpClient = httpClient;
13+
}
14+
15+
public async createOwner(parameters: ProfileCreationScheme): Promise<Result<ProfileScheme>>
16+
{
17+
const response = await this.httpClient.post("/api/v1/profiles/owner", parameters);
18+
if (response.status < 200 || response.status >= 300) {
19+
return Result.failure(Error.from(response.data.code, response.data.description));
20+
}
21+
22+
return Result.success(response.data);
523
}
624

7-
public static async createCustomer(parameters: ProfileCreationScheme): Promise<void> {
25+
public async createCustomer(parameters: ProfileCreationScheme): Promise<Result<void>>
26+
{
27+
const response = await this.httpClient.post("/api/v1/profiles/owner", parameters);
28+
if (response.status < 200 || response.status >= 300) {
29+
return Result.failure(Error.from(response.data.code, response.data.description));
30+
}
31+
32+
return Result.success(response.data);
833
}
934
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export class Error {
2+
public constructor(public code: string, public description: string) { }
3+
4+
public static readonly None = new Error("", "");
5+
public static readonly Unknown = new Error("Unknown", "An unknown error has occurred.");
6+
7+
public static from(code: string, description: string): Error {
8+
return new Error(code, description);
9+
}
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Error } from "../patterns/error";
2+
3+
export class Result<TData = void> {
4+
public readonly isSuccess: boolean;
5+
public readonly isFailure: boolean;
6+
7+
public readonly error: Error;
8+
public readonly data?: TData;
9+
10+
private constructor(isSuccess: boolean, data?: TData, error: Error = Error.None) {
11+
this.isSuccess = isSuccess;
12+
this.isFailure = !isSuccess;
13+
14+
this.error = error;
15+
this.data = data;
16+
}
17+
18+
public static success<TData>(data: TData): Result<TData> {
19+
return new Result<TData>(true, data, Error.None);
20+
}
21+
22+
public static failure<TData>(error: Error): Result<TData> {
23+
return new Result<TData>(false, undefined, error);
24+
}
25+
}

0 commit comments

Comments
 (0)