@scalar/api-reference
Version:
Generate beautiful API references from OpenAPI documents
38 lines (37 loc) • 1.8 kB
JavaScript
import { normalizeMimeTypeObject } from "./normalize-mime-type-object.js";
import { objectKeys } from "@scalar/helpers/object/object-keys";
//#region src/features/example-responses/helpers/has-response-content.ts
/**
* Checks if a media type object has any displayable content.
* This includes having a schema, a single example, or multiple examples.
*
* Note: We use explicit property checks for `example` because falsy values
* like `0`, `false`, and `""` are valid JSON examples that should be displayed.
* We still treat `null` as "no content" since it explicitly indicates absence.
*/
function hasMediaTypeContent(mediaType) {
if (!mediaType) return false;
const hasSchema = Boolean(mediaType.schema);
const hasExample = "example" in mediaType && mediaType.example !== null;
const hasExamples = Boolean(mediaType.examples);
return hasSchema || hasExample || hasExamples;
}
function isResponseKey(responseKey) {
return responseKey === "default" || /^[1-5][0-9]{2}$/.test(responseKey) || /^[1-5]XX$/.test(responseKey);
}
/**
* Checks if a response object has body content (schema, example, or examples).
* Looks through common media types in priority order.
*/
function hasResponseContent(response, responseKey) {
if (responseKey !== void 0) {
if (!isResponseKey(responseKey)) return false;
return Boolean(response);
}
const normalizedContent = normalizeMimeTypeObject(response?.content);
const keys = objectKeys(normalizedContent ?? {});
return hasMediaTypeContent(normalizedContent?.["application/json"] ?? normalizedContent?.["application/xml"] ?? normalizedContent?.["text/plain"] ?? normalizedContent?.["text/html"] ?? normalizedContent?.["*/*"] ?? normalizedContent?.[keys[0] ?? ""]);
}
//#endregion
export { hasResponseContent };
//# sourceMappingURL=has-response-content.js.map