diff --git a/example/index.js b/example/index.js index 7d417b5..6dfa805 100644 --- a/example/index.js +++ b/example/index.js @@ -5,7 +5,9 @@ const swaggerPath = path.resolve(__dirname, './.swagger') const schemaPath = path.resolve(__dirname, '.') mock({ - swaggerPath: swaggerPath, - schemaPath: schemaPath + schemaOption: { + schemaPath, + swaggerPath + } }) diff --git a/src/index.ts b/src/index.ts index f1b5ec0..52dcd88 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,14 +4,35 @@ import { generateData } from './tasks/generateData' import { startServer } from './tasks/server' import { error } from './util/commonUtil' +/** + * schemaOption swagger文本解析的设置 + * dbOption db上的设置 + * serverOption 本地服务的设置 + * */ export interface option { - swaggerPath?: string, - schemaPath?: string, + schemaOption?: schemaOption + dbOption?: dbOption + serverOption?: serverOption +} + +export interface schemaOption { + schemaPath?: string + swaggerPath?: string +} + +export interface dbOption { +} + +export interface serverOption { port?: number } const defaultOption: option = { - port: 3000 + schemaOption: {}, + dbOption: {}, + serverOption: { + port: 3000 + } } export type optionTuple = [option, any] diff --git a/src/tasks/generateSchema.ts b/src/tasks/generateSchema.ts index fb70b70..892916f 100644 --- a/src/tasks/generateSchema.ts +++ b/src/tasks/generateSchema.ts @@ -10,7 +10,8 @@ const SCHEMA_FILE = './schema.json' const ERROR_PATH = '2. 生成 schema.json: ' export function generateSchema ([option]: optionTuple, force: boolean = false): Promise { - const { swaggerPath, schemaPath } = option + const { schemaOption } = option + const { swaggerPath, schemaPath } = schemaOption! const SCHEMA_FILE_PATH = path.resolve(schemaPath as string, SCHEMA_FILE) return accessFile(SCHEMA_FILE_PATH) .then(([exists, write]: access) => { diff --git a/src/tasks/loadConfig.ts b/src/tasks/loadConfig.ts index 2879acc..c85ca30 100644 --- a/src/tasks/loadConfig.ts +++ b/src/tasks/loadConfig.ts @@ -18,10 +18,10 @@ export function loadConfig (option: option = {}): Promise { } else { mergeOption = option } - if (!mergeOption.schemaPath) { + if (!mergeOption.schemaOption!.schemaPath) { throw new Error(ERROR_PATH + '不存在 schemaPath 字段') } - if (!mergeOption.swaggerPath) { + if (!mergeOption.schemaOption!.swaggerPath) { throw new Error(ERROR_PATH + '不存在 swaggerPath 字段') } success(`${ERROR_PATH} 成功解析配置文件`) diff --git a/src/tasks/server.ts b/src/tasks/server.ts index aa8fdda..495f002 100644 --- a/src/tasks/server.ts +++ b/src/tasks/server.ts @@ -3,15 +3,16 @@ import { optionTuple } from '../index' import { success } from '../util/commonUtil' export const startServer = ([option, dataPath]: optionTuple) => { - const { port } = option - const server = jsonServer.create() + const { serverOption } = option + const { port } = serverOption! + const app = jsonServer.create() const router = jsonServer.router(dataPath) const middleware = jsonServer.defaults() - server.use(middleware) - server.use(jsonServer.bodyParser) - server.use(router) - server.listen(port, () => { + app.use(middleware) + app.use(jsonServer.bodyParser) + app.use(router) + app.listen(port, () => { success(`mock server is start localhost: ${port}`, true) }) }