diff --git a/README.md b/README.md index 6952f1099a6eff21415001c1ad0bdda30fef69ea..73e518b64305057e15664c359df40ced7cc07d2c 100644 --- a/README.md +++ b/README.md @@ -94,8 +94,8 @@ $ npm start # visit http://localhost:8000 ```bash # preview -$ docker pull chenshuai2144/ant-design-pro -$ docker run -p 80:80 chenshuai2144/ant-design-pro +$ docker pull antdesign/ant-design-pro +$ docker run -p 80:80 antdesign/ant-design-pro # open http://localhost # dev diff --git a/functions/index.js b/functions/index.js index f4c65f902973e024bc6cff8edf9a6c34a9ca4310..609b51ca4df69bf827cac9ba7e600bd6685eec89 100644 --- a/functions/index.js +++ b/functions/index.js @@ -1,9 +1,10 @@ // [START functionsimport] const functions = require('firebase-functions'); const express = require('express'); + const matchMock = require('./matchMock'); + const app = express(); app.use(matchMock); - exports.api = functions.https.onRequest(app); diff --git a/functions/matchMock.js b/functions/matchMock.js index 4ef7ae890c89a6233a145c127587c9bef7d86078..924573a53a6c1cf5b58a45f43487b06b96516929 100644 --- a/functions/matchMock.js +++ b/functions/matchMock.js @@ -1,10 +1,11 @@ -const mockFile = require('./mock/index'); const pathToRegexp = require('path-to-regexp'); -const debug = console.log; const bodyParser = require('body-parser'); +const mockFile = require('./mock/index'); + const BODY_PARSED_METHODS = ['post', 'put', 'patch']; +const debug = console.log; function parseKey(key) { let method = 'get'; let path = key; @@ -20,7 +21,14 @@ function parseKey(key) { } function createHandler(method, path, handler) { - return function(req, res, next) { + return (req, res, next) => { + function sendData() { + if (typeof handler === 'function') { + handler(req, res, next); + } else { + res.json(handler); + } + } if (BODY_PARSED_METHODS.includes(method)) { bodyParser.json({ limit: '5mb', strict: false })(req, res, () => { bodyParser.urlencoded({ limit: '5mb', extended: true })(req, res, () => { @@ -30,14 +38,6 @@ function createHandler(method, path, handler) { } else { sendData(); } - - function sendData() { - if (typeof handler === 'function') { - handler(req, res, next); - } else { - res.json(handler); - } - } }; } @@ -63,6 +63,24 @@ const mockData = normalizeConfig(mockFile); function matchMock(req) { const { path: exceptPath } = req; const exceptMethod = req.method.toLowerCase(); + 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.statusCode = 400; + err.status = 400; + } + + throw err; + } + } + // eslint-disable-next-line no-restricted-syntax for (const mock of mockData) { const { method, re, keys } = mock; if (method === exceptMethod) { @@ -70,7 +88,7 @@ function matchMock(req) { if (match) { const params = {}; - for (let i = 1; i < match.length; i = i + 1) { + for (let i = 1; i < match.length; i += 1) { const key = keys[i - 1]; const prop = key.name; const val = decodeParam(match[i]); @@ -85,33 +103,13 @@ function matchMock(req) { } } - 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 mockData.filter(({ method, re }) => method === exceptMethod && re.test(exceptPath))[0]; } module.exports = (req, res, next) => { const match = matchMock(req); if (match) { debug(`mock matched: [${match.method}] ${match.path}`); return match.handler(req, res, next); - } else { - return next(); } + return next(); }; diff --git a/functions/package.json b/functions/package.json index 1fde857f4d49ef034a6d55afd188a496c3cae466..dd7281977e6816559ab0d5142b98a57358248d96 100644 --- a/functions/package.json +++ b/functions/package.json @@ -2,19 +2,19 @@ "name": "functions", "description": "Cloud Functions for Firebase", "scripts": { - "serve": "npm run mock && firebase serve --only functions", + "serve": "firebase serve --only functions", "shell": "firebase functions:shell", "start": "npm run shell", - "deploy": "firebase deploy --only functions", + "deploy": "npm run mock && firebase deploy --only functions", "logs": "firebase functions:log", "mock": "node ../scripts/generateMock.js" }, "dependencies": { "@babel/runtime": "^7.0.0", "body-parser": "^1.18.3", - "express": "^4.16.3", - "firebase-admin": "^5.12.1", - "firebase-functions": "^2.0.5", + "express": "^4.16.4", + "firebase-admin": "^6.4.0", + "firebase-functions": "^2.1.0", "mockjs": "^1.0.1-beta3", "moment": "^2.22.2", "path-to-regexp": "^2.2.1" diff --git a/package.json b/package.json index e0b38ec347005bd78f6d3e634f34f916f073cc42..7b39ad7b792b1e336f47a9c1c5f82afea2c904f6 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "An out-of-box UI solution for enterprise applications", "private": true, "scripts": { - "presite": "node ./scripts/generateMock.js && cd functions && npm install", + "presite": "cd functions && npm install", "start": "cross-env APP_TYPE=site umi dev", "start:no-mock": "cross-env MOCK=none umi dev", "build": "umi build", @@ -27,8 +27,8 @@ "docker-prod:dev": "docker-compose -f ./docker/docker-compose.yml up", "docker-prod:build": "docker-compose -f ./docker/docker-compose.yml build", "docker-hub:build": "docker build -f Dockerfile.hub -t ant-design-pro ./", - "docker:tag": "docker tag ant-design-pro chenshuai2144/ant-design-pro", - "docker:push": "npm run docker-hub:build && npm run docker:tag && docker push chenshuai2144/ant-design-pro" + "docker:tag": "docker tag ant-design-pro antdesign/ant-design-pro", + "docker:push": "npm run docker-hub:build && npm run docker:tag && docker push antdesign/ant-design-pro" }, "dependencies": { "@antv/data-set": "^0.10.0", @@ -83,7 +83,7 @@ "husky": "^1.2.0", "jest-puppeteer": "^3.5.1", "lint-staged": "^8.1.0", - "merge-umi-mock-data": "^0.0.3", + "merge-umi-mock-data": "^1.0.4", "mockjs": "^1.0.1-beta3", "prettier": "1.15.2", "pro-download": "^1.0.1",