import type { PersonaProjectSubscription } from './types'; type ProjectsApiConfig = { baseUrl: string; apiKey: string; }; class ProjectsApi { private readonly baseUrl: string; private readonly apiKey: string; constructor(config: ProjectsApiConfig) { this.baseUrl = config.baseUrl.replace(/\/$/, ''); this.apiKey = config.apiKey; } private async request(path: string, init?: RequestInit): Promise { const response = await fetch(`${this.baseUrl}${path}`, { ...init, headers: { 'Content-Type': 'application/json', 'x-persona-apikey': this.apiKey, ...(init?.headers ?? {}), }, }); if (!response.ok) { throw new Error(await response.text()); } return (await response.json()) as T; } async getSubscription(projectId: string): Promise { return this.request(`/projects/${projectId}/subscription`); } async changeSubscriptionPlan( projectId: string, toPlanType: PersonaProjectSubscription['planType'], ): Promise { return this.request(`/projects/${projectId}/subscription/actions/change-plan`, { method: 'POST', body: JSON.stringify({ toPlanType }), }); } async addSubscriptionCredits(projectId: string, credits: number): Promise { return this.request(`/projects/${projectId}/subscription/actions/add-credits`, { method: 'POST', body: JSON.stringify({ credits }), }); } } export { ProjectsApi }; export type { ProjectsApiConfig };