http-problem-details icon indicating copy to clipboard operation
http-problem-details copied to clipboard

Deno compatability?

Open viztastic opened this issue 4 years ago • 8 comments
trafficstars

Is it possible to include an export map and entry point to make this library deno friendly?

We could even add it to the deno registry that way and make the library the default for Deno's equivalent to express (Oak).

Thanks so much!

viztastic avatar Sep 12 '21 07:09 viztastic

@viztastic I’m open to your proposal but I don’t have any experience with deno. If you can provide a PR with the necessary changes, I’m happy to merge it.

AlexZeitler avatar Sep 12 '21 11:09 AlexZeitler

No problems @AlexZeitler - I've just had a go at aligning the module in a fork. I'm just dealing with one error:

image

Node JS has deprecated url.parse, would it be a good idea to use the newer WHATWG URL implementation? It would benefit both Node and Deno.

I had a very naive idea which was to check if the instance had a base path, and if it didn't, we insert a fake one and then remove it again... but then also what if we just stored the string directly, is that bad?

viztastic avatar Sep 12 '21 13:09 viztastic

@viztastic We could default to http://localhost or http://tempuri.org - does this make sense to you?

AlexZeitler avatar Sep 12 '21 17:09 AlexZeitler

Or should we expect a valid URL including a base URL?

AlexZeitler avatar Sep 12 '21 17:09 AlexZeitler

I don’t mind forcing the base path but I’m worried it won’t be compliant with the spec because they explicitly support without the base path.

Maybe we ask the user to include it but they another option to strip out the base?

viztastic avatar Sep 12 '21 20:09 viztastic

I think I would do something like this then:

yarn add is-relative-url
import isRelativeUrl from 'is-relative-url'

const getUrl = (url: string) => {
  return isRelativeUrl(url) ? getRelativeUrl(url) : url
}

const getRelativeUrl = (path: string) => {
  const url = new URL(path, 'http://tempuri.org')
  const { pathname, search, hash } = url
  const result = pathname + search + hash
  return result
}

// relative
const url = '/test?test=test#test'
const result = getUrl(url.toString())
result.should.equal('/test?test=test#test')

// absolute
 const url = new URL('/test?test=test#test', 'http://tempuri.org')
 const result = getUrl(url.toString())
 result.should.equal('http://tempuri.org/test?test=test#test')

AlexZeitler avatar Sep 13 '21 08:09 AlexZeitler

Yep that makes a lot of sense 👌

viztastic avatar Sep 13 '21 14:09 viztastic

Ok, I build a npm package for it (getRelativeUrl) 🤪

https://www.npmjs.com/package/get-relative-whatwg-url

AlexZeitler avatar Sep 13 '21 18:09 AlexZeitler