//import chalk from "chalk"; import fs from "fs"; import { basename } from "path"; import ora from "ora"; import { error, heading, success, warning } from "../../helper/consoleWrapper"; import { isCI } from "../../helper/isCI"; import { isURL } from "../../helper/isURL"; import { ValidateCommandOptions } from "../../models"; import { ApiResponse } from "../../models/api-response"; import { ImportApiReferenceSummary } from "../../models/import.model"; import d360APIFetch from "../d360APIFetch"; export default async function validateFlow(options: ValidateCommandOptions) { const spinner = ora({ text: "Validating..", stream: process.stdout, isEnabled: !isCI() && process.stdout.isTTY }); spinner.start(); const formData = getImportRequestFormData(options); const requestOptions = { method: "POST", body: formData, headers: new Headers({ api_token: options.apiKey, }), }; const requestUrl = `/v2/apidocs/validate`; const response = await d360APIFetch>(requestUrl, requestOptions); spinner.stop(); handleValidateResponse(response); } function getImportRequestFormData(options: ValidateCommandOptions) { const formData = new FormData(); if (!isURL(options.path)) { const fileBuffer = fs.readFileSync(options.path); formData.append("file", new Blob([fileBuffer]), basename(options.path)); } else { formData.append("url", options.path); } return formData; } function showAlerts(importWarnings) { heading("\n"); heading("Here are the alert(s) found in the OpenAPI spec file."); for (let i = 0; i < importWarnings.length; i++) { warning("Message :" + importWarnings[i]["message"] + "\n" + "Path :" + importWarnings[i]["pointer"] + "\n"); } } function showErrors(importErrors) { heading("Here are the errors found in the OpenAPI spec file."); for (let i = 0; i < importErrors.length; i++) { error("Message :" + importErrors[i]["message"] + "\n" + "Path :" + importErrors[i]["pointer"] + "\n"); } } function handleValidateResponse(response: ApiResponse) { if (response?.success) { const importWarnings = response?.result?.warnings; const importErrors = response?.result?.errors; if ((importErrors == null && importWarnings == null) || (importErrors.length <= 0 && importWarnings.length <= 0)) { success("No errors were found. Your spec file has been validated successfully."); } else if (importWarnings.length > 0 && importErrors.length > 0) { showAlerts(importWarnings); showErrors(importErrors); error(`Validation failed! We found ${importWarnings.length} alert(s) and ${importErrors.length} errors.`); } else if (importWarnings.length > 0) { showAlerts(importWarnings); warning(`Validation failed! We found ${importWarnings.length} alert(s).`); } else if (importErrors.length > 0) { showErrors(importErrors); error(`Validation failed! We found ${importErrors.length} errors.`); } } }