Skip to content

Commit

Permalink
update signature and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 21, 2019
1 parent 95c53fe commit b780f66
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
14 changes: 5 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,14 @@ export interface OpenApiMiddlewareOpts extends OpenAPIFrameworkArgs {
apiSpecPath: string;
}

export function OpenApiMiddleware(
app: ExpressApp,
opts: OpenApiMiddlewareOpts
) {
export function OpenApiMiddleware(opts: OpenApiMiddlewareOpts) {
if (!opts.apiSpecPath) throw new Error('apiSpecPath required');
const apiContents = loadSpecFile(opts.apiSpecPath);
if (!apiContents)
throw new Error(`spec could not be read at ${opts.apiSpecPath}`);

const apiDoc = handleYaml(apiContents);
const framework = createFramework({ ...opts, apiDoc });
this.app = app;
this.apiDoc = framework.apiDoc;
this.opts = opts;
this.opts.name = this.opts.name || 'express-middleware-openapi';
Expand All @@ -63,7 +59,7 @@ export function OpenApiMiddleware(
console.log(opts);
}

OpenApiMiddleware.prototype.install = function() {
OpenApiMiddleware.prototype.install = function(app: ExpressApp) {
const noPathParamRoutes = [];
const pathParms = [];
for (const route of this.routes) {
Expand All @@ -76,11 +72,11 @@ OpenApiMiddleware.prototype.install = function() {

// install param on routes with paths
for (const p of _.uniq(pathParms)) {
this.app.param(p, this.middleware()); //pathParamMiddleware);
app.param(p, this.middleware()); //pathParamMiddleware);
}
// install use on routes without paths
this.app.all(_.uniq(noPathParamRoutes), this.middleware());
// this.app.use(_.uniq(noPathParamRoutes), this.middleware());
app.all(_.uniq(noPathParamRoutes), this.middleware());
// app.use(_.uniq(noPathParamRoutes), this.middleware());
};

OpenApiMiddleware.prototype.middleware = function() {
Expand Down
4 changes: 2 additions & 2 deletions test/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'public')));

new OpenApiMiddleware(app, {
new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
validateApiDoc: true, // is the default
enableObjectCoercion: true, // should be default
Expand All @@ -25,7 +25,7 @@ new OpenApiMiddleware(app, {

return a;
},
}).install();
}).install(app);

app.get('/v1/pets', function(req, res, next) {
console.log('at /v1/pets here');
Expand Down
14 changes: 6 additions & 8 deletions test/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ const { OpenApiMiddleware } = require('../');
const packageJson = require('../package.json');

describe(packageJson.name, () => {
it('existing spec', async () => {
it('should succeed when spec exists and is valid', async () => {
const oam = new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
validate: true,
enableObjectCoercion: true // should be default
enableObjectCoercion: true, // should be default
});

expect(oam)
.to.have.property('middleware')
.to.have.property('install')
.that.is.a('function');
});

it('missing spec', async () => {
it('should throw when spec is missing', async () => {
const createMiddleware = () =>
new OpenApiMiddleware({
apiSpecPath: './not-found.yaml',
validate: true,
enableObjectCoercion: true // should be default
}).middleware();
enableObjectCoercion: true, // should be default
});

expect(createMiddleware).to.throw(
'spec could not be read at ./not-found.yaml'
Expand Down
2 changes: 1 addition & 1 deletion test/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe(packageJson.name, () => {
// TODO write test when route exists, but doc does not
});

describe.only('GET /pets/:id', () => {
describe('GET /pets/:id', () => {
it('should return 400 when path param should be int, but instead is string', async () => {
const id = 'my_id';
return request(app)
Expand Down

0 comments on commit b780f66

Please sign in to comment.