balazsbotond / urlcat

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

Add params to an url who already have params #151

Open mehdihaddad opened 3 years ago

mehdihaddad commented 3 years ago

For exemple,

const url = 'http://myurl.com?myparams=1'
urlcat(url, {foo: 2, bar: 3})

OUTPUT: http://myurl.com?myparams=1?foo=2&bar=2 WANTED: http://myurl.com?myparams=1&foo=2&bar=2

harshilparmar commented 3 years ago

@mehdihaddad Nice catch !! My approach for this is to

  1. Check ? with regex
  2. Append next params after that @balazsbotond What you say !! I would like to take this 😀
mehdihaddad commented 3 years ago

To check :

var objURL = new URL(url)
if(objURL.search.length > 1) {
   // Yes, i already have queryParams
} else {
  // Nope !
}

.search give you only the queryParams string like http://myurl.com?foo=bar -> ?foo=bar

mehdihaddad commented 3 years ago

To check :

var objURL = new URL(url)
if(objURL.search.length > 1) {
   // Yes, i already have queryParams
} else {
  // Nope !
}

.search give you only the queryParams string like http://myurl.com?foo=bar -> ?foo=bar

Maybe a regex would be better, because of url value. In urlcat you can have urlcat('test/test', {....}) and new URL('test/test') will gave you an error.

iSplasher commented 3 years ago

Hello, would it be possible to fix this?

balazsbotond commented 2 years ago

Thanks for reporting this, @mehdihaddad. Could you please show me a realistic use case where this:

const url = 'http://myurl.com'
urlcat(url, {foo: 2, bar: 3, myparams: 1})

wouldn't solve the problem?

balazsbotond commented 1 year ago

Closing this for now. If someone can demonstrate that this would be a useful feature to have, I'll reopen the issue.

iSplasher commented 1 year ago

A realistic use case is when you just want to ensure a url, which may already have query params, contains a specific set of params, like the first post demonstrated. If this is not going to be supported then could you please point it out in the readme?

balazsbotond commented 1 year ago

@iSplasher reopening because there seems to be demand for this feature.