nxtgauge-frontend-solid/src/routes/api/runtime/onboarding/state.ts

43 lines
1.5 KiB
TypeScript

import { gatewayUrl, withAuthHeaders } from '~/lib/server/gateway';
export async function GET({ request }: { request: Request }) {
try {
const url = new URL(request.url);
const roleKey = String(url.searchParams.get('roleKey') || '').trim();
const upstream = await fetch(
`${gatewayUrl('/onboarding/state')}${roleKey ? `?roleKey=${encodeURIComponent(roleKey)}` : ''}`,
{
method: 'GET',
headers: withAuthHeaders(request, { Accept: 'application/json', 'x-portal-target': 'public' }),
cache: 'no-store',
},
);
const payload = await upstream.json().catch(() => ({}));
if (!upstream.ok) {
return new Response(
JSON.stringify({ success: false, error: payload?.message || payload?.error || 'Failed to load onboarding state' }),
{ status: upstream.status, headers: { 'Content-Type': 'application/json' } },
);
}
// Normalise the response so the frontend always sees { status, currentStep }
return new Response(
JSON.stringify({
success: true,
data: {
status: payload?.status ?? 'NOT_STARTED',
currentStep: payload?.currentStep ?? 0,
progress: payload?.progress ?? null,
},
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
} catch (error: any) {
return new Response(JSON.stringify({ success: false, error: error?.message || 'Internal Server Error' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}