balazsbotond / urlcat

A URL builder library for JavaScript.
https://urlcat.org
MIT License
1.82k stars 57 forks source link

Support for RFC-6570 URI Templates #243

Closed sazzer closed 1 year ago

sazzer commented 1 year ago

This library is fantastic for building URLs simply, but it would be really useful if it could also support RFC-6570 URI Templates.

So, for example, I can do the following:

const url = urlcat('https://example.com', '/users/{id}{?full}', { id: '123', full: 'true', ignored: 'here' });

to produce https://example.com/users/123?full=true

I can probably do this using this library without expansions and then using something like https://www.npmjs.com/package/url-template for the rest, but it would be awesome to have it all-in-one.

Cheers

balazsbotond commented 1 year ago

Thank you for your interest in urlcat!

While I can see the advantages of using a standard format for the URL templates, I also think it wouldn’t be a good fit to urlcat’s current feature set and it would make parsing unnecessarily complicated.

An issue, for example, is that urlcat currently adds all unreferenced properties as query parameters. An RFC-compliant implementation should ignore these. Even if we could automatically determine if the user supplied an RFC-6570 or a regular template (which is in many cases impossible - I could construct a template which is valid for both formats), I’m not sure how we could handle these extra properties in a way that doesn’t confuse users. We could either keep the old behavior for RFC templates too, resulting in a noncompliant implementation or dynamically choose how to operate based on the detected template format.

So while the idea is good, I think it would work better if implemented in a separate library, possibly a fork of urlcat.