balazsbotond / urlcat

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

Can we remove URLSearchParams from index.js? #82

Closed callmeaponte closed 4 years ago

callmeaponte commented 4 years ago

First of all, this is a very useful library - thank you for taking the time to build it.

Describe the bug The URLSearchParams call in index.js file causes the script to fail when running node.js < v10. For whatever reason, I only see URLSearchParams when npm installing this library - but when cloning it locally and running a build I don't:

npm version

function query(params) {
    return new URLSearchParams(params).toString();
}

build version

function query(params, config) {
    var _a, _b;
    var qsConfiguration = {
        format: (_b = (_a = config === null || config === void 0 ? void 0 : config.objectFormat) === null || _a === void 0 ? void 0 : _a.format) !== null && _b !== void 0 ? _b : 'RFC1738',
        arrayFormat: config === null || config === void 0 ? void 0 : config.arrayFormat
    };
    return qs_1.default.stringify(params, qsConfiguration);
}
exports.query = query;

To Reproduce Steps to reproduce the behavior:

  1. Switch to a node version < 10.
  2. Create an empty folder and npm install urlcat.
  3. Write a simple node function that calls urlcat.
  4. Run the node function.
  5. Watch it fail because URLSearchParams does not exist in the global scope.

Expected behavior I was expecting to see the same code when npm installing, as when I do when building this library locally.

Desktop (please complete the following information): N/A

Smartphone (please complete the following information): N/A

Additional context Without getting into too much detail about the project I'm working on - it uses SSR with a node version that is less than v10. Additionally, I don't have access to simply add a "URLSearchParams" global to act as a polyfill/workaround.

balazsbotond commented 4 years ago

@callmeaponte thank you for the detailed report and I'm glad you like urlcat!

The reason for the discrepancy you noticed is that this library is currently in a transitional state. We have recently dropped URLSearchParams in favor of qs to support array and object query params better and will soon release a new major version. This state is what you see on the master branch. The latest release, v2.0.4, still depends on URLSearchParams which you see in your node_modules directory. As far as I know, qs supports older Node versions so after this change, urlcat will too (maybe not officially because our test tools may not support them, but the compiled result will most probably work fine).

The reason we haven't done a release yet is because we want to continue to support Deno which got a bit harder after we introduced a dependency.

You can expect the new release in the coming weeks. Until then, please check if v1.0.1 fits your needs (it was the latest release before v2.0.0 which introduced URLSearchParams). If it doesn't, you can build the qs-based new version yourself and use it in your project as a temporary - though admittedly not very elegant - workaround.

callmeaponte commented 4 years ago

Ah okay, makes sense. I'm going to switch to v1.0.1 for now, and make a note to check back here in a month or so for the new release. Thanks!