UNPKG

milady

Version:

Generate the front-end code by parsing the interface document

44 lines (38 loc) 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _handelStr = require("../../utils/handelStr"); function main(SwaggerData) { const { paths } = SwaggerData; let str = "import { stringify } from 'qs';\nimport request from '@/utils/request';\n"; Object.keys(paths).forEach(api => { Object.keys(paths[api]).forEach(method => { if (api !== '/') { const { description } = paths[api][method]; str = str.concat(services((0, _handelStr.apiToName)(api, method), api, description && description.replace(/\n/g, ''), method)); } }); }); const file = [{ fileName: 'api.js', fileStr: str }]; return file; } function services(name, api, desc, method) { if (method === 'post') { return `\nexport async function ${name}(params) {\n return request('${api}', {\n method: 'POST',\n body: params,\n });\n} // ${desc}\n`; } return `\nexport async function ${name}(params) {\n return request(\`${api}?\${stringify(params)}\`);\n} // ${desc}\n`; } var _default = { outPath: 'src/services', handelData: main }; exports.default = _default;