I’m working on next js react (ts) app and do I need a server to make github api request as I have added below in my next.config.mjs.
My app only requires call to GitHub api to fetch content of file of public repo.
const nextConfig = {
async headers() {
return [
{
source: "/:path*",
headers: [
{ key: "Access-Control-Allow-Credentials", value: "true" },
{ key: "Access-Control-Allow-Origin", value: "*" },
{ key: "Access-Control-Allow-Methods", value: "GET,DELETE,PATCH,POST,PUT" },
{ key: "Access-Control-Allow-Headers", value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" },
]
}
]
}
}
Below is my code for call
useEffect(() => {
const fetchCode = async () => {
const response = await fetch(`https://api.github.com/repos/Kanav-Arora/DSA-Guide/contents/Binary Search/binary_search.cpp`, {
headers: {
Authorization: `Bearer ${github_api}`,
},
method: 'get',
mode: 'cors',
});
console.log(response);
}
fetchCode();
}, [cpp])
Below error is returned
Response {type: ‘cors’, url: ‘https://api.github.com/repos/Kanav-Arora/DSA-Guide/contents/Binary%20Search/binary_search.cpp’, redirected: false, status: 401, ok: false, …}
I have tried adding cors configuration and I expect that Github api should work. Its working perfectly in postman but not in development environment.