diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/env.ts | 23 | ||||
| -rw-r--r-- | src/lib/drupalClient.ts | 36 | ||||
| -rw-r--r-- | src/vite-env.d.ts | 11 |
3 files changed, 70 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}` +} diff --git a/src/lib/drupalClient.ts b/src/lib/drupalClient.ts new file mode 100644 index 0000000..7a30729 --- /dev/null +++ b/src/lib/drupalClient.ts @@ -0,0 +1,36 @@ +import axios from 'axios' +import type { AxiosRequestConfig } from 'axios' +import { DrupalJsonApiParams } from 'drupal-jsonapi-params' +import { drupalEnv, getDrupalApiBaseUrl } from '../config/env' + +export const drupalClient = axios.create({ + baseURL: getDrupalApiBaseUrl(), + headers: { + Accept: 'application/vnd.api+json', + 'Content-Type': 'application/vnd.api+json', + }, +}) + +drupalClient.interceptors.request.use((config) => { + if (drupalEnv.authToken) { + config.headers.Authorization = `Bearer ${drupalEnv.authToken}` + } + + return config +}) + +export const createDrupalParams = (): DrupalJsonApiParams => new DrupalJsonApiParams() + +export const fetchDrupalResource = async <T>( + resourcePath: string, + options?: { + params?: DrupalJsonApiParams + config?: AxiosRequestConfig + }, +): Promise<T> => { + const queryString = options?.params?.getQueryString() + const url = queryString ? `${resourcePath}?${queryString}` : resourcePath + const { data } = await drupalClient.get<T>(url, options?.config) + + return data +} diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts new file mode 100644 index 0000000..55d593d --- /dev/null +++ b/src/vite-env.d.ts @@ -0,0 +1,11 @@ +/// <reference types="vite/client" /> + +interface ImportMetaEnv { + readonly VITE_DRUPAL_BASE_URL?: string + readonly VITE_DRUPAL_API_PREFIX?: string + readonly VITE_DRUPAL_AUTH_TOKEN?: string +} + +interface ImportMeta { + readonly env: ImportMetaEnv +} |
