Update example for nested pipes
This commit is contained in:
parent
6fe9e04f45
commit
e056cdc02c
1 changed files with 46 additions and 19 deletions
65
README.md
65
README.md
|
@ -78,37 +78,64 @@ const triggerEmailCampaign = ({
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// Bad
|
// Bad
|
||||||
const refreshUserToken = (user: User) =>
|
const convertDollarAmountInCountryCurrency = ({
|
||||||
|
countryName,
|
||||||
|
amountInDollar,
|
||||||
|
}: {
|
||||||
|
countryName: CountryName;
|
||||||
|
amountInDollar: number;
|
||||||
|
}) =>
|
||||||
pipe(
|
pipe(
|
||||||
user.token,
|
getCountryCode(countryName),
|
||||||
option.match(
|
either.map(
|
||||||
() => AuthClient.createToken(user),
|
countryCode =>
|
||||||
(token) =>
|
|
||||||
pipe(
|
pipe(
|
||||||
AuthClient.refreshToken({ user, token }),
|
getCountryCurrency(countryCode),
|
||||||
rte.chainW(({ newToken }) =>
|
option.map(currency =>
|
||||||
pipe(newToken.hash, user.updateToken, rte.chainEitherKW(storeUser)),
|
pipe(
|
||||||
|
amountInDollar,
|
||||||
|
converFromDollar(currency),
|
||||||
|
convertedAmount =>
|
||||||
|
console.log(
|
||||||
|
`converted amount for country ${countryCode} is ${convertedAmount}`,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Good
|
// Good
|
||||||
const updateUserToken = (user: User) =>
|
const convertDollarAmountInCountryCodeCurrency = ({
|
||||||
flow(user.updateToken, rte.chainEitherKW(storeUser));
|
amountInDollar,
|
||||||
|
countryCode,
|
||||||
const refreshAndUpdateUserToken = (user: User) => (token: Token) =>
|
}: {
|
||||||
|
amountInDollar: number;
|
||||||
|
countryCode: CountryCode;
|
||||||
|
}) =>
|
||||||
pipe(
|
pipe(
|
||||||
AuthClient.refreshToken({ user, token }),
|
getCurrencyFromCountryCode(countryCode),
|
||||||
rte.chainW(({ newToken }) => updateUserToken(user)(newToken.hash)),
|
option.map(currency => {
|
||||||
|
const convertedDollarAmount = convertFromDollar(currency)(currency);
|
||||||
|
console.log(
|
||||||
|
`converted amount for country ${countryCode} is ${convertedAmount}`,
|
||||||
|
);
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const refreshUserToken = (user: User) =>
|
const convertDollarAmountInCountryCurrency = ({
|
||||||
|
amountInDollar,
|
||||||
|
countryName,
|
||||||
|
}: {
|
||||||
|
amountInDollar: number;
|
||||||
|
countryName: CountryName;
|
||||||
|
}) =>
|
||||||
pipe(
|
pipe(
|
||||||
user.token,
|
getCountryCode(countryName),
|
||||||
option.match(
|
either.map(countryCode =>
|
||||||
() => AuthClient.createToken(user),
|
convertDollarAmountToCountryCodeCurrency({ amountInDollar, countryCode }),
|
||||||
renewAndUpdateUserToken(user),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
Reference in a new issue