Skip to content

Commit

Permalink
fixing default export function issue (#846)
Browse files Browse the repository at this point in the history
Co-authored-by: Kesha Shah <keshashah@wolkus.com>
  • Loading branch information
kesha-shah and Kesha Shah authored Aug 16, 2023
1 parent 9d215be commit 268d38a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function defaultResolver(
tmpModules[modulePath] = require(modulePath);
}

const handler = tmpModules[modulePath][oId] || tmpModules[modulePath].default;
const handler = tmpModules[modulePath][oId] || tmpModules[modulePath].default[oId] || tmpModules[modulePath].default;

if (!handler) {
throw Error(
Expand Down
46 changes: 46 additions & 0 deletions test/default.export.fn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as express from 'express';
import * as OpenApiValidator from '../src';
import { expect } from 'chai';
import * as request from 'supertest';
import * as path from 'path';

describe('default export resolver', () => {
let server = null;
let app = express();

before(async () => {
app.use(
OpenApiValidator.middleware({
apiSpec: {
openapi: '3.0.0',
info: { version: '1.0.0', title: 'test bug OpenApiValidator' },
paths: {
'/': {
get: {
operationId: 'test#get',
// @ts-ignore
'x-eov-operation-handler': 'routes/default-export-fn',
responses: { 200: { description: 'homepage' } }
}
},
},
},
operationHandlers: path.join(__dirname, 'resources'),
}),
);

server = app.listen(3000);
console.log('server start port 3000');
});

after(async () => server.close());

it('should use default export operation', async () => {
return request(app)
.get(`/`)
.expect(200)
.then((r) => {
expect(r.body).to.have.property('message').that.equals("It Works!");
});
});
});
5 changes: 5 additions & 0 deletions test/resources/routes/default-export-fn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.default = {
'test#get': (req, res) => {
res.status(200).json({ message: 'It Works!' });
},
};

0 comments on commit 268d38a

Please sign in to comment.