UNPKG

kirapay-sdk

Version:

Official TypeScript/JavaScript SDK for KiraPay API

163 lines (161 loc) 4.06 kB
// src/lib/http-client.ts var DefaultHttpClient = class { constructor(baseUrl, apiKey) { this.baseUrl = baseUrl; this.apiKey = apiKey; } async request(method, url, data, params) { const fullUrl = new URL(url, this.baseUrl); if (params) { Object.keys(params).forEach((key) => { if (params[key] !== void 0 && params[key] !== null) { fullUrl.searchParams.append(key, params[key]); } }); } const headers = { "x-api-key": this.apiKey }; if (data) { headers["Content-Type"] = "application/json"; } const config = { method, headers }; if (data) { config.body = JSON.stringify(data); } try { const response = await fetch(fullUrl.toString(), config); if (!response.ok) { let errorData; try { errorData = await response.json(); } catch { errorData = { statusCode: response.status, message: response.statusText }; } throw new KiraPayError(errorData); } const result = await response.json(); return result; } catch (error) { if (error instanceof KiraPayError) { throw error; } throw new KiraPayError({ statusCode: 500, message: error instanceof Error ? error.message : "Unknown error occurred" }); } } async get(url, params) { return this.request("GET", url, void 0, params); } async post(url, data) { return this.request("POST", url, data); } async put(url, data) { return this.request("PUT", url, data); } async delete(url) { return this.request("DELETE", url); } }; var KiraPayError = class extends Error { constructor(error) { super(error.message); this.name = "KiraPayError"; this.statusCode = error.statusCode; this.timestamp = error.timestamp; this.path = error.path; } }; // src/lib/kirapay.ts var KiraPay = class { constructor(config, httpClient) { this.config = { baseUrl: "https://kirapay.focalfossa.site/api", ...config }; this.httpClient = httpClient || new DefaultHttpClient(this.config.baseUrl, this.config.apiKey); } /** * Create a new payment link * @param request Payment link creation data * @returns Created payment link URL */ async createPaymentLink(request) { const response = await this.httpClient.post( "/link/generate", request ); return response.data; } /** * Get user's payment links with pagination * @param request Query parameters for filtering and pagination * @returns Paginated list of payment links */ async getPaymentLinks(request = {}) { const response = await this.httpClient.get( "/link", request ); return response.data; } /** * Get payment link details by code (public endpoint) * @param code Payment link code * @returns Payment link details */ async getPaymentLinkByCode(code) { const response = await this.httpClient.get( `/link/${code}` ); return response.data; } /** * Get wallet transactions with filtering and pagination * @param request Query parameters for filtering and pagination * @returns Paginated list of transactions */ async getWalletTransactions(request = {}) { const response = await this.httpClient.get( "/wallet/transactions", request ); return response.data; } /** * Get the current configuration */ getConfig() { return { ...this.config }; } /** * Update the API key * @param apiKey New API key */ setApiKey(apiKey) { this.config.apiKey = apiKey; this.httpClient = new DefaultHttpClient(this.config.baseUrl, apiKey); } /** * Update the base URL * @param baseUrl New base URL */ setBaseUrl(baseUrl) { this.config.baseUrl = baseUrl; this.httpClient = new DefaultHttpClient(baseUrl, this.config.apiKey); } }; export { DefaultHttpClient, KiraPay, KiraPayError, KiraPay as default };