Default TypeScript export is not properly built for a CommonJS use
Closed this issue · 1 comments
Is your feature request related to a problem? Please describe.
When building default export of this package in src/index.ts, CommonJS users will have to access it via default property.
dist/index.js
// ...
exports.default = createMiddleware;
// ...
using the module
// not working
const express = require('express');
const mockServer = require('openapi-mock-express-middleware');
const app = express();
app.use(
'/api' /* root path for the mock server */,
mockServer({ file: '/absolute/path/to/your/openapi/spec.yml' })
);
// working
const express = require('express');
const mockServer = require('openapi-mock-express-middleware');
const app = express();
app.use(
'/api' /* root path for the mock server */,
mockServer.default({ file: '/absolute/path/to/your/openapi/spec.yml' })
);
Describe the solution you'd like
tsconfig.json file is written as intended but the issue is due to TypeScript transpiling. I don't think there is a perfect way to handle this, I would suggest to not use a default export and export createMiddleware as a const. I would also rename it to createMockMiddleware that I personnaly find more meaningful.
src/index.ts
// ...
export const createMockMiddleware = ({
// ...
using the module
const express = require('express');
const { createMockMiddleware } = require('openapi-mock-express-middleware');
const app = express();
app.use(
'/api' /* root path for the mock server */,
createMockMiddleware({ file: '/absolute/path/to/your/openapi/spec.yml' })
);
Describe alternatives you've considered
We also could use the export =
syntax but would mean to use a import ... = require(...)
syntax, which is just mixing ES6 with CommonJS and I don't think it would be the best option to stay consistent.
You are right. Named imports look better.