Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64 decode/encode #155

Closed
piu130 opened this issue Feb 19, 2021 · 2 comments
Closed

Base64 decode/encode #155

piu130 opened this issue Feb 19, 2021 · 2 comments

Comments

@piu130
Copy link

piu130 commented Feb 19, 2021

🚀 Feature request

Desired Behavior

Would be cool, if this library supports base64 decode and encode natively.
My scenario: I fetch some base64 encoded Json StringFromBase64.pipe(JsonFromString).

Suggested Solution

I'm new to io-ts, so this might not be the correct way to implement it:

export const StringFromBase64 = new t.Type<string, string, string>(
    'StringFromBase64',
    (u): u is string => typeof u === 'string',
    (s, c) => {
        try {
            return t.success(atob(s));
        } catch (e) {
            return t.failure(s, c);
        }
    },
    s => btoa(s)
);
@piu130
Copy link
Author

piu130 commented Feb 25, 2021

There is no atob and btoa in NodeJS.

@piu130 piu130 closed this as completed Feb 25, 2021
@DenisFrezzato
Copy link

@piu130 there isn't, but the same can be achieved using Buffer and character encoding, see this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants