Fp ts fp ts:过滤掉某些左侧值,并在右侧出错

Fp ts fp ts:过滤掉某些左侧值,并在右侧出错,fp-ts,Fp Ts,我希望使用fp ts忽略某些错误(如果它们发生,就意味着一切顺利,即注册过程中缺少帐户) 我有以下代码: export const handleSignup = async (server: FastifyInstance): Promise<void> => { server.post('/signup', async (req, res) => { const {email} = req.body as SignupPostData const {

我希望使用fp ts忽略某些错误(如果它们发生,就意味着一切顺利,即注册过程中缺少帐户)

我有以下代码:

export const handleSignup = async (server: FastifyInstance): Promise<void> => {
  server.post('/signup', async (req, res) => {
    const {email} = req.body as SignupPostData
    const {redirectUri} = req.query as Record<'redirectUri', string>

    return pipe(
      withDb(lookupAccountByEmail)(email),
      TE.chain(() => flow(generateMagicLinkToken, TE.fromEither)(email)),
      TE.chain(sendSignupEmail(email, redirectUri))
    )().then(foldReply<SignupApiResponse>(res))
  })
}
export const handleSignup=async(服务器:fastfyInstance):Promise=>{ server.post('/signup',异步(req,res)=>{ const{email}=req.body作为SignupPostData const{redirectUri}=req.query作为记录 回流管( 使用DB(通过电子邮件查找帐户)(电子邮件), TE.chain(()=>flow(generateMagicLinkToken,TE.fromOne)(电子邮件)), TE.chain(sendSignupEmail(email,redirectUri)) )()然后(折叠(res)) }) }
lookupAccountByEmail
函数将返回一个帐户,或返回一个错误对象。 如果返回帐户,我需要返回一个错误,代码为“account exists”。如果返回一个代码为“account not found”的错误对象,我希望一切都像没有问题一样继续。如果返回了带有任何其他代码的错误对象,则该对象仍将出错

在fp ts中处理此问题的最佳方法是什么?

您可以使用TE.fold

const doSignup = pipe(
  generateMagicLinkToken(email),
  TE.fromEither,
  TE.chain(sendSignupEmail(email, redirectUri))
)
return pipe(
  email,
  withDb(lookupAccountByEmail),
  TE.fold(
    left => left.error === 'account-not-found' ? doSignup : TE.left(left)
    right => TE.left({error: 'account-exits'})
  ),
  T.map(foldReply<SignupApiResponse>(res))
)()
const doSignup=管道(
generateMagicLinkToken(电子邮件),
或者,
TE.chain(sendSignupEmail(email,redirectUri))
)
回流管(
电子邮件,
使用DB(通过电子邮件查找帐户),
折叠(
left=>left.error===“找不到帐户”?doSignup:TE.left(左)
right=>TE.left({error:'account exits'})
),
T.map(折页(res))
)()

谢谢,这帮了大忙!对于最后一位,foldReply基本上返回一个E.fold()。我怎样才能改变它来返回一个任务(使它满足T.chain)?啊,我看到了@AlexanderMattoni的问题。你能试试T.map而不是T.chain吗?我试过T.map,不幸的是返回的折叠没有执行,我不确定为什么。下面是签名:
export const foldReply=(reply:fastfyreply,successCode?:number):((ma:E.other)=>void)=>{return E.fold(replyWithError(reply),replyWithSuccess(reply,successCode))
我认为需要在管道后面添加一个调用。我将更新上面的答案。我尽力在这里重现你的逻辑。如果我遗漏了什么,请告诉我ts node example.ts未定义:调用成功```