diff options
Diffstat (limited to 'src/config/env.ts')
| -rw-r--r-- | src/config/env.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/config/env.ts b/src/config/env.ts new file mode 100644 index 0000000..c530b3a --- /dev/null +++ b/src/config/env.ts @@ -0,0 +1,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}` +} |
