From e68a6041eb15a39bb98433f157cbecefbd1e43b8 Mon Sep 17 00:00:00 2001 From: Gabriel Zerbib Date: Mon, 29 Nov 2021 23:20:03 +0200 Subject: [PATCH] default export in handler #671 (#675) --- src/resolvers.ts | 12 +++--- test/default-export.spec.ts | 46 +++++++++++++++++++++++ test/resources/controller-with-default.ts | 3 ++ 3 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 test/default-export.spec.ts create mode 100644 test/resources/controller-with-default.ts diff --git a/src/resolvers.ts b/src/resolvers.ts index 7d7445f5..68dd21b7 100644 --- a/src/resolvers.ts +++ b/src/resolvers.ts @@ -33,18 +33,16 @@ export function defaultResolver( const modulePath = path.join(handlersPath, baseName); if (!tmpModules[modulePath]) { tmpModules[modulePath] = require(modulePath); - if (!tmpModules[modulePath][oId]) { - // if oId is not found only module, try the module's default export - tmpModules[modulePath] = tmpModules[modulePath].default; - } } - if (!tmpModules[modulePath][oId]) { + + const handler = tmpModules[modulePath][oId] || tmpModules[modulePath].default; + + if (!handler) { throw Error( - `Could not find 'x-eov-operation-handler' with id ${oId} in module '${modulePath}'. Make sure operation '${oId}' defined in your API spec exists as a handler function in '${modulePath}'.`, + `Could not find 'x-eov-operation-handler' with id ${oId} in module '${modulePath}'. Make sure operation '${oId}' defined in your API spec exists as a handler function (or module has a default export) in '${modulePath}'.`, ); } - const handler = tmpModules[modulePath][oId]; cache[cacheKey] = handler; return handler; } diff --git a/test/default-export.spec.ts b/test/default-export.spec.ts new file mode 100644 index 00000000..6b4cd2fc --- /dev/null +++ b/test/default-export.spec.ts @@ -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: 'anything', + // @ts-ignore + 'x-eov-operation-handler': 'controller-with-default', + responses: { 200: { description: 'home api' } } + } + }, + }, + }, + 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('success').that.equals(true); + }); + }); +}); diff --git a/test/resources/controller-with-default.ts b/test/resources/controller-with-default.ts new file mode 100644 index 00000000..b52090a2 --- /dev/null +++ b/test/resources/controller-with-default.ts @@ -0,0 +1,3 @@ +export default function (req, res) { + res.json({success: true}).end(); +}