Skip to content
Snippets Groups Projects
api.ts 901 B
Newer Older
  • Learn to ignore specific revisions
  • function createURLBuilder(basepath: string, useTrailingSlash = true) {
      // Strip all trailing slashes from the basepath.
      // We handle slashes when building URLs.
      basepath = basepath.replace(/\/*$/, '')
    
      return function buildURL(...subPaths: string[] | [...string[], string | URLSearchParams]) {
        let params
        if (subPaths.at(-1) instanceof URLSearchParams) {
          params = subPaths.pop()
        }
        if (subPaths.some((path) => String(path).includes('/'))) {
          throw new Error('Subpaths must not contain slashes')
        }
        const subPath = subPaths.length > 0 ? '/' + subPaths.join('/') : ''
        const url = basepath + subPath + (useTrailingSlash ? '/' : '')
        return params ? url + `?${params}` : url
      }
    }
    
    export const createTankURL = createURLBuilder(import.meta.env.VUE_APP_API_TANK, false)
    export const createSteeringURL = createURLBuilder(import.meta.env.VUE_APP_API_STEERING)