{"id":13670694,"url":"https://github.com/hellosean1025/yapi-gen-js-code","last_synced_at":"2025-07-24T12:15:56.394Z","repository":{"id":97402085,"uuid":"170438880","full_name":"hellosean1025/yapi-gen-js-code","owner":"hellosean1025","description":"根据 YApi 的接口定义生成 javascript 的请求函数，目前内置了 axios 请求模板","archived":false,"fork":false,"pushed_at":"2019-02-14T04:14:56.000Z","size":184,"stargazers_count":29,"open_issues_count":1,"forks_count":12,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T04:28:09.238Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/hellosean1025.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}},"created_at":"2019-02-13T04:15:11.000Z","updated_at":"2025-03-10T02:20:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"dec60929-01bc-40b1-b7cd-5be294de764b","html_url":"https://github.com/hellosean1025/yapi-gen-js-code","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/hellosean1025%2Fyapi-gen-js-code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellosean1025%2Fyapi-gen-js-code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellosean1025%2Fyapi-gen-js-code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellosean1025%2Fyapi-gen-js-code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hellosean1025","download_url":"https://codeload.github.com/hellosean1025/yapi-gen-js-code/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248357748,"owners_count":21090400,"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-02T09:00:48.070Z","updated_at":"2025-04-11T06:34:38.420Z","avatar_url":"https://github.com/hellosean1025.png","language":"JavaScript","readme":"# yapi-gen-js-code\n\n\u003e 该工具目前还不完善，请勿正式使用\n\n## 环境\n* node \u003e 8.0\n* yapi \u003e= 1.5.2\n\n根据 YApi 的接口定义生成 javascript 的请求函数，目前内置了 axios 请求模板\n\n## 功能\n1. 支持 restful api\n2. 支持 get, post 等多种请求体\n3. 支持验证请求参数，需要依赖 ava \n\n## 安装\n\n```\nnpm i -g yapi-gen-js-code\n\n```\n\n## 使用\n\n1.首先需要创建 `yapi-gen.config.js` 配置文件，如下所示：\n\n```js\nmodule.exports = {\n  server: 'http://127.0.0.1:3000',\n  token: '1f048e410bc22208297dec1113136cda58306d28fb0fa819652e659b16764be6',\n  categoryId: 323\n}\n\n```\n\n2.在当前目录下执行 yapi-gen-js-code\n\n```\nyapi-gen-js-code\n```\n\n执行完成后，即可生成 yapi-gen-js-code.js 请求文件\n\n可参考该 [demo](https://github.com/hellosean1025/yapi-gen-js-code/blob/master/demo/yapi-gen-js-code.js)\n\n### 生成代码示例\n\n![](2019-02-14-11-50-00.png)\n\n## yapi-gen.config.js配置项说明\n\n| name | 类型 | 默认值 | 描述信息 |  \n| ---- | --- | --- | ---- | \n| server | String | - | 服务器地址，比如: http://yapi.demo.qunar.com | \n| token | String | - | 项目token |  \n| dist | String | - | 生成文件路径 |  \n| template | String Or Function | axios | 模板名，目前仅内置了 axios 模板，自定义请查看下面文档 | \n| globalCode | String |const axios = require('axios'); | 全局代码，会注入到最前面 |  \n| methodName | Function | 请参考源码 | 方法名生成函数，一般无需改动 |\n| categoryId | String | - | 项目分类id, 填写后只生成某个分类下的接口，默认生成该项目所有接口请求代码 | \n| enableValidte | Boolean | true| 是否开启参数验证，默认是开启的，需要引入 ava 依赖|\n\n\n## 自定义模板\n\n自定义模板就是写一个函数，返回一个 request 函数片段，接收两个参数，第一个 baseinfo,包含了请求 url, method, 请求参数等\n\n```js\n{\n  template: function(){\n    return `function request(baseinfo, options = {}){\n        let params = baseinfo.params;\n\n        options = Object.assign({}, {\n          url: baseinfo.url,\n          method: baseinfo.method,\n          data: params\n        }, options)\n\n        if(checkRequestParams \u0026\u0026 typeof checkRequestParams === 'function'){\n          checkRequestParams(interfaceData ,params)\n        }\n\n        return axios(options)\n      }`\n  }\n}\n\n\n```\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellosean1025%2Fyapi-gen-js-code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhellosean1025%2Fyapi-gen-js-code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellosean1025%2Fyapi-gen-js-code/lists"}