Skip to content
Draft
Changes from all commits
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
51 changes: 51 additions & 0 deletions web/src/routes/api/v1/organization-onboarding-requests/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const DEFAULT_API_BASE_URL = "http://localhost:4621";
const TARGET_PATH = "/api/v1/organization-onboarding-requests";

function apiBaseURL() {
const configured = (import.meta.env.VITE_API_BASE_URL || "").trim();
return (configured || DEFAULT_API_BASE_URL).replace(/\/$/, "");
}

function forwardedHeaders(request) {
const headers = new Headers();

for (const name of ["accept", "authorization", "content-type"]) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Since the application uses cookie-based session management (e.g., SESSION_COOKIE), any client-side requests to this endpoint will rely on cookies for authentication. Currently, the cookie header is not forwarded to the upstream Go API, which will cause authenticated requests to fail with a 401 Unauthorized error. Adding 'cookie' to the list of forwarded headers resolves this issue.

Suggested change
for (const name of ["accept", "authorization", "content-type"]) {
for (const name of ["accept", "authorization", "content-type", "cookie"]) {

const value = request.headers.get(name);
if (value) {
headers.set(name, value);
}
}

if (!headers.has("accept")) {
headers.set("accept", "application/json");
}

return headers;
}

function responseHeaders(upstream) {
const headers = new Headers();

for (const name of ["content-type", "cache-control"]) {
const value = upstream.headers.get(name);
if (value) {
headers.set(name, value);
}
}

return headers;
}

export async function POST({ request, fetch }) {
const upstream = await fetch(`${apiBaseURL()}${TARGET_PATH}`, {
method: "POST",
headers: forwardedHeaders(request),
body: await request.text(),
});

return new Response(upstream.body, {
status: upstream.status,
statusText: upstream.statusText,
headers: responseHeaders(upstream),
});
}
Comment on lines +39 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If the upstream Go API is down or unreachable, fetch will throw an error, causing SvelteKit to return a generic 500 error page or response. Wrapping the fetch call in a try-catch block allows us to handle connection failures gracefully and return a consistent JSON error response (e.g., 502 Bad Gateway) matching the API's standard error format.

export async function POST({ request, fetch }) {
	try {
		const upstream = await fetch(apiBaseURL() + TARGET_PATH, {
			method: "POST",
			headers: forwardedHeaders(request),
			body: await request.text(),
		});

		return new Response(upstream.body, {
			status: upstream.status,
			statusText: upstream.statusText,
			headers: responseHeaders(upstream),
		});
	} catch (error) {
		return new Response(
			JSON.stringify({
				error: {
					code: "upstream_connection_failed",
					message: "Failed to communicate with the upstream API service.",
				},
			}),
			{
				status: 502,
				headers: {
					"content-type": "application/json"
				}
			}
		);
	}
}

Loading