Skip to content

Commit 6bdf0fe

Browse files
authored
fix: compatible with post request form data acquisition (#135) (#135)
1 parent 8759a88 commit 6bdf0fe

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

packages/vite-plugin-mock/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-mock",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "A mock plugin for vite",
55
"main": "dist/index.cjs",
66
"module": "dist/index.mjs",

packages/vite-plugin-mock/src/createMockServer.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,29 @@ function cleanRequireCache(opt: ViteMockOptions) {
153153

154154
function parseJson(req: IncomingMessage): Promise<Recordable> {
155155
return new Promise((resolve) => {
156-
let body = ''
157-
let jsonStr = ''
156+
let jsonStr: Recordable = {}
157+
let str = ''
158158
req.on('data', function (chunk) {
159-
body += chunk
159+
str += chunk
160160
})
161-
req.on('end', function () {
161+
req.on('end', () => {
162162
try {
163-
jsonStr = JSON.parse(body)
164-
} catch (err) {
165-
jsonStr = ''
163+
// json
164+
jsonStr = JSON.parse(str)
165+
} catch (e) {
166+
// x-www-form-urlencoded
167+
const params = new URLSearchParams(str)
168+
const body: Recordable = {}
169+
params.forEach((value, key) => {
170+
body[key] = value
171+
})
172+
jsonStr = body
166173
}
167-
resolve(jsonStr as any)
174+
resolve(jsonStr)
168175
return
169176
})
170177
})
171178
}
172-
173179
// load mock .ts files and watch
174180
async function getMockConfig(opt: ViteMockOptions, config: ResolvedConfig) {
175181
const { absConfigPath, absMockPath } = getPath(opt)

0 commit comments

Comments
 (0)