PayForPress-Server-SDK/src/client.ts
2025-06-01 00:26:35 +02:00

49 lines
1.4 KiB
TypeScript

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<T>(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
return this.client.post(endpoint, data, config);
}
protected async get<T>(endpoint: string, params?: any, config?: AxiosRequestConfig): Promise<T> {
return this.client.get(endpoint, { ...config, params });
}
protected async put<T>(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
return this.client.put(endpoint, data, config);
}
protected async delete<T>(endpoint: string, config?: AxiosRequestConfig): Promise<T> {
return this.client.delete(endpoint, config);
}
}