fix(spa): normalize angular api client base URL for django endpoints
This commit is contained in:
@@ -52,6 +52,14 @@ function normalizeCode(code: string): string {
|
||||
return code.trim().toUpperCase();
|
||||
}
|
||||
|
||||
function normalizeBaseUrl(baseUrl: string): string {
|
||||
return baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
|
||||
}
|
||||
|
||||
function buildUrl(baseUrl: string, path: string): string {
|
||||
return `${normalizeBaseUrl(baseUrl)}${path}`;
|
||||
}
|
||||
|
||||
async function wrap<T>(call: () => Promise<T>): Promise<ApiResult<T>> {
|
||||
try {
|
||||
const data = await call();
|
||||
@@ -67,17 +75,17 @@ async function wrap<T>(call: () => Promise<T>): Promise<ApiResult<T>> {
|
||||
|
||||
export function createAngularApiClient(http: AngularHttpClientLike, baseUrl = ''): AngularApiClient {
|
||||
return {
|
||||
health: () => wrap(() => http.get<HealthResponse>(`${baseUrl}/healthz`, { withCredentials: true })),
|
||||
health: () => wrap(() => http.get<HealthResponse>(buildUrl(baseUrl, '/healthz'), { withCredentials: true })),
|
||||
getSession: (code: string) =>
|
||||
wrap(() =>
|
||||
http.get<SessionDetailResponse>(`${baseUrl}/lobby/sessions/${encodeURIComponent(normalizeCode(code))}`, {
|
||||
http.get<SessionDetailResponse>(buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}`), {
|
||||
withCredentials: true
|
||||
})
|
||||
),
|
||||
joinSession: (payload: JoinSessionRequest) =>
|
||||
wrap(() =>
|
||||
http.post<JoinSessionResponse>(
|
||||
`${baseUrl}/lobby/sessions/join`,
|
||||
buildUrl(baseUrl, '/lobby/sessions/join'),
|
||||
{
|
||||
code: normalizeCode(payload.code),
|
||||
nickname: payload.nickname.trim()
|
||||
@@ -88,7 +96,7 @@ export function createAngularApiClient(http: AngularHttpClientLike, baseUrl = ''
|
||||
startRound: (code: string, payload: StartRoundRequest) =>
|
||||
wrap(() =>
|
||||
http.post<StartRoundResponse>(
|
||||
`${baseUrl}/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/start`,
|
||||
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/start`),
|
||||
payload,
|
||||
{ withCredentials: true }
|
||||
)
|
||||
|
||||
@@ -109,6 +109,86 @@ describe('createAngularApiClient', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('normalizes baseUrl with trailing slash to keep Django endpoint paths canonical', async () => {
|
||||
const get = vi.fn<AngularHttpClientLike['get']>(async <T>(url: string) => {
|
||||
if (url === '/api/healthz') {
|
||||
return { ok: true, service: 'partyhub' } as T;
|
||||
}
|
||||
if (url === '/api/lobby/sessions/ABCD12') {
|
||||
return {
|
||||
session: { code: 'ABCD12', status: 'lobby', host_id: 1, current_round: 1, players_count: 2 },
|
||||
players: [],
|
||||
round_question: null,
|
||||
phase_view_model: {
|
||||
status: 'lobby',
|
||||
round_number: 1,
|
||||
players_count: 2,
|
||||
constraints: {
|
||||
min_players_to_start: 2,
|
||||
max_players_mvp: 8,
|
||||
min_players_reached: true,
|
||||
max_players_allowed: true
|
||||
},
|
||||
host: {
|
||||
can_start_round: true,
|
||||
can_show_question: false,
|
||||
can_mix_answers: false,
|
||||
can_calculate_scores: false,
|
||||
can_reveal_scoreboard: false,
|
||||
can_start_next_round: false,
|
||||
can_finish_game: false
|
||||
},
|
||||
player: {
|
||||
can_join: true,
|
||||
can_submit_lie: false,
|
||||
can_submit_guess: false,
|
||||
can_view_final_result: false
|
||||
}
|
||||
}
|
||||
} as T;
|
||||
}
|
||||
throw { status: 404, error: { error: 'Not found' } };
|
||||
});
|
||||
|
||||
const post = vi.fn<AngularHttpClientLike['post']>(async <T>(url: string) => {
|
||||
if (url === '/api/lobby/sessions/join') {
|
||||
return {
|
||||
player: { id: 9, nickname: 'Maja', session_token: 'token-1', score: 0 },
|
||||
session: { code: 'ABCD12', status: 'lobby' }
|
||||
} as T;
|
||||
}
|
||||
if (url === '/api/lobby/sessions/ABCD12/rounds/start') {
|
||||
return {
|
||||
session: { code: 'ABCD12', status: 'lie', current_round: 1 },
|
||||
round: { number: 1, category: { slug: 'history', name: 'History' } }
|
||||
} as T;
|
||||
}
|
||||
throw { status: 404, error: { error: 'Not found' } };
|
||||
});
|
||||
|
||||
const client = createAngularApiClient({ get, post } as AngularHttpClientLike, '/api/');
|
||||
|
||||
await client.health();
|
||||
await client.getSession('abcd12');
|
||||
await client.joinSession({ code: 'abcd12', nickname: 'Maja' });
|
||||
await client.startRound('abcd12', { category_slug: 'history' });
|
||||
|
||||
expect(get).toHaveBeenNthCalledWith(1, '/api/healthz', { withCredentials: true });
|
||||
expect(get).toHaveBeenNthCalledWith(2, '/api/lobby/sessions/ABCD12', { withCredentials: true });
|
||||
expect(post).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'/api/lobby/sessions/join',
|
||||
{ code: 'ABCD12', nickname: 'Maja' },
|
||||
{ withCredentials: true }
|
||||
);
|
||||
expect(post).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'/api/lobby/sessions/ABCD12/rounds/start',
|
||||
{ category_slug: 'history' },
|
||||
{ withCredentials: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('maps HttpErrorResponse-style failures to ApiResult errors', async () => {
|
||||
const http = {
|
||||
get: vi.fn<AngularHttpClientLike['get']>(async () => {
|
||||
|
||||
Reference in New Issue
Block a user