import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; import { PayForPressConfig, Error } from './types'; export class PayForPressClient { private client: AxiosInstance; private config: PayForPressConfig; constructor(config: PayForPressConfig) { this.config = config; this.client = axios.create({ baseURL: config.baseUrl || 'https://payfor.press/api/v3', headers: { 'pfp-api-key': config.apiKey, 'Content-Type': 'application/json' } }); // Add response interceptor for error handling this.client.interceptors.response.use( (response) => response.data, (error) => { if (error.response) { throw error.response.data; } throw { success: false, message: error.message || 'An unexpected error occurred' } as Error; } ); } protected async post(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise { return this.client.post(endpoint, data, config); } protected async get(endpoint: string, params?: any, config?: AxiosRequestConfig): Promise { return this.client.get(endpoint, { ...config, params }); } protected async put(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise { return this.client.put(endpoint, data, config); } protected async delete(endpoint: string, config?: AxiosRequestConfig): Promise { return this.client.delete(endpoint, config); } }