dopecodez / Wikipedia

Wikipedia for node and the browser
MIT License
82 stars 19 forks source link

geoSearchError: wikiError: TypeError: url_1.URLSearchParams is not a constructor #13

Closed dolbex closed 3 years ago

dolbex commented 3 years ago

I'm currently working on a Vue application that has the following method

    async getLocations() {
      this.pages = []
      try {
        const geoResult = await wiki.geoSearch(2.088, 4.023, {
          radius: 5000,
          limit: 20,
        })
        console.log(geoResult[0]) // the closest page to given coordinates
      } catch (error) {
        console.log(error)
      }
}

Unfortunately it returns this exception:

geoSearchError: wikiError: TypeError: url_1.URLSearchParams is not a constructor
    at AsyncFunction.wiki.geoSearch (webpack-internal:///./node_modules/wikipedia/dist/index.js:469:15)    
wiki.geoSearch = async (latitude, longitude, geoOptions) => {
    try {
        const geoSearchParams = {
            'list': 'geosearch',
            'gsradius': (geoOptions === null || geoOptions === void 0 ? void 0 : geoOptions.radius) || 1000,
            'gscoord': `${latitude}|${longitude}`,
            'gslimit': (geoOptions === null || geoOptions === void 0 ? void 0 : geoOptions.limit) || 10,
            'gsprop': 'type'
        };
        const results = await request_1.default(geoSearchParams);
        const searchPages = results.query.geosearch;
        return searchPages;
    }
    catch (error) {
        throw new errors_1.geoSearchError(error);
    }
};
dopecodez commented 3 years ago

This looks like a known issue with some frameworks that cannot import the URLSearchParams object.

We might be able to get wiki working without using this object. Will need some more investigation.

On pure js and node the constructor works but it does not work for frameworks like Vue or React

friendofdog commented 3 years ago

Is this something I could pitch in with?

I looked at the issue you linked to and I get the gist of it. If you could give me any tips on how to approach the problem, I have time this week to work on it.

dopecodez commented 3 years ago

Thanks for the offer @friendofdog .

I am currently unsure on how to go about solving this issue. We could take one of either ways.

  1. We could probably remove the dependency on the URLSearchParams object, and find some other way to get queries working for wiki.
  2. There appears to be a myriad of options and methods to solve the bug. This will mean trying each out and seeing which one best suits our case.

Helpful links could be this one on swagger, this one on stack overflow.

Also, this only seems to happen to older node versions <8 or <10. If that's the case, it might not be worth fixing considering node v10 is under manintainence and a lot of libraries are dropping support for the same.

I haven't had the time to debug and reproduce this issue. The first step would definitely be that.

@dolbex , could you tell us the node version and the vue version you are encountering this error for?

dolbex commented 3 years ago

@dopecodez - I'm running node v14.16.0 and for this particular project nuxt 2.15.2 and vue 2.6.12

friendofdog commented 3 years ago

@dolbex Many thanks for providing details.

I reproduced this error using Node 15.5.1 and Vue 2.6.11. I went over the links you provided but none of the proposed solutions seem to have any effect.

I looked at the built file for requests.js in dist. It imports url as const url_1 = require("url");. But if you console log url_1, it's almost empty. The URLSearchParams method is not there.

I'm not sure why url isn't being required properly. Seems like a very straightforward import to me.

dopecodez commented 3 years ago

The url import fails because its a node js object @friendofdog and not available in the browser.

I think this issue can be treated as a broader wiki does not work with any of the FE js frameworks because of the node specific functions we are using.

I played around with this issue as well. While we can get wiki to work using the encodeURI function there are still more issues with making it browser compatible.

Everything seems to be working fine and you can check this out in #16. The main thing i still need to test is #12 and then we can think about if this would be a good release.

dopecodez commented 3 years ago

For anyone still looking at this, https://github.com/dopecodez/Wikipedia/releases/tag/v1.1.0 is published and this version will be the new browser compatible version of wikipedia.