Skip to content

Instantly share code, notes, and snippets.

@josete89
Created September 4, 2018 14:27
Show Gist options
  • Save josete89/57a7ef94f11b270c551944328a3694a5 to your computer and use it in GitHub Desktop.
Save josete89/57a7ef94f11b270c551944328a3694a5 to your computer and use it in GitHub Desktop.
Free monad for http request
class GetParam<A>{
readonly _tag: 'GetParam' = 'GetParam';
readonly _A!: A;
readonly _URI!: RequestLifeCycleFURI;
constructor(readonly url: string,readonly more: (param:either.Either<Error,string>)=> A){ }
}
class Validate<A> {
readonly _tag: 'Validate' = 'Validate';
readonly _A!: A;
readonly _URI!: RequestLifeCycleFURI;
constructor(readonly result: either.Either<Error,string>,readonly more: (param:either.Either<Error,string>)=> A){ }
}
class DomainLogic<A> {
readonly _tag: 'DomainLogic' = 'DomainLogic';
readonly _A!: A;
readonly _URI!: RequestLifeCycleFURI;
constructor(readonly logic: any,readonly result:either.Either<Error,string>,readonly more: (param:A)=> A){ }
}
class SendResponse<A>{
readonly _tag: 'SendResponse' = 'SendResponse';
readonly _A!: A;
readonly _URI!: RequestLifeCycleFURI;
constructor(readonly result: either.Either<Error,string>,readonly more: A){ }
}
export type RequestLifeCycle<A> = GetParam<A> | Validate<A> | DomainLogic<A> | SendResponse<A>
const getParam = (message: string) => fp.free.liftF(new GetParam(message,a => a))
const validate = (res:either.Either<Error,string>)=> fp.free.liftF(new Validate(res,a => a))
const execDomainLogic = (logic:any) => (result:either.Either<Error,string>) => fp.free.liftF(new DomainLogic(logic,result,a => a))
const sendResponse = (res:either.Either<Error,string>) => fp.free.liftF(new SendResponse(res,a => a))
const program = getParam("http://hack.com/tst?param=1")
.chain(validate)
.chain(execDomainLogic( (x) => { return x } ))
.chain(sendResponse)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment