UNPKG

friday-sdk

Version:

Official JavaScript/TypeScript SDK for the Friday API

44 lines (35 loc) 1.05 kB
import { SDKOptions } from "./types"; export class APIClient { private apiKey: string; private baseUrl: string; constructor(options: SDKOptions) { this.apiKey = options.apiKey; this.baseUrl = options.baseUrl || "https://api.fridaydata.com"; } async request<T = any>( path: string, options: RequestInit = {}, auth?: { useApiKey?: boolean; bearerToken?: string } ): Promise<T> { const headers: Record<string, string> = { "Content-Type": "application/json", ...(options.headers as Record<string, string> | undefined), }; const useApiKey = auth?.useApiKey ?? true; if (useApiKey) { headers["X-API-Key"] = this.apiKey; } if (auth?.bearerToken) { headers["Authorization"] = `Bearer ${auth.bearerToken}`; } const res = await fetch(`${this.baseUrl}${path}`, { ...options, headers, }); if (!res.ok) { const errorText = await res.text(); throw new Error(`API Error ${res.status}: ${errorText}`); } return res.json(); } }