Skip to content

Commit

Permalink
fix: production xhr
Browse files Browse the repository at this point in the history
  • Loading branch information
vben-admin committed Apr 1, 2021
1 parent f5a2af3 commit 6c94783
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
19 changes: 18 additions & 1 deletion es/createProdMockServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
24 changes: 23 additions & 1 deletion src/createProdMockServer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
/* eslint-disable */
import Mock from 'mockjs';

export function createProdMockServer(mockList: any[]) {
// @ts-ignore
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));
Expand Down

0 comments on commit 6c94783

Please sign in to comment.