UNPKG

@apistudio/apim-cli

Version:

CLI for API Management Products

102 lines (101 loc) 2.75 kB
import { AppConstants } from '../constants/app.constants.js'; import TypeNotFoundException from '../exeptions/type-not-found-exception.js'; import { getBaseAsset, isBaseAsset } from './asset.helper.js'; import { isJSON, parseJSON } from './json.helper.js'; import { isOpenAPI } from './open-api.helper.js'; import { isSwagger } from './swagger-api.helper.js'; import { isYAML, loadYaml } from './yaml.helper.js'; import * as yaml from 'js-yaml'; export function getDocumentBasedOnLanguage(fileContent, language) { try { if ((language === 'yaml') || (language === 'yml')) { return loadYaml(fileContent); } else if (language === 'json') { return parseJSON(fileContent); } else { return undefined; } } catch (error) { console.error(error); } return undefined; } export function getType(data, language) { let type = undefined; if (isOpenAPI(data, language) || isSwagger(data, language)) { type = 'rest'; } else if (isBaseAsset(data, language)) { type = 'asset'; } else { return new TypeNotFoundException(AppConstants.TYPE_NOT_FOUND_EXCEPTION, 404); } return type; } export function getBaseKind(data, language) { if (isOpenAPI(data, language)) { return 'openapi'; } else if (isSwagger(data, language)) { return 'swagger'; } else if (isBaseAsset(data, language)) { return getBaseAsset(data, language); } else { return ''; } } export function getLanguage(data) { let language = ''; if (isJSON(data)) { language = 'json'; } else if (isYAML(data)) { language = 'yaml'; } return language; } export function getContentType(language) { let contentType = ''; if ((language === 'yaml') || (language === 'yml')) { contentType = AppConstants.APPLICATION_YAML_REQ_TYPE; } else if (language === 'json') { contentType = AppConstants.APPLICATION_JSON_REQ_TYPE; } return contentType; } export function isInvalidFile(text, fileExt) { if (fileExt === 'yaml' || fileExt === 'yml') { try { if (text.trim().startsWith('{') || text.trim().startsWith('[')) { return true; } if (text.trim() !== '') { yaml.loadAll(text); } return false; } catch (error) { return true; } } if (fileExt === 'json') { try { if (text.trim() !== '') { JSON.parse(text); } return false; } catch (error) { return true; } } return true; } ;