From fcfa86a1caf31bd927346b4793ebae2d23aeb3bc Mon Sep 17 00:00:00 2001 From: chencheng Date: Thu, 14 Jun 2018 18:19:29 +0800 Subject: [PATCH 1/2] fix(umi-build-dev): dynamic param don't work with mock --- .../src/plugins/mock/createMockMiddleware.js | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/umi-build-dev/src/plugins/mock/createMockMiddleware.js b/packages/umi-build-dev/src/plugins/mock/createMockMiddleware.js index e0099335a5b3..fc14df5667ba 100644 --- a/packages/umi-build-dev/src/plugins/mock/createMockMiddleware.js +++ b/packages/umi-build-dev/src/plugins/mock/createMockMiddleware.js @@ -4,6 +4,7 @@ import bodyParser from 'body-parser'; import glob from 'glob'; import assert from 'assert'; import chokidar from 'chokidar'; +import pathToRegexp from 'path-to-regexp'; const VALID_METHODS = ['get', 'post', 'put', 'patch', 'delete']; @@ -107,9 +108,13 @@ export default function getMockMiddleware(api) { `mock value of ${key} should be function or object, but got ${type}`, ); const { method, path } = parseKey(key); + const keys = []; + const re = pathToRegexp(path, keys); memo.push({ method, path, + re, + keys, handler: createHandler(method, path, handler), }); return memo; @@ -128,13 +133,53 @@ export default function getMockMiddleware(api) { const { path: exceptPath } = req; const exceptMethod = req.method.toLowerCase(); - return mockData.filter(({ method, path }) => { - return method === exceptMethod && path === exceptPath; + for (const mock of mockData) { + const { method, re, keys } = mock; + if (method === exceptMethod) { + const match = re.exec(req.path); + if (match) { + const params = {}; + + for (let i = 1; i < match.length; i = i + 1) { + const key = keys[i - 1]; + const prop = key.name; + const val = decodeParam(match[i]); + + if (val !== undefined || !hasOwnProperty.call(params, prop)) { + params[prop] = val; + } + } + req.params = params; + return mock; + } + } + } + + function decodeParam(val) { + if (typeof val !== 'string' || val.length === 0) { + return val; + } + + try { + return decodeURIComponent(val); + } catch (err) { + if (err instanceof URIError) { + err.message = `Failed to decode param ' ${val} '`; + err.status = err.statusCode = 400; + } + + throw err; + } + } + + return mockData.filter(({ method, re }) => { + return method === exceptMethod && re.test(exceptPath); })[0]; } return (req, res, next) => { const match = matchMock(req); + if (match) { debug(`mock matched: [${match.method}] ${match.path}`); return match.handler(req, res, next); From 70889f80927fbd4d3b435a2da839e2df31e1af58 Mon Sep 17 00:00:00 2001 From: chencheng Date: Thu, 14 Jun 2018 18:24:22 +0800 Subject: [PATCH 2/2] add path-to-regexp deps --- packages/umi-build-dev/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/umi-build-dev/package.json b/packages/umi-build-dev/package.json index a4312bc685cc..6f7db6c056d2 100644 --- a/packages/umi-build-dev/package.json +++ b/packages/umi-build-dev/package.json @@ -27,6 +27,7 @@ "mkdirp": "^0.5.1", "object-assign": "^4.1.1", "path-is-absolute": "^1.0.1", + "path-to-regexp": "^1.7.0", "postcss-plugin-px2rem": "^0.7.0", "preact": "^8.2.5", "preact-compat": "^3.17.0",