Skip to content

Commit

Permalink
feat: 隔离传参,分组传
Browse files Browse the repository at this point in the history
  • Loading branch information
limengke123 committed Jul 11, 2019
1 parent 61285d7 commit 4fa3b7d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
6 changes: 4 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const swaggerPath = path.resolve(__dirname, './.swagger')
const schemaPath = path.resolve(__dirname, '.')

mock({
swaggerPath: swaggerPath,
schemaPath: schemaPath
schemaOption: {
schemaPath,
swaggerPath
}
})

27 changes: 24 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/generateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const SCHEMA_FILE = './schema.json'
const ERROR_PATH = '2. 生成 schema.json: '

export function generateSchema ([option]: optionTuple, force: boolean = false): Promise<optionTuple> {
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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export function loadConfig (option: option = {}): Promise<optionTuple> {
} 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} 成功解析配置文件`)
Expand Down
13 changes: 7 additions & 6 deletions src/tasks/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 4fa3b7d

Please sign in to comment.