UNPKG

mnotify-ts-sdk

Version:

Modern, zero-dependency TypeScript SDK for mNotify BMS API - Type-safe SMS, contacts, and account management with Railway-Oriented Programming

85 lines (84 loc) 2.79 kB
import { MNotifyError } from "../errors/MNotifyError"; import { Result } from "../types/Result"; export interface MNotifyConfig { apiKey: string; baseUrl?: string; timeout?: number; maxRetries?: number; } export interface RequestConfig { method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; url: string; data?: unknown; params?: Record<string, string>; } /** * Core HTTP client for mNotify API communication using native fetch * * @remarks * This class handles all low-level HTTP requests to the mNotify API, * including authentication, retries, and error handling. */ export declare class HttpClient { private readonly apiKey; private readonly baseUrl; private readonly timeout; private readonly maxRetries; /** * Creates a new HttpClient instance * @param config - Configuration object * @param config.apiKey - Your mNotify API key * @param config.baseUrl - Base API URL (default: 'https://api.mnotify.com/api') * @param config.timeout - Request timeout in ms (default: 10000) * @param config.maxRetries - Maximum retry attempts for failed requests (default: 3) */ constructor(config: MNotifyConfig); /** * Makes an HTTP request to the mNotify API with Result type (railway-oriented programming) * @param config - Request configuration * @param retryCount - Current retry attempt (used internally) * @returns Promise with Result containing either the response data or an error * * @example * ```typescript * const client = new HttpClient({ apiKey: 'your-key' }); * const result = await client.requestSafe<T>({ * method: 'GET', * url: '/account/balance' * }); * * if (result.isOk()) { * console.log(result.value); * } else { * console.error(result.error); * } * ``` */ requestSafe<T>(config: RequestConfig, retryCount?: number): Promise<Result<T, MNotifyError>>; /** * Makes an HTTP request to the mNotify API (throws on error - legacy API) * @param config - Request configuration * @param retryCount - Current retry attempt (used internally) * @returns Promise with the parsed response data * @throws {MNotifyError} When API returns an error response * * @example * ```typescript * const client = new HttpClient({ apiKey: 'your-key' }); * const response = await client.request<T>({ * method: 'GET', * url: '/account/balance' * }); * ``` */ request<T>(config: RequestConfig, retryCount?: number): Promise<T>; /** * Builds the full URL with query parameters */ private buildUrl; private redactUrl; /** * Sleep utility for retry logic */ private sleep; }