diff options
| author | IwanIDev <iwan@iwani.dev> | 2026-03-19 20:16:23 +0000 |
|---|---|---|
| committer | IwanIDev <iwan@iwani.dev> | 2026-03-19 20:16:23 +0000 |
| commit | a706dcf6a9b91ef2c3d1e1d28449b9b8e0e8187d (patch) | |
| tree | ca3ea838179472713e1e2d089813f0f39ac72adb /src/lib/drupalClient.ts | |
| parent | 572a393440b39a838b99227ba2222a210a495fac (diff) | |
Add support for headless Drupal integration with environment variables and Docker setup
- Create .env.example for environment variable configuration
- Update Dockerfile to accept Drupal-related build arguments
- Enhance docker_build.yml to pass environment variables during Docker build
- Add drupalClient and env configuration for API interaction
- Implement local development instructions and Docker deployment steps in README
- Add drupal and mariadb services to docker-stack.yml for complete setup
- Update package.json and bun.lock to include axios and drupal-jsonapi-params dependencies
- Add TypeScript definitions for new environment variables
Diffstat (limited to 'src/lib/drupalClient.ts')
| -rw-r--r-- | src/lib/drupalClient.ts | 36 |
1 files changed, 36 insertions, 0 deletions
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 +} |
