Skip to content

Commit

Permalink
adding examples for swagger performance eval
Browse files Browse the repository at this point in the history
  • Loading branch information
jkyberneees committed Dec 31, 2021
1 parent 88220f7 commit b5f5e13
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"body-parser": "^1.19.0",
"chai": "^4.3.4",
"cross-env": "^7.0.3",
"express": "^4.17.1",
"express": "^4.17.2",
"express-jwt": "^6.1.0",
"fastify": "^2.15.3",
"http-cache-middleware": "^1.3.8",
Expand All @@ -57,6 +57,7 @@
"morgan": "^1.10.0",
"muneem": "^2.4.5",
"nyc": "^15.1.0",
"openapi-validator-middleware": "^3.2.4",
"pem": "^1.14.4",
"polka": "^0.5.2",
"response-time": "^2.3.2",
Expand All @@ -66,7 +67,6 @@
"socket.io": "^4.3.1",
"socket.io-client": "^4.3.2",
"supertest": "^6.1.6",
"swagger-tools": "^0.10.4",
"winston": "^3.3.3"
}
}
15 changes: 15 additions & 0 deletions performance/swagger/express-open-api-validator.js
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)
35 changes: 35 additions & 0 deletions performance/swagger/restana-swagger-validator.js
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()
59 changes: 59 additions & 0 deletions performance/swagger/swagger.json
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"
}
}
}
}
}
}
}
}
}

0 comments on commit b5f5e13

Please sign in to comment.