Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions src/domains/models/SpecificationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

getFileURL() {
return this.fileURL;
}
}

getKind() {
return this.kind;
Expand Down Expand Up @@ -95,18 +95,15 @@
let targetUrl = URLpath;
let proxyUrl = '';

// Check if URLpath contains a proxy URL
if (URLpath.includes(delimiter)) {
[targetUrl, proxyUrl] = URLpath.split(delimiter);
}

try {
// Validate the target URL
new URL(targetUrl);

const fetchOptions: RequestInit & { agent?: HttpsProxyAgent<string> } = { method: 'GET' };

// If proxy URL is provided, create a proxy agent
if (proxyUrl) {
try {
new URL(proxyUrl);
Expand Down Expand Up @@ -158,12 +155,10 @@
context?: boolean;
}

/* eslint-disable sonarjs/cognitive-complexity */
export async function load(

Check failure on line 158 in src/domains/models/SpecificationFile.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ3jHKltHkn-ybDJuUtr&open=AZ3jHKltHkn-ybDJuUtr&pullRequest=2154
filePathOrContextName?: string,
loadType?: LoadType,
): Promise<Specification> {
// NOSONAR
try {
if (filePathOrContextName) {
if (loadType?.file) {
Expand Down Expand Up @@ -237,7 +232,8 @@
return true;
}

const extension = name.split('.')[1];
// FIX (Issue #1940): Verwende path.extname() statt name.split('.')[1]
const extension = path.extname(name).replace(/^\./, '');

const allowedExtenstion = ['yml', 'yaml', 'json'];

Expand Down Expand Up @@ -283,24 +279,15 @@
JSON.parse(content);
return 'json';
}
// below yaml.load is not a definitive way to determine if a file is yaml or not.
// it is able to load .txt text files also.
yaml.load(content);
return 'yaml';
} catch {
return undefined;
}
}

/**
* Converts a JSON or YAML specification to YAML format.
*
* @param spec - The specification content as a string
* @returns The YAML formatted string, or undefined if conversion fails
*/
export function convertToYaml(spec: string): string | undefined {
try {
// JS object -> YAML string
const jsonContent = yaml.load(spec);
return yaml.dump(jsonContent);
} catch (err: unknown) {
Expand All @@ -309,17 +296,9 @@
}
}

/**
* Converts a JSON or YAML specification to JSON format.
*
* @param spec - The specification content as a string
* @returns The JSON formatted string, or undefined if conversion fails
*/
export function convertToJSON(spec: string): string | undefined {
try {
// JSON or YAML String -> JS object
const jsonContent = yaml.load(spec);
// JS Object -> pretty JSON string
return JSON.stringify(jsonContent, null, 2);
} catch (err: unknown) {
logger.error(`Failed to convert spec to JSON: ${getErrorMessage(err)}`);
Expand Down
Loading
Loading