Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): add logic to validate and properly handle url response when running fern init --openapi {url} #5352

Merged
Changes from 3 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
19 changes: 16 additions & 3 deletions packages/cli/init/src/utils/loadOpenApiFromUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export interface FailedLoadOpenAPI {

export async function loadOpenAPIFromUrl({ url, logger }: { url: string; logger: Logger }): Promise<LoadOpenAPIResult> {
try {
const response = await axios.get(url);
const jsonData = response.data;
const yamlData = dump(jsonData);
const yamlData = await fetchOpenAPIFromUrl({ url, logger });
const tmpDir = await tmp.dir();
const filePath = join(tmpDir.path, "openapi.yml");
logger.debug("tmpDir", tmpDir.path);
Expand All @@ -44,3 +42,18 @@ export async function loadOpenAPIFromUrl({ url, logger }: { url: string; logger:
};
}
}

async function fetchOpenAPIFromUrl({ url, logger }: { url: string; logger: Logger }): Promise<string> {
const response = await axios.get(url);
const contentType = response.headers["content-type"] ?? "";
if (contentType.includes("json")) {
return dump(response.data);
}
if (contentType.includes("yaml")) {
return response.data;
}
logger.warn(
`Unrecognized Content-Type "${contentType}" from endpoint ${url}. Please ensure you're pointing to a URL that returns JSON or YAML and not HTML (e.g. Swagger UI webpage)`
);
return response.data;
amckinney marked this conversation as resolved.
Show resolved Hide resolved
}
Loading