gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

feat: add `URLFromString` #163

Closed MakhBeth closed 2 years ago

MakhBeth commented 3 years ago

Simply add a URLFromString codec. It takes a string and uses the native URL api to detect if it's valid.

Note: I reference version 0.5.17 in the doc string, but I haven't bumped the version anywhere, I'm not aware of the preferred publish strategy

DenisFrezzato commented 3 years ago

@MakhBeth documentation has not been updated, please run npm run docs.

I'm not aware of the preferred publish strategy

Recent features were added with a minor bump.

MakhBeth commented 3 years ago

Right, I had some prettier issues and a missing support to URL for old TS versions. Now it should be ok

MakhBeth commented 3 years ago

I had to do something awful, to make everything work on browser:

import * as nodeURL from 'url'

declare const window: any // tslint:disable-next-line
/* istanbul ignore next */ const URL = typeof window === 'undefined' ? nodeURL.URL : window.URL

It's the only way that I know to do the match. Suggestions are (very) welcome

MakhBeth commented 3 years ago

Ok, it could be a lot simpler Edited and removed the horrible ternary on window 👍

DenisFrezzato commented 3 years ago

The issue that I see here is that the type of window.URL is different from URL from url (NodeJS module), one can't be assigned to the other, and I don't see a way to make this codec work with both environments.