-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from aleksandryackovlev/release-4.0.1
Release 4.0.1
- Loading branch information
Showing
12 changed files
with
1,262 additions
and
1,260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,13 @@ | |
</div> | ||
|
||
[![npm][npm]][npm-url] | ||
[![deps][deps]][deps-url] | ||
[](https://travis-ci.org/aleksandryackovlev/openapi-mock-express-middleware) | ||
[](https://github.com/aleksandryackovlev/openapi-mock-express-middleware/actions) | ||
[](https://codecov.io/gh/aleksandryackovlev/openapi-mock-express-middleware) | ||
[](https://packagephobia.now.sh/result?p=openapi-mock-express-middleware) | ||
|
||
# openapi-mock-express-middleware | ||
|
||
Generates an express mock server from an [Open API 3.0](https://swagger.io/docs/specification/about/) documentation. | ||
Generates express mock-servers from [Open API 3.0](https://swagger.io/docs/specification/about/) specs. | ||
|
||
## Installation | ||
|
||
|
@@ -42,7 +41,7 @@ app.listen(80, () => console.log('Server listening on port 80')); | |
``` | ||
|
||
### 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)) | ||
The middleware uses [json-schmea-faker](https://github.com/json-schema-faker/json-schema-faker) under the hood. To configure it, you can pass 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'); | ||
|
@@ -54,13 +53,12 @@ app.use( | |
'/api', | ||
createMockMiddleware({ | ||
spec: '/absolute/path/to/your/openapi/spec.yml', // string or OpenAPIV3.Document object | ||
locale: 'ru', // json-schema-faker locale, default to 'en' | ||
options: { // json-schema-faker options | ||
alwaysFakeOptionals: true, | ||
useExamplesValue: true, | ||
// ... | ||
}, | ||
jsfCallback: (jsf, faker) => { | ||
configure: (jsf) => { | ||
// function where you can extend json-schema-faker | ||
... | ||
} | ||
|
@@ -105,10 +103,35 @@ paths: | |
} | ||
``` | ||
|
||
### 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). | ||
### Faker or Chance generated responses | ||
In addition `faker.js` or `chance.js` methods can be specified for data generation. In order to use these generators you have to configure middleware through the `configure` option of the factory function. | ||
|
||
**doc.yml** | ||
```javascript | ||
const express = require('express'); | ||
const { createMockMiddleware } = require('openapi-mock-express-middleware'); | ||
import faker from '@faker-js/faker'; | ||
import Chance from 'chance'; | ||
|
||
const app = express(); | ||
|
||
app.use( | ||
'/api', | ||
createMockMiddleware({ | ||
spec: '/absolute/path/to/your/openapi/spec.yml', | ||
configure: (jsf) => { | ||
jsf.extend('faker', () => faker); | ||
jsf.extend('chance', () => new Chance()); | ||
} | ||
}), | ||
); | ||
|
||
app.listen(80, () => console.log('Server listening on port 80')); | ||
|
||
``` | ||
|
||
After that you can use 'x-faker' and/or 'x-chance' attributes in your openapi specs. | ||
|
||
**spec.yml** | ||
``` | ||
... | ||
paths: | ||
|
@@ -130,14 +153,20 @@ paths: | |
name: | ||
type: string | ||
x-faker: name.findName | ||
email: | ||
type: string | ||
x-chance: | ||
email: | ||
domain: fake.com | ||
... | ||
``` | ||
|
||
**GET /user response** | ||
```javascript | ||
{ | ||
id: '8c4a4ed2-efba-4913-9604-19a27f36f322', | ||
name: 'Mr. Braxton Dickens'. | ||
name: 'Mr. Braxton Dickens', | ||
email: '[email protected]' | ||
} | ||
``` | ||
|
||
|
@@ -224,6 +253,34 @@ paths: | |
} | ||
``` | ||
|
||
If you want to use some other logic for generating responses from the `examples` attributes you can easily implement it by overwriting this behavior in the `configure` option of the middleware's factory function: | ||
|
||
```javascript | ||
const express = require('express'); | ||
const { createMockMiddleware } = require('openapi-mock-express-middleware'); | ||
|
||
const app = express(); | ||
|
||
app.use( | ||
'/api', | ||
createMockMiddleware({ | ||
spec: '/absolute/path/to/your/openapi/spec.yml', | ||
configure: (jsf) => { | ||
jsf.define('examples', (value) => { | ||
if (typeof value === 'object' && value !== null && Object.keys(value).length) { | ||
return value[Object.keys(value)[0]].value; | ||
} | ||
|
||
return ''; | ||
}); | ||
} | ||
}), | ||
); | ||
|
||
app.listen(80, () => console.log('Server listening on port 80')); | ||
|
||
``` | ||
|
||
## Contributing | ||
|
||
Please take a moment to read our contributing guidelines if you haven't yet done so. | ||
|
@@ -237,5 +294,3 @@ Please take a moment to read our contributing guidelines if you haven't yet done | |
|
||
[npm]: https://img.shields.io/npm/v/openapi-mock-express-middleware.svg | ||
[npm-url]: https://npmjs.com/package/openapi-mock-express-middleware | ||
[deps]: https://david-dm.org/aleksandryackovlev/openapi-mock-express-middleware.svg | ||
[deps-url]: https://david-dm.org/aleksandryackovlev/openapi-mock-express-middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "openapi-mock-express-middleware", | ||
"version": "3.0.2", | ||
"description": "Generates an express mock server from an Open API spec", | ||
"version": "4.0.1", | ||
"description": "Generates express mock-servers from OpenAPI specs", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"scripts": { | ||
|
@@ -31,13 +31,9 @@ | |
"openapi", | ||
"swagger", | ||
"typescript", | ||
"fetch", | ||
"client", | ||
"sdk", | ||
"mock", | ||
"server", | ||
"express", | ||
"webpack" | ||
"express" | ||
], | ||
"author": "Aleksandr Yackovlev <[email protected]>", | ||
"license": "MIT", | ||
|
@@ -52,46 +48,44 @@ | |
], | ||
"dependencies": { | ||
"@apidevtools/swagger-parser": "^10.0.3", | ||
"ajv": "^8.6.3", | ||
"chokidar": "^3.5.2", | ||
"cookie-parser": "^1.4.5", | ||
"express": "^4.17.1", | ||
"faker": "^5.5.3", | ||
"json-schema-faker": "0.5.0-rcv.40", | ||
"ajv": "^8.9.0", | ||
"chokidar": "^3.5.3", | ||
"cookie-parser": "^1.4.6", | ||
"express": "^4.17.2", | ||
"json-schema-faker": "0.5.0-rcv.41", | ||
"lodash": "^4.17.20", | ||
"method-override": "^3.0.0", | ||
"openapi-types": "^9.3.0", | ||
"openapi-types": "^10.0.0", | ||
"path-to-regexp": "^6.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/cookie-parser": "^1.4.2", | ||
"@types/express": "^4.17.13", | ||
"@types/faker": "^5.5.9", | ||
"@types/jest": "^27.0.2", | ||
"@types/lodash": "^4.14.176", | ||
"@types/jest": "^27.4.0", | ||
"@types/lodash": "^4.14.178", | ||
"@types/method-override": "0.0.32", | ||
"@types/node": "^16.11.4", | ||
"@types/node": "^17.0.13", | ||
"@types/supertest": "^2.0.11", | ||
"@typescript-eslint/eslint-plugin": "^5.1.0", | ||
"@typescript-eslint/parser": "^5.1.0", | ||
"@typescript-eslint/eslint-plugin": "^5.10.2", | ||
"@typescript-eslint/parser": "^5.10.2", | ||
"commitizen": "^4.2.4", | ||
"concurrently": "^6.3.0", | ||
"concurrently": "^7.0.0", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"eslint": "^8.1.0", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint": "^8.8.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.25.2", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"husky": "^7.0.4", | ||
"jest": "^27.3.1", | ||
"jest": "^27.4.7", | ||
"json-schema-faker-types": "^0.1.6", | ||
"nodemon": "^2.0.14", | ||
"prettier": "^2.4.1", | ||
"nodemon": "^2.0.15", | ||
"prettier": "^2.5.1", | ||
"standard-version": "^9.3.2", | ||
"supertest": "^6.1.6", | ||
"ts-jest": "^27.0.7", | ||
"supertest": "^6.2.2", | ||
"ts-jest": "^27.1.3", | ||
"ts-node": "^10.4.0", | ||
"typescript": "^4.4.4" | ||
"typescript": "^4.5.5" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.