refactor: add base in url

This commit is contained in:
Net 2024-09-18 09:10:43 +07:00
parent f2b318060e
commit 62269efbb6

View file

@ -237,8 +237,10 @@ export function resetScrollBar(elementId: string) {
export function manageAttachment(api: AxiosInstance, base: string) {
return {
listAttachment: async () => {
const res = await api.get<string[]>(`${base}/attachment`);
listAttachment: async (opts: { parentId: string }) => {
const res = await api.get<string[]>(
`${base}/${opts.parentId}/attachment`,
);
if (res.status >= 400) return false;
return res.data;
},
@ -247,7 +249,7 @@ export function manageAttachment(api: AxiosInstance, base: string) {
name: string;
download?: boolean;
}) => {
const url = `/${opts.parentId}/attachment/${opts.name}`;
const url = `${base}/${opts.parentId}/attachment/${opts.name}`;
const res = await api.get<string>(url);
if (opts.download) {
fetch(res.data)
@ -268,7 +270,7 @@ export function manageAttachment(api: AxiosInstance, base: string) {
file: File;
}) => {
const res = await api.put(
`/${opts.parentId}/attachment/${opts.name}`,
`${base}/${opts.parentId}/attachment/${opts.name}`,
opts.file,
{
headers: { 'Content-Type': opts.file.type },
@ -279,7 +281,9 @@ export function manageAttachment(api: AxiosInstance, base: string) {
return false;
},
delAttachment: async (opts: { parentId: string; name: string }) => {
const res = await api.delete(`/${opts.parentId}/attachment/${opts.name}`);
const res = await api.delete(
`${base}/${opts.parentId}/attachment/${opts.name}`,
);
if (res.status < 400) return true;
return false;
},
@ -288,8 +292,10 @@ export function manageAttachment(api: AxiosInstance, base: string) {
export function manageFile<T extends string>(api: AxiosInstance, base: string) {
return {
listFile: async (opts: { group: T }) => {
const res = await api.get<string[]>(`${base}/file-${opts.group}`);
listFile: async (opts: { group: T; parentId: string }) => {
const res = await api.get<string[]>(
`${base}/${opts.parentId}/file-${opts.group}`,
);
if (res.status >= 400) return false;
return res.data;
},
@ -299,7 +305,7 @@ export function manageFile<T extends string>(api: AxiosInstance, base: string) {
fileId: string;
download?: boolean;
}) => {
const url = `/${opts.parentId}/file-${opts.group}/${opts.fileId}`;
const url = `${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`;
const res = await api.get<string>(url);
if (opts.download) {
fetch(res.data)
@ -321,7 +327,7 @@ export function manageFile<T extends string>(api: AxiosInstance, base: string) {
file: File;
}) => {
const res = await api.put(
`/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
`${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
opts.file,
{
headers: { 'Content-Type': opts.file.type },
@ -333,7 +339,7 @@ export function manageFile<T extends string>(api: AxiosInstance, base: string) {
},
delFile: async (opts: { group: T; parentId: string; fileId: string }) => {
const res = await api.delete(
`/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
`${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
);
if (res.status < 400) return true;
return false;