-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carmine DiMascio
committed
Mar 19, 2019
0 parents
commit d48c5e9
Showing
12 changed files
with
4,919 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.vscode | ||
node_modules | ||
*.orig |
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,3 @@ | ||
# express-middelware-openapi | ||
|
||
in construction |
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,45 @@ | ||
var express = require('express'); | ||
var path = require('path'); | ||
var cookieParser = require('cookie-parser'); | ||
var bodyParser = require('body-parser'); | ||
var logger = require('morgan'); | ||
const http = require('http'); | ||
const { OpenApiMiddleware } = require('./middleware'); | ||
// const OpenAPIRequestValidator = require('openapi-request-validator'); | ||
// var indexRouter = require('./routes/index'); | ||
// var usersRouter = require('./routes/users'); | ||
|
||
|
||
var app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
|
||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
// const ov = OpenAPIRequestValidator(); | ||
// var router = express.Router(); | ||
|
||
app.use(new OpenApiMiddleware({ | ||
apiSpecPath: './openapi.yaml', | ||
validate: true, | ||
enableObjectCoercion: true // should be default | ||
}).middleware()) | ||
/* GET home page. */ | ||
app.get('/v1/pets', function(req, res, next) { | ||
console.log('at /v1/pets here'); | ||
res.json({ | ||
test: 'hi', | ||
}); | ||
}); | ||
|
||
const server = http.createServer(app); | ||
server.listen(3000); | ||
console.log('Listening on port 3000'); | ||
console.log('Try visiting http://localhost:3000/greet?name=Jason'); | ||
console.log('-----STARTED[---'); | ||
|
||
module.exports = app; |
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,37 @@ | ||
import { OpenAPIV3 } from 'openapi-types'; | ||
import { URL } from 'url'; | ||
|
||
export default class BasePath { | ||
public readonly variables: { [key: string]: { enum: string[] } } = {}; | ||
public readonly path: string = ''; | ||
|
||
constructor(server: OpenAPIV3.ServerObject) { | ||
// break the url into parts | ||
// baseUrl param added to make the parsing of relative paths go well | ||
const serverUrl = new URL(server.url, 'http://localhost'); | ||
console.log(serverUrl); | ||
let urlPath = decodeURI(serverUrl.pathname).replace(/\/$/, ''); | ||
if (/{\w+}/.test(urlPath)) { | ||
// has variable that we need to check out | ||
urlPath = urlPath.replace(/{(\w+)}/g, (substring, p1) => `:${p1}`); | ||
} | ||
console.log(urlPath); | ||
this.path = urlPath; | ||
for (const variable in server.variables) { | ||
if (server.variables.hasOwnProperty(variable)) { | ||
this.variables[variable] = { enum: server.variables[variable].enum }; | ||
} | ||
} | ||
} | ||
|
||
public hasVariables() { | ||
return Object.keys(this.variables).length > 0; | ||
} | ||
|
||
public static fromServers(servers: OpenAPIV3.ServerObject[]) { | ||
if (!servers) { | ||
return [new BasePath({ url: '' })]; | ||
} | ||
return servers.map(server => new BasePath(server)); | ||
} | ||
} |
Oops, something went wrong.