PR review fix & improvements

This commit is contained in:
Scott Picquerey 2022-04-06 14:20:55 +02:00
parent a8c6a4bb7e
commit 6fe9e04f45

View file

@ -33,6 +33,7 @@ complete each is detailed in the comments.
For readability purpose, we replace `ReaderTaskEither` by `rte` For readability purpose, we replace `ReaderTaskEither` by `rte`
- Use `flow` instead of `pipe` when possible - Use `flow` instead of `pipe` when possible
> Why? Using flow reduces the amount of variables to declare in a method, hence the visibility and readability of the code
```typescript ```typescript
// Bad // Bad
@ -52,8 +53,7 @@ const triggerEmailCampaign = ({
user, user,
...emailSettings ...emailSettings
}: { }: {
user: User & EmailSettings; user: User} & EmailSettings) =>
}) =>
pipe( pipe(
user.nationality === 'FR', user.nationality === 'FR',
boolean.match( boolean.match(
@ -65,15 +65,12 @@ const triggerEmailCampaign = ({
// Good // Good
const triggerEmailCampaign = ({ const triggerEmailCampaign = ({
user, user,
...otherProps ...emailSettings
}: { }: { user: User } & EmailSettings) => {
user: User & OtherProps;
}) => {
if (user.nationality === 'FR') { if (user.nationality === 'FR') {
return triggerFrenchEmailCampaign({ to: user.email, emailSettings }); return triggerFrenchEmailCampaign({ to: user.email, emailSettings });
} }
return triggerGlobalEmailCampain({ to: user.email, emailSettings }); return triggerGlobalEmailCampain({ to: user.email, emailSettings });
};
``` ```
- Avoid nested pipes - Avoid nested pipes
@ -81,14 +78,14 @@ const triggerEmailCampaign = ({
```typescript ```typescript
// Bad // Bad
const renewUserToken = (user: User) => const refreshUserToken = (user: User) =>
pipe( pipe(
user.token, user.token,
option.match( option.match(
() => AuthClient.createToken(user), () => AuthClient.createToken(user),
(token) => (token) =>
flow( pipe(
AuthClient.renewToken(user), AuthClient.refreshToken({ user, token }),
rte.chainW(({ newToken }) => rte.chainW(({ newToken }) =>
pipe(newToken.hash, user.updateToken, rte.chainEitherKW(storeUser)), pipe(newToken.hash, user.updateToken, rte.chainEitherKW(storeUser)),
), ),
@ -97,16 +94,16 @@ const renewUserToken = (user: User) =>
); );
// Good // Good
const updateUserToken = (user: User) => (newToken: Token) => const updateUserToken = (user: User) =>
pipe(newToken.hash, user.updateToken, rte.chainEitherKW(storeUser)); flow(user.updateToken, rte.chainEitherKW(storeUser));
const renewAndUpdateUserToken = (user: User) => const refreshAndUpdateUserToken = (user: User) => (token: Token) =>
flow( pipe(
AuthClient.renewToken(user), AuthClient.refreshToken({ user, token }),
rte.chainW(({ newToken }) => updateUserToken(user)), rte.chainW(({ newToken }) => updateUserToken(user)(newToken.hash)),
); );
const renewUserToken = (user: User) => const refreshUserToken = (user: User) =>
pipe( pipe(
user.token, user.token,
option.match( option.match(