-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding examples for swagger performance eval
- Loading branch information
1 parent
88220f7
commit b5f5e13
Showing
4 changed files
with
111 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const swaggerValidation = require('openapi-validator-middleware') | ||
const express = require('express') | ||
const path = require('path') | ||
|
||
swaggerValidation.init(path.join(__dirname, 'swagger.json')) | ||
|
||
const app = express() | ||
app.get('/api/sayHi/:name', swaggerValidation.validate, (req, res, next) => { | ||
res.send({ | ||
name: req.params.name, | ||
format: req.query.format | ||
}) | ||
}) | ||
|
||
app.listen(3000) |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const restana = require('../../index') | ||
const path = require('path') | ||
|
||
const { | ||
SwaggerValidationError, | ||
SwaggerValidator | ||
} = require('restana-swagger-validator') | ||
|
||
const app = restana({ | ||
errorHandler: (err, req, res) => { | ||
if (err instanceof SwaggerValidationError) { | ||
res.statusCode = err.statusCode | ||
res.send({ | ||
message: err.message, | ||
errors: err.errors | ||
}) | ||
} else { | ||
res.send(err) | ||
} | ||
} | ||
}) | ||
|
||
SwaggerValidator(app, path.join(__dirname, '/swagger.json'), { | ||
buildResponses: false, | ||
publicApiEndpoint: 'http://localhost:3000' | ||
}) | ||
|
||
app.get('/api/sayHi/:name', (req, res) => { | ||
res.send({ | ||
name: req.params.name, | ||
format: req.query.format | ||
}) | ||
}) | ||
|
||
app.start() |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "Example Swagger spec.", | ||
"version": "1.0.0", | ||
"title": "Swagger Hello" | ||
}, | ||
"basePath": "/api", | ||
"schemes": [ | ||
"http" | ||
], | ||
"paths": { | ||
"/sayHi/{name}": { | ||
"get": { | ||
"tags": [ | ||
"hello" | ||
], | ||
"summary": "Say hi", | ||
"description": "Helloer endpoint", | ||
"produces": [ | ||
"application/json" | ||
], | ||
"parameters": [ | ||
{ | ||
"name": "name", | ||
"in": "path", | ||
"minLength": 3, | ||
"required": true, | ||
"description": "The name to say hi", | ||
"type": "string" | ||
}, { | ||
"name": "format", | ||
"in": "query", | ||
"minLength": 3, | ||
"required": true, | ||
"description": "A query parameter", | ||
"type": "string" | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "successful operation", | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"format": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |