Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Simplify fp-ts function

I have this function:

export const getProducts = makeRequest(
  /** Your query */
  `asasd*[_type == "product"] {
      _id,
      title
    }`,
  /** Optionally transform the response */
  (i) => i,
  /** Validate exact type */
  D.array(Product)
);

which is of type:

const getProducts: TaskEither<Error, {
    _id: string;
    title: string;
}[]>

This is used in a Next.js API route like this:

export default async (
  _: NextApiRequest,
  res: NextApiResponse<Array<Product>>
) => {
  const request = await getProducts();
  return pipe(
    request,
    E.fold((v) => res.status(400).end(v.message), res.status(200).json)
  );
};

It work, but how can I simplify the last function? I would like for await getProducts() to be inline in the pipe, so I don't have to do the request assignment up front.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use map from the Task module:

import * as T from 'fp-ts/Task'

export default async (
  _: NextApiRequest,
  res: NextApiResponse<Array<Product>>;
) => pipe(
  request,
  T.map(E.fold((v) => res.status(400).end(v.message), res.status(200).json))
)();

pipe(request, T.map(...))) returns a Task, so you must call it at the end so that it is executed.

If you really wanted to avoid the () at the end, you could do something like this:

const runTask = async <A>(task: T.Task<A>): Promise<A> => task();

export default async (
  _: NextApiRequest,
  res: NextApiResponse<Array<Product>>;
) => pipe(
  request,
  T.map(E.fold((v) => res.status(400).end(v.message), res.status(200).json)),
  runTask
);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda