/
fetchOpenSaucedApiData.ts
210 lines (183 loc) · 6.25 KB
/
fetchOpenSaucedApiData.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { cachedFetch } from "./cache";
import {
OPEN_SAUCED_USERS_ENDPOINT,
OPEN_SAUCED_SESSION_ENDPOINT,
OPEN_SAUCED_REPOS_ENDPOINT,
OPEN_SAUCED_USER_INSIGHTS_ENDPOINT,
OPEN_SAUCED_USER_HIGHLIGHTS_ENDPOINT,
OPEN_SAUCED_HIGHLIGHTS_ENDPOINT,
} from "../constants";
import { IInsight } from "../ts/InsightDto";
import { Highlights } from "../ts/types";
export const isOpenSaucedUser = async (username: string) => {
try {
const response = await fetch(
`${OPEN_SAUCED_USERS_ENDPOINT}/${username}`,
);
if (response.status === 200) {
const data = await response.json();
return data.is_open_sauced_member;
}
return false;
} catch (error) {
return false;
}
};
export const checkTokenValidity = async (token: string) => {
const response = await fetch(OPEN_SAUCED_SESSION_ENDPOINT, {
method: "GET",
headers: { Authorization: `Bearer ${token}` },
});
return response.status === 200;
};
export const getUserData = async (userName: string, forceRefresh: boolean = false) => cachedFetch(`${OPEN_SAUCED_USERS_ENDPOINT}/${userName}`, {
expireInSeconds: 2 * 60 * 60,
forceRefresh,
headers: { Accept: "application/json" },
}).then(async resp => {
if (!resp?.ok) {
console.log("error getting user info");
}
return resp?.json();
})
.then(json => json);
export const getUserPRData = async (userName: string, forceRefresh: boolean = false) => cachedFetch(`${OPEN_SAUCED_USERS_ENDPOINT}/${userName}/prs`, {
expireInSeconds: 2 * 60 * 60,
forceRefresh,
headers: { Accept: "application/json" },
}).then(async resp => {
if (!resp?.ok) {
console.log("error getting user PR info");
}
return resp?.json();
})
.then(json => json);
export const getUserHighlightsData = async (userName: string, forceRefresh: boolean = false) => cachedFetch(`${OPEN_SAUCED_USERS_ENDPOINT}/${userName}/highlights`, {
expireInSeconds: 2 * 60 * 60,
forceRefresh,
headers: { Accept: "application/json" },
}).then(async resp => {
if (!resp?.ok) {
console.log("error getting user highlights info");
}
return resp?.json();
})
.then(json => json);
const getUserVotes = async (userToken: string, page: number = 1, limit: number = 1000, repos: any[] = []): Promise<any[]> => {
const response = await fetch(
`${OPEN_SAUCED_REPOS_ENDPOINT}/listUserVoted?page=${page}&limit=${limit}`,
{
method: "GET",
headers: { Authorization: `Bearer ${userToken}` },
},
);
if (response.status === 200) {
const votesData = await response.json();
const newRepos = repos.concat(votesData.data);
if (votesData.data.length === limit) {
return getUserVotes(userToken, page + 1, limit, newRepos);
}
return newRepos;
}
return repos;
};
export const checkUserVotedRepo = async (userToken: string, repoName: string) => {
const userVotes = await getUserVotes(userToken);
return userVotes.some((repo: any) => repo.name === repoName);
};
export const repoExistsOnOpenSauced = async (ownerName: string, repoName: string) => {
const response = await fetch(
`${OPEN_SAUCED_REPOS_ENDPOINT}/${ownerName}/${repoName}`,
);
return response.status === 200;
};
export const getRepoData = async (ownerName: string, repoName: string, forceRefresh: boolean = false) => cachedFetch(`${OPEN_SAUCED_REPOS_ENDPOINT}/${ownerName}/${repoName}`, {
expireInSeconds: 2 * 60 * 60,
forceRefresh,
headers: { Accept: "application/json" },
}).then(async resp => {
if (!resp?.ok) {
console.log("error getting repo info");
}
return resp?.json();
})
.then(json => json);
export const voteOrUnvoteRepo = async (userToken: string, ownerName: string, repoName: string, method: "PUT" | "DELETE") => {
const response = await fetch(
`${OPEN_SAUCED_REPOS_ENDPOINT}/${ownerName}/${repoName}/vote`,
{
method,
headers: { Authorization: `Bearer ${userToken}` },
},
);
return response.status === 200;
};
export const getUserInsightsData = async (userToken: string) => {
const response = await fetch(
`${OPEN_SAUCED_USER_INSIGHTS_ENDPOINT}`,
{
method: "GET",
headers: { Authorization: `Bearer ${userToken}` },
},
);
return response.json();
};
export const updateInsight = async (userToken: string, repoId: string, checked: boolean, repoFullName: string, insight: IInsight) => {
insight.repos = insight.repos.map((repo: any) => ({
id: repo.repo_id,
fullName: repo.full_name,
}));
const response = await fetch(
`${OPEN_SAUCED_USER_INSIGHTS_ENDPOINT}/${insight.id}`,
{
method: "PATCH",
headers: {
Authorization: `Bearer ${userToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: insight.name,
is_public: insight.is_public,
repos: checked
? [
...insight.repos,
{
id: repoId,
fullName: `${repoFullName}`,
},
]
: insight.repos.filter(
(repo: any) => repo.id !== repoId,
),
}),
},
);
return response.status === 200;
};
export const getHighlights = async (): Promise<Highlights | undefined> => {
const response = await cachedFetch(
`${OPEN_SAUCED_HIGHLIGHTS_ENDPOINT}`,
{
method: "GET",
expireInSeconds: 300,
},
);
if (!response) {
return;
}
return response.json();
};
export const cerateHighlight = async (userToken: string, url: string, title: string, highlight: string, shipped_at?: string) => fetch(OPEN_SAUCED_USER_HIGHLIGHTS_ENDPOINT, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${userToken}`,
},
method: "POST",
body: JSON.stringify({
url,
title,
shipped_at,
highlight,
}),
});