Files
khata-ui/react-openapi/src/spec-loader.ts

18 lines
511 B
TypeScript

import * as yaml from "js-yaml";
import type { OpenApiSpec } from "./types";
export async function loadSpec(url: string): Promise<OpenApiSpec> {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch spec: ${response.status} ${response.statusText}`);
}
const text = await response.text();
const parsed = yaml.load(text);
if (!parsed || typeof parsed !== "object") {
throw new Error("Spec is empty or not an object");
}
return parsed as OpenApiSpec;
}