Primakov Alexandr Alexandrovich 67f29c103a
Some checks failed
platform/bro-js/challenge-pl/pipeline/head There was a failure building this commit
Add transformResponse handlers to API queries and mutations for consistent response structure
2025-11-04 11:15:55 +03:00

139 lines
4.5 KiB
TypeScript

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { getConfigValue } from '@brojs/cli'
import type {
ChallengeAuthResponse,
ChallengeChain,
ChallengeSubmitPayload,
ChallengeSubmitResponse,
ChallengeSubmission,
ChallengeTask,
QueueStatus,
SystemStats,
UserStats,
} from '../types'
const normalizeBaseUrl = (url: string) => (url.endsWith('/') ? url.slice(0, -1) : url)
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({
baseUrl: challengeBaseUrl,
prepareHeaders: (headers) => {
headers.set('Content-Type', 'application/json;charset=utf-8')
return headers
},
}),
tagTypes: ['Chains', 'Chain', 'UserStats', 'SystemStats', 'Submissions', 'Queue'],
endpoints: (builder) => ({
authUser: builder.mutation<ChallengeAuthResponse, { nickname: string }>({
query: (body) => ({
url: '/auth',
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>({
query: (chainId) => ({
url: `/chain/${chainId}`,
method: 'GET',
}),
transformResponse: (response: ApiResponse<ChallengeChain>) => response.body,
providesTags: (_result, _error, arg) => [{ type: 'Chain', id: arg }],
}),
submitSolution: builder.mutation<ChallengeSubmitResponse, ChallengeSubmitPayload>({
query: (body) => ({
url: '/submit',
method: 'POST',
body,
}),
transformResponse: (response: ApiResponse<ChallengeSubmitResponse>) => response.body,
invalidatesTags: ['Queue', 'Submissions', 'UserStats'],
}),
checkQueueStatus: builder.query<QueueStatus, string>({
query: (queueId) => ({
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>({
query: (userId) => ({
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 }>({
query: ({ userId, taskId }) => ({
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>({
query: () => ({
url: '/stats',
method: 'GET',
}),
transformResponse: (response: ApiResponse<SystemStats>) => response.body,
providesTags: ['SystemStats'],
}),
getTask: builder.query<ChallengeTask, string>({
query: (taskId) => ({
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>({
query: () => ({
url: '/submissions',
method: 'GET',
}),
transformResponse: (response: ApiResponse<ChallengeSubmission[]>) => response.body,
providesTags: ['Submissions'],
}),
}),
})
export const {
useAuthUserMutation,
useGetChainsQuery,
useLazyGetChainsQuery,
useGetChainQuery,
useSubmitSolutionMutation,
useCheckQueueStatusQuery,
useLazyCheckQueueStatusQuery,
useGetUserStatsQuery,
useLazyGetUserStatsQuery,
useGetUserSubmissionsQuery,
useLazyGetUserSubmissionsQuery,
useGetSystemStatsQuery,
useLazyGetSystemStatsQuery,
useGetTaskQuery,
useLazyGetTaskQuery,
useGetAllSubmissionsQuery,
useLazyGetAllSubmissionsQuery,
} = api