import { GHLError } from './errors'; export interface AgencyClientConfig { agencyApiKey: string; agencyId: string; } export class GHLAgencyClient { private baseUrl = 'https://services.leadconnectorhq.com'; private agencyApiKey: string; private agencyId: string; constructor(config: AgencyClientConfig) { this.agencyApiKey = config.agencyApiKey; this.agencyId = config.agencyId; } async request(endpoint: string, options: RequestInit = {}): Promise { const url = `${this.baseUrl}${endpoint}`; const response = await fetch(url, { ...options, headers: { 'Authorization': `Bearer ${this.agencyApiKey}`, 'Version': '2021-07-28', 'Content-Type': 'application/json', ...options.headers, }, }); if (!response.ok) { const errorBody = await response.text(); throw new GHLError(response.status, `GHL Agency API Error`, errorBody); } const text = await response.text(); if (!text) return {} as T; return JSON.parse(text) as T; } // Create a new sub-account (location) for a user async createLocation(data: { name: string; email: string; phone?: string; address?: string; city?: string; state?: string; country?: string; postalCode?: string; timezone?: string; }) { return this.request('/locations/', { method: 'POST', body: JSON.stringify({ ...data, companyId: this.agencyId, }), }); } // Get location details async getLocation(locationId: string) { return this.request(`/locations/${locationId}`); } // List all locations async listLocations() { return this.request(`/locations/search?companyId=${this.agencyId}`); } }