努力使用Ramda.js进行重构,这是我走了多远

时间:2018-12-22 14:12:27

标签: javascript functional-programming ramda.js

我正在学习Ramda.js。我尝试使用Ramda重构的功能如下。它调用函数firestore数据库方法来获取一些数据。但是firestore在snapshot方法内返回了数据,我们需要调用.data()。根据结果​​,我想构造不同的响应。

很乐意在这里进行您的思考过程

const queryForUsersQuizResults = async (request, response) => {
  try {
    const snapshot = await firestore
      .collection("quizResults")
      .doc(request.user.uid)
      .collection("courses_chapters")
      .doc(request.params.courseId + "_" + request.params.chapterId)
      .get();

    let data = snapshot.data();
    if (!data) {
      data = {
        message: "result not found for this user for this course and chapter"
      };
    }

    return response.send(data);
  } catch (error) {
    return response.status(500).send(error);
  }
}

...这是我已经能够重构的东西,很想看看这样做的更好/其他方式(我不确定这是否可行)。我正在努力使用sendResult方法。

//get user id
export const getUserId = pathOr('', ['user', 'uid']);

// get chapter id
export const getChapterId = concat(pathOr('', ['params', 'courseId']), '_', pathOr('', ['params', 'chapterId']));

//queryQuizResult
export const query = curry(async (firestore, request) => {
  return tryCatch(() =>
    firestore
    .collection("quizResults")
    .doc(getUserId(request))
    .collection("courses_chapters")
    .doc(getChapterId(request))
    .get(), F)();
});

//Receives "response" object and calls response.status with the value passed to the new status function
export const status = invoker(1, "status");

//Receives "response" object and calls response.send witht he value passed to the new send function
export const send = invoker(1, "send");

//Create {"message",Your_Error} object
export const constructError = objOf('message');

//Returns JSON from Firestore's snapshot object
export const getDataFromSnapshot = (snapshot) => snapshot.data();

//Actual error message
const QUIZ_RESULTS_NOT_FOUND = "Quiz results not found for this user for this course and chapter";

//Returns error message
export const quizResultsNotFoundError = constructError(QUIZ_RESULTS_NOT_FOUND);

//Receives "response" object and calls response.status and then respose.send
export const sendError = pipe(
  status(401),
  send(quizResultsNotFoundError)
);
//Sends valid result  //IS there any better way of doing this?
export const sendResult = (snapshot, response) => {
  const data = getDataFromSnapshot(snapshot);
  response.send(data); //Especially this, I wanted to use the "send" method and pipe things
}

//Main Method
export const  queryForUsersQuizResults = async (firestore, request, response) => {
 const snapshot = await query(firestore, request);
 snapshot ? sendResult(snapshot, response) :sendError(response);
}

2 个答案:

答案 0 :(得分:2)

这是我看到的运动部件。这只是编写程序的多种可能方式之一–

const tryCatch = f =>
  new Promise
    ( (resolve, reject) =>
        { try { resolve (f ()) }
          catch (err) { reject (err) }
        }
    )

const handleSuccess = res => data =>
  res .send (data)

const handleError = res => err =>
  res .status (500) .send (err)

const getSnapshot = (uid, docid) =>
  firestore
    .collection ("quizResults")
    .doc (uid)
    .collection ("courses_chapters")
    .doc (docid)
    .get ()
    .data ()

const queryForUsersQuizResults = (req, res) =>
  tryCatch
    ( () =>
        getSnapshot
          ( req.user.uid
          , req.params.courseId + "_" + req.params.chapterId
          )
    )
    .catch
      ( _ => Promise .reject (Error ("result not found for this user for this course and chapter"))
      )

const main = (req, res) =>
  queryForUsersQuizResults (req, res)
    .then
      ( handleSuccess (res)
      , handleError (res)
      )

这是一个工作示例,展示了实际的概念-

const DB =
  { 1: "ant"
  , 2: "bear"
  , 3: "cuttlefish"
  }

const queryForUsersQuizResults = (id = 0) =>
{ if (DB[id] === undefined)
    throw Error (`no record for id: ${id}`)
  else
    return DB[id]
}

const tryCatch = f =>
{ try
  { return Promise .resolve (f ()) }
  catch (err)
  { return Promise .reject (err) }
}

const fakeResponse = (status = 200) =>
  ({ send: data =>
       console .log ("=>", status, data)
   , status: n =>
       fakeResponse (n)
  })

const demo = (req, res) =>
  tryCatch (() => queryForUsersQuizResults (req.params.uid))
    .then
      ( data => res .send (data)
      , err => res .status (500) .send (err)
      )

demo
  ( { params: { uid: 1 } }
  , fakeResponse ()
  ) // => 200 ant

demo
  ( { params: { uid: 2 } }
  , fakeResponse ()
  ) // => 200 bear

demo
  ( { params: { uid: 3 } }
  , fakeResponse ()
  ) // => 200 cuttlefish

demo
  ( { params: { uid: 4 } }
  , fakeResponse ()
  ) // => 500 Error: no record for id: 4

答案 1 :(得分:1)

我也认为您已经拥有的东西很好,如果您真的想使用合成,可能是这样的:

export const sendResult = (snapshot, response) =>
  pipe(getDataFromSnapshot, flip(send)(response))
    (snapshot);

您需要使用flip,因为(在管道定义时)尚没有getDataFromSnapshot(snapshot)的结果,但是您已经有了保存{{ 1}}方法。