-
Notifications
You must be signed in to change notification settings - Fork 0
PEN-123 expose onboarding API bridge #91
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"]) { | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the upstream Go API is down or unreachable, 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"
}
}
);
}
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, thecookieheader is not forwarded to the upstream Go API, which will cause authenticated requests to fail with a401 Unauthorizederror. Adding'cookie'to the list of forwarded headers resolves this issue.