A Mock tool that integrates data emulation with HTTP services.

features

  • 🤟 Easy to use
  • 🔄 model reusable
  • 💎 support TypeScript

The installation

$NPM install mokia --save-dev # or $yarn add mokia --devCopy the code

Basic usage

  1. Add a file like “mock.ts” :
import { mock, PORT, ServerConfig } from '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 configCopy the code
  1. Run the script to start the server:
$ npx mokia mock.tsCopy the code

Advanced usage

To minimize code duplication and keep code reusable, we recommend using JS class style writing:

import { decorators, mock, PORT, ServerConfig } from '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 configCopy the code

APIs

Server configuration parameters:

  • HOSTServer host. Default value'localhost'
  • PORTThe default value is port number of the server8080
  • PREFIXURL prefix, default is' '
  • SILENTWhether to hide request logs. The default value isfalse

Note: these parameters are not passed as strings, but as symbols, which you should import from the Mokia package.

import { PORT } from 'mokia'

export default {
  [HOST]: 'localhost',
  [PORT]: 3000,
  [PREFIX]: '/apis',
  [SILENT]: true,
  // ...
}Copy the code

The generator

All generators can be used directly as functions or decorators.

  • basis

    • boolean(chance? : number, value? : boolean): boolean
    • integer(max? : number): number
    • integer(min: number, max: number): number
    • natural(max? : number): number
    • natural(min: number, max: number): number
    • float(max? : number): number
    • float(min: number, max, fixed?) : number
    • float(min: number, max, dmin: number, dmax: number): number
    • char(pool: string): string
    • string(pool: string, length? : number): string
    • string(pool: string, min: number, max: number): string
  • composite

    • generate(mockable: Object | Function): any
    • array(proto: any, length? : number): any[]
    • array(proto: any, min: number, max: number): any[]
    • oneOf(list: any[]): any
    • manyOf(list: any[], length? : number): any[]
    • manyOf(list: any[], min: number, max: number): any[]
    • pick(proto: Object, length? : number): Object
    • pick(proto: Object, props: string | string[]): Object
    • pick(proto: Object, min: number, max: number): Object
  • The date of

    • datetime(format? : string): string
    • datetime(format: string, max: DateType): string
    • datetime(format: string, min: DateType, max: DateType): string
    • date(format? : string): string
    • date(format: string, max: DateType): string
    • date(format: string, min: DateType, max: DateType): string
    • time(format? : string): string
    • time(format: string, max: DateType): string
    • time(format: string, min: DateType, max: DateType): string
    • now(format? : string): string
  • The picture

    • image(size? : string, text? : string, background? : string, foreground? : string, format? : string): string
    • dataImage(size? : string, text? : string, background? : string, foreground? : string, format? : string): string
  • The text

    • word(length? : number): string
    • word(min: number, max: number): string
    • title(length? : number): string
    • title(min: number, max: number): string
    • sentence(length? : number): string
    • sentence(min: number, max: number): string
    • paragraph(length? : number): string
    • paragraph(min: number, max: number): string
    • passage(length? : number): string
    • passage(min: number, max: number): string
    • zh.word(length? : number): string
    • zh.word(min: number, max: number): string
    • zh.title(length? : number): string
    • zh.title(min: number, max: number): string
    • zh.sentence(length? : number): string
    • zh.sentence(min: number, max: number): string
    • zh.paragraph(length? : number): string
    • zh.paragraph(min: number, max: number): string
    • zh.passage(length? : number): string
    • zh.passage(min: number, max: number): string
  • color

    • color(): string
    • rgb(): string
    • rgba(): string
    • hex(): string
    • hsl(): string
  • Web

    • protocol(): string
    • tld(): string
    • ip(): string
    • ipv6(): string
    • port(min? : number, max? : number): number
    • domain(tld? : string): string
    • url(protocol? : string, host? : string, prefix? : string): string
    • email(domain? : string)
  • figure

    • age(min? : number, max? : number): number
    • birthday(format? : string): string
    • fullName(): string
    • firstName(): string
    • lastName(): string
    • zh.fullName(): string
    • zh.firstName(): string
    • zh.lastName(): string
    • zh.phone(): string
    • zh.idNumber(): string
  • region

    • zh.region(): { code: string, name: string }
    • zh.regionName(): string
    • zh.province(): { code: string, name: string }
    • zh.provinceName(): string
    • zh.city(): { code: string, name: string }
    • zh.cityName(): string
    • zh.county(): { code: string, name: string }
    • zh.countyName(): string
    • zh.zipCode(): string
  • Id

    • uuid(): string
    • increment(step: number): number

License

MIT