Skip to content

Commit

Permalink
add support to pass an openapi spec in as a json
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Jul 7, 2019
1 parent 4a6a295 commit c84e47e
Show file tree
Hide file tree
Showing 10 changed files with 801 additions and 251 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-openapi-validator",
"version": "1.1.0",
"version": "1.2.0",
"description": "Automatically validate API requests using an OpenAPI 3 and Express.",
"main": "dist/index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion src/framework/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ export default class OpenAPIFramework implements IOpenAPIFramework {
});

// this.enableObjectCoercion = !!args.enableObjectCoercion;
this.originalApiDoc = handleYaml(loadSpecFile(args.apiDoc));
const apiDoc = (typeof args.apiDoc === 'string')
? handleYaml(loadSpecFile(args.apiDoc))
: args.apiDoc

this.originalApiDoc = apiDoc;

if (!this.originalApiDoc) {
throw new Error(`spec could not be read at ${args.apiDoc}`);
}
Expand Down
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { OpenAPIFrameworkArgs } from './framework';
import { OpenApiContext } from './openapi.context';
import * as middlewares from './middlewares';
import ono from 'ono';
import { OpenAPIV3 } from 'openapi-types';
import { OpenApiRequest } from './framework/types';

const loggingKey = 'express-openapi-validator';

export interface OpenApiValidatorOpts {
apiSpecPath: string;
apiSpecPath?: string;
apiSpec?: OpenAPIV3.Document | string;
multerOpts?: {};
}

Expand All @@ -19,9 +21,16 @@ export class OpenApiValidator {
private multerOpts: {};

constructor(options: OpenApiValidatorOpts) {
if (!options.apiSpecPath) throw ono('apiSpecPath required');
if (!options.apiSpecPath && !options.apiSpec)
throw ono('apiSpecPath or apiSpec required');
if (options.apiSpecPath && options.apiSpec)
throw ono('apiSpecPath or apiSpec required. not both.');

this.multerOpts = options.multerOpts;
const openApiContext = new OpenApiContext({ apiDoc: options.apiSpecPath });

const openApiContext = new OpenApiContext({
apiDoc: options.apiSpecPath || options.apiSpec,
});

const opts: OpenAPIFrameworkArgs = {
enableObjectCoercion: true,
Expand Down
2 changes: 1 addition & 1 deletion test/app.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function startServer(app, port) {
console.log(`Listening on port ${port}`);
app.server = server;
app.basePath = BASE_PATH;
return app;
return server;
}

export function routes(app) {
Expand Down
55 changes: 36 additions & 19 deletions test/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,45 @@ import * as logger from 'morgan';
import { OpenApiValidator } from '../src';
import { startServer, routes } from './app.common';

var app = express();
export function createApp(opts?: any, port: number = 3000) {
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')));
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')));

new OpenApiValidator({
apiSpecPath: './openapi.yaml',
}).install(app);
new OpenApiValidator(opts).install(app);

routes(app);
routes(app);

// Register error handler
app.use((err, req, res, next) => {
res.status(err.status).json({
errors: err.errors,
// Register error handler
app.use((err, req, res, next) => {
res.status(err.status).json({
errors: err.errors,
});
});
});

startServer(app, 3000);

export default app;
const server = startServer(app, port);
const shutDown = () => {
console.log('Received kill signal, shutting down gracefully');
server.close(() => {
console.log('Closed out remaining connections');
process.exit(0);
});

setTimeout(() => {
console.error(
'Could not close connections in time, forcefully shutting down',
);
process.exit(1);
}, 10000);
};
process.on('SIGTERM', shutDown);
process.on('SIGINT', shutDown);

// export default app;
return app;
}
3 changes: 2 additions & 1 deletion test/headers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import * as request from 'supertest';
import app from './app';
import { createApp } from './app';

const app = createApp({ apiSpecPath: './openapi.yaml' }, 3004);
const packageJson = require('../package.json');
const basePath = (<any>app).basePath;

Expand Down
3 changes: 2 additions & 1 deletion test/multipart.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import * as request from 'supertest';
import app from './app';
import { createApp } from './app';

const app = createApp({ apiSpecPath: './openapi.yaml' }, 3003);
const packageJson = require('../package.json');
const basePath = (<any>app).basePath;

Expand Down
Loading

0 comments on commit c84e47e

Please sign in to comment.