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

How to use SwaggerClient in web worker #2008

Closed
tmkasun opened this issue Apr 1, 2021 · 7 comments
Closed

How to use SwaggerClient in web worker #2008

tmkasun opened this issue Apr 1, 2021 · 7 comments

Comments

@tmkasun
Copy link

tmkasun commented Apr 1, 2021

I'm getting

browser.js:1 Uncaught ReferenceError: window is not defined

This exception is coming from
swagger-client -> HTTP -> form-data-monkey-patch -> isomorphic-form-data)

Exception when trying to use SwaggerClient in a web worker. I did a little bit of a search and found that there is an issue created in isomorphic-form-data repo asking for a solution.

I tried setting outputglobalobject in webpack output also, But that didn't work.

Q&A (please complete the following information)

  • OS: macOS
  • Environment: Chrome Version 89.0.4389.90
  • Method of installation: npm
  • Swagger-Client version: 3.12.0
  • Swagger/OpenAPI version: OpenAPI 3.0.1

Content & configuration

Swagger/OpenAPI definition:

Link to definition

Swagger-Client usage:

import SwaggerClient from 'swagger-client';

SwaggerClient.resolve({
    url: '/api/am/publisher/v2/swagger.yaml',
    requestInterceptor: (request) => {
        request.headers.Accept = 'text/yaml';
    },
}).then((spec) => {
    self.postMessage(spec);
});

Screenshots

image

How can we help?

Can you please help me in using the SwaggerClient in the work environment? Is SwaggerClient support it or am I doing anything wrong here?

@tmkasun tmkasun changed the title Using swagger client in web worker How to use SwaggerClient in web worker Apr 1, 2021
@char0n char0n self-assigned this Apr 1, 2021
@char0n
Copy link
Member

char0n commented Apr 1, 2021

swagger-js should work properly in Web Worker environment. The problem is (as you already mentioned correctly in this issue) isomorphic-form-data library that we use. Let's continue the discussion there, becase this problem needs to be fix upstream before it's fixed in swagger-js.

@char0n
Copy link
Member

char0n commented Apr 1, 2021

Added a comment in isomorphic-form-data. If it's not going to be resolved there in some reasonable time, we'll have to find an alternative solution to move things forward.

@char0n
Copy link
Member

char0n commented Apr 5, 2021

@tmkasun,

Current workaround could be to use resolve alias in your webpack config to get around this problem.

Webpack config

...
resolve: {
  alias: {
    'isomorphic-form-data': path.resolve(__dirname, 'src/isomorphic-form-data.js/')
  }
}   
...

isomorphic-form-data.js

module.exports = globalThis.FormData

This will bypass the ismorphic-form-data library in your worker build fragment and will use src/ismorphic-form-data.js local file with correct global symbol.

Please let me know if this temporary workaround worked for you.

@tmkasun
Copy link
Author

tmkasun commented Apr 26, 2021

Thanks @char0n , I will check and let you know

@char0n
Copy link
Member

char0n commented Jul 7, 2021

I've issued a PR against https://github.com/form-data/isomorphic-form-data to fix this issue. Let's see if authors merge it and release a new major release.

@char0n
Copy link
Member

char0n commented Jul 25, 2021

It's high time to fix this; isomorphic-form-data library looks completely dead and the fix is not going to come from upstream.

Here is a plan to resolving this issue in SwaggerJS repo:

  1. wait for isomorphic support for formdata-node library (Making this library isomorphic octet-stream/form-data#38)
  2. replace isomorphic-form-data for https://www.npmjs.com/package/formdata-node
  3. Get rid of monkey patching
    this.entryList.push(createEntry(field, value));
  4. release new version of swagger-client

More context here: #1547

After we have this resolved, swagger-client will be natively support in WebWorker env without workarounds provided in comments of this issue.

PR is ready here: #2154

char0n added a commit that referenced this issue Jul 25, 2021
This remove use of isomorphic-form-data.

Refs #2008
char0n added a commit that referenced this issue Jul 26, 2021
This remove use of isomorphic-form-data.

Refs #2008
char0n added a commit that referenced this issue Jul 27, 2021
- remove use of isomorphic-form-data
- set minimum Node.js version to 12.4.x
- introduce browser package.json field override

Refs #2008
swagger-bot pushed a commit that referenced this issue Jul 27, 2021
# [3.14.0](v3.13.7...v3.14.0) (2021-07-27)

### Features

* **formdata:** use formdata-node to handle form data ([#2154](#2154)) ([59c4b79](59c4b79)), closes [#2008](#2008)
@char0n
Copy link
Member

char0n commented Jul 27, 2021

@char0n char0n closed this as completed Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants