Skip to content

Commit

Permalink
feat: 🎸 master
Browse files Browse the repository at this point in the history
修复style
  • Loading branch information
陈家敬 committed Sep 30, 2020
1 parent b341784 commit d2c8f70
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"printWidth": 160,
"tabWidth": 2,
"useTabs": true,
"singleQuote": true,
"singleQuote": false,
"semi": true,
"trailingComma": "none",
"bracketSpacing": true
Expand Down
30 changes: 15 additions & 15 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';
import getPort from 'get-port';
import path from 'path';
import { getMockApis, HTTP_METHODS, METHODS } from './utils';
import express from "express";
import getPort from "get-port";
import path from "path";
import { getMockApis, HTTP_METHODS, METHODS } from "./utils";

const app = express();

Expand All @@ -16,7 +16,7 @@ interface IServerOptions {
export default class Mock {
private isHttps = false;
private port = 3000;
private host = '127.0.0.1';
private host = "127.0.0.1";
private resTime = 500;

constructor(options: IServerOptions) {
Expand All @@ -37,19 +37,19 @@ export default class Mock {
}

onCreateServer() {
const protocol = this.isHttps ? 'https://' : 'http://';
app.use(express.static(path.join(__dirname, './')));
const protocol = this.isHttps ? "https://" : "http://";
app.use(express.static(path.join(__dirname, "./")));
app.use((req, _res, next) => {
console.log(require('chalk').default.green(`请求地址URL: ${protocol}${this.host}:${this.port}${req.url}`));
console.log(require("chalk").default.green(`请求地址URL: ${protocol}${this.host}:${this.port}${req.url}`));
next();
});

app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', HTTP_METHODS.join());
app.all("*", function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild");
res.header("Access-Control-Allow-Methods", HTTP_METHODS.join());

if (req.method == 'OPTIONS') {
if (req.method == "OPTIONS") {
res.send(200);
} else {
next();
Expand Down Expand Up @@ -133,9 +133,9 @@ export default class Mock {

async start() {
const port = await getPort({ port: this.port });
const protocol = this.isHttps ? 'https://' : 'http://';
const protocol = this.isHttps ? "https://" : "http://";
app.listen(port, this.host, () => {
console.log(require('chalk').default.green(`\n数据 mock 服务已启动,Server 地址: ${protocol}${this.host}:${port}`));
console.log(require("chalk").default.green(`\n数据 mock 服务已启动,Server 地址: ${protocol}${this.host}:${port}`));
});
}
}
36 changes: 18 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import path from 'path';
import glob from 'glob';
import fs from 'fs';
import { Key, pathToRegexp } from 'path-to-regexp';
import path from "path";
import glob from "glob";
import fs from "fs";
import { Key, pathToRegexp } from "path-to-regexp";

export const MOCK_DIR = 'mock/data';
export const MOCK_DIR = "mock/data";
export const enum METHODS {
GET = 'GET',
POST = 'POST',
HEAD = 'HEAD',
PUT = 'PUT',
DELETE = 'DELETE',
CONNECT = 'CONNECT',
OPTIONS = 'OPTIONS',
TRACE = 'TRACE',
PATCH = 'PATCH'
GET = "GET",
POST = "POST",
HEAD = "HEAD",
PUT = "PUT",
DELETE = "DELETE",
CONNECT = "CONNECT",
OPTIONS = "OPTIONS",
TRACE = "TRACE",
PATCH = "PATCH"
}

export const HTTP_METHODS = [
Expand Down Expand Up @@ -41,7 +41,7 @@ export function getMockConfigs({ appPath, mocks }: { appPath: string; mocks?: {
const mockDir = path.join(appPath, MOCK_DIR);
let mockConfigs = {};
if (fs.existsSync(mockDir)) {
const mockFiles = glob.sync('**/*.[tj]s', {
const mockFiles = glob.sync("**/*.[tj]s", {
cwd: mockDir
});

Expand Down Expand Up @@ -70,7 +70,7 @@ export function parseMockApis(mockConfigs: { [x: string]: any }) {
Object.keys(mockConfigs).map((key) => {
const result = mockConfigs[key];
let method = METHODS.GET;
let apiPath = '';
let apiPath = "";
let responseTime = 500; // 数据返回时间毫秒,默认为500
const keyList = key.split(/\s+/g);
if (keyList.length >= 2) {
Expand All @@ -81,7 +81,7 @@ export function parseMockApis(mockConfigs: { [x: string]: any }) {
throw `配置的 HTTP 方法名 ${method} 不正确,应该是 ${HTTP_METHODS.toString()} 中的一员!`;
}
} else if (keyList.length <= 1) {
apiPath = keyList[0] ?? '';
apiPath = keyList[0] ?? "";
}
const keys: Key[] = [];
const reg = pathToRegexp(apiPath, keys);
Expand All @@ -93,7 +93,7 @@ export function parseMockApis(mockConfigs: { [x: string]: any }) {
keys,
result
});
console.log(require('chalk').default.cyan(`Generating api ${method} ${apiPath}`));
console.log(require("chalk").default.cyan(`Generating api ${method} ${apiPath}`));
});
return apiList;
}
Expand Down

0 comments on commit d2c8f70

Please sign in to comment.