export class SkinsharkError extends Error {
constructor(
public requestId: string,
public code: number,
public key: string,
message: string,
) {
super(message);
this.name = "SkinsharkError";
}
}
export async function api<T>(
path: string,
init: RequestInit = {},
onBehalfOf?: string,
): Promise<T> {
const headers = new Headers(init.headers);
headers.set("api-key", process.env.SKINSHARK_API_KEY!);
if (init.body) headers.set("content-type", "application/json");
if (onBehalfOf) headers.set("on-behalf-of", onBehalfOf);
const res = await fetch("https://api.skinshark.gg" + path, { ...init, headers });
const body = await res.json();
if (!body.success) {
throw new SkinsharkError(body.requestId, body.error.code, body.error.key, body.error.message);
}
return body.data as T;
}