forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
157 lines (145 loc) · 4.13 KB
/
Copy pathapi.ts
File metadata and controls
157 lines (145 loc) · 4.13 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { API_BASE_URL } from "./config";
import { getToken } from "./auth";
import {
adaptBounty,
adaptMilestone,
adaptMaintenancePool,
adaptReputation,
type RawBounty,
type RawMilestone,
type RawMaintenancePool,
type RawReputationSnapshot,
type RawUserProfile,
} from "./adapters";
import type { Bounty, Milestone, MaintenancePool, ReputationProfile } from "@/types";
export class ApiUnavailableError extends Error {}
export class ApiRequestError extends Error {
constructor(
message: string,
public status: number,
) {
super(message);
}
}
async function request<T>(path: string, init?: RequestInit): Promise<T> {
const res = await fetch(`${API_BASE_URL}${path}`, {
...init,
headers: { "Content-Type": "application/json", ...init?.headers },
cache: "no-store",
});
if (!res.ok) {
throw new ApiUnavailableError(`Request to ${path} failed: ${res.status}`);
}
return res.json() as Promise<T>;
}
/**
* Fetches live data from the MergeFi backend, falling back to the provided
* mock value when the backend is unreachable (e.g. during frontend-only demos).
*/
export async function fetchWithFallback<T>(
path: string,
fallback: T,
): Promise<T> {
try {
return await request<T>(path);
} catch {
return fallback;
}
}
/**
* Client-side call that attaches the signed-in user's JWT (if any) and
* surfaces backend error bodies instead of silently falling back — used for
* actions the user explicitly triggers (claim, fund, deposit, ...), where
* hiding a failure behind mock data would be misleading.
*/
export async function apiRequest<T>(
path: string,
init?: RequestInit,
): Promise<T> {
const token = getToken();
const res = await fetch(`${API_BASE_URL}${path}`, {
...init,
headers: {
"Content-Type": "application/json",
...(token ? { Authorization: `Bearer ${token}` } : {}),
...init?.headers,
},
});
if (!res.ok) {
const body = await res.text();
let message = body;
try {
message = JSON.parse(body).message ?? body;
} catch {
// plain-text error body, use as-is
}
throw new ApiRequestError(message || `Request failed (${res.status})`, res.status);
}
if (res.status === 204) return undefined as T;
return res.json() as Promise<T>;
}
export function apiPost<T>(path: string, body?: unknown): Promise<T> {
return apiRequest<T>(path, {
method: "POST",
body: body !== undefined ? JSON.stringify(body) : undefined,
});
}
/**
* Live-data fetchers that adapt mergefi-backend's nested TypeORM entity JSON
* into the flat shapes the UI renders, falling back to mock data (already in
* the target shape) when the backend is unreachable.
*/
export async function fetchBounties(fallback: Bounty[]): Promise<Bounty[]> {
try {
const raw = await request<RawBounty[]>("/bounties");
return raw.map(adaptBounty);
} catch {
return fallback;
}
}
export async function fetchBounty(
id: string,
fallback: Bounty | undefined,
): Promise<Bounty | undefined> {
try {
const raw = await request<RawBounty>(`/bounties/${id}`);
return adaptBounty(raw);
} catch {
return fallback;
}
}
export async function fetchMilestones(fallback: Milestone[]): Promise<Milestone[]> {
try {
const raw = await request<RawMilestone[]>("/milestones");
return raw.map(adaptMilestone);
} catch {
return fallback;
}
}
export async function fetchMaintenancePools(
fallback: MaintenancePool[],
): Promise<MaintenancePool[]> {
try {
const raw = await request<RawMaintenancePool[]>("/maintenance-pools");
return raw.map(adaptMaintenancePool);
} catch {
return fallback;
}
}
export async function fetchReputationByUsername(
username: string,
fallback: ReputationProfile | null,
): Promise<ReputationProfile | null> {
try {
const users = await request<(RawUserProfile & { id: string })[]>("/users");
const user = users.find((u) => u.username === username);
if (!user) return fallback;
const snapshot = await request<RawReputationSnapshot | null>(
`/reputation/${user.id}`,
);
return adaptReputation(user, snapshot);
} catch {
return fallback;
}
}
export { request };