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