Add transformResponse handlers to API queries and mutations for consistent response structure
Some checks failed
platform/bro-js/challenge-pl/pipeline/head There was a failure building this commit

This commit is contained in:
Primakov Alexandr Alexandrovich 2025-11-04 11:15:55 +03:00
parent b2eaaebd7f
commit 67f29c103a

View File

@ -17,6 +17,11 @@ const normalizeBaseUrl = (url: string) => (url.endsWith('/') ? url.slice(0, -1)
const backendBaseUrl = normalizeBaseUrl(getConfigValue('challenge.api'))
const challengeBaseUrl = `${backendBaseUrl}/challenge`
interface ApiResponse<T> {
success: boolean
body: T
}
export const api = createApi({
reducerPath: 'challengeApi',
baseQuery: fetchBaseQuery({
@ -34,12 +39,14 @@ export const api = createApi({
method: 'POST',
body,
}),
transformResponse: (response: ApiResponse<ChallengeAuthResponse>) => response.body,
}),
getChains: builder.query<ChallengeChain[], void>({
query: () => ({
url: '/chains',
method: 'GET',
}),
transformResponse: (response: ApiResponse<ChallengeChain[]>) => response.body,
providesTags: ['Chains'],
}),
getChain: builder.query<ChallengeChain, string>({
@ -47,6 +54,7 @@ export const api = createApi({
url: `/chain/${chainId}`,
method: 'GET',
}),
transformResponse: (response: ApiResponse<ChallengeChain>) => response.body,
providesTags: (_result, _error, arg) => [{ type: 'Chain', id: arg }],
}),
submitSolution: builder.mutation<ChallengeSubmitResponse, ChallengeSubmitPayload>({
@ -55,6 +63,7 @@ export const api = createApi({
method: 'POST',
body,
}),
transformResponse: (response: ApiResponse<ChallengeSubmitResponse>) => response.body,
invalidatesTags: ['Queue', 'Submissions', 'UserStats'],
}),
checkQueueStatus: builder.query<QueueStatus, string>({
@ -62,6 +71,7 @@ export const api = createApi({
url: `/check-status/${queueId}`,
method: 'GET',
}),
transformResponse: (response: ApiResponse<QueueStatus>) => response.body,
providesTags: (_result, _error, arg) => [{ type: 'Queue', id: arg }],
}),
getUserStats: builder.query<UserStats, string>({
@ -69,6 +79,7 @@ export const api = createApi({
url: `/user/${userId}/stats`,
method: 'GET',
}),
transformResponse: (response: ApiResponse<UserStats>) => response.body,
providesTags: (_result, _error, arg) => [{ type: 'UserStats', id: arg }],
}),
getUserSubmissions: builder.query<ChallengeSubmission[], { userId: string; taskId?: string }>({
@ -76,6 +87,7 @@ export const api = createApi({
url: `/user/${userId}/submissions${taskId ? `?taskId=${taskId}` : ''}`,
method: 'GET',
}),
transformResponse: (response: ApiResponse<ChallengeSubmission[]>) => response.body,
providesTags: (_result, _error, arg) => [{ type: 'Submissions', id: arg.userId }],
}),
getSystemStats: builder.query<SystemStats, void>({
@ -83,6 +95,7 @@ export const api = createApi({
url: '/stats',
method: 'GET',
}),
transformResponse: (response: ApiResponse<SystemStats>) => response.body,
providesTags: ['SystemStats'],
}),
getTask: builder.query<ChallengeTask, string>({
@ -90,6 +103,7 @@ export const api = createApi({
url: `/task/${taskId}`,
method: 'GET',
}),
transformResponse: (response: ApiResponse<ChallengeTask>) => response.body,
providesTags: (_result, _error, arg) => [{ type: 'Submissions', id: `task-${arg}` }],
}),
getAllSubmissions: builder.query<ChallengeSubmission[], void>({
@ -97,6 +111,7 @@ export const api = createApi({
url: '/submissions',
method: 'GET',
}),
transformResponse: (response: ApiResponse<ChallengeSubmission[]>) => response.body,
providesTags: ['Submissions'],
}),
}),