Swagger-codegen: [Typescript-fetch] url.URLSearchParams is not a constructor

Created on 30 Aug 2017  Â·  18Comments  Â·  Source: swagger-api/swagger-codegen

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.

TypeScript Bug help wanted

Most helpful comment

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

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

All 18 comments

cc @TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx

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 notifications@github.com 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
https://github.com/swagger-api/swagger-codegen/issues/6403#issuecomment-327050183,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMPLtWMBFhLeNuRfUctr5zxRp5_2Ys9qks5sfKeEgaJpZM4PGwOo
.

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?

This sounds like a good plan .

On Tue, Sep 5, 2017, 12:55 AM PLACE notifications@github.com 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 https://github.com/swagger-api/swagger-codegen/pull/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
https://github.com/swagger-api/swagger-codegen/issues/6403#issuecomment-327070953,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMPLtSqtUzW8GO480RFcV1A47BK4njXgks5sfNQjgaJpZM4PGwOo
.

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

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.

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

What about customizing the templates with the -t option?

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 * 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;

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;

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.

Was this page helpful?
0 / 5 - 0 ratings