Refactor API response structure to use 'body' instead of 'data' for consistency across all endpoints. Update stub responses and documentation accordingly.

This commit is contained in:
Primakov Alexandr Alexandrovich 2025-11-03 18:13:02 +03:00
parent e777b57991
commit daa44521b9
3 changed files with 23 additions and 21 deletions

View File

@ -41,12 +41,12 @@ export const api = createApi({
// Tasks
getTasks: builder.query<ChallengeTask[], void>({
query: () => '/challenge/tasks',
transformResponse: (response: { data: ChallengeTask[] }) => response.data,
transformResponse: (response: { body: ChallengeTask[] }) => response.body,
providesTags: ['Task'],
}),
getTask: builder.query<ChallengeTask, string>({
query: (id) => `/challenge/task/${id}`,
transformResponse: (response: { data: ChallengeTask }) => response.data,
transformResponse: (response: { body: ChallengeTask }) => response.body,
providesTags: (_result, _error, id) => [{ type: 'Task', id }],
}),
createTask: builder.mutation<ChallengeTask, CreateTaskRequest>({
@ -55,7 +55,7 @@ export const api = createApi({
method: 'POST',
body,
}),
transformResponse: (response: { data: ChallengeTask }) => response.data,
transformResponse: (response: { body: ChallengeTask }) => response.body,
invalidatesTags: ['Task'],
}),
updateTask: builder.mutation<ChallengeTask, { id: string; data: UpdateTaskRequest }>({
@ -64,7 +64,7 @@ export const api = createApi({
method: 'PUT',
body: data,
}),
transformResponse: (response: { data: ChallengeTask }) => response.data,
transformResponse: (response: { body: ChallengeTask }) => response.body,
invalidatesTags: (_result, _error, { id }) => [{ type: 'Task', id }, 'Task'],
}),
deleteTask: builder.mutation<void, string>({
@ -78,12 +78,12 @@ export const api = createApi({
// Chains
getChains: builder.query<ChallengeChain[], void>({
query: () => '/challenge/chains',
transformResponse: (response: { data: ChallengeChain[] }) => response.data,
transformResponse: (response: { body: ChallengeChain[] }) => response.body,
providesTags: ['Chain'],
}),
getChain: builder.query<ChallengeChain, string>({
query: (id) => `/challenge/chain/${id}`,
transformResponse: (response: { data: ChallengeChain }) => response.data,
transformResponse: (response: { body: ChallengeChain }) => response.body,
providesTags: (_result, _error, id) => [{ type: 'Chain', id }],
}),
createChain: builder.mutation<ChallengeChain, CreateChainRequest>({
@ -92,7 +92,7 @@ export const api = createApi({
method: 'POST',
body,
}),
transformResponse: (response: { data: ChallengeChain }) => response.data,
transformResponse: (response: { body: ChallengeChain }) => response.body,
invalidatesTags: ['Chain'],
}),
updateChain: builder.mutation<ChallengeChain, { id: string; data: UpdateChainRequest }>({
@ -101,7 +101,7 @@ export const api = createApi({
method: 'PUT',
body: data,
}),
transformResponse: (response: { data: ChallengeChain }) => response.data,
transformResponse: (response: { body: ChallengeChain }) => response.body,
invalidatesTags: (_result, _error, { id }) => [{ type: 'Chain', id }, 'Chain'],
}),
deleteChain: builder.mutation<void, string>({
@ -115,19 +115,19 @@ export const api = createApi({
// Users
getUsers: builder.query<ChallengeUser[], void>({
query: () => '/challenge/users',
transformResponse: (response: { data: ChallengeUser[] }) => response.data,
transformResponse: (response: { body: ChallengeUser[] }) => response.body,
providesTags: ['User'],
}),
// Statistics
getSystemStats: builder.query<SystemStats, void>({
query: () => '/challenge/stats',
transformResponse: (response: { data: SystemStats }) => response.data,
transformResponse: (response: { body: SystemStats }) => response.body,
providesTags: ['Stats'],
}),
getUserStats: builder.query<UserStats, string>({
query: (userId) => `/challenge/user/${userId}/stats`,
transformResponse: (response: { data: UserStats }) => response.data,
transformResponse: (response: { body: UserStats }) => response.body,
providesTags: (_result, _error, userId) => [{ type: 'User', id: userId }],
}),
@ -137,12 +137,12 @@ export const api = createApi({
const params = taskId ? `?taskId=${taskId}` : ''
return `/challenge/user/${userId}/submissions${params}`
},
transformResponse: (response: { data: ChallengeSubmission[] }) => response.data,
transformResponse: (response: { body: ChallengeSubmission[] }) => response.body,
providesTags: ['Submission'],
}),
getAllSubmissions: builder.query<ChallengeSubmission[], void>({
query: () => '/challenge/submissions',
transformResponse: (response: { data: ChallengeSubmission[] }) => response.data,
transformResponse: (response: { body: ChallengeSubmission[] }) => response.body,
providesTags: ['Submission'],
}),
}),

View File

@ -50,18 +50,19 @@ stubs/api/
### Успешный ответ
```json
{
"error": null,
"data": <данные>
"success": true,
"body": <данные>
}
```
### Ошибка
```json
{
"success": false,
"body": null,
"error": {
"message": "Описание ошибки"
},
"data": null
}
}
```

View File

@ -11,14 +11,15 @@ const loadJSON = (filename) => {
return JSON.parse(data);
};
const respond = (res, data) => {
res.json({ error: null, data });
const respond = (res, body) => {
res.json({ success: true, body });
};
const respondError = (res, message, statusCode = 400) => {
res.status(statusCode).json({
error: { message },
data: null
success: false,
body: null,
error: { message }
});
};