blob: 7a307290392c68f7851e30bc692118357a26c024 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
}
|