Skip to content

Commit 68e12b8

Browse files
fix(github): address discussions proxy review
1 parent 23c4c6b commit 68e12b8

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

api/github-discussions.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,11 @@ async function fetchGitHubDiscussions(token, limit) {
125125
};
126126
}
127127

128-
export default async function handler(req, res) {
128+
async function handler(req, res) {
129129
const token = getToken();
130130

131-
res.setHeader(
132-
"Cache-Control",
133-
"public, s-maxage=300, stale-while-revalidate=600",
134-
);
135-
136131
if (!token) {
132+
res.setHeader("Cache-Control", "no-store");
137133
res.status(503).json({
138134
available: false,
139135
message: UNAVAILABLE_MESSAGE,
@@ -147,8 +143,13 @@ export default async function handler(req, res) {
147143
try {
148144
const limit = parseLimit(req.query.limit);
149145
const data = await fetchGitHubDiscussions(token, limit);
146+
res.setHeader(
147+
"Cache-Control",
148+
"public, s-maxage=300, stale-while-revalidate=600",
149+
);
150150
res.status(200).json(data);
151151
} catch (error) {
152+
res.setHeader("Cache-Control", "no-store");
152153
res.status(502).json({
153154
available: false,
154155
message:
@@ -161,3 +162,5 @@ export default async function handler(req, res) {
161162
});
162163
}
163164
}
165+
166+
module.exports = handler;

src/services/githubService.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,7 @@ class GitHubService {
252252
}
253253

254254
try {
255-
const [, repositories] = await Promise.all([
256-
this.fetchOrganizationInfo(signal),
257-
this.fetchAllRepositories(signal),
258-
]);
255+
const repositories = await this.fetchAllRepositories(signal);
259256

260257
// Filter out archived repositories for active stats
261258
const activeRepos = repositories.filter((repo) => !repo.archived);
@@ -341,10 +338,25 @@ class GitHubService {
341338
},
342339
);
343340

344-
const payload = (await response.json()) as GitHubDiscussionsResponse;
341+
const contentType = response.headers.get("Content-Type") || "";
342+
const isJsonResponse = contentType.includes("application/json");
343+
const fallbackMessage = `Failed to fetch GitHub discussions: ${response.status} ${response.statusText}`;
344+
let payload: GitHubDiscussionsResponse | null = null;
345+
346+
if (isJsonResponse) {
347+
try {
348+
payload = (await response.json()) as GitHubDiscussionsResponse;
349+
} catch {
350+
throw new Error(`${fallbackMessage} (invalid JSON response)`);
351+
}
352+
}
345353

346354
if (!response.ok) {
347-
throw new Error(payload.message || "Failed to fetch GitHub discussions.");
355+
throw new Error(payload?.message || fallbackMessage);
356+
}
357+
358+
if (!payload) {
359+
throw new Error(`${fallbackMessage} (non-JSON response)`);
348360
}
349361

350362
return payload;

wiki/Documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ For this site, GitHub Discussions are now fetched dynamically through a server-s
591591

592592
Go to GitHub Settings → Developer settings → Personal access tokens → Tokens (classic)
593593
Generate new token
594-
Select scopes: public_repo, read:org
594+
Select scopes: public_repo, read:org, read:discussion
595595
Copy the token (you won't see it again!)
596596
597597
#### Storing the Token:

0 commit comments

Comments
 (0)