import type { paths } from "./skinshark-types";
type Method = "get" | "post" | "patch" | "delete";
type SuccessData<P extends keyof paths, M extends Method> =
paths[P][M] extends { responses: { 200: { content: { "application/json": { data: infer D } } } } }
? D
: paths[P][M] extends { responses: { 201: { content: { "application/json": { data: infer D } } } } }
? D
: never;
export async function call<P extends keyof paths, M extends Method>(
method: M,
path: P,
init?: { body?: unknown; onBehalfOf?: string; query?: Record<string, string>; idempotencyKey?: string },
): Promise<SuccessData<P, M>> {
const qs = init?.query
? "?" + new URLSearchParams(init.query).toString()
: "";
const headers: Record<string, string> = {
"api-key": process.env.SKINSHARK_API_KEY!,
"content-type": "application/json",
};
if (init?.onBehalfOf) headers["on-behalf-of"] = init.onBehalfOf;
if (init?.idempotencyKey) headers["idempotency-key"] = init.idempotencyKey;
const res = await fetch(`https://api.skinshark.gg${path}${qs}`, {
method: method.toUpperCase(),
headers,
body: init?.body ? JSON.stringify(init.body) : undefined,
});
const env = await res.json();
if (!env.success) {
const e = env.error;
throw new SkinsharkError(env.requestId, e.code, e.key, e.message);
}
return env.data;
}