fern
fern copied to clipboard
Validate Swagger/OpenAPI response when running `fern init --openapi {url}`
Feature Description
Add a simple validation check to prevent users from accidentally running the fern init command against the Swagger HTML endpoint and inform them that they need to point it at an endpoint that returns pure JSON or Y(A)ML.
I was guilty of personally making this mistake on two occasions. Would be a nice bump in DX.
# returns JSON - good ✅
$ fern init --openapi http://localhost:3000/api-json # returns JSON
# returns HTML - bad ❌
$ fern init --openapi http://localhost:3000/api
Proposed Generated Code (Optional)
Simple approach:
const response = await axios.get(url);
const contentType = response.headers['content-type'];
if (contentType.includes('text/html')) {
throw Error("Please point to a URL that returns JSON or YML")
}
// continue...
More robust:
const response = await axios.get(url);
const contentType = response.headers['content-type'];
const validContentTypes = ['application/json', 'application/x-yaml', 'text/yaml'];
if (!validContentTypes.some(type => contentType.includes(type))) {
throw new Error("Please point to a URL that returns one of the supported content types: ", validContentTypes.join(", "));
}
// continue...
@dannysheridan