Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typescript-fetch] url.URLSearchParams is not a constructor #6403

Closed
Place1 opened this issue Aug 30, 2017 · 25 comments · Fixed by swagger-api/swagger-codegen-generators#1249 or #12338

Comments

@Place1
Copy link

Place1 commented Aug 30, 2017

Description

The typescript-fetch client using swagger 2.3.0 uses import * as url from 'url' which is a native node_module.

I'm using webpack to to bundle my application and it's using node-libs-browser to substitute native node modules for browser compatible ones, including the url module.

The url module is being pulled in through webpack:
webpack --> escope --> node-libs-browser --> url

Are we suppose to import a pollyfill to use this generator? I'd hope not :(

Swagger-codegen version

2.3.0. I'm not sure if this is a regression as there was intentional breaking changes in this version. I also believe this version introduced the dependency on the 'url' module.

Swagger declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

My solution would be to not use native node modules given the typescript/fetch generator is supposed to work in the browser as well.

@wing328
Copy link
Contributor

wing328 commented Sep 4, 2017

@kenisteward
Copy link
Contributor

Please check the compatibility table here https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams#Browser_compatibility

Make sure the browser you are using supports it. urlsearch params is a browser spec.

@Place1
Copy link
Author

Place1 commented Sep 5, 2017

Browser compatibility isn't really the issue here (I'm using latest chrome while developing). The issue is that webpack, by default, injects a different version of the 'url' module which is missing the urlsearchparam object constructor.

So I guess my point is that we should refactor the generator to avoid using the 'URL' module.

@kenisteward
Copy link
Contributor

kenisteward commented Sep 5, 2017

The typescript fetch lib should be a browser only client which is why I'm confused you are getting the error.

Are you getting this error at compile time or runtime?

Also can't you tell webpack not to inject that module or are you using it for other things in your app?

@Place1
Copy link
Author

Place1 commented Sep 5, 2017

It's a run time error. I could tell webpack to ignore the url module, but that's not really ideal for a few reasons, it may adversely effect other dependencies in my application that (intentionally or unintentionally) bring in node's url module, but also for new developers trying to use the typescript fetch generator with their webpack based apps (most people) will run into this same issue which hurts the usability of the generator. It's a fair bit of effort to polyfill, configure webpack, solve problems with other dependencies (potentially) just for the generator to use a method or two from the 'url' module. Wouldn't it be best for the generator to go about it another way? I'm happy to put in the effort and make a pull request if others are open to the idea :D

@kenisteward
Copy link
Contributor

kenisteward commented Sep 5, 2017 via email

@Place1
Copy link
Author

Place1 commented Sep 5, 2017

The problem doesn't seem to be the generated code but how the code is being used.

Yeah pretty much.

don't think this has come up before even for others using webpack because they aren't using that extra module but I can't be sure.

My guess is that because most people are using the stable version (2.2.3) of swagger they haven't yet encountered this as it was introduced somewhere after 2.2.3 (i think in 2.3.0)

Anywho what would your solution be?

as a quick way to remove the dependency on URLSearchParams we could use querystring.stringify() instead. The good thing about querystring is that it's just a light wrapper around encodeURIComponent which means no polyfills are ever required and it's unlikely that webpack or other build tools will get in the way.
#4852 is adding support for FormData so perhaps we could see the best way to make this change after that PR is accepted?

@kenisteward
Copy link
Contributor

kenisteward commented Sep 5, 2017 via email

@wing328 wing328 modified the milestones: v2.3.0, Future Dec 18, 2017
@romor
Copy link

romor commented Jan 24, 2018

I am facing the same problem.
How can I temporary solve it by telling webpack to ignore the url module?

@Place1
Copy link
Author

Place1 commented Jan 24, 2018

I just had a look at #4852 and it looks like it is using querystring.stringify. It looks like this issue will be solved by that pull. I haven’t checked it out to confirm though.

@romor my solution was to just not upgrade yet :/ sorry if that’s not helpful.

@wing328
Copy link
Contributor

wing328 commented Jan 29, 2018

How can I temporary solve it by telling webpack to ignore the url module?

What about customizing the templates with the -t option?

@salim7
Copy link

salim7 commented Mar 5, 2018

Hi, we also have this issue. Will #4852 solve this? Otherwise, how can I help to get this issue fixed?

@ikorolev93
Copy link

Workaround: before using anything from the generated code, do:

import * as url from "url";
url.URLSearchParams = URLSearchParams;

@hiroppy
Copy link

hiroppy commented Aug 14, 2018

I have the same problem...

@maschnetwork
Copy link

Same here

@scyclops
Copy link

I worked around this by adding the following to the generated code below the line import * as url from "url";

import {stringify as qs_stringify} from "querystring";

class URLSearchParams {
    constructor() {
        this.data = {};
    }

    set(k, v) {
        this.data[k] = v;
    }

    toString() {
        return qs_stringify(this.data);
    }
}

url.URLSearchParams = URLSearchParams;

@chawax
Copy link

chawax commented Oct 11, 2018

I solved the problem by adding url-search-params-polyfill as dependency and patching the url lib in node_modules with patch-package

exports.URLSearchParams = URLSearchParams;

@mcamenzind
Copy link

Workaround: before using anything from the generated code, do:

import * as url from "url";
url.URLSearchParams = URLSearchParams;

This is THE solution. You monkey-patch the url package before calling the swagger generated client lib.

@wallid
Copy link

wallid commented Jan 11, 2021

I'm currently having this issues. What is the current progress on this issue?

@ald4-rgb
Copy link

ald4-rgb commented Mar 7, 2021

i find the solved the problem is than angular some time expor these library ajaja or vs
i think is better solved is erase because couse much troubles
//import { URLSearchParams } from 'url';

@ortegapedro88
Copy link

ortegapedro88 commented Jul 20, 2022

is working for me using

const params = new URL(document.baseURI).searchParams;
const code = params.get("code");

@stearm
Copy link

stearm commented Aug 8, 2023

any update on this? I would not use workarounds 😢

@dimuska139
Copy link

Any news?

@dimuska139
Copy link

@HugoMario Hello. Please tell me when to expect the Docker image swaggerapi/swagger-codegen-cli release with this fix?

@RAJI-Zakaria
Copy link

RAJI-Zakaria commented Mar 11, 2024

no need for import from 'url' ==> it's imported by default.
you can check
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

        const params = new URLSearchParams();
.......
        if(searchParams.get('orderBy')) params.append('orderBy', searchParams.get('orderBy')!)

it works fine for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment