-
Notifications
You must be signed in to change notification settings - Fork 6k
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
[Typescript-fetch] url.URLSearchParams is not a constructor #6403
Comments
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. |
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. |
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? |
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 |
I'd like to see what you have planned.
The problem doesn't seem to be the generated code but how the code is being
used.
Like you said, the problem is that a module that doesn't contain those
classes is being substituted for the browser API that should be using it.
Imho that library should pass through to the browser API if it doesn't have
them or define those classes itself if it is supposed to replace the
browser based one.
I 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.
Anywho what would your solution be?
…On Mon, Sep 4, 2017, 9:44 PM PLACE ***@***.***> wrote:
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
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6403 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AMPLtWMBFhLeNuRfUctr5zxRp5_2Ys9qks5sfKeEgaJpZM4PGwOo>
.
|
Yeah pretty much.
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)
as a quick way to remove the dependency on |
This sounds like a good plan .
…On Tue, Sep 5, 2017, 12:55 AM PLACE ***@***.***> wrote:
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 <#4852> is
adding support for FormData so perhaps we could see the best way to make
this change after that PR is accepted?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6403 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AMPLtSqtUzW8GO480RFcV1A47BK4njXgks5sfNQjgaJpZM4PGwOo>
.
|
I am facing the same problem. |
What about customizing the templates with the |
Hi, we also have this issue. Will #4852 solve this? Otherwise, how can I help to get this issue fixed? |
Workaround: before using anything from the generated code, do: import * as url from "url";
url.URLSearchParams = URLSearchParams; |
I have the same problem... |
Same here |
I worked around this by adding the following to the generated code below the line 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; |
I solved the problem by adding
|
This is THE solution. You monkey-patch the url package before calling the swagger generated client lib. |
I'm currently having this issues. What is the current progress on this issue? |
i find the solved the problem is than angular some time expor these library ajaja or vs |
is working for me using const params = new URL(document.baseURI).searchParams; |
any update on this? I would not use workarounds 😢 |
Any news? |
@HugoMario Hello. Please tell me when to expect the Docker image swaggerapi/swagger-codegen-cli release with this fix? |
no need for import from 'url' ==> it's imported by default.
it works fine for me |
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 theurl
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.
The text was updated successfully, but these errors were encountered: