UNPKG

@apistudio/apim-cli

Version:

CLI for API Management Products

46 lines (38 loc) 1.1 kB
/** * Copyright Super iPaaS Integration LLC, an IBM Company 2024 */ import yaml from 'js-yaml'; import { parseDocument, stringify, Document } from "yaml"; import { ERROR_PROCESSING_YAML_FILE } from '../../constants/message-constants.js'; const readYaml = <T> (input: string): T => { return yaml.load(input) as T; }; const readMultiYaml = <T> (fileName: string, input: string): T[] => { try { return yaml.loadAll(input) as T[]; } catch (error) { throw new Error(`${ERROR_PROCESSING_YAML_FILE} ${fileName}: Invalid Yaml`); } }; const convertToYAMLString = <T> (input: T): string => { return yaml.dump(input); }; const parseYamlContent = (content: string): Document => { try { return parseDocument(content); } catch (error) { throw new Error("Failed to parse YAML content."); } }; const convertParsedYmlToString = (yamlDoc: Document): string => { try { return stringify(yamlDoc); } catch (error) { throw new Error("Failed to convert YAML document to string."); } }; export {readYaml, convertToYAMLString, readMultiYaml, parseYamlContent, convertParsedYmlToString};