Sardine
is one step mock tool, via swagger v2
, chance
, koa
. It will check request
and return response
. You can set swagger format
property with chance method name to fake different types data~
npm install @pregalaxyer/sardine -D
//or
yarn add @pregalaxyer/sardine -D
// mock.js
const {
default: Sardine,
chanceInstance,
responseBodyMiddleware // a middleware Examples
} = require('@pregalaxyer/sardine')
new Sardine({
url: 'https://petstore.swagger.io/v2/swagger.json',
port: 9000,
responseMiddleWares: [
// sardine will fake the code & msg field in body with provide value
// { code: '0', 'msg': 'success', ...other fake data}
responseBodyMiddleware([
{ key: 'code', value: '0' },
{ key: 'msg', value: 'success' }
])
]
})
Run node mock.js
to start your first sardine mock server
Options
:
interface SardineOptions {
url: string
port?: number
requestMiddlewares?: Middleware[]
responseMiddleWares?: Middleware[]
}
/* make it is possible to config your chanceInstance
*/
public chanceInstance: {
/**
* config chance fake count of arrays
*/
__DEFAULT_ARRAY_COUNT?: number
/**
* max stack size for nest object
*/
_MAX_NEST_STACK_SIZE?: number
} = chanceInstance
url
swagger json urlport
mock sever portrequestMiddlewares
&responseMiddleWares
middleware use in koa mock server, more about koa
💡: Newton's 🍎
However, the format property is an open string-valued property, and can have any value to support documentation needs. Formats such as "email", "uuid", etc., can be used even though they are not defined by this specification
You can create a lot fake data via different format type(id, card, png, city)
🚥 : Rules
-
number
types useminimum
&maximum
-
string
types usemaxLength
&minLength
-
array
types usemaxItems
&minItems
-
enum
types use foroptions
-
default
value use for fake value
- swagger v3 support
- request validtor middleware(options 😄)