refactor: add function manageMeta
This commit is contained in:
parent
3586a623de
commit
48bf222f4e
2 changed files with 120 additions and 11 deletions
|
|
@ -347,4 +347,72 @@ export function manageFile<T extends string>(api: AxiosInstance, base: string) {
|
|||
};
|
||||
}
|
||||
|
||||
export function manageMeta<T extends string>(api: AxiosInstance, base: string) {
|
||||
return {
|
||||
postMeta: async (opts: {
|
||||
group: T;
|
||||
parentId: string;
|
||||
meta: any;
|
||||
file: File;
|
||||
}) => {
|
||||
const url = `${base}/${opts.parentId}/${opts.group}`;
|
||||
const res = await api.post<typeof opts.meta>(url, opts.meta);
|
||||
|
||||
if (res.status < 400) {
|
||||
await manageFile(api, base).putFile({
|
||||
parentId: opts.parentId,
|
||||
group: opts.group,
|
||||
fileId: res.data.id,
|
||||
file: opts.file,
|
||||
});
|
||||
|
||||
return res.data;
|
||||
}
|
||||
},
|
||||
getMetaList: async (opts: { group: T; parentId: string }) => {
|
||||
const url = `${base}/${opts.parentId}/${opts.group}`;
|
||||
const res = await api.get(url);
|
||||
return res.data;
|
||||
},
|
||||
getMeta: async (opts: { group: T; parentId: string; metaId: string }) => {
|
||||
const url = `${base}/${opts.parentId}/${opts.group}/${opts.metaId}`;
|
||||
const res = await api.get<string>(url);
|
||||
return res.data;
|
||||
},
|
||||
putMeta: async (opts: {
|
||||
group: T;
|
||||
parentId: string;
|
||||
metaId: string;
|
||||
meta: any;
|
||||
file?: File;
|
||||
}) => {
|
||||
const res = await api.put<typeof opts.meta>(
|
||||
`${base}/${opts.parentId}/${opts.group}/${opts.metaId}`,
|
||||
opts.meta,
|
||||
);
|
||||
if (res.status < 400) {
|
||||
if (opts.file !== undefined)
|
||||
await manageFile(api, base).putFile({
|
||||
parentId: opts.parentId,
|
||||
group: opts.group,
|
||||
fileId: res.data.id,
|
||||
file: opts.file,
|
||||
});
|
||||
return res.data;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
delMeta: async (opts: { group: T; parentId: string; metaId: string }) => {
|
||||
const res = await api.delete(
|
||||
`${base}/${opts.parentId}/${opts.group}/${opts.metaId}`,
|
||||
);
|
||||
if (res.status < 400) return true;
|
||||
return false;
|
||||
},
|
||||
};
|
||||
}
|
||||
export async function waitAll<T extends Promise<any>[]>(arr: T) {
|
||||
return await Promise.all(arr);
|
||||
}
|
||||
|
||||
export default useUtilsStore;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue