UNPKG

@montarist/nilvera-api

Version:

An unofficial SDK for integrating with Nilvera e-Invoice, e-Archive services

106 lines (105 loc) 3.99 kB
import { AxiosRequestConfig } from 'axios'; interface RetryConfig { maxRetries: number; retryDelay: number; retryableStatuses: number[]; } type ApiResponse<T> = { data: T; curlCommand: string; }; /** * @class ApiClient * @description A client for making HTTP requests with automatic token, API key handling */ export declare class ApiClient { private apiKey; /** @private The axios instance used for making HTTP requests */ private client; private lastCurlCommand; private retryConfig; /** * @constructor * @param {string} baseUrl - The base URL for all API requests * @param {string} apiKey - The API key for authentication * @param {RetryConfig} [retryConfig] - Optional retry configuration */ constructor(baseUrl: string, apiKey: string, retryConfig?: Partial<RetryConfig>); /** * @private * @method sleep * @description Utility method to pause execution */ private sleep; /** * @private * @method shouldRetry * @description Determines if a request should be retried based on the error */ private shouldRetry; /** * @private * @method executeWithRetry * @description Executes a request with retry logic */ private executeWithRetry; /** * @private * @method handleAxiosError * @description Handles Axios errors and converts them to NilveraApiError */ private handleAxiosError; /** * @private * @method generateCurlCommand * @description Generates a curl command from the request configuration */ private generateCurlCommand; /** * @private * @method setupInterceptors * @description Configures request interceptors for authentication headers and curl command generation */ private setupInterceptors; /** * @method get * @description Performs a GET request to the specified URL with retry mechanism * @template T - The expected response data type * @param {string} url - The endpoint URL * @param {Record<string, any>} [params] - Optional query parameters * @param {AxiosRequestConfig} [config] - Optional Axios request configuration * @returns {Promise<ApiResponse<T>>} The response data with curl command */ get<T>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; /** * @method post * @description Performs a POST request to the specified URL with retry mechanism * @template T - The expected response data type * @param {string} url - The endpoint URL * @param {any} [data] - The request body * @param {AxiosRequestConfig} [config] - Optional Axios request configuration * @returns {Promise<ApiResponse<T>>} The response data with curl command */ post<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; /** * @method put * @description Performs a PUT request to the specified URL with retry mechanism * @template T - The expected response data type * @param {string} url - The endpoint URL * @param {any} [data] - The request body * @param {AxiosRequestConfig} [config] - Optional Axios request configuration * @returns {Promise<ApiResponse<T>>} The response data with curl command */ put<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; /** * @method delete * @description Performs a DELETE request to the specified URL with retry mechanism * @template T - The expected response data type * @param {string} url - The endpoint URL * @param {any} [data] - Optional request body * @param {AxiosRequestConfig} [config] - Optional Axios request configuration * @returns {Promise<ApiResponse<T>>} The response data with curl command */ delete<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>>; } export {};