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
|
import type {
PublicKeyCredentialCreationOptionsJSON,
RegistrationResponseJSON,
} from '@simplewebauthn/typescript-types';
const backendUrl = '/api';
export async function captureOrder(
orderId: string
): Promise<TypedResponse<CaptureOrderResponseBody>> {
const body = JSON.stringify({
id: orderId,
isApproved: true,
});
const headers = {
'Content-Type': 'application/json',
};
const response = await fetch(`${backendUrl}/payments/order`, {
method: 'PATCH',
body,
headers,
});
return typeResponse(response);
}
type CaptureOrderResponseBody = {
orderId: string;
paypalStatus: string;
};
export async function createOrder(
params: CreateOrderParams
): Promise<TypedResponse<CreateOrderResponseBody>> {
const response = await fetch(`${backendUrl}/payments/order`, {
method: 'POST',
body: JSON.stringify(params),
headers: { 'Content-Type': 'application/json' },
});
return typeResponse(response);
}
type CreateOrderParams = {
productDescription: string;
shortDescription: string;
totalPrice: string;
};
type CreateOrderResponseBody = {
orderId: string;
approvalLink: string;
};
export async function getRegistrationOptions(
params: GetRegistrationOptionsParams
): Promise<TypedResponse<GetRegistrationOptionsResponseBody>> {
const queryString = new URLSearchParams(params).toString();
// If last param ends with, e.g., `.com` as in an email address, the `.com`
// will be interpreted as a file extension, and this will break everything.
// Hack to avoid this bug.
const hackyQueryString = `${queryString}&hack=toavoidbug`;
const response = await fetch(`${backendUrl}/auth/registration?${hackyQueryString}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
return typeResponse(response);
}
type GetRegistrationOptionsParams = {
userId: string;
displayName: string;
};
type GetRegistrationOptionsResponseBody = PublicKeyCredentialCreationOptionsJSON;
export async function verifyRegistrationResponse(params: VerifyRegistrationResponseParams) {
return await fetch(`${backendUrl}/auth/registration`, {
method: 'POST',
body: JSON.stringify(params),
headers: { 'Content-Type': 'application/json' },
});
}
type VerifyRegistrationResponseParams = {
userId: string;
displayName: string;
registrationResponse: RegistrationResponseJSON;
};
|