Skip to content

Commit 85aef6d

Browse files
committed
[javascript] Fixed fs files support for Electron
1 parent 034fc44 commit 85aef6d

File tree

6 files changed

+54
-33
lines changed

6 files changed

+54
-33
lines changed

modules/swagger-codegen/src/main/resources/Javascript-es6/ApiClient.mustache

+9-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,15 @@ export default class ApiClient {
160160
*/
161161
{{/emitJSDoc}}
162162
isFileParam(param) {
163-
// fs.ReadStream in Node.js (but not in runtime like browserify)
164-
if (typeof window === 'undefined' && typeof require === 'function' && require('fs') && param instanceof require('fs').ReadStream) {
165-
return true;
163+
// fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
164+
if (typeof require === 'function') {
165+
let fs
166+
try {
167+
fs = require('fs')
168+
} catch (err) {}
169+
if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
170+
return true;
171+
}
166172
}
167173

168174
// Buffer in Node.js

modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache

+9-6
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,15 @@
168168
* @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
169169
*/
170170
{{/emitJSDoc}} exports.prototype.isFileParam = function(param) {
171-
// fs.ReadStream in Node.js (but not in runtime like browserify)
172-
if (typeof window === 'undefined' &&
173-
typeof require === 'function' &&
174-
require('fs') &&
175-
param instanceof require('fs').ReadStream) {
176-
return true;
171+
// fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
172+
if (typeof require === 'function') {
173+
var fs
174+
try {
175+
fs = require('fs')
176+
} catch (err) {}
177+
if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
178+
return true;
179+
}
177180
}
178181
// Buffer in Node.js
179182
if (typeof Buffer === 'function' && param instanceof Buffer) {

samples/client/petstore/javascript-es6/src/ApiClient.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,15 @@
174174
* @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
175175
*/
176176
exports.prototype.isFileParam = function(param) {
177-
// fs.ReadStream in Node.js (but not in runtime like browserify)
178-
if (typeof window === 'undefined' &&
179-
typeof require === 'function' &&
180-
require('fs') &&
181-
param instanceof require('fs').ReadStream) {
182-
return true;
177+
// fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
178+
if (typeof require === 'function') {
179+
var fs
180+
try {
181+
fs = require('fs')
182+
} catch (err) {}
183+
if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
184+
return true;
185+
}
183186
}
184187
// Buffer in Node.js
185188
if (typeof Buffer === 'function' && param instanceof Buffer) {

samples/client/petstore/javascript-promise-es6/src/ApiClient.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,15 @@
174174
* @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
175175
*/
176176
exports.prototype.isFileParam = function(param) {
177-
// fs.ReadStream in Node.js (but not in runtime like browserify)
178-
if (typeof window === 'undefined' &&
179-
typeof require === 'function' &&
180-
require('fs') &&
181-
param instanceof require('fs').ReadStream) {
182-
return true;
177+
// fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
178+
if (typeof require === 'function') {
179+
var fs
180+
try {
181+
fs = require('fs')
182+
} catch (err) {}
183+
if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
184+
return true;
185+
}
183186
}
184187
// Buffer in Node.js
185188
if (typeof Buffer === 'function' && param instanceof Buffer) {

samples/client/petstore/javascript-promise/src/ApiClient.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,15 @@
174174
* @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
175175
*/
176176
exports.prototype.isFileParam = function(param) {
177-
// fs.ReadStream in Node.js (but not in runtime like browserify)
178-
if (typeof window === 'undefined' &&
179-
typeof require === 'function' &&
180-
require('fs') &&
181-
param instanceof require('fs').ReadStream) {
182-
return true;
177+
// fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
178+
if (typeof require === 'function') {
179+
var fs
180+
try {
181+
fs = require('fs')
182+
} catch (err) {}
183+
if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
184+
return true;
185+
}
183186
}
184187
// Buffer in Node.js
185188
if (typeof Buffer === 'function' && param instanceof Buffer) {

samples/client/petstore/javascript/src/ApiClient.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,15 @@
174174
* @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
175175
*/
176176
exports.prototype.isFileParam = function(param) {
177-
// fs.ReadStream in Node.js (but not in runtime like browserify)
178-
if (typeof window === 'undefined' &&
179-
typeof require === 'function' &&
180-
require('fs') &&
181-
param instanceof require('fs').ReadStream) {
182-
return true;
177+
// fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
178+
if (typeof require === 'function') {
179+
var fs
180+
try {
181+
fs = require('fs')
182+
} catch (err) {}
183+
if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
184+
return true;
185+
}
183186
}
184187
// Buffer in Node.js
185188
if (typeof Buffer === 'function' && param instanceof Buffer) {

0 commit comments

Comments
 (0)