Cody2333 / koa-swagger-decorator

using decorator to automatically generate swagger doc for koa-router
347 stars 81 forks source link

Could this support ESM when I set module 'ESNext' at tsconfig.json #164

Open stevenhobs opened 1 year ago

stevenhobs commented 1 year ago

我编译程序的js文件为 mjs, SwaggerRouter 的mapDir函数操作会报错,因为 require 语句 .pnpm\koa-swagger-decorator@1.8.6\node_modules\koa-swagger-decorator\dist\utils.js:46 const obj = require(filepath); ^

Error [ERR_REQUIRE_ESM]: require() of ES Module .......\router\pub.router.js from E:\project\mtd-koa\node_modules.pnpm\koa-swagger-decorator@1.8.6\node_modules\koa-swagger-decorator\dist\utils.js not supported.

ggomaeng commented 1 year ago

+1

regis-underdog commented 2 months ago

I am also facing this issue with Vitest. It appears to be an issue in the lines referencing a require statement.

src/main.js

import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import cors from '@koa/cors';
import logger from 'koa-logger';
import errorHandler from 'koa-better-error-handler';
import koa404Handler from 'koa-404-handler';

import { router } from '@/controller/index';

const app = new Koa();

app.context.onerror = errorHandler();
app.context.api = true;

app
  .use(logger())
  .use(router.allowedMethods())
  .use(cors())
  .use(bodyParser())
  .use(koa404Handler)
  .use(router.routes());

export { app };

And src/controller/index.ts

import Router from '@koa/router';
import { SwaggerRouter, request, summary, query, path, body, tags } from 'koa-swagger-decorator';

const router = new SwaggerRouter();
const testTag = tags(['test']);

// swagger docs avaliable at http://localhost:3000/swagger-html
router.swagger({
  title: 'API Server',
  description: 'API DOC',
  version: '1.0.0',
});

export class HealthChecker {
  @request('get', '/readyz')
  @summary('Check if webserver is ready to receive traffic')
  @query({
    verbose: {
      type: 'boolean',
      required: false,
      default: true,
      description: 'Show full output',
    },
  })
  static async readyz(ctx) {
    ctx.body = { message: 'hello world' };
  }
}

router.map(HealthChecker, { doValidation: false });

export { router };

This code above works when running the Koa server with tsx.

However, with Vitest file src/test/controller/index.test.ts

import { vi, expect, test } from 'vitest';
import request from 'supertest';

// This does not work, see https://github.com/Cody2333/koa-swagger-decorator/issues/164
import { app } from 'src/main';

test('readyz probe works', async () => {
  const response = await request(app.callback()).get('/readyz');
  expect(response.status).toBe(200);
  expect(response.text).toBe('Hello World');
});

Returns the following errors

 FAIL  src/test/controller/index.test.ts [ src/test/controller/index.test.ts ]
SyntaxError: Cannot use import statement outside a module
 ❯ loadModule node_modules/.pnpm/koa-swagger-decorator@1.8.7_@types+koa@2.15.0/node_modules/koa-swagger-decorator/lib/utils.ts:43:15
 ❯ loadClass node_modules/.pnpm/koa-swagger-decorator@1.8.7_@types+koa@2.15.0/node_modules/koa-swagger-decorator/lib/utils.ts:51:15
 ❯ node_modules/.pnpm/koa-swagger-decorator@1.8.7_@types+koa@2.15.0/node_modules/koa-swagger-decorator/lib/utils.ts:60:22

Module src/controller/v1/index.ts:1 seems to be an ES Module but shipped in a CommonJS package. To fix this issue, change the file extension to .mjs or add "type": "module" in your package.json.

Please note, that type: module is correctly set in my package.json file. It is actually complaining about the node_modules directory.