FP-TSを利用した簡単サインアップ機能
7776 ワード
FP-TSを使って簡単なユーザー登録機能を作ってみました.
呼び出し方:
関数を構成するためのより良い方法があるに違いありません.
// user-controller.ts
import argon2 from "argon2";
import * as E from "fp-ts/Either";
import { flow, pipe } from "fp-ts/lib/function";
import * as TE from "fp-ts/TaskEither";
import {
UserProfile,
UserProfileRepository,
} from "../db/entities/user_profile";
function hashPassword(plainPassword: string) {
return () => argon2.hash(plainPassword).then(E.right).catch(E.left);
}
export function signup(userName: string, email: string, password: string) {
const makeUserProfile = (userPassword: string) =>
new UserProfile({ userName, email, userPassword });
return pipe(
password,
hashPassword,
TE.chain(flow(makeUserProfile, UserProfileRepository.insertProfile))
)();
}
呼び出し方:
// user-router.ts
import express from "express";
import { pipe } from "fp-ts/lib/function";
import { signup } from "../controllers/user-controller";
import * as E from "fp-ts/lib/Either";
router.post("/signup", async (req, res) => {
const { username, password } = req.body;
const userE = await signup(username, username, password);
pipe(
userE,
E.bimap(
(e) => res.send({ ok: false, message: e.message }),
(user) =>
res.send({
ok: true,
message: `user ${user.userName} registered `,
})
)
);
});
関数を構成するためのより良い方法があるに違いありません.
Reference
この問題について(FP-TSを利用した簡単サインアップ機能), 我々は、より多くの情報をここで見つけました https://dev.to/tomliang/simple-signup-function-using-fp-ts-193jテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol