summaryrefslogtreecommitdiff
path: root/src/config/env.ts
blob: c530b3a5e040b0f6897730e22487ab22de40fe33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const trimTrailingSlashes = (value: string): string => value.replace(/\/+$/, '')

const normalizePathPrefix = (value: string): string => {
  if (!value) {
    return '/jsonapi'
  }

  return value.startsWith('/') ? value : `/${value}`
}

export const drupalEnv = {
  baseUrl: trimTrailingSlashes(import.meta.env.VITE_DRUPAL_BASE_URL ?? ''),
  apiPrefix: normalizePathPrefix(import.meta.env.VITE_DRUPAL_API_PREFIX ?? '/jsonapi'),
  authToken: import.meta.env.VITE_DRUPAL_AUTH_TOKEN ?? '',
}

export const getDrupalApiBaseUrl = (): string => {
  if (!drupalEnv.baseUrl) {
    throw new Error('Missing VITE_DRUPAL_BASE_URL. Set it in your environment before using the Drupal client.')
  }

  return `${drupalEnv.baseUrl}${drupalEnv.apiPrefix}`
}