File tree Expand file tree Collapse file tree
Artifacts/Comanda.Internal.Sdk/source Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { AxiosInstance } from "axios" ;
2+ import { Result } from "../patterns/result" ;
3+ import { Error } from "../patterns/error" ;
4+
15import { ProfileCreationScheme } from "../transport/profiles/profile-creation-scheme" ;
6+ import { ProfileScheme } from "../transport/profiles/profile-scheme" ;
27
38export 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments