mxHeadless
REST API gateway для headless-фронтендов на MODX 3. Ресурсы, объекты, OpenAPI, API keys и OAuth

fetch для браузера или Node 18+. Подставьте свой URL.
const base = 'https://example.com/api/v1'async function api(path, options = {}) {
const res = await fetch(`${base}${path}`, {
...options,
headers: {
Accept: 'application/json',
...(options.headers || {}),
},
})
const body = await res.json().catch(() => null)
if (!res.ok) {
const err = new Error(body?.detail || res.statusText)
err.status = res.status
err.code = body?.code
err.body = body
throw err
}
return body
}Успешный ответ: { data, meta, links }. Ошибки приходят как problem+json с полями status, detail, часто code.
await api('')
await api('/health')
const list = await api(
'/resources?' +
new URLSearchParams({
limit: '5',
'filter[published]': '1',
sort: '-id',
fields: 'id,pagetitle,uri',
}),
)const key = process.env.MXHEADLESS_API_KEY
const chunks = await api('/chunks', {
headers: { Authorization: `Bearer ${key}` },
})await api('/resources', {
method: 'POST',
headers: {
Authorization: `Bearer ${key}`,
'Content-Type': 'application/json',
'Idempotency-Key': 'create-about-001',
},
body: JSON.stringify({
pagetitle: 'About',
parent: 2,
template: 1,
published: 0,
}),
})Для другого origin включите CORS и укажите origin фронтенда.