85 lines
3.8 KiB
Markdown
85 lines
3.8 KiB
Markdown
# payload-helpers
|
|
|
|
Typed access to the plugin's globals (SiteSettings, SiteIntegrations) through the
|
|
Local API. Following the rule: helpers take `payload` as an argument —
|
|
the plugin never calls `getPayload` itself.
|
|
|
|
## Config
|
|
|
|
The `site-settings` (`SITE_SETTINGS_SLUG`), `site-integrations` (`SITE_INTEGRATIONS_SLUG`),
|
|
`cookie-settings` and `notifications` globals are **currently always added** by the plugin (when
|
|
`enabled !== false`) — there is no option to skip them or change their slug.
|
|
|
|
| `ipalKit()` option | What it does |
|
|
|---|---|
|
|
| `siteSettingsFields?: Field[]` | extra SiteSettings fields in a separate **"Custom"** tab (flat data: `settings.myField`) |
|
|
| `integrationsFields?: Field[]` | extra SiteIntegrations fields in the "Custom" tab |
|
|
|
|
SiteSettings tabs (unnamed — flat data): **General** (`siteName`, `siteDescription`,
|
|
`titleOrder`, `titleSeparator`, `logo`, `defaultShareImage`, `sameAs`, `favicon`, …),
|
|
**Theme** (`defaultTheme`, `allowThemeToggle`, `themeToggleLabel`), **System pages**
|
|
(from `pages` / `content`), **E-mails** (from `forms.emailLayout`), **Maintenance**
|
|
(from `pages`), **Custom** (from `siteSettingsFields`).
|
|
|
|
**Access:** SiteSettings has `read: () => true` — **the whole global, including fields from
|
|
`siteSettingsFields`, is public** over REST/GraphQL (`/api/globals/site-settings`).
|
|
Keep nothing confidential there (use SiteIntegrations for that, `read`/`update` admin only).
|
|
SiteSettings does not set `update` — Payload's default applies (any logged-in
|
|
user of an auth collection).
|
|
|
|
## Front end — reading globals
|
|
|
|
```ts
|
|
import { getSiteSettings, getSiteIntegrations } from '@intecion/ipal-kit'
|
|
import type { SiteSetting, SiteIntegration } from '@/payload-types'
|
|
|
|
const payload = await getPayload({ config })
|
|
|
|
// SiteSettings (public — siteName, logo, favicon, theme, system pages)
|
|
const settings = await getSiteSettings<SiteSetting>(payload, { locale: 'pl' })
|
|
|
|
// SiteIntegrations (admin-only; the Local API bypasses access control)
|
|
const integrations = await getSiteIntegrations<SiteIntegration>(payload)
|
|
```
|
|
|
|
The generic `<T>` lets you inject the client's generated type. Without it, it returns
|
|
`Record<string, unknown>`.
|
|
|
|
Options (`GlobalQueryOptions`): `locale` (a code or `'all'`; none → Payload's default),
|
|
`depth` (none → Payload's default). The helpers don't catch errors and don't cache — in
|
|
Next use `getSettings(locale)` from `@/lib/content` (React `cache()`, `depth: 1` — `settingsDepth` changes it).
|
|
|
|
## ⚠️ SiteIntegrations holds secrets
|
|
|
|
The Local API bypasses access control by default (`overrideAccess: true`), so
|
|
`getSiteIntegrations` **returns secrets** (SMTP password, Turnstile secret,
|
|
R2 keys) despite the admin-only barrier on the global. This is intended — server logic
|
|
needs them.
|
|
|
|
**Never pass the raw result to the browser.** Read specific
|
|
values server-side, send only safe ones to the client (e.g. `turnstileSiteKey`,
|
|
not `turnstileSecretKey`):
|
|
|
|
```ts
|
|
// WRONG — leaks secrets to the client
|
|
return <Form data={await getSiteIntegrations(payload)} />
|
|
|
|
// RIGHT — only the public value
|
|
const { turnstileSiteKey } = await getSiteIntegrations(payload)
|
|
return <Form siteKey={turnstileSiteKey} />
|
|
```
|
|
|
|
## Lower level: getGlobal
|
|
|
|
```ts
|
|
import { getGlobal } from '@intecion/ipal-kit'
|
|
const data = await getGlobal<MyType>(payload, 'my-global', { locale: 'pl', depth: 1 })
|
|
```
|
|
|
|
## Turn off, override, extend
|
|
|
|
- The plugin's globals are currently always added (no option to turn them off / change slugs); `enabled: false` removes the whole plugin, including them.
|
|
- Extension: `siteSettingsFields` / `integrationsFields` (the "Custom" tab); the shape of the existing tabs is fixed.
|
|
- You can change a global's access, hooks and fields only by overriding it in the config after `ipalKit` (your own plugin mapping `config.globals`, example in hooks.md).
|
|
- `getGlobal(payload, slug, { locale, depth })` works with any project global.
|