Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 19, 2019
0 parents commit d48c5e9
Show file tree
Hide file tree
Showing 12 changed files with 4,919 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
node_modules
*.orig
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# express-middelware-openapi

in construction
45 changes: 45 additions & 0 deletions app.ts
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;
37 changes: 37 additions & 0 deletions fw/base.path.ts
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));
}
}
Loading

0 comments on commit d48c5e9

Please sign in to comment.