Skip to content

Commit b5f5e13

Browse files
committed
adding examples for swagger performance eval
1 parent 88220f7 commit b5f5e13

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"body-parser": "^1.19.0",
4747
"chai": "^4.3.4",
4848
"cross-env": "^7.0.3",
49-
"express": "^4.17.1",
49+
"express": "^4.17.2",
5050
"express-jwt": "^6.1.0",
5151
"fastify": "^2.15.3",
5252
"http-cache-middleware": "^1.3.8",
@@ -57,6 +57,7 @@
5757
"morgan": "^1.10.0",
5858
"muneem": "^2.4.5",
5959
"nyc": "^15.1.0",
60+
"openapi-validator-middleware": "^3.2.4",
6061
"pem": "^1.14.4",
6162
"polka": "^0.5.2",
6263
"response-time": "^2.3.2",
@@ -66,7 +67,6 @@
6667
"socket.io": "^4.3.1",
6768
"socket.io-client": "^4.3.2",
6869
"supertest": "^6.1.6",
69-
"swagger-tools": "^0.10.4",
7070
"winston": "^3.3.3"
7171
}
7272
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const swaggerValidation = require('openapi-validator-middleware')
2+
const express = require('express')
3+
const path = require('path')
4+
5+
swaggerValidation.init(path.join(__dirname, 'swagger.json'))
6+
7+
const app = express()
8+
app.get('/api/sayHi/:name', swaggerValidation.validate, (req, res, next) => {
9+
res.send({
10+
name: req.params.name,
11+
format: req.query.format
12+
})
13+
})
14+
15+
app.listen(3000)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const restana = require('../../index')
2+
const path = require('path')
3+
4+
const {
5+
SwaggerValidationError,
6+
SwaggerValidator
7+
} = require('restana-swagger-validator')
8+
9+
const app = restana({
10+
errorHandler: (err, req, res) => {
11+
if (err instanceof SwaggerValidationError) {
12+
res.statusCode = err.statusCode
13+
res.send({
14+
message: err.message,
15+
errors: err.errors
16+
})
17+
} else {
18+
res.send(err)
19+
}
20+
}
21+
})
22+
23+
SwaggerValidator(app, path.join(__dirname, '/swagger.json'), {
24+
buildResponses: false,
25+
publicApiEndpoint: 'http://localhost:3000'
26+
})
27+
28+
app.get('/api/sayHi/:name', (req, res) => {
29+
res.send({
30+
name: req.params.name,
31+
format: req.query.format
32+
})
33+
})
34+
35+
app.start()

performance/swagger/swagger.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"description": "Example Swagger spec.",
5+
"version": "1.0.0",
6+
"title": "Swagger Hello"
7+
},
8+
"basePath": "/api",
9+
"schemes": [
10+
"http"
11+
],
12+
"paths": {
13+
"/sayHi/{name}": {
14+
"get": {
15+
"tags": [
16+
"hello"
17+
],
18+
"summary": "Say hi",
19+
"description": "Helloer endpoint",
20+
"produces": [
21+
"application/json"
22+
],
23+
"parameters": [
24+
{
25+
"name": "name",
26+
"in": "path",
27+
"minLength": 3,
28+
"required": true,
29+
"description": "The name to say hi",
30+
"type": "string"
31+
}, {
32+
"name": "format",
33+
"in": "query",
34+
"minLength": 3,
35+
"required": true,
36+
"description": "A query parameter",
37+
"type": "string"
38+
}
39+
],
40+
"responses": {
41+
"200": {
42+
"description": "successful operation",
43+
"schema": {
44+
"type": "object",
45+
"properties": {
46+
"name": {
47+
"type": "string"
48+
},
49+
"format": {
50+
"type": "string"
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)