use-fetch.ts 717 B

123456789101112131415161718192021
  1. import { onlyId } from "@/utils/shared";
  2. import { useInstanceProps } from "./use-global-vars";
  3. export const useResourceHandler = () => {
  4. const handler = useInstanceProps().get().handlerResource;
  5. const mateMap: Record<string, string> = {
  6. svg: "image/svg+xml",
  7. };
  8. return async (data: string | Blob | File, type?: string) => {
  9. type = type && mateMap[type!] ? mateMap[type!] : type
  10. if (typeof data === "string") {
  11. data = new Blob([data], { type });
  12. }
  13. if (!(data instanceof File)) {
  14. const entrie = Object.entries(mateMap).find(([_k, v]) => v === type)
  15. data = new File([data], onlyId() + (entrie ? `.${entrie![0]}` : ''))
  16. }
  17. return await handler(data as File)
  18. };
  19. };