forked from albinkarvling/medito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.ts
74 lines (63 loc) · 2.6 KB
/
requests.ts
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
import { DonationProgress, Donator, MessageProps } from "@/types";
export const APIRequest = {
// Mock function to get donation progress, i.e., current donations and goal donations.
// Should return an object with the 'current' and 'goal' properties.
getDonationProgress: async () => {
const CURRENT_DONATIONS = 12424.9;
const GOAL_DONATIONS = 20000;
const data: DonationProgress = await new Promise(res => {
res({
current: CURRENT_DONATIONS,
goal: GOAL_DONATIONS,
})
})
return data;
},
// Mock function to get donator list.
// Should return an array of type Donator.
getDonators: async () => {
const DONATORS = [
{ name: 'Poxen', amount: 100, currency: 'sek', timestamp: Date.now() - 500000 },
{ name: 'Nick', amount: 5, currency: 'usd', timestamp: Date.now() - 750000 },
{ name: 'Ashley', amount: 12, currency: 'usd', timestamp: Date.now() - 1700000 },
{ name: 'Emily', amount: 1, currency: 'usd', timestamp: Date.now() - 500000000 },
]
const donators: Donator[] = await new Promise(res => res(DONATORS));
return donators;
},
// Function to get specific donation session data.
getDonationSession: async (sessionId: string) => {
const res = await fetch(`/payments/${sessionId}`);
if(!res.ok) throw new Error('Failed to confirm donation');
return await res.json() as Donator;
},
// Function to get a stripe checkout link based on amount.
getStripeLink: async (amount: string | number, currency: string, interval?: 'month') => {
const res = await fetch('/payments', {
method: 'POST',
body: JSON.stringify({ amount, currency, interval }),
headers: {
'Content-Type': 'application/json',
}
})
if(!res.ok) throw new Error((await res.json()).message);
const { url } = await res.json();
return url;
},
// Function to get currency exchange rates.
getCurrencyRates: async () => {
const res = await fetch('/currencies');
if(!res.ok) throw new Error('Failed to get currency rates');
return await res.json();
},
// Mock function to send a message.
// Should return a Response object.
sendMessage: async ({ name, email, message }: MessageProps) => {
const res = await new Promise((res, rej) => {
setTimeout(() => {
res(new Response());
}, 1000);
});
return res as Response;
}
}