Skip to content

Commit

Permalink
fix: fix server error with sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
Shramkoweb committed Aug 31, 2022
1 parent 8e1eae7 commit f5e74c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
5 changes: 1 addition & 4 deletions next.config.js
Expand Up @@ -68,10 +68,7 @@ const nextConfig = {

const nextConfigByEnv = {
production: withSentryConfig(nextConfig, {
silent: true,
// https://github.com/getsentry/sentry-javascript/issues/5667
disableServerWebpackPlugin: true,
disableClientWebpackPlugin: false
silent: true
}),
test: nextConfig,
development: nextConfig
Expand Down
47 changes: 18 additions & 29 deletions pages/api/github.ts
@@ -1,5 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';

const GH_HEADERS = new Headers({
Authorization: `Bauer ${process.env.GITHUB_TOKEN}`,
Authorization: `Bauer ${process.env.GITHUB_TOKEN}`
});

const starReducer = (acc: number, repo: { stargazers_count: number }) => {
Expand All @@ -8,47 +10,34 @@ const starReducer = (acc: number, repo: { stargazers_count: number }) => {
return result;
};

export default async function handler() {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const [userResponse, repositoriesResponse] = await Promise.all([
fetch('https://api.github.com/users/shramkoweb', {
headers: GH_HEADERS,
headers: GH_HEADERS
}),
fetch('https://api.github.com/users/shramkoweb/repos?per_page=100', {
headers: GH_HEADERS,
}),
headers: GH_HEADERS
})
]);
const [user, repos] = await Promise.all([
userResponse.json(),
repositoriesResponse.json(),
repositoriesResponse.json()
]);

const mineRepos = repos.filter((repo: { fork: boolean }) => !repo.fork);
const stars = mineRepos.reduce(starReducer, 0);

return new Response(
JSON.stringify({
followers: user.followers,
stars,
}),
{
status: 200,
headers: {
'content-type': 'application/json',
'cache-control': 'public, s-maxage=1200, stale-while-revalidate=600',
},
},
);
} catch (e) {
return new Response(JSON.stringify(e), {
status: 500,
headers: {
'content-type': 'application/json',
},
// With edge error we have error
// https://github.com/getsentry/sentry-javascript/issues/5667
return res.status(200).json({
stars,
followers: user.followers
});
} catch ({ message }) {
return res.status(500).json({ message });
}
}

export const config = {
runtime: 'experimental-edge',
};

0 comments on commit f5e74c5

Please sign in to comment.