Skip to content

Commit 80e7e5d

Browse files
kesha-shahKesha Shah
and
Kesha Shah
authored
fixing default export function issue (#846)
Co-authored-by: Kesha Shah <[email protected]>
1 parent aa71ddf commit 80e7e5d

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/resolvers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function defaultResolver(
3535
tmpModules[modulePath] = require(modulePath);
3636
}
3737

38-
const handler = tmpModules[modulePath][oId] || tmpModules[modulePath].default;
38+
const handler = tmpModules[modulePath][oId] || tmpModules[modulePath].default[oId] || tmpModules[modulePath].default;
3939

4040
if (!handler) {
4141
throw Error(

test/default.export.fn.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as express from 'express';
2+
import * as OpenApiValidator from '../src';
3+
import { expect } from 'chai';
4+
import * as request from 'supertest';
5+
import * as path from 'path';
6+
7+
describe('default export resolver', () => {
8+
let server = null;
9+
let app = express();
10+
11+
before(async () => {
12+
app.use(
13+
OpenApiValidator.middleware({
14+
apiSpec: {
15+
openapi: '3.0.0',
16+
info: { version: '1.0.0', title: 'test bug OpenApiValidator' },
17+
paths: {
18+
'/': {
19+
get: {
20+
operationId: 'test#get',
21+
// @ts-ignore
22+
'x-eov-operation-handler': 'routes/default-export-fn',
23+
responses: { 200: { description: 'homepage' } }
24+
}
25+
},
26+
},
27+
},
28+
operationHandlers: path.join(__dirname, 'resources'),
29+
}),
30+
);
31+
32+
server = app.listen(3000);
33+
console.log('server start port 3000');
34+
});
35+
36+
after(async () => server.close());
37+
38+
it('should use default export operation', async () => {
39+
return request(app)
40+
.get(`/`)
41+
.expect(200)
42+
.then((r) => {
43+
expect(r.body).to.have.property('message').that.equals("It Works!");
44+
});
45+
});
46+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.default = {
2+
'test#get': (req, res) => {
3+
res.status(200).json({ message: 'It Works!' });
4+
},
5+
};

0 commit comments

Comments
 (0)