github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/utils/serverId.ts (about)

     1  import {useCache} from "@/hooks/web/useCache";
     2  import {ApiImage} from "@/api/stub";
     3  
     4  const {wsCache} = useCache()
     5  
     6  export const prepareUrl = function (url: string | undefined): string {
     7    if (!url) {
     8      return ''
     9    }
    10    if (url?.includes('serverId')) {
    11      return url
    12    }
    13    const serverId = wsCache.get('serverId')
    14    if (!serverId) {
    15      return url;
    16    }
    17    if (url?.includes('?')) {
    18      return url + '&serverId=' + serverId;
    19    } else {
    20      return url + '?serverId=' + serverId;
    21    }
    22  
    23  }
    24  
    25  export const GetFullUrl = (uri: string | undefined): string => {
    26    if (!uri) {
    27      return '';
    28    }
    29    return prepareUrl(import.meta.env.VITE_API_BASEPATH as string + uri);
    30  }
    31  
    32  export const GetFullImageUrl = (image?: ApiImage | undefined): string => {
    33      if (!image) {
    34          return '';
    35      }
    36      if (image?.url?.includes(import.meta.env.VITE_API_BASEPATH)) {
    37          return prepareUrl(image?.url || '')
    38      }
    39      return prepareUrl(import.meta.env.VITE_API_BASEPATH as string + image?.url)
    40  }