mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-08 22:58:17 +00:00
improve endpoints
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { api } from "./crud";
|
||||
import { endpoint, unwrap } from "@/utils/endpoint";
|
||||
import { client } from "@/utils/api";
|
||||
|
||||
import type {
|
||||
CreateFileUploadFieldDto,
|
||||
@@ -8,6 +9,9 @@ import type {
|
||||
UpdateFileUploadFieldDto,
|
||||
UpdateFileUploadSettingDto,
|
||||
} from "@/types/fileUploadSettings";
|
||||
import { URL_CONSTANTS } from "@/constants/URLS";
|
||||
import { QUERY_KEYS } from "@/constants/TANSTACK_QUEY_KEY";
|
||||
import { ApiResponse } from "@/types/apiResponse";
|
||||
|
||||
const BASE = "/api/file-upload-settings";
|
||||
|
||||
@@ -18,25 +22,24 @@ const BASE = "/api/file-upload-settings";
|
||||
*/
|
||||
type Envelope<T> = { data: T } | T;
|
||||
|
||||
function unwrap<T>(payload: Envelope<T>): T {
|
||||
if (
|
||||
payload &&
|
||||
typeof payload === "object" &&
|
||||
"data" in (payload as object)
|
||||
) {
|
||||
return (payload as { data: T }).data;
|
||||
}
|
||||
// function unwrap<T>(payload: Envelope<T>): T {
|
||||
// if (
|
||||
// payload &&
|
||||
// typeof payload === "object" &&
|
||||
// "data" in (payload as object)
|
||||
// ) {
|
||||
// return (payload as { data: T }).data;
|
||||
// }
|
||||
|
||||
return payload as T;
|
||||
}
|
||||
// return payload as T;
|
||||
// }
|
||||
|
||||
export const fileUploadSettingsService = {
|
||||
// GET /file-upload-settings
|
||||
list: async (): Promise<FileUploadSetting[]> => {
|
||||
const response =
|
||||
await api.get<Envelope<FileUploadSetting[]>>(BASE);
|
||||
|
||||
return unwrap(response);
|
||||
await client.get<ApiResponse<FileUploadSetting[]>>(BASE);
|
||||
return unwrap(response.data);
|
||||
},
|
||||
|
||||
// GET /file-upload-settings/:id
|
||||
@@ -44,11 +47,11 @@ export const fileUploadSettingsService = {
|
||||
id: string
|
||||
): Promise<FileUploadSetting> => {
|
||||
const response =
|
||||
await api.get<Envelope<FileUploadSetting>>(
|
||||
await client.get<ApiResponse<FileUploadSetting>>(
|
||||
`${BASE}/${id}`
|
||||
);
|
||||
|
||||
return unwrap(response);
|
||||
return unwrap(response.data);
|
||||
},
|
||||
|
||||
// GET /file-upload-settings/by-code/:code
|
||||
@@ -56,11 +59,11 @@ export const fileUploadSettingsService = {
|
||||
code: string
|
||||
): Promise<FileUploadSetting> => {
|
||||
const response =
|
||||
await api.get<Envelope<FileUploadSetting>>(
|
||||
await client.get<ApiResponse<FileUploadSetting>>(
|
||||
`${BASE}/by-code/${encodeURIComponent(code)}`
|
||||
);
|
||||
|
||||
return unwrap(response);
|
||||
return unwrap(response.data);
|
||||
},
|
||||
|
||||
// POST /file-upload-settings
|
||||
@@ -68,12 +71,12 @@ export const fileUploadSettingsService = {
|
||||
payload: CreateFileUploadSettingDto
|
||||
): Promise<FileUploadSetting> => {
|
||||
const response =
|
||||
await api.post<Envelope<FileUploadSetting>>(
|
||||
await client.post<ApiResponse<FileUploadSetting>>(
|
||||
BASE,
|
||||
payload
|
||||
);
|
||||
|
||||
return unwrap(response);
|
||||
return unwrap(response.data);
|
||||
},
|
||||
|
||||
// PATCH /file-upload-settings/:id
|
||||
@@ -82,17 +85,17 @@ export const fileUploadSettingsService = {
|
||||
payload: UpdateFileUploadSettingDto
|
||||
): Promise<FileUploadSetting> => {
|
||||
const response =
|
||||
await api.patch<Envelope<FileUploadSetting>>(
|
||||
await client.patch<ApiResponse<FileUploadSetting>>(
|
||||
`${BASE}/${id}`,
|
||||
payload
|
||||
);
|
||||
|
||||
return unwrap(response);
|
||||
return unwrap(response.data);
|
||||
},
|
||||
|
||||
// DELETE /file-upload-settings/:id
|
||||
remove: async (id: string): Promise<void> => {
|
||||
await api.delete(`${BASE}/${id}`);
|
||||
await client.delete(`${BASE}/${id}`);
|
||||
},
|
||||
|
||||
// PUT /file-upload-settings/:id/fields
|
||||
@@ -101,12 +104,11 @@ export const fileUploadSettingsService = {
|
||||
fields: CreateFileUploadFieldDto[]
|
||||
): Promise<FileUploadField[]> => {
|
||||
const response =
|
||||
await api.put<Envelope<FileUploadField[]>>(
|
||||
await client.put<ApiResponse<FileUploadField[]>>(
|
||||
`${BASE}/${id}/fields`,
|
||||
fields
|
||||
);
|
||||
|
||||
return unwrap(response);
|
||||
return unwrap(response.data);
|
||||
},
|
||||
|
||||
// POST /file-upload-settings/:id/fields
|
||||
@@ -115,12 +117,12 @@ export const fileUploadSettingsService = {
|
||||
payload: CreateFileUploadFieldDto
|
||||
): Promise<FileUploadField> => {
|
||||
const response =
|
||||
await api.post<Envelope<FileUploadField>>(
|
||||
await client.post<ApiResponse<FileUploadField>>(
|
||||
`${BASE}/${id}/fields`,
|
||||
payload
|
||||
);
|
||||
|
||||
return unwrap(response);
|
||||
return unwrap(response.data);
|
||||
},
|
||||
|
||||
// PATCH /file-upload-settings/fields/:fieldId
|
||||
@@ -129,20 +131,29 @@ export const fileUploadSettingsService = {
|
||||
payload: UpdateFileUploadFieldDto
|
||||
): Promise<FileUploadField> => {
|
||||
const response =
|
||||
await api.patch<Envelope<FileUploadField>>(
|
||||
await client.patch<ApiResponse<FileUploadField>>(
|
||||
`${BASE}/fields/${fieldId}`,
|
||||
payload
|
||||
);
|
||||
|
||||
return unwrap(response);
|
||||
return unwrap(response.data);
|
||||
},
|
||||
|
||||
// DELETE /file-upload-settings/fields/:fieldId
|
||||
removeField: async (
|
||||
fieldId: string
|
||||
): Promise<void> => {
|
||||
await api.delete(
|
||||
await client.delete(
|
||||
`${BASE}/fields/${fieldId}`
|
||||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
export const getFileUploadSettingByCode = endpoint<string, FileUploadSetting>(
|
||||
QUERY_KEYS.FILES.FILE_UPLOAD_SETTINGS,
|
||||
QUERY_KEYS.FILES.BY_CODE,
|
||||
(code) =>
|
||||
client
|
||||
.get<ApiResponse<FileUploadSetting>>(`${URL_CONSTANTS.FILES.FILE_UPLOAD_SETTINGS_BY_CODE}/${code}`)
|
||||
.then(res => res.data.data)
|
||||
);
|
||||
Reference in New Issue
Block a user