Unverified Commit fdeecc67 authored by ι™ˆεΈ…'s avatar ι™ˆεΈ… Committed by GitHub

use a new mirror address (#3314)

parent 09fbaa96
...@@ -94,8 +94,8 @@ $ npm start # visit http://localhost:8000 ...@@ -94,8 +94,8 @@ $ npm start # visit http://localhost:8000
```bash ```bash
# preview # preview
$ docker pull chenshuai2144/ant-design-pro $ docker pull antdesign/ant-design-pro
$ docker run -p 80:80 chenshuai2144/ant-design-pro $ docker run -p 80:80 antdesign/ant-design-pro
# open http://localhost # open http://localhost
# dev # dev
......
// [START functionsimport] // [START functionsimport]
const functions = require('firebase-functions'); const functions = require('firebase-functions');
const express = require('express'); const express = require('express');
const matchMock = require('./matchMock'); const matchMock = require('./matchMock');
const app = express(); const app = express();
app.use(matchMock); app.use(matchMock);
exports.api = functions.https.onRequest(app); exports.api = functions.https.onRequest(app);
const mockFile = require('./mock/index');
const pathToRegexp = require('path-to-regexp'); const pathToRegexp = require('path-to-regexp');
const debug = console.log;
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const mockFile = require('./mock/index');
const BODY_PARSED_METHODS = ['post', 'put', 'patch']; const BODY_PARSED_METHODS = ['post', 'put', 'patch'];
const debug = console.log;
function parseKey(key) { function parseKey(key) {
let method = 'get'; let method = 'get';
let path = key; let path = key;
...@@ -20,7 +21,14 @@ function parseKey(key) { ...@@ -20,7 +21,14 @@ function parseKey(key) {
} }
function createHandler(method, path, handler) { 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)) { if (BODY_PARSED_METHODS.includes(method)) {
bodyParser.json({ limit: '5mb', strict: false })(req, res, () => { bodyParser.json({ limit: '5mb', strict: false })(req, res, () => {
bodyParser.urlencoded({ limit: '5mb', extended: true })(req, res, () => { bodyParser.urlencoded({ limit: '5mb', extended: true })(req, res, () => {
...@@ -30,14 +38,6 @@ function createHandler(method, path, handler) { ...@@ -30,14 +38,6 @@ function createHandler(method, path, handler) {
} else { } else {
sendData(); sendData();
} }
function sendData() {
if (typeof handler === 'function') {
handler(req, res, next);
} else {
res.json(handler);
}
}
}; };
} }
...@@ -63,6 +63,24 @@ const mockData = normalizeConfig(mockFile); ...@@ -63,6 +63,24 @@ const mockData = normalizeConfig(mockFile);
function matchMock(req) { function matchMock(req) {
const { path: exceptPath } = req; const { path: exceptPath } = req;
const exceptMethod = req.method.toLowerCase(); 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) { for (const mock of mockData) {
const { method, re, keys } = mock; const { method, re, keys } = mock;
if (method === exceptMethod) { if (method === exceptMethod) {
...@@ -70,7 +88,7 @@ function matchMock(req) { ...@@ -70,7 +88,7 @@ function matchMock(req) {
if (match) { if (match) {
const params = {}; 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 key = keys[i - 1];
const prop = key.name; const prop = key.name;
const val = decodeParam(match[i]); const val = decodeParam(match[i]);
...@@ -85,33 +103,13 @@ function matchMock(req) { ...@@ -85,33 +103,13 @@ function matchMock(req) {
} }
} }
function decodeParam(val) { return mockData.filter(({ method, re }) => method === exceptMethod && re.test(exceptPath))[0];
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];
} }
module.exports = (req, res, next) => { module.exports = (req, res, next) => {
const match = matchMock(req); const match = matchMock(req);
if (match) { if (match) {
debug(`mock matched: [${match.method}] ${match.path}`); debug(`mock matched: [${match.method}] ${match.path}`);
return match.handler(req, res, next); return match.handler(req, res, next);
} else {
return next();
} }
return next();
}; };
...@@ -2,19 +2,19 @@ ...@@ -2,19 +2,19 @@
"name": "functions", "name": "functions",
"description": "Cloud Functions for Firebase", "description": "Cloud Functions for Firebase",
"scripts": { "scripts": {
"serve": "npm run mock && firebase serve --only functions", "serve": "firebase serve --only functions",
"shell": "firebase functions:shell", "shell": "firebase functions:shell",
"start": "npm run shell", "start": "npm run shell",
"deploy": "firebase deploy --only functions", "deploy": "npm run mock && firebase deploy --only functions",
"logs": "firebase functions:log", "logs": "firebase functions:log",
"mock": "node ../scripts/generateMock.js" "mock": "node ../scripts/generateMock.js"
}, },
"dependencies": { "dependencies": {
"@babel/runtime": "^7.0.0", "@babel/runtime": "^7.0.0",
"body-parser": "^1.18.3", "body-parser": "^1.18.3",
"express": "^4.16.3", "express": "^4.16.4",
"firebase-admin": "^5.12.1", "firebase-admin": "^6.4.0",
"firebase-functions": "^2.0.5", "firebase-functions": "^2.1.0",
"mockjs": "^1.0.1-beta3", "mockjs": "^1.0.1-beta3",
"moment": "^2.22.2", "moment": "^2.22.2",
"path-to-regexp": "^2.2.1" "path-to-regexp": "^2.2.1"
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"description": "An out-of-box UI solution for enterprise applications", "description": "An out-of-box UI solution for enterprise applications",
"private": true, "private": true,
"scripts": { "scripts": {
"presite": "node ./scripts/generateMock.js && cd functions && npm install", "presite": "cd functions && npm install",
"start": "cross-env APP_TYPE=site umi dev", "start": "cross-env APP_TYPE=site umi dev",
"start:no-mock": "cross-env MOCK=none umi dev", "start:no-mock": "cross-env MOCK=none umi dev",
"build": "umi build", "build": "umi build",
...@@ -27,8 +27,8 @@ ...@@ -27,8 +27,8 @@
"docker-prod:dev": "docker-compose -f ./docker/docker-compose.yml up", "docker-prod:dev": "docker-compose -f ./docker/docker-compose.yml up",
"docker-prod:build": "docker-compose -f ./docker/docker-compose.yml build", "docker-prod:build": "docker-compose -f ./docker/docker-compose.yml build",
"docker-hub:build": "docker build -f Dockerfile.hub -t ant-design-pro ./", "docker-hub:build": "docker build -f Dockerfile.hub -t ant-design-pro ./",
"docker:tag": "docker tag ant-design-pro 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 chenshuai2144/ant-design-pro" "docker:push": "npm run docker-hub:build && npm run docker:tag && docker push antdesign/ant-design-pro"
}, },
"dependencies": { "dependencies": {
"@antv/data-set": "^0.10.0", "@antv/data-set": "^0.10.0",
...@@ -83,7 +83,7 @@ ...@@ -83,7 +83,7 @@
"husky": "^1.2.0", "husky": "^1.2.0",
"jest-puppeteer": "^3.5.1", "jest-puppeteer": "^3.5.1",
"lint-staged": "^8.1.0", "lint-staged": "^8.1.0",
"merge-umi-mock-data": "^0.0.3", "merge-umi-mock-data": "^1.0.4",
"mockjs": "^1.0.1-beta3", "mockjs": "^1.0.1-beta3",
"prettier": "1.15.2", "prettier": "1.15.2",
"pro-download": "^1.0.1", "pro-download": "^1.0.1",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment