diff --git a/README.md b/README.md index 976471a2d..1b71279ef 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Swagger Client [![Build Status](https://travis-ci.org/swagger-api/swagger-js.svg?branch=master)](https://travis-ci.org/swagger-api/swagger-js) +[![Build Status](https://jenkins.swagger.io/view/OSS%20-%20JavaScript/job/oss-swagger-js-master/badge/icon?subject=jenkins%20build)](https://jenkins.swagger.io/view/OSS%20-%20JavaScript/job/oss-swagger-js-master/) **Swagger Client** is a JavaScript module that allows you to fetch, resolve, and interact with Swagger/OpenAPI documents. diff --git a/package-lock.json b/package-lock.json index aa92d7df3..92e6e2b97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "swagger-client", - "version": "3.8.26", + "version": "3.9.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1599,7 +1599,8 @@ "base64-js": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.0.tgz", - "integrity": "sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw==" + "integrity": "sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw==", + "dev": true }, "bcrypt-pbkdf": { "version": "1.0.2", @@ -1925,15 +1926,6 @@ "integrity": "sha1-M3dm2hWAEhD92VbCLpxokaudAzc=", "dev": true }, - "buffer": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.1.0.tgz", - "integrity": "sha512-YkIRgwsZwJWTnyQrsBTWefizHh+8GYj3kbL1BTiAQ/9pwpino0G7B2gp5tx/FUBqUlvtxV85KNR3mwfAtv15Yw==", - "requires": { - "base64-js": "^1.0.2", - "ieee754": "^1.1.4" - } - }, "buffer-alloc": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz", @@ -5909,7 +5901,8 @@ "ieee754": { "version": "1.1.12", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.12.tgz", - "integrity": "sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==" + "integrity": "sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==", + "dev": true }, "ignore": { "version": "3.3.10", diff --git a/package.json b/package.json index 312718ac7..32de2859a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-client", - "version": "3.8.26", + "version": "3.9.0", "description": "SwaggerJS - a collection of interfaces for OAI specs", "main": "dist/index.js", "unpkg": "browser/index.js", @@ -91,7 +91,6 @@ "@kyleshockey/object-assign-deep": "^0.4.0", "babel-runtime": "^6.26.0", "btoa": "1.1.2", - "buffer": "^5.1.0", "cookie": "^0.3.1", "cross-fetch": "0.0.8", "deep-extend": "^0.5.1", diff --git a/src/execute/oas3/build-request.js b/src/execute/oas3/build-request.js index 116ca5af8..139afb7a4 100644 --- a/src/execute/oas3/build-request.js +++ b/src/execute/oas3/build-request.js @@ -3,7 +3,7 @@ import assign from 'lodash/assign' import get from 'lodash/get' import btoa from 'btoa' -import {Buffer} from 'buffer/' +import {isFile} from '../../http' export default function (options, req) { const { @@ -57,21 +57,7 @@ export default function (options, req) { const val = requestBody[k] let newVal - let isFile - - if (typeof File !== 'undefined') { - isFile = val instanceof File // eslint-disable-line no-undef - } - - if (typeof Blob !== 'undefined') { - isFile = isFile || val instanceof Blob // eslint-disable-line no-undef - } - - if (typeof Buffer !== 'undefined') { - isFile = isFile || Buffer.isBuffer(val) - } - - if (typeof val === 'object' && !isFile) { + if (typeof val === 'object' && !isFile(val)) { if (Array.isArray(val)) { newVal = val.toString() } diff --git a/src/http.js b/src/http.js index 5303dfa74..41e56d214 100644 --- a/src/http.js +++ b/src/http.js @@ -139,15 +139,17 @@ export function isFile(obj, navigatorObj) { // eslint-disable-next-line no-undef navigatorObj = navigator } + if (typeof Blob !== 'undefined' && obj instanceof Blob) { // eslint-disable-line no-undef + return true + } if (navigatorObj && navigatorObj.product === 'ReactNative') { if (obj && typeof obj === 'object' && typeof obj.uri === 'string') { return true } return false } - if (typeof File !== 'undefined') { - // eslint-disable-next-line no-undef - return obj instanceof File + if (typeof File !== 'undefined' && obj instanceof File) { // eslint-disable-line no-undef + return true } return obj !== null && typeof obj === 'object' && typeof obj.pipe === 'function' } diff --git a/test/http.js b/test/http.js index 678156964..6f193cbbd 100644 --- a/test/http.js +++ b/test/http.js @@ -1,5 +1,6 @@ import xmock from 'xmock' import fetchMock from 'fetch-mock' +import {Readable} from 'stream' import http, { serializeHeaders, mergeInQueryOrForm, encodeFormOrQuery, serializeRes, shouldDownloadAsText, isFile @@ -429,6 +430,7 @@ describe('http', () => { describe('isFile', () => { // mock browser File class global.File = class MockBrowserFile {} + global.Blob = class MockBlob {} const mockBrowserNavigator = { product: 'Gecko' @@ -438,9 +440,11 @@ describe('http', () => { } const browserFile = new global.File() + const browserBlobFile = new global.Blob() const reactNativeFileObject = { uri: '/mock/path' } + const nodeStream = new Readable() test('should return true for browser File type', () => { expect(isFile(browserFile)).toEqual(true) @@ -462,6 +466,14 @@ describe('http', () => { expect(isFile(reactNativeFileObject, mockReactNativeNavigator)).toEqual(true) }) + test('should return true for browser Blob type and browser user agent', () => { + expect(isFile(browserBlobFile, mockBrowserNavigator)).toEqual(true) + }) + + test('should return true for readable node streams', () => { + expect(isFile(nodeStream)).toEqual(true) + }) + test('should return false for non-File type and browser user agent', () => { expect(isFile(undefined, mockBrowserNavigator)).toEqual(false) expect(isFile('', mockBrowserNavigator)).toEqual(false) @@ -477,5 +489,13 @@ describe('http', () => { expect(isFile([], mockReactNativeNavigator)).toEqual(false) expect(isFile({}, mockReactNativeNavigator)).toEqual(false) }) + + test('should return false for non-object type and no user agent', () => { + expect(isFile(undefined)).toEqual(false) + expect(isFile('')).toEqual(false) + expect(isFile(123)).toEqual(false) + expect(isFile([])).toEqual(false) + expect(isFile({})).toEqual(false) + }) }) })