{"id":13781049,"url":"https://github.com/libery/think-swagger","last_synced_at":"2025-05-08T21:29:41.954Z","repository":{"id":81916933,"uuid":"98206264","full_name":"libery/think-swagger","owner":"libery","description":null,"archived":false,"fork":false,"pushed_at":"2017-07-28T07:38:47.000Z","size":4,"stargazers_count":9,"open_issues_count":3,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-08T21:29:41.872Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/libery.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-24T15:32:02.000Z","updated_at":"2022-11-11T15:23:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"b275b079-634c-4ee6-a585-17338e866e65","html_url":"https://github.com/libery/think-swagger","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libery%2Fthink-swagger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libery%2Fthink-swagger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libery%2Fthink-swagger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libery%2Fthink-swagger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/libery","download_url":"https://codeload.github.com/libery/think-swagger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253153073,"owners_count":21862308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:01:22.486Z","updated_at":"2025-05-08T21:29:41.927Z","avatar_url":"https://github.com/libery.png","language":null,"readme":"# think-swagger\n\nThinkJS 是一个 Node.js 框架，自带脚手架通过简单的指令生成项目目录，集成了大量开箱即用的插件，适合 Node.js 环境下的快速开发。本项目为 ThinkJS 3.x 提供 swagger 的支持，本项目包含三个子项目和一个示例：\n\n| 名称 | 说明 |  \n| --- | --- |  \n| [think-swagger-parser](https://github.com/libery/think-swagger-parser) | swagger文档解析模块 |  \n| [think-swagger-router](https://github.com/libery/think-swagger-router) | 路由模块 | \n| [think-swagger-controller](https://github.com/libery/think-swagger-controller) | 控制器模块 | \n| [think-swagger-demo](https://github.com/libery/think-swagger-demo) | 示例程序 | \n\n通过三个 middleware 模块让 ThinkJS 支持 swagger，基于 swagger 的 yam l文件实现路由和接口实现的映射，可以快速开发 RESTful 的接口。\n\n## 安装\n\n要实现该功能，在新建 ThinkJS 3.x 项目后，需要修改`src/config/middleware.js`引入并启用以上3个 middleware，并配置 swagger 文档路径和 ThinkJS controller 路径：\n\n1. 安装 3 个模块\n\n    ```bash\n    npm i think-swagger-parser think-swagger-router think-swagger-controller --save  \n    ```  \n    \n2. 修改`src/config/middleware.js`文件  \n\n+ 增加swaggerParser：\n\n    ```js\n    {\n      handle: swaggerParser,\n        options: {\n          debug: isDev,\n          api_doc: './api/swagger.yaml',\n          controller_dir: './app/controller'\n        }\n    }\n    \n    ```\n+ 增加swaggerRouter：\n\n    ```js\n    {\n      handle: swaggerRouter,\n        options: {\n          debug: isDev\n      }\n    }\n  \n    ```\n\n+ 增加swaggerController：\n\n    ```js\n    {\n      handle: swaggerController,\n        options: {\n          debug: isDev\n      }\n    }\n    \n    ```\n+ 删除默认的controller：\n\n    ```js\n    // 'controller'\n\n    ```\n\n+ 完整middleware文件：\n\n    ```js\n    const path = require('path');\n    const swaggerParser = require('think-swagger-parser');\n    const swaggerRouter = require('think-swagger-router');\n    const swaggerController = require('think-swagger-controller');\n    const isDev = think.env === 'development';\n\n    module.exports = [\n      {\n        handle: 'meta',\n        options: {\n          logRequest: isDev,\n          sendResponseTime: isDev\n        }\n      },\n      {\n        handle: 'resource',\n        enable: isDev,\n        options: {\n          root: path.join(think.ROOT_PATH, 'www'),\n          publicPath: /^\\/(static|favicon\\.ico)/\n        }\n      },\n      {\n        handle: 'trace',\n        enable: !think.isCli,\n        options: {\n          debug: isDev\n        }\n      },\n      {\n        handle: 'payload',\n        options: {}\n      },\n      {\n        handle: swaggerParser,\n        options: {\n          debug: isDev,\n          api_doc: './api/swagger.yaml',\n          controller_dir: './app/controller'\n        }\n      },\n      {\n        handle: 'router',\n        options: {}\n      },\n      {\n        handle: swaggerRouter,\n        options: {\n          debug: isDev\n        }\n      },\n      'logic',\n      {\n        handle: swaggerController,\n        options: {\n          debug: isDev\n        }\n      }\n      // 'controller'\n    ];\n\n    ```\n\n\n","funding_links":[],"categories":["Middlewares"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibery%2Fthink-swagger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flibery%2Fthink-swagger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibery%2Fthink-swagger/lists"}