Skip to content

fix: respect custom routes.api in alt-text, pages, and vercel-deployments#99

Open
jhb-dev wants to merge 9 commits intomainfrom
claude/check-plugins-admin-routes-UTuBW
Open

fix: respect custom routes.api in alt-text, pages, and vercel-deployments#99
jhb-dev wants to merge 9 commits intomainfrom
claude/check-plugins-admin-routes-UTuBW

Conversation

@jhb-dev
Copy link
Copy Markdown
Contributor

@jhb-dev jhb-dev commented Apr 11, 2026

Summary

Fixes three plugins where client components hardcoded /api/... for their fetch calls, so they broke when the user configured a non-default routes.api. All touched components now read routes.api and serverURL from Payload's useConfig() (or from a caller that has it) and build the full URL at runtime.

Per-plugin changes

alt-text

  • GenerateAltTextButton and BulkGenerateAltTextsButton now read config.routes.api / config.serverURL from useConfig() instead of hardcoding /api/alt-text-plugin/....
  • AltTextHealthWidget now uses Payload's formatAdminURL helper from payload/shared when linking into the admin (replaces a manual ${config.routes.admin}/collections/... concat, matching how admin-search already builds admin URLs).
  • Added vitest + jsdom + @testing-library/react infra so client components can be covered by failing-first tests:
    • AltTextHealthWidget.test.tsx — verifies links use both default /admin and a custom admin route.
    • GenerateAltTextButton.test.tsx and BulkGenerateAltTextsButton.test.tsx — verify fetch is called with the custom routes.api + serverURL.
  • Added tsconfig.build.json and updated the build:swc --ignore pattern so .test.ts / .test.tsx files don't leak into the published dist.

vercel-deployments

  • DeploymentStatusPoller and TriggerFrontendDeploymentButton now read routes.api / serverURL from useConfig() instead of hardcoding /api/vercel-deployments.
  • Extended the existing DeploymentStatusPoller.test.tsx and added a new TriggerDeploymentButton.test.tsx to verify both default and custom API routes.
  • Fixed a preexisting build:swc --ignore pattern that only excluded .test.ts (not .test.tsx), so test files were being published to npm.

pages

  • getBreadcrumbs now accepts an optional apiURL: string param (required when called without a req, i.e. from client-side field components). The PathField forwards ${config.serverURL ?? ''}${config.routes.api} sourced from useConfig().
  • Deleted the trivial internal fetchRestApi.ts helper — its logic is inlined into getBreadcrumbs now that the caller supplies the URL.
  • Added vitest config and test/getBreadcrumbs.test.ts covering the client-side URL construction (default /api, custom routes.api, and a clear error when called without either req or apiURL).

CI coverage

Updated .github/workflows/ci.yml's test-unit job so the new tests actually run on CI:

  • The alt-text step changed from node --experimental-strip-types --test test/*.test.ts (which only picked up the legacy node:test files) to pnpm test, which chains the node:test health tests with the new vitest component tests.
  • Added a new pages step that runs pnpm test:unit (vitest). The pages plugin previously had no unit-test job in CI — only the dev-app integration tests.

Without these, a regression reintroducing hardcoded /api in any of the affected client components would still pass CI.

Test plan

  • cd alt-text && pnpm test — both node:test and vitest suites pass.
  • cd vercel-deployments && pnpm test — vitest passes.
  • cd pages && pnpm test:unit — new vitest suite passes.
  • pnpm lint in each affected plugin — 0 errors.
  • pnpm format in each affected plugin — no diff.
  • pnpm build in each affected plugin — .test.tsx files excluded from dist.

https://claude.ai/code/session_01YEgtR29SVbLQtSwbK7QWZh

claude added 5 commits April 11, 2026 07:58
The GenerateAltTextButton and BulkGenerateAltTextsButton hardcoded
`/api/alt-text-plugin/...` for their fetch calls, so they broke for
users who configured a non-default `routes.api`. Both now read the
api route and serverURL from `useConfig()`.

Also refactors AltTextHealthWidget to use Payload's `formatAdminURL`
helper (matching how admin-search builds admin links), and adds a
vitest + jsdom + @testing-library/react setup so the client
components can be covered by failing-first tests.
DeploymentStatusPoller and TriggerFrontendDeploymentButton hardcoded
`/api/vercel-deployments` for their polling and redeploy fetches, so
they broke for users who configured a non-default `routes.api`. Both
now read the api route and serverURL from `useConfig()`.

Adds failing-first tests covering both components with a custom
route, and fixes a preexisting build:swc ignore pattern that only
excluded `.test.ts` (not `.test.tsx`) files from the published dist.
`fetchRestApi` (used by `getBreadcrumbs` when called from the
PathField client component without a PayloadRequest) hardcoded
`/api`, so it broke for users who configured a non-default
`routes.api`. It now takes a `FetchRestApiConfig` argument that
callers must supply. `getBreadcrumbs` exposes this via a new
optional `restApiConfig` param and PathField forwards
`config.routes.api` / `config.serverURL` from `useConfig()`.
Replace the FetchRestApiConfig indirection with a plain `apiURL` string
that the client caller (PathField) builds directly from useConfig().
The `fetchRestApi` helper is deleted and its logic is inlined in
getBreadcrumbs, since it was trivial once the URL is supplied by the
caller.

PathField now passes `apiURL: ${serverURL}${routes.api}` sourced from
useConfig() — no new config type is introduced, and getBreadcrumbs
stays a plain async function without needing to know anything about
Payload's config shape.
Trivial formatting-only fixes in two files touched by the
routes.api PR:
- alt-text/src/components/BulkGenerateAltTextsButton.test.tsx
- pages/src/utils/getBreadcrumbs.ts

Caught by the `format` CI check on PR #99.
@jhb-dev jhb-dev changed the title Respect custom routes.api configuration in plugins fix: respect custom routes.api in alt-text, pages, and vercel-deployments Apr 11, 2026
jhb-dev pushed a commit that referenced this pull request Apr 11, 2026
The new CI steps in this branch invoke `pnpm test` (alt-text) and
`pnpm test:unit` (pages), which are defined in PR #99. Merging PR #99
in so CI can actually run the intended suites until PR #99 lands on
main.
claude and others added 4 commits April 14, 2026 10:39
The existing alt-text test step globbed `test/*.test.ts`, so it only
picked up the legacy node:test health tests and skipped the new
vitest component tests under `src/components/*.test.tsx`. Switch to
`pnpm test`, which chains `test:health` (node:test) and
`test:components` (vitest).

The pages plugin had no unit-test job in CI at all — only the
dev-app integration tests. Add a step that runs `pnpm test:unit`
(vitest) so the client-side URL construction in `getBreadcrumbs` is
exercised.

Without this, a regression reintroducing a hardcoded `/api` in any
of the client components touched by this PR would still pass CI.
…or test files

The new vitest test files use `vi.mock('@payloadcms/ui', ...)` to
stub hooks like `useConfig`, `useLocale`, `useRouter`, etc. The mock
arrow functions intentionally share names with the real hooks so the
mock matches the real export shape, which triggers
`@eslint-react/hooks-extra/no-useless-custom-hooks`.

The rule's intent doesn't apply to test mocks — they are supposed to
be plain functions that return canned values, not real hooks.
Disable it for `src/**/*.test.ts` / `*.test.tsx` in both plugins.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants