-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.ts
148 lines (141 loc) · 3.56 KB
/
client.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
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
import { z } from "zod";
import getHeaders from "happy-headers";
import { type ApiFunction, type APIMap, type ApiValidators } from "~/types.ts";
export const headers = (_templates: TemplateStringsArray, token: string) => ({
"Authorization": `Bearer ${token}`,
...getHeaders(),
});
const refresh = () => ({
"Accept": "*/*",
"User-Agent": `BeReal/8586 CFNetwork/1240.0.4 Darwin/20.6.0`,
"x-ios-bundle-identifier": "AlexisBarreyat.BeReal",
"Content-Type": "application/json",
});
export const validators: ApiValidators = {
search: z.object({
query: z.string(),
}),
feed: z.object({}),
add: z.object({
userId: z.string(),
source: z.literal("search"),
}),
friends: z.object({}),
me: z.object({}),
react: z.object({
postId: z.string(),
emoji: z.string(),
postUserId: z.string(),
}),
refresh: z.object({
refreshToken: z.string(),
}),
user: z.object({
profile_id: z.string(),
}),
};
const generic = async (url: string, token: string, body?: unknown) => {
const result = await fetch(
url,
{
method: body ? "POST" : "GET",
headers: {
...headers`${token}`,
...url.includes("refresh_token") ? refresh() : {},
},
body: body ? JSON.stringify(body) : undefined,
},
);
if (!result.ok) {
return {
error: {
message: `(${result.status}) failed to fetch \`${result.url}\``,
data: await result.text(),
},
};
}
return { data: await result.json() };
};
const map: ApiFunction = {
search: ({ query }, token) =>
generic(
`https://mobile.bereal.com/api/search/profile?query=${query}&limit=20`,
token,
),
feed: (_none, token) =>
generic(
`https://mobile.bereal.com/api/feeds/friends-v1`,
token,
),
add: (body, token) =>
generic(
`https://mobile.bereal.com/api/relationships/friend-requests`,
token,
body,
),
friends: (_none, token) =>
generic(
`https://mobile.bereal.com/api/relationships/friends`,
token,
),
me: (_none, token) =>
generic(
`https://mobile.bereal.com/api/person/me`,
token,
),
react: ({ emoji, postId, postUserId }, token) =>
generic(
`https://mobile.bereal.com/api/content/realmojis?postId=${postId}&postUserId=${postUserId}`,
token,
{ emoji },
),
refresh: ({ refreshToken: refresh_token }, token) =>
generic(
`https://auth.bereal.team/token?grant_type=refresh_token`,
token,
{
grant_type: "refresh_token",
client_id: "ios",
client_secret: "962D357B-B134-4AB6-8F53-BEA2B7255420",
refresh_token,
},
),
user: ({ profile_id }, token) =>
generic(
`https://mobile.bereal.com/api/person/profiles/${profile_id}`,
token,
),
};
export const perform = async <T extends keyof APIMap>(
key: T,
data: APIMap[T]["in"],
token: string,
): Promise<APIMap[T]["out"]> => {
const valid = validators[key].safeParse(data);
if (!valid.success) {
return { error: valid.error.message };
}
return await map[key](data, token);
};
export const execute = async <T extends keyof APIMap>(
key: T,
data: APIMap[T]["in"],
token: string,
): Promise<APIMap[T]["out"]> => {
const request = await fetch(`/api/execute`, {
method: "POST",
headers: {
authorization: `Bearer ${token}`,
},
body: JSON.stringify({ key, data }),
});
if (!request.ok) {
return {
error: {
message: `(${request.status}) failed to fetch \`${request.url}\``,
data: await request.text(),
},
};
}
return await request.json();
};