UNPKG

@apistudio/apim-cli

Version:

CLI for API Management Products

29 lines (24 loc) 972 B
import { COMMA } from "../constants/app-constants.js"; import { INVALID_ENDPOINT } from "../constants/message-constants.js"; import { showError } from "../helpers/common/message-helper.js"; const isValidHttpUrl = (endpoint: string): boolean => { try { const parsedUrl = new URL(endpoint); return parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:'; } catch { return false; } }; const validateEndpoints = (endpoints: string): string[] | false => { const endpointArray = endpoints.split(COMMA).map(endpoint => endpoint.trim()); const validEndpoints: string[] = []; for (const endpoint of endpointArray) { if (isValidHttpUrl(endpoint)) { validEndpoints.push(endpoint); } else { showError(INVALID_ENDPOINT(endpoint)); } } return validEndpoints.length > 0 ? validEndpoints : false; }; export default validateEndpoints;