MCPcopy Create free account
hub / github.com/DTStack/Taier / swagger

Function swagger

taier-ui/scripts/getSwagger.js:233–278  ·  view source on GitHub ↗

* @param {string} rawPath-swagger地址 * @param {string} filePath-生成的api接口文件地址 * @param {boolean} ts-是否生成ts文件

(rawPath, filePath, ts = false)

Source from the content-addressed store, hash-verified

231 * @param {boolean} ts-是否生成ts文件
232 */
233function swagger(rawPath, filePath, ts = false) {
234 const parseUrl = url.parse(rawPath);
235 const requestSwaggerPath = `${parseUrl.protocol}//${parseUrl.host}/swagger-resources`;
236 getSwaggerPath(requestSwaggerPath, parseUrl)
237 .then((api) => {
238 get({}, encodeURI(api))
239 .then((res) => {
240 fs.writeFileSync('./test.js', JSON.stringify(res, null, 2));
241 const FILE_NAME = ts ? DEFAULT_TS_FILE_NAME : DEFAULT_FILE_NAME;
242 const folderExist = fs.existsSync(filePath); // 文件夹是否存在
243 let interfaceData = getTags(res.tags);
244 interfaceData = sortByTags(res.paths, interfaceData);
245 const renderString = renderMustache(interfaceData);
246 if (folderExist) {
247 const fileExist = fs.existsSync(path.join(filePath, FILE_NAME));
248 if (fileExist) {
249 // 若文件存在,则处理内容
250 const fileResult = handleFileData(
251 path.join(filePath, FILE_NAME),
252 interfaceData,
253 );
254 writerFile(path.join(filePath, FILE_NAME), fileResult);
255 } else {
256 // 文件夹存在,但文件不存在
257 writerFile(path.join(filePath, FILE_NAME), renderString);
258 }
259 } else {
260 // 文件夹不存在,则文件必不存在;
261 try {
262 fs.mkdirSync(filePath, 0777);
263 writerFile(path.join(filePath, FILE_NAME), renderString);
264 } catch (err) {
265 logs(colors.red(`请检查文件路径 ${filePath} 是否合法`));
266 }
267 }
268 })
269 .catch((err) => {
270 logs(err);
271 logs(colors.red('请检查Swagger地址是否包含特殊字符'));
272 });
273 })
274 .catch((err) => {
275 logs(err);
276 logs(colors.red('请检查Swagger地址是否合法'));
277 });
278}

Callers 1

getSwagger.jsFile · 0.85

Calls 8

getSwaggerPathFunction · 0.85
getFunction · 0.85
getTagsFunction · 0.85
sortByTagsFunction · 0.85
renderMustacheFunction · 0.85
handleFileDataFunction · 0.85
writerFileFunction · 0.85
parseMethod · 0.65

Tested by

no test coverage detected