swagger-codegen-generators icon indicating copy to clipboard operation
swagger-codegen-generators copied to clipboard

[JAVASCRIPT] Added logic to properly process multipart/form-data

Open georgedias opened this issue 3 years ago • 0 comments

This PR addresses the issue when uploading a multipart/form-data utilizing a node - javascript client SDK as reported in issue 11000:

File modified (ApiClient.mustache - es5 and es6)

Added the following check in the isFileParam(param) method

    {{#emitJSDoc}}/**
    * Checks whether the given parameter value represents file-like content.
    * @param param The parameter to check.
    * @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
    */
    {{/emitJSDoc}}
    isFileParam(param) {
        // fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
        if (typeof require === 'function') {
            let fs;
            try {
                fs = require('fs');
            } catch (err) {}
            if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
                return true;
            }
        }

        // HACK (issue #11000)
        // Readstream - fs.createReadStream or fs.ReadStream
        if (isStream.readable(param)) {
            return true;
        }
                
        // Buffer in Node.js
        if (typeof Buffer === 'function' && param instanceof Buffer) {
            return true;
        }

        // Blob in browser
        if (typeof Blob === 'function' && param instanceof Blob) {
            return true;
        }

        // File in browser (it seems File object is also instance of Blob, but keep this for safe)
        if (typeof File === 'function' && param instanceof File) {
            return true;
        }

        return false;
    }

georgedias avatar May 17 '21 15:05 georgedias