Skip to content

Commit

Permalink
fix: fix post proxy error
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Feb 6, 2021
1 parent 8c3cd9d commit d3ae41e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
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.1.0",
"version": "2.1.1",
"description": "A mock plugin for vite",
"main": "dist/index.js",
"files": [
Expand Down
28 changes: 24 additions & 4 deletions src/createMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import { rollup } from 'rollup';
import esbuildPlugin from 'rollup-plugin-esbuild';
import dayjs from 'dayjs';

import { NextHandleFunction } from 'connect';
import createServer, { NextHandleFunction } from 'connect';
import { Connect } from 'vite';

const pathResolve = require('@rollup/plugin-node-resolve');

let mockData: MockMethod[] = [];
export let mockData: MockMethod[] = [];

export async function createMockServer(
opt: ViteMockOptions = { mockPath: 'mock', ignoreFiles: [], configPath: 'vite.mock.config' }
Expand Down Expand Up @@ -62,7 +63,7 @@ function getInvokeTime(opt: ViteMockOptions): string {

// request match
// @ts-ignore
export function requestMiddle(opt: ViteMockOptions) {
export async function requestMiddle(opt: ViteMockOptions) {
const middleware: NextHandleFunction = async (req, res, next) => {
let queryParams: {
query?: {
Expand Down Expand Up @@ -90,7 +91,7 @@ export function requestMiddle(opt: ViteMockOptions) {
}
const { query = {} } = queryParams;

const body = (req as any).body;
const body = (await parseJson(req)) as Record<string, any>;
const mockRes = isFunction(response) ? response({ body, query }) : response;
console.log(
`${chalk.green(
Expand Down Expand Up @@ -142,6 +143,25 @@ function cleanRequireCache(opt: ViteMockOptions) {
});
}

function parseJson(req: createServer.IncomingMessage) {
return new Promise((resolve) => {
let body = '';
let jsonStr = '';
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
try {
jsonStr = JSON.parse(body);
} catch (err) {
jsonStr = '';
}
resolve(jsonStr);
return;
});
});
}

// load mock .ts files and watch
async function getMockConfig(opt: ViteMockOptions) {
cleanRequireCache(opt);
Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ViteMockOptions } from './types';
import { Plugin, ResolvedConfig, normalizePath } from 'vite';
import { createMockServer, requestMiddle } from './createMockServer';
import bodyParser from 'body-parser';
import path from 'path';

export function viteMockServe(opt: ViteMockOptions): Plugin {
Expand All @@ -24,15 +23,17 @@ export function viteMockServe(opt: ViteMockOptions): Plugin {
needSourcemap = resolvedConfig.isProduction && !!resolvedConfig.build.sourcemap;
},

configureServer: ({ middlewares }) => {
configureServer: async ({ middlewares }) => {
const { localEnabled = isDev } = opt;
if (!localEnabled) return;
createMockServer(opt);

// parse application/x-www-form-urlencoded
middlewares.use(bodyParser.urlencoded({ extended: false }));
// middlewares.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
middlewares.use(bodyParser.json());
middlewares.use(requestMiddle(opt));
// middlewares.use(bodyParser.json());
const middleware = await requestMiddle(opt);
middlewares.use(middleware);
},
async transform(code: string, id: string) {
const getMap = () => (needSourcemap ? this.getCombinedSourcemap() : null);
Expand Down

0 comments on commit d3ae41e

Please sign in to comment.