"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwaggerMiddleware = void 0;
const decorator_1 = require("@midwayjs/decorator");
const koa2_swagger_ui_1 = require("koa2-swagger-ui");
const koaSwaggerMiddleware = (0, koa2_swagger_ui_1.koaSwagger)({
    swaggerCdnUrl: 'https://cdn.bootcdn.net/ajax/libs/swagger-ui',
    swaggerVersion: '4.14.0',
    routePrefix: '/docs',
    swaggerOptions: {
        jsonEditor: true,
        urls: [
            {
                name: 'Magic Node API 接口定义',
                url: '/docs/spec',
            },
        ],
    },
});
let SwaggerMiddleware = class SwaggerMiddleware {
    resolve() {
        return async (ctx, next) => {
            if (this.magicConfig.swagger.enabled) {
                koaSwaggerMiddleware(ctx, next);
            }
            else {
                ctx.status = 404;
            }
        };
    }
    match(ctx) {
        // 下面的匹配到的路由会执行此中间件
        return ctx.path === '/docs';
    }
    static getName() {
        return 'swagger';
    }
};
__decorate([
    (0, decorator_1.Config)('magic'),
    __metadata("design:type", Object)
], SwaggerMiddleware.prototype, "magicConfig", void 0);
SwaggerMiddleware = __decorate([
    (0, decorator_1.Middleware)()
], SwaggerMiddleware);
exports.SwaggerMiddleware = SwaggerMiddleware;
//# sourceMappingURL=swagger.middleware.js.map