Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cors and requestData errors #59

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/** @type {import('next').NextConfig} */
const nextConfig = {
async headers() {
return [
{
// matching all API routes
source: "/api/v1/:path*",
headers: [
{ key: "Access-Control-Allow-Credentials", value: "true" },
{ key: "Access-Control-Allow-Origin", value: "*" },
{ key: "Access-Control-Allow-Methods", value: "GET,OPTIONS,PATCH,DELETE,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" },
]
}
]
},
async rewrites() {
return [
{
source: '/api/v1/:path*', // Match all routes starting with /api/v1/
destination: 'http://localhost:8080/api/v1/:path*', // Proxy to backend server
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the http://localhost:8080 prefix should be hardcoded. Is it possible to just use the remaining suffix?
Granted, I don't know next.js, but this seems off.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the nextJS rewrites to an external server work. If I try to replace the hardcoded url with an env variable then I get a 500 internal server error.

Here is the rewrite docs: https://nextjs.org/docs/pages/api-reference/config/next-config-js/rewrites#rewriting-to-an-external-url

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me now.
Tested it with the container PR, and the frontend works and pulls the javascript sources.

},
];
},
};

module.exports = nextConfig;

13 changes: 0 additions & 13 deletions frontend/next.config.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 9000",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,21 @@ export default function Home() {

const onMint = async () => {
const requestData = {
asset_name: 'WST',
asset_name: Buffer.from('WST', 'utf8').toString('hex'), // Convert "WST" to hex
issuer: mintAccount,
amount: mintTokens,
quantity: mintTokens,
};

try {
const response = await axios.post(
'http://localhost:9000/api/tx/programmable-token/issue',
'/api/v1/tx/programmable-token/issue',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks more like a route I would expect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is the direct api call not the rewrite.

requestData,
{
headers: {
'Content-Type': 'application/json;charset=utf-8',
},
}
);

console.log('Mint response:', response.data);
} catch (error) {
console.error('Minting failed:', error);
Expand Down
Loading