const DEFAULT_BASE_URL = "https://api.unmarkdown.com";
export class ApiError extends Error {
constructor(
public status: number,
public code: string,
message: string,
) {
super(message);
this.name = "ApiError";
}
}
export class UnmarkdownClient {
private baseUrl: string;
private apiKey: string;
constructor(apiKey: string, baseUrl?: string) {
this.apiKey = apiKey;
this.baseUrl = (baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
}
async request<T>(
method: string,
path: string,
body?: Record<string, unknown>,
query?: Record<string, string>,
): Promise<T> {
let url = `${this.baseUrl}${path}`;
if (query) {
const params = new URLSearchParams(query);
url += `?${params.toString()}`;
}
const headers: Record<string, string> = {
Authorization: `Bearer ${this.apiKey}`,
"User-Agent": "unmarkdown-mcp/1.0",
};
if (body) {
headers["Content-Type"] = "application/json";
}
const res = await fetch(url, {
method,
headers,
body: body ? JSON.stringify(body) : undefined,
});
const data = await res.json();
if (!res.ok) {
throw new ApiError(
res.status,
data.error?.code ?? "unknown",
data.error?.message ?? `API returned ${res.status}`,
);
}
return data as T;
}
}