javascript - แปลงหรือกรอง `Task` จาก `Some` เป็น `Left` ด้วย `fp-ts`
ฉันพยายามจะเรียนรู้วิธีการใช้ type guards และฟังก์ชันที่คาดการณ์ได้กับ io-ts | fp-ts และสิ่งที่ฉันต้องทำคือดังต่อไปนี้:
ฉันมีฟังก์ชันนี้:
createOne: ({ password, ...creatableUser }: CreatableUser): taskEither.TaskEither<ExceptionError, User> => {
....
}
และมันจะต้อง:
- ตรวจสอบว่าที่อยู่อีเมลของผู้ใช้ยังไม่มีอยู่หรือไม่
- สร้างผู้ใช้ด้วยข้อมูลที่ให้มา
- ส่งอีเมลไปยังผู้ใช้ใหม่ ดังนั้นฉันต้องการอะไรบางอย่างเช่น:
// * Doubt Function
export const createOne = ({ password, ...creatableUserData }: CreatableUser): taskEither.TaskEither<Error, User> => {
// Generate the new user
const newUser: User = {
...creatableUserData,
id: randomUUID(),
};
return pipe(
// * Check that the user doesn't exists yet
creatableUserData.email,
findByEmail,
taskEither.fromTaskOption(() => new Error('User already exists')), // TODO: need to return a error on `Some`
// * Save the new `User` on the repository
taskEither.chain(() => save(newUser, password)),
// * Send the confirmation email to the user
taskEither.chain(() =>
sendMail({
body: 'Welcome to App Team',
}),
),
taskEither.map(() => newUser),
);
};
ฉันจำเป็นต้อง 'แยกวิเคราะห์' คำตอบจาก taskOption.Some เป็น taskEither.Left แต่ฉันไม่พบวิธีที่จะทำได้
นี่คือประเภทต่าง ๆ เพื่อบริบทที่ดีกว่า:
import { randomUUID } from 'crypto';
import { TaskOption } from 'fp-ts/lib/TaskOption';
import { taskEither } from 'fp-ts';
import { TaskEither } from 'fp-ts/lib/TaskEither';
import { pipe } from 'fp-ts/lib/function';
// * Types for context
type CreatableUser = {
password: string;
email: string;
//...
}
type User = {
email: string;
id: string;
//...
}
declare const findByEmail: (email: string) => TaskOption<User>;
declare const save: (user: User, password: string) => TaskEither<Error, void>;
declare const sendMail: (message: {body: string}) => TaskEither<Error, void>;
มีใครมีไอเดียเกี่ยวกับวิธีกรองหรือใช้ฟังก์ชันทำนายเพื่อคืนค่าตั้งแต่ต้นหากผู้ใช้นั้นมีอยู่แล้วไหม?
อัปเดต
ฉันสามารถแก้ไขปัญหานี้ได้ด้วยโค้ดดังต่อไปนี้:
export const makeCreateOne =
(usersRepository: UsersRepository, mailProvider: MailProvider) =>
({ password, ...creatableUserData }: CreatableUser): TaskEither<ExceptionError, User> => {
// Generate the new user
const newUser: User = {
...creatableUserData,
id: randomUUID(),
};
return pipe(
// * Get the user with the given email
creatableUserData.email,
usersRepository.findByEmail,
// * Return `null` if `Some`
TO.match(
// TODO: improve that
// None: The user doesn't exists, we can create a new user
() => true,
// Some: The user already exists, so we don't wanna keep the creation process
() => null,
),
TE.fromNullable(createExceptionError('User already exists', REQUEST_STATUS.not_found)),
// * Save the new `User` on the repository
TE.chain(() => usersRepository.save(newUser, password)),
// * Send the confirmation email to the user
TE.chain(() =>
mailProvider.sendMail({
body: 'Welcome to App Team',
}),
),
TE.map(() => newUser),
);
};
แต่ว่ายังดูไม่ดีนัก ถ้าใครมีไอเดียเกี่ยวกับวิธีปรับปรุง ฉันยินดีรับฟัง
คำตอบ #1
หากมีใครสงสัยเหมือนกัน นี่คือวิธีแก้ของผม: ผมคิดว่าที่จุดนี้ควรสร้างเมธอดขึ้นมาเพื่อตรวจสอบว่าอีเมลพร้อมใช้งานหรือไม่ ดังนั้นผมจึงสร้างอินเทอร์เฟซนี้:
export type UsersRepository = {
readonly findByEmail: (email: string) => TaskOption<User>;
readonly findByID: (id: string) => TaskOption<User>;
readonly save: (user: User, password: string) => TaskEither<ExceptionError, void>;
readonly update: (user: User, password?: string) => TaskEither<ExceptionError, void>;
readonly delete: (userID: ID) => TaskEither<ExceptionError, void>;
readonly all: () => TaskEither<ExceptionError, ReadonlyArray<User>>;
readonly isUserPasswordValid: (email: string, password: string) => Task<boolean>;
readonly isEmailAvailable: (email: string) => Task<Boolean>;
};
และปรับปรุงฟังก์ชันให้ทำงานเหมือนกับ:
export const makeCreateOne =
(usersRepository: UsersRepository, mailProvider: MailProvider) =>
({ password, ...creatableUserData }: CreatableUser): TaskEither<ExceptionError, User> => {
// Generate the new user
const newUser: User = {
...creatableUserData,
id: randomUUID(),
};
return pipe(
// * Get the user with the given email
creatableUserData.email,
usersRepository.isEmailAvailable,
TE.fromTask,
TE.filterOrElse(
isEmailAvailable => isEmailAvailable,
() => createExceptionError('Check your password and try again', REQUEST_STATUS.bad),
),
// * Save the new `User` on the repository
TE.chain(() => usersRepository.save(newUser, password)),
// * Send the confirmation email to the user
TE.chain(() =>
mailProvider.sendMail({
body: 'Welcome to App Team',
}),
),
TE.map(() => newUser),
);
};
