import { getUserAgent } from "universal-user-agent";
import { createAshraError } from "./errors.js";
const USER_AGENT = `modelcontextprotocol/servers/ashra/v1 ${getUserAgent()}`;
type RequestOptions = {
method?: string;
body?: unknown;
headers?: Record<string, string>;
}
async function parseResponseBody(response: Response): Promise<unknown> {
const contentType = response.headers.get("content-type");
if (contentType?.includes("application/json")) {
return response.json();
}
return response.text();
}
export async function ashraRequest(
url: string,
options: RequestOptions = {}
): Promise<unknown> {
const headers: Record<string, string> = {
"Accept": "application/vnd.ashra.v1+json",
"Content-Type": "application/json",
"User-Agent": USER_AGENT,
...options.headers,
};
const response = await fetch(url, {
method: options.method || "GET",
headers,
body: options.body ? JSON.stringify(options.body) : undefined,
});
const responseBody = await parseResponseBody(response);
if (!response.ok) {
throw createAshraError(response.status, responseBody);
}
return responseBody;
}