Add files via upload
This commit is contained in:
63
src/services/api.ts
Normal file
63
src/services/api.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { UserResponse } from '../types';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL;
|
||||
|
||||
export const apiService = {
|
||||
async getUserInfo(pubkey: string): Promise<UserResponse> {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/api/user/info`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
identifier: pubkey,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) {
|
||||
// Return a default response for non-existent users
|
||||
return {
|
||||
pubkey,
|
||||
npub: '',
|
||||
is_whitelisted: false
|
||||
};
|
||||
}
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('Error fetching user info:', error);
|
||||
// Return a default response on error
|
||||
return {
|
||||
pubkey,
|
||||
npub: '',
|
||||
is_whitelisted: false
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
async whitelistUser(pubkey: string, apiKey: string): Promise<void> {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/api/whitelist/add`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Api-Key': apiKey,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
identifier: pubkey,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error whitelisting user:', error);
|
||||
throw error; // Re-throw as this is a critical operation
|
||||
}
|
||||
},
|
||||
};
|
||||
166
src/services/lnbits.ts
Normal file
166
src/services/lnbits.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const LNBITS_URL = import.meta.env.VITE_LNBITS_URL;
|
||||
const API_KEY = import.meta.env.VITE_LNBITS_API_KEY;
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: LNBITS_URL,
|
||||
headers: {
|
||||
'X-Api-Key': API_KEY,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
export interface CreateInvoiceParams {
|
||||
amount: number;
|
||||
memo: string;
|
||||
unit?: string;
|
||||
webhook?: string;
|
||||
internal?: boolean;
|
||||
extra?: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface Invoice {
|
||||
payment_hash: string;
|
||||
payment_request: string;
|
||||
checking_id: string;
|
||||
amount: number;
|
||||
fee: number;
|
||||
memo: string;
|
||||
time: number;
|
||||
bolt11: string;
|
||||
preimage: string;
|
||||
expiry: number;
|
||||
extra: Record<string, any>;
|
||||
webhook?: string;
|
||||
webhook_status?: number;
|
||||
}
|
||||
|
||||
export interface PaymentStatus {
|
||||
paid: boolean;
|
||||
preimage?: string;
|
||||
details?: {
|
||||
bolt11: string;
|
||||
checking_id: string;
|
||||
pending: boolean;
|
||||
amount: number;
|
||||
fee: number;
|
||||
memo: string;
|
||||
time: number;
|
||||
payment_hash: string;
|
||||
};
|
||||
}
|
||||
|
||||
export const lnbitsService = {
|
||||
// Create a new invoice
|
||||
async createInvoice({
|
||||
amount,
|
||||
memo,
|
||||
unit = 'sat',
|
||||
webhook = '',
|
||||
internal = false,
|
||||
extra = {},
|
||||
}: CreateInvoiceParams): Promise<Invoice> {
|
||||
try {
|
||||
const response = await api.post('/api/v1/payments', {
|
||||
out: false,
|
||||
amount,
|
||||
memo,
|
||||
unit,
|
||||
webhook,
|
||||
internal,
|
||||
extra,
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error creating invoice:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Check payment status
|
||||
async checkPayment(paymentHash: string): Promise<PaymentStatus> {
|
||||
try {
|
||||
const response = await api.get(`/api/v1/payments/${paymentHash}`);
|
||||
return {
|
||||
paid: response.data.paid,
|
||||
preimage: response.data.preimage,
|
||||
details: response.data.details,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error checking payment:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Get wallet info
|
||||
async getWalletInfo() {
|
||||
try {
|
||||
const response = await api.get('/api/v1/wallet');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error getting wallet info:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Get payment history
|
||||
async getPaymentHistory(limit = 10) {
|
||||
try {
|
||||
const response = await api.get('/api/v1/payments', {
|
||||
params: {
|
||||
limit,
|
||||
offset: 0,
|
||||
sortby: 'time',
|
||||
direction: 'desc',
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error getting payment history:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Get current exchange rate
|
||||
async getExchangeRate(currency: string = 'USD') {
|
||||
try {
|
||||
const response = await api.get(`/api/v1/rate/${currency.toLowerCase()}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error getting exchange rate:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Convert fiat to sats
|
||||
async convertFiatToSats(amount: number, from: string = 'USD') {
|
||||
try {
|
||||
const response = await api.post('/api/v1/conversion', {
|
||||
from_: from.toLowerCase(),
|
||||
amount,
|
||||
to: 'sat',
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error converting fiat to sats:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Long poll payment status
|
||||
async longPollPayment(paymentHash: string, timeout = 60000): Promise<boolean> {
|
||||
try {
|
||||
const response = await api.get(`/public/v1/payment/${paymentHash}`, {
|
||||
timeout,
|
||||
});
|
||||
return response.data.paid;
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error) && error.code === 'ECONNABORTED') {
|
||||
return false; // Timeout reached
|
||||
}
|
||||
console.error('Error polling payment:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user