4lessandrodev / rich-domain

A lib to help you create a robust project based on domain driven-design (ddd) principles with typescript and zero dependencies.
https://www.npmjs.com/package/rich-domain
MIT License
122 stars 5 forks source link

feat: added adapter custom err type (#282) #73

Closed 4lessandrodev closed 1 year ago

4lessandrodev commented 1 year ago

https://github.com/4lessandrodev/types-ddd/issues/282

Example implementation


    type In = { a: number };
    type Out = { b: string };
    type Err = { err: string; stack?: string };

    class CustomAdapter implements IAdapter<In, Out, Err>{
        build(target: In): IResult<Out, Err> {
            if (typeof target.a !== 'number') return Fail({ err: 'target.a is not a number' });
            return Ok({ b: target.a.toString() });
        }
    }

    const adapter = new CustomAdapter();

Example payload


    const result = adapter.build({ a: null as any });
    expect(result.isFail()).toBeTruthy();
    expect(result.error()).toEqual({ err: 'target.a is not a number' });