aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/helpers/utils/url.ts
blob: 08c27e3c1e907c9ebb6e746f13ecb6bc2fbc8f88 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

                                                               












                                               



                                          




































                                                                                           

                    
             

                     
import { environment } from '../../../environments/environment'

function getAbsoluteAPIUrl () {
  let absoluteAPIUrl = environment.hmr === true
    ? 'http://localhost:9000'
    : environment.apiUrl

  if (!absoluteAPIUrl) {
    // The API is on the same domain
    absoluteAPIUrl = window.location.origin
  }

  return absoluteAPIUrl
}

function getAPIHost () {
  return new URL(getAbsoluteAPIUrl()).host
}

function getAbsoluteEmbedUrl () {
  let absoluteEmbedUrl = environment.originServerUrl
  if (!absoluteEmbedUrl) {
    // The Embed is on the same domain
    absoluteEmbedUrl = window.location.origin
  }

  return absoluteEmbedUrl
}

// Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
function objectToFormData (obj: any, form?: FormData, namespace?: string) {
  const fd = form || new FormData()
  let formKey

  for (const key of Object.keys(obj)) {
    if (namespace) formKey = `${namespace}[${key}]`
    else formKey = key

    if (obj[key] === undefined) continue

    if (Array.isArray(obj[key]) && obj[key].length === 0) {
      fd.append(key, null)
      continue
    }

    if (obj[key] !== null && typeof obj[key] === 'object' && !(obj[key] instanceof File)) {
      objectToFormData(obj[key], fd, formKey)
    } else {
      fd.append(formKey, obj[key])
    }
  }

  return fd
}

export {
  objectToFormData,
  getAbsoluteAPIUrl,
  getAPIHost,
  getAbsoluteEmbedUrl
}