Fork of varHarrie/mokia;
- Remove image & canvas .
- Switch tslint to eslint.
- Upgrade department expired dependency packages.
A mock server integrated data simulation and http service.
- π€ Simple, easy to use
- π Reusable model
- π Support TypeScript
- Install mokia
$ npm install @feq/mokia --save-dev
# Or
$ yarn add @feq/mokia --dev
- Adds a ts file like
mock.ts
:
import { mock, PORT, ServerConfig } from '@feq/mokia'
const config: ServerConfig = {
[PORT]: 3000,
'GET /users': () => {
return {
users: mock.array({
id: mock.uuid(),
name: mock.fullName()
}, 0, 5)
}
},
'GET /users/:id': () => {
return {
id: mock.uuid(),
name: mock.fullName()
}
}
}
export default config
- Add script to
package.json
:
"scripts": {
"mock": "mokia mock.ts",
}
- Run script
npm run mock
to start a http server.
To reduce duplicated code and keep reusability, we recommend to use class style:
import { decorators, mock, PORT, ServerConfig } from '@feq/mokia'
class User {
@decorators.uuid()
id: string
@decorators.fullName()
name: string
}
const config: ServerConfig = {
[PORT]: 3000,
'GET /users': () => {
return {
users: mock.array(User, 0, 5)
}
},
'GET /users/:id': () => {
return mock(User)
}
}
export default config
HOST
Server host, default to'localhost'
PORT
Server port, default to8080
PREFIX
URL prefix, default to''
PRIORITY
priority urlοΌall requests are redirected to this address first, default to''
SILENT
whether to hide request logs, default tofalse
INTERCEPTORS
route interceptors, default to{}
Note: The keys of those parameters are Symbol
, instead of string
, so you should import they from mokia
.
import { HOST, PORT, PREFIX, PRIORITY, SILENT, INTERCEPTORS } from '@feq/mokia'
export default {
[HOST]: 'localhost',
[PORT]: 3000,
[PREFIX]: '/apis',
[PRIORITY]: 'http://another.domain.com',
[SILENT]: true,
[INTERCEPTORS]: {
request: (req, res) => {
console.log('before')
},
response: (req, res, data) => {
console.log('after')
return {
code: 200,
data
}
}
}
// ...
}
All generators can be use as function
or decorator
.
import { decorators, generators, mock } from '@feq/mokia'
// As decorator
class User {
@decorators.boolean()
isAdmin: boolean
}
// Or just a normal function
const bool = generators.boolean()
// Be equivalent to
const bool = mock.boolean()
-
Basic
boolean
(chance?: number, value?: boolean): booleaninteger
(max?: number): numberinteger
(min: number, max: number): numbernatural
(max?: number): numbernatural
(min: number, max: number): numberfloat
(max?: number): numberfloat
(min: number, max, fixed?): numberfloat
(min: number, max, dmin: number, dmax: number): numberchar
(pool: string): stringstring
(pool: string, length?: number): stringstring
(pool: string, min: number, max: number): string
-
Complex
generate
(mockable: Object | Function): anyarray
(proto: any, length?: number): any[]array
(proto: any, min: number, max: number): any[]oneOf
(list: any[]): anymanyOf
(list: any[], length?: number): any[]manyOf
(list: any[], min: number, max: number): any[]pick
(proto: Object, length?: number): Objectpick
(proto: Object, props: string | string[]): Objectpick
(proto: Object, min: number, max: number): Object
-
Date
datetime
(format?: string): stringdatetime
(format: string, max: DateType): stringdatetime
(format: string, min: DateType, max: DateType): stringdate
(format?: string): stringdate
(format: string, max: DateType): stringdate
(format: string, min: DateType, max: DateType): stringtime
(format?: string): stringtime
(format: string, max: DateType): stringtime
(format: string, min: DateType, max: DateType): stringtimestamp
(max?: DateType): stringtimestamp
(min: DateType, max: DateType): stringnow
(format?: string): string
-
Text
word
(length?: number): stringword
(min: number, max: number): stringtitle
(length?: number): stringtitle
(min: number, max: number): stringsentence
(length?: number): stringsentence
(min: number, max: number): stringparagraph
(length?: number): stringparagraph
(min: number, max: number): stringpassage
(length?: number): stringpassage
(min: number, max: number): stringzh.word
(length?: number): stringzh.word
(min: number, max: number): stringzh.title
(length?: number): stringzh.title
(min: number, max: number): stringzh.sentence
(length?: number): stringzh.sentence
(min: number, max: number): stringzh.paragraph
(length?: number): stringzh.paragraph
(min: number, max: number): stringzh.passage
(length?: number): stringzh.passage
(min: number, max: number): string
-
Color
color
(): stringrgb
(): stringrgba
(): stringhex
(): stringhsl
(): string
-
Web
protocol
(): stringtld
(): stringip
(): stringipv6
(): stringport
(min?: number, max?: number): numberdomain
(tld?: string): stringurl
(protocol?: string, host?: string, prefix?: string): stringemail
(domain?: string)
-
Person
age
(min?: number, max?: number): numberbirthday
(format?: string): stringfullName
(): stringfirstName
(): stringlastName
(): stringzh.fullName
(): stringzh.firstName
(): stringzh.lastName
(): stringzh.phone
(): stringzh.idNumber
(): string
-
Region
zh.region
(): { code: string, name: string }zh.regionName
(): stringzh.province
(): { code: string, name: string }zh.provinceName
(): stringzh.city
(): { code: string, name: string }zh.cityName
(): stringzh.county
(): { code: string, name: string }zh.countyName
(): stringzh.zipCode
(): string
-
Id
uuid
(): stringincrement
(step: number): number