openapi-mock-express-middleware
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -5,2 +5,4 @@ # Changelog | ||
## [0.2.0](https://github.com/aleksandryackovlev/openapi-mock-express-middleware/compare/v0.1.0...v0.2.0) (2020-02-25) | ||
## 0.1.0 (2020-02-24) | ||
@@ -7,0 +9,0 @@ |
{ | ||
"name": "openapi-mock-express-middleware", | ||
"version": "0.1.0", | ||
"description": "Openapi mock middleware", | ||
"version": "0.2.0", | ||
"description": "Generates an express mock server from an Open API spec", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "typings": "dist/index.d.ts", |
178
README.md
@@ -29,3 +29,3 @@ <div align="center"> | ||
## Usage | ||
### Simple Usage | ||
### Simple Config | ||
```javascript | ||
@@ -43,2 +43,178 @@ const express = require('express'); | ||
### Advanced Config | ||
The middleware uses [json-schmea-faker](https://github.com/json-schema-faker/json-schema-faker) under the hood. To configure it, you can pass locale and the options object to the factory function. (The full list of available options can be seen [here](https://github.com/json-schema-faker/json-schema-faker/tree/master/docs#available-options)) | ||
```javascript | ||
const express = require('express'); | ||
const mockServer = require('openapi-mock-express-middleware'); | ||
const app = express(); | ||
app.use( | ||
'/api', // root path for the mock server | ||
mockServer({ | ||
file: '/absolute/path/to/your/openapi/spec.yml' | ||
locale: 'ru', // json-schema-faker locale, default to 'en' | ||
{ | ||
alwaysFakeOptionals: true, | ||
useDefaultValue: true, | ||
// ... | ||
}, // json-schema-faker options | ||
}) | ||
); | ||
app.listen(80, () => console.log('Server listening on port 80'))'' | ||
``` | ||
## Mock data | ||
### Basic behavior | ||
By default midleware generates random responses depending on the types specified in the openapi docs. | ||
**doc.yml** | ||
``` | ||
... | ||
paths: | ||
/company | ||
get: | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
required: | ||
- id | ||
- number | ||
properties: | ||
id: | ||
type: string | ||
number: | ||
type: integer | ||
... | ||
``` | ||
**GET /company response** | ||
```javascript | ||
{ | ||
id: 'dolor veniam consequat laborum', | ||
number: 68385409. | ||
} | ||
``` | ||
### Faker generated responses | ||
In addition faker functions can be specified for data generation. The list of all available function can be found in the [faker documentation](https://github.com/marak/Faker.js/#api-methods). | ||
**doc.yml** | ||
``` | ||
... | ||
paths: | ||
/user | ||
get: | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
required: | ||
- id | ||
- name | ||
properties: | ||
id: | ||
type: string | ||
x-faker: random.uuid | ||
name: | ||
type: string | ||
x-faker: name.findName | ||
... | ||
``` | ||
**GET /user response** | ||
```javascript | ||
{ | ||
id: '8c4a4ed2-efba-4913-9604-19a27f36f322', | ||
name: 'Mr. Braxton Dickens'. | ||
} | ||
``` | ||
### Responses generated from examples | ||
If an example for the response object is specified, it will be used as a resulting sever response. | ||
**doc.yml** | ||
``` | ||
... | ||
paths: | ||
/user | ||
get: | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
example: | ||
id: 'id-125' | ||
name: 'John Smith' | ||
required: | ||
- id | ||
- name | ||
properties: | ||
id: | ||
type: string | ||
x-faker: random.uuid | ||
name: | ||
type: string | ||
x-faker: name.findName | ||
... | ||
``` | ||
**GET /user response** | ||
```javascript | ||
{ | ||
id: 'id-125', | ||
name: 'John Smith'. | ||
} | ||
``` | ||
If multiple examples for the response object are specified, the first one will be used as a resulting sever response. | ||
**doc.yml** | ||
``` | ||
... | ||
paths: | ||
/user | ||
get: | ||
responses: | ||
'200': | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
examples: | ||
first: | ||
value: | ||
id: 'id-125' | ||
name: 'John Smith' | ||
second: | ||
value: | ||
id: 'some-other-id' | ||
name: 'Joe Doe' | ||
required: | ||
- id | ||
- name | ||
properties: | ||
id: | ||
type: string | ||
x-faker: random.uuid | ||
name: | ||
type: string | ||
x-faker: name.findName | ||
... | ||
``` | ||
**GET /user response** | ||
```javascript | ||
{ | ||
id: 'id-125', | ||
name: 'John Smith'. | ||
} | ||
``` | ||
## Contributing | ||
@@ -45,0 +221,0 @@ |
92239
234