@@ -37,6 +37,7 @@ npm i express-jsdoc-swagger
3737## Basic Usage
3838
3939``` javascript
40+ // index.js file
4041const express = require (' express' );
4142const expressJSDocSwagger = require (' express-jsdoc-swagger' );
4243
@@ -54,6 +55,7 @@ const options = {
5455 scheme: ' basic' ,
5556 },
5657 },
58+ // Base directory which we use to locate your JSDOC files
5759 baseDir: __dirname ,
5860 // Glob pattern to find your jsdoc files (multiple patterns can be added in an array)
5961 filesPattern: ' ./**/*.js' ,
@@ -94,7 +96,7 @@ app.listen(PORT, () => console.log(`Example app listening at http://localhost:${
9496
9597## Basic Examples
9698
97- 1 . Basic configuration
99+ 1 . Basic configuration options.
98100
99101``` javascript
100102const options = {
@@ -129,7 +131,7 @@ const options = {
129131 */
130132```
131133
132- 3 . Endpoint that returns a ` Songs ` model array
134+ 3 . Endpoint which returns a ` Songs ` model array in the response.
133135
134136``` javascript
135137/**
@@ -145,6 +147,24 @@ app.get('/api/v1/albums', (req, res) => (
145147));
146148```
147149
150+ 3 . Endpoint PUT with body and path params which returns a ` Songs ` model array in the response.
151+
152+ ``` javascript
153+ /**
154+ * PUT /api/v1/albums/{id}
155+ * @summary Update album
156+ * @tags album
157+ * @param {string} name.path - name param description
158+ * @param {Song} request.body.required - songs info
159+ * @return {array<Song>} 200 - success response - application/json
160+ */
161+ app .put (' /api/v1/albums/:id' , (req , res ) => (
162+ res .json ([{
163+ title: ' abum 1' ,
164+ }])
165+ ));
166+ ```
167+
1481684 . Basic endpoint definition with tags, params and basic authentication
149169
150170``` javascript
@@ -203,23 +223,34 @@ Install using the node package registry:
203223npm install --save express-oas-validator
204224```
205225
206- After this you have to initialize using the ` finish ` event. More info in this [ sections ] ( eventEmitter.md ) .
226+ We have to wait until we have the full swagger schema to initiate the validator .
207227
208228``` js
209- const instance = expressJSDocSwagger (app)(options);
229+ // validator.js
230+ const { init } = require (' express-oas-validator' );
210231
211- instance .on (' finish' , data => {
212- init (data);
213- resolve (app);
232+ const validators = instance => new Promise ((resolve , reject ) => {
233+ instance .on (' finish' , (swaggerDef ) => {
234+ const { validateRequest , validateResponse } = init (swaggerDef);
235+ resolve ({ validateRequest, validateResponse });
236+ });
237+
238+ instance .on (' error' , (error ) => {
239+ reject (error);
240+ });
214241});
242+
243+ module .exports = validators;
244+
215245```
216246
217- This is a full example of how it works .
247+ You can check out this also in our [ example folder ] ( https://github.com/BRIKEV/express-jsdoc-swagger/tree/master/examples/validator ) .
218248
219249``` js
250+ // index.js
220251const express = require (' express' );
221252const expressJSDocSwagger = require (' express-jsdoc-swagger' );
222- const { init , validateRequest , validateResponse } = require (' express-oas- validator' );
253+ const validator = require (' ./ validator' );
223254
224255const options = {
225256 info: {
@@ -236,15 +267,10 @@ const options = {
236267const app = express ();
237268const instance = expressJSDocSwagger (app)(options);
238269
239- const serverApp = () => new Promise (resolve => {
240- instance .on (' finish' , data => {
241- init (data);
242- resolve (app);
243- });
244-
270+ const serverApp = async () => {
271+ const { validateRequest , validateResponse } = await validator (instance);
245272 app .use (express .urlencoded ({ extended: true }));
246273 app .use (express .json ());
247-
248274 /**
249275 * A song
250276 * @typedef {object} Song
@@ -292,9 +318,21 @@ const serverApp = () => new Promise(resolve => {
292318 app .use ((err , req , res , next ) => {
293319 res .status (err .status ).json (err);
294320 });
295- });
296321
297- module .exports = serverApp;
322+ return app;
323+ };
324+
325+ const PORT = process .env .PORT || 4000 ;
326+
327+ serverApp ()
328+ .then (app =>
329+ app .listen (PORT , () =>
330+ console .log (` Listening PORT: ${ PORT } ` )
331+ ))
332+ .catch ((err ) => {
333+ console .error (err);
334+ process .exit (1 );
335+ });
298336```
299337
300338You can visit our [ documentation] ( https://brikev.github.io/express-jsdoc-swagger-docs/#/validator ) .
0 commit comments