MCPcopy
hub / github.com/YMFE/yapi / exportData

Method exportData

exts/yapi-plugin-gen-services/controller.js:77–206  ·  view source on GitHub ↗
(ctx, fullPath)

Source from the content-addressed store, hash-verified

75 }
76
77 async exportData(ctx, fullPath) {
78 let pid = ctx.request.query.pid;
79 let type = ctx.request.query.type;
80 let status = ctx.request.query.status;
81 let isWiki = ctx.request.query.isWiki;
82
83 if (!pid) {
84 return ctx.body = yapi.commons.resReturn(null, 200, 'pid 不为空');
85 }
86 let curProject, wikiData;
87 let tp = '';
88 try {
89 curProject = await this.projectModel.get(pid);
90 const basepath = curProject.basepath;
91 if (isWiki === 'true') {
92 const wikiModel = require('../yapi-plugin-wiki/wikiModel.js');
93 wikiData = await yapi.getInst(wikiModel).get(pid);
94 }
95 ctx.set('Content-Type', 'application/octet-stream');
96 const list = await this.handleListClass(pid, status);
97
98 switch (type) {
99 case 'markdown': {
100 tp = await createMarkdown.bind(this)(list, false);
101 ctx.set('Content-Disposition', `attachment; filename=api.md`);
102 return (ctx.body = tp);
103 }
104 case 'json': {
105 let data = this.handleExistId(list);
106 if (Array.isArray(data) && fullPath === 'full-path' && basepath) {
107 data.forEach(function(cate) {
108 if (Array.isArray(cate.list)) {
109 cate.proBasepath = basepath;
110 cate.proName = curProject.name;
111 cate.proDescription = curProject.desc;
112 cate.list = cate.list.map(function(api) {
113 api.path = api.query_path.path = (basepath + '/' + api.path).replace(/[\/]{2,}/g, '/');
114 return api;
115 });
116 }
117 })
118 }
119 tp = JSON.stringify(data, null, 2);
120 ctx.set('Content-Disposition', `attachment; filename=api.json`);
121 return (ctx.body = tp);
122 }
123 default: {
124 //默认为html
125 tp = await createHtml.bind(this)(list);
126 ctx.set('Content-Disposition', `attachment; filename=api.html`);
127 return (ctx.body = tp);
128 }
129 }
130 } catch (error) {
131 yapi.commons.log(error, 'error');
132 ctx.body = yapi.commons.resReturn(null, 502, '下载出错');
133 }
134

Callers 1

exportFullDataMethod · 0.95

Calls 3

handleListClassMethod · 0.95
handleExistIdMethod · 0.95
getMethod · 0.45

Tested by

no test coverage detected