@hestjs/scalar
Version:
HestJS Scalar API Reference Integration - Beautiful API documentation for HestJS applications
57 lines • 2.65 kB
JavaScript
import { createLogger } from "@hestjs/core";
import { setupScalar, setupScalarWithControllers } from "./scalar.middleware";
const logger = createLogger("Scalar");
/**
* 为 HestApplicationInstance 添加 Scalar 方法
*/
export function extendWithScalar() {
const HestApplicationInstancePrototype = require("@hestjs/core").HestApplicationInstance.prototype;
HestApplicationInstancePrototype.useScalar = function (config) {
setupScalar(this.getHonoInstance(), config);
};
// 弃用,使用统一的自动发现方法
// HestApplicationInstancePrototype.useScalarWithControllers = function (
// controllers: ControllerConstructor[],
// generatorConfig: OpenAPIGeneratorConfig,
// scalarConfig: Omit<ScalarConfig, 'spec'> = {}
// ) {
// // 如果没有传入控制器,尝试从容器中获取
// if (controllers.length === 0) {
// const autoDiscoveredControllers = this.autoDiscoverControllers();
// if (autoDiscoveredControllers.length > 0) {
// controllers = autoDiscoveredControllers;
// } else {
// console.warn('No controllers found. Please pass controllers explicitly or ensure controllers are properly registered.');
// return;
// }
// }
// setupScalarWithControllers(this.hono(), controllers, generatorConfig, scalarConfig);
// };
HestApplicationInstancePrototype.useSwagger = function (generatorConfig, scalarConfig = {}) {
const controllers = this.autoDiscoverControllers();
if (controllers.length === 0) {
console.warn('No controllers found for auto-discovery. Please ensure controllers are properly registered.');
return;
}
setupScalarWithControllers(this.getHonoInstance(), controllers, generatorConfig, scalarConfig);
};
// 添加自动发现控制器的辅助方法
// 从逻辑容器中获取所有控制器
HestApplicationInstancePrototype.autoDiscoverControllers = function () {
try {
const container = this.getContainer();
const controllerItems = container.getAllControllers();
// 提取控制器类
const controllers = controllerItems.map((item) => item.provider);
logger.info(`🔍 Auto-discovered ${controllers.length} controllers:${controllers.map((ctrl) => ctrl.name).join(', ')}`);
return controllers;
}
catch (error) {
logger.error('Failed to auto-discover controllers:', error);
return [];
}
};
}
// 自动扩展
extendWithScalar();
//# sourceMappingURL=extensions.js.map