PDMLab / http-problem-details

This library implements HTTP Problem details (RFC 7807) for HTTP APIs build with Node.js.
https://www.npmjs.com/package/http-problem-details
MIT License
48 stars 8 forks source link

Deno compatability? #27

Open viztastic opened 3 years ago

viztastic commented 3 years ago

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!

AlexZeitler commented 3 years ago

@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.

viztastic commented 3 years ago

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?

AlexZeitler commented 3 years ago

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

AlexZeitler commented 3 years ago

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

viztastic commented 3 years ago

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?

AlexZeitler commented 3 years ago

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')
viztastic commented 3 years ago

Yep that makes a lot of sense 👌

AlexZeitler commented 3 years ago

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

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