-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiClient.ts
More file actions
49 lines (45 loc) · 1.64 KB
/
Copy pathapiClient.ts
File metadata and controls
49 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { env } from '../config/env';
import { logger } from '../utils/logger';
import { Payment, PaymentStatus } from '../core/types';
/**
* This service does not own payment storage — freclean-api does
* (src/modules/payments.ts there). This client lets the verification
* worker fetch pending payments and push status transitions back through
* freclean-api's existing, audited /transition endpoint, rather than
* writing to freclean-api's data store directly.
*/
async function apiRequest<T>(path: string, options: RequestInit = {}): Promise<T> {
const res = await fetch(`${env.freCleanApiUrl}${path}`, {
...options,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${env.freCleanApiWorkerToken}`,
...options.headers,
},
});
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.error ?? `freclean-api request failed: ${res.status}`);
}
return res.json();
}
export async function fetchPendingWeb3Payments(): Promise<Payment[]> {
const res = await apiRequest<{ data: Payment[] }>('/api/payments');
return res.data.filter(
(p) => p.method === 'web3' && !['confirmed', 'failed', 'expired', 'refunded'].includes(p.status),
);
}
export async function pushTransition(paymentId: string, status: PaymentStatus, txHash?: string): Promise<void> {
try {
await apiRequest(`/api/payments/${paymentId}/transition`, {
method: 'POST',
body: JSON.stringify({ status, txHash }),
});
} catch (err) {
logger.error('Failed to push payment transition to freclean-api', {
paymentId,
status,
err: (err as Error).message,
});
}
}