diff --git a/es/createProdMockServer.js b/es/createProdMockServer.js index 1cfdf62..98a4c9b 100644 --- a/es/createProdMockServer.js +++ b/es/createProdMockServer.js @@ -4,10 +4,27 @@ export function createProdMockServer(mockList) { Mock.XHR.prototype.__send = Mock.XHR.prototype.send; // @ts-ignore Mock.XHR.prototype.send = function () { - if (this.custom.xhr) this.custom.xhr.withCredentials = this.withCredentials || false; + if (this.custom.xhr) { + this.custom.xhr.withCredentials = this.withCredentials || false; + if (this.responseType) { + this.custom.xhr.responseType = this.responseType; + } + } // eslint-disable-next-line this.__send.apply(this, arguments); }; + // @ts-ignore + Mock.XHR.prototype.proxy_open = Mock.XHR.prototype.open; + // @ts-ignore + Mock.XHR.prototype.open = function () { + let responseType = this.responseType; + this.proxy_open(...arguments); + if (this.custom.xhr) { + if (responseType) { + this.custom.xhr.responseType = responseType; + } + } + }; for (const { url, method, response, timeout } of mockList) { __setupMock__(timeout); Mock.mock(new RegExp(url), method || 'get', __XHR2ExpressReqWrapper__(response, timeout)); diff --git a/package.json b/package.json index 2938433..12293b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-mock", - "version": "2.4.1", + "version": "2.4.2", "description": "A mock plugin for vite", "main": "dist/index.js", "files": [ diff --git a/src/createProdMockServer.ts b/src/createProdMockServer.ts index 812d586..f9db34d 100644 --- a/src/createProdMockServer.ts +++ b/src/createProdMockServer.ts @@ -1,3 +1,4 @@ +/* eslint-disable */ import Mock from 'mockjs'; export function createProdMockServer(mockList: any[]) { @@ -5,11 +6,32 @@ export function createProdMockServer(mockList: any[]) { Mock.XHR.prototype.__send = Mock.XHR.prototype.send; // @ts-ignore Mock.XHR.prototype.send = function () { - if (this.custom.xhr) this.custom.xhr.withCredentials = this.withCredentials || false; + if (this.custom.xhr) { + this.custom.xhr.withCredentials = this.withCredentials || false; + + if (this.responseType) { + this.custom.xhr.responseType = this.responseType; + } + } // eslint-disable-next-line this.__send.apply(this, arguments); }; + // @ts-ignore + Mock.XHR.prototype.proxy_open = Mock.XHR.prototype.open; + + // @ts-ignore + Mock.XHR.prototype.open = function () { + let responseType = this.responseType; + // @ts-ignore + this.proxy_open(...arguments); + if (this.custom.xhr) { + if (responseType) { + this.custom.xhr.responseType = responseType; + } + } + }; + for (const { url, method, response, timeout } of mockList) { __setupMock__(timeout); Mock.mock(new RegExp(url), method || 'get', __XHR2ExpressReqWrapper__(response, timeout));