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 { 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({ query: (body) => ({ url: '/auth', method: 'POST', body, }), transformResponse: (response: ApiResponse) => response.body, }), getChains: builder.query({ query: () => ({ url: '/chains', method: 'GET', }), transformResponse: (response: ApiResponse) => response.body, providesTags: ['Chains'], }), getChain: builder.query({ query: (chainId) => ({ url: `/chain/${chainId}`, method: 'GET', }), transformResponse: (response: ApiResponse) => response.body, providesTags: (_result, _error, arg) => [{ type: 'Chain', id: arg }], }), submitSolution: builder.mutation({ query: (body) => ({ url: '/submit', method: 'POST', body, }), transformResponse: (response: ApiResponse) => response.body, invalidatesTags: ['Queue', 'Submissions', 'UserStats'], }), checkQueueStatus: builder.query({ query: (queueId) => ({ url: `/check-status/${queueId}`, method: 'GET', }), transformResponse: (response: ApiResponse) => response.body, providesTags: (_result, _error, arg) => [{ type: 'Queue', id: arg }], keepUnusedDataFor: 0, }), getUserStats: builder.query({ query: (userId) => ({ url: `/user/${userId}/stats`, method: 'GET', }), transformResponse: (response: ApiResponse) => response.body, providesTags: (_result, _error, arg) => [{ type: 'UserStats', id: arg }], }), getUserSubmissions: builder.query({ query: ({ userId, taskId }) => ({ url: `/user/${userId}/submissions${taskId ? `?taskId=${taskId}` : ''}`, method: 'GET', }), transformResponse: (response: ApiResponse) => response.body, providesTags: (_result, _error, arg) => [{ type: 'Submissions', id: arg.userId }], }), getSystemStats: builder.query({ query: () => ({ url: '/stats', method: 'GET', }), transformResponse: (response: ApiResponse) => response.body, providesTags: ['SystemStats'], }), getTask: builder.query({ query: (taskId) => ({ url: `/task/${taskId}`, method: 'GET', }), transformResponse: (response: ApiResponse) => response.body, providesTags: (_result, _error, arg) => [{ type: 'Submissions', id: `task-${arg}` }], }), getAllSubmissions: builder.query({ query: () => ({ url: '/submissions', method: 'GET', }), transformResponse: (response: ApiResponse) => response.body, providesTags: ['Submissions'], }), }), }) export const { useAuthUserMutation, useGetChainsQuery, useLazyGetChainsQuery, useGetChainQuery, useSubmitSolutionMutation, useCheckQueueStatusQuery, useLazyCheckQueueStatusQuery, useGetUserStatsQuery, useLazyGetUserStatsQuery, useGetUserSubmissionsQuery, useLazyGetUserSubmissionsQuery, useGetSystemStatsQuery, useLazyGetSystemStatsQuery, useGetTaskQuery, useLazyGetTaskQuery, useGetAllSubmissionsQuery, useLazyGetAllSubmissionsQuery, } = api