From fdeecc670a2319d3d658ca4c996ffc21887ad636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Tue, 8 Jan 2019 16:42:29 +0800 Subject: [PATCH] use a new mirror address (#3314) --- README.md | 4 +-- functions/index.js | 3 +- functions/matchMock.js | 66 ++++++++++++++++++++---------------------- functions/package.json | 10 +++---- package.json | 8 ++--- 5 files changed, 45 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 6952f109..73e518b6 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 f4c65f90..609b51ca 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 4ef7ae89..924573a5 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 1fde857f..dd728197 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 e0b38ec3..7b39ad7b 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", -- GitLab