Skip to content

refactor(api): migrate data layer from axios+qs to @payloadcms/sdk - #56

Merged
Ardnived merged 4 commits into
mainfrom
refactor/payload-sdk
Jul 17, 2026
Merged

Ardnived merged 4 commits into
mainfrom
refactor/payload-sdk

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

Replaces the hand-rolled axios + qs REST client with a typed
PayloadSDK<Config> (fetch-based). The SDK's payload dependency is types-only
(its dist imports only qs-esm at runtime — verified), so the public bundle drops
axios + qs and gains only the SDK + qs-esm.

  • Bundle is smaller: total JS −62,798 bytes (−2.08% raw, −2.51% gzip); the
    api chunk shrank 240.8 kB → 178.0 kB. (AC: equal-or-smaller ✓)
  • Collection reads are now compile-checked — select/populate/where are typed
    against the generated CMS Config.
  • Behaviour is unchanged: same query keys, same zod parsing, same auth + locale on
    every request, same error contract.

Changes

  • src/config/api/client.ts — one PayloadSDK<Config> whose custom fetch is the
    single interceptor
    : applyRequestContext attaches API-key auth + the active locale
    to every request, plus the live-preview secret header + draft=true during a preview
    session (feat(widget): add sahajcloud live preview for region and event views #40). Auth stays late-bound in the fetch wrapper (the apiKey is set after
    module load). Adds validateSDKResponse (guards SDK does not throw errors instead returns undefined payloadcms/payload#14495's
    undefined-on-error) and a requestJson helper over sdk.request.
  • src/config/api/fetch.ts — collection reads → typed sdk.find (regions, event
    titles) / sdk.findByID (event); custom endpoints (geojson, preview populate) +
    /clients/me → requestJson. Extracted select objects gained as const so their
    true literals satisfy the generated select types.
  • src/config/api/mutate.ts — createRegistration → requestJson.
  • src/config/api/fetch.test.ts — mocks @payloadcms/sdk instead of axios; the
    interceptor is tested directly via applyRequestContext, plus an interceptFetch
    round-trip test proving SDK-serialized params + injected locale/auth coexist.
  • .claude/rules/data-layer.md + two stale "axios client" comments updated.
  • package.json — add @payloadcms/sdk; drop axios, qs, @types/qs.

Verification

  • Lint: ✓ (pnpm lint)
  • Types: ✓ (pnpm typecheck) — the type-checked selects are the main risk surface
  • Tests: ✓ 241 unit tests green (pnpm test:run)
  • Build: ✓ (pnpm build) — bundle smaller (see Summary)

Manual / visual verification

Ran the widget against the seeded local SahajCloud (:3000) and drove it with
Playwright — 0 console errors across the flow. Confirmed every SDK path returns
200 with the expected select/populate/locale on the wire:

Path Fetcher Result
/clients/me (API-key authed) requestJson ✅ 200
/regions sdk.find ✅ 200
/events/geojson sdk.request ✅ 200
/events/:id sdk.findByID ✅ 200
/events?select[title] (titles) sdk.find, re-keyed on locale ✅ refetched locale=fr

Country list, region drill-down, event detail all rendered; a locale switch
(en → fr) refetched the localized titles sliver (AC ✓). The qs-esm bracket encoding
is accepted by SahajCloud (all 200s).

Notes for reviewer

  • getClient uses the raw request helper, not sdk.me() — the bare me()
    sends no select/populate/depth, and SahajCloud requires an explicit select
    on every client read. Trade-off: that one select isn't compile-checked (inherent to
    the raw path); the runtime gate is still satisfied.
  • Dependency surface: @payloadcms/sdk pulls a types-only payload (a large
    server package) into the install/CI tree — not the shipped bundle (verified: no
    runtime payload import in the SDK dist; the browser bundle gains only the SDK +
    qs-esm). No install scripts on payload/qs-esm. The lockfile pins the exact tree;
    kept the ^3.86.0 range to match repo convention + the CMS 3.x line.
  • Out of scope: pnpm types:cms currently shows unrelated CMS contract drift
    (locale codes, Region.eventDefaults); left for a separate contract-sync PR so this
    stays a pure transport swap.
  • Security review (public-bundle threat model) + code review both came back clean —
    no material findings beyond the interceptFetch test that's now added.

Closes #41

🤖 Generated with Claude Code

Ardnived and others added 4 commits July 16, 2026 16:39
Replace the hand-rolled axios + qs REST client with a typed
`PayloadSDK<Config>` (fetch-based; its `payload` dep is types-only, so
only the SDK + qs-esm reach the public bundle — axios and qs are gone).

- client.ts: one `PayloadSDK<Config>` whose custom `fetch` is the single
  interceptor — `applyRequestContext` attaches API-key auth + the active
  locale to every request, plus the preview secret header + `draft=true`
  during a live-preview session. Auth stays late-bound (apiKey is set
  after module load). Add `validateSDKResponse` (guards payload#14495's
  undefined-on-error) and a `requestJson` helper over `sdk.request`.
- Collection reads → typed `sdk.find` (regions, event titles) /
  `sdk.findByID` (event); selects/populate now compile-checked (needed
  `as const` on the extracted objects to keep the `true` literals).
- Custom endpoints (geojson, register, preview populate) and
  `/clients/me` → `requestJson`. `/clients/me` can't use `sdk.me()`
  because the bare `me()` sends no `select` (SahajCloud requires one).
- zod parsing and query keys unchanged; error contract preserved.
- fetch.test.ts mocks @payloadcms/sdk instead of axios; the interceptor
  is tested directly via `applyRequestContext`.

Closes #41

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Reflect the axios→@payloadcms/sdk migration: rewrite the data-layer rule
(SDK client, applyRequestContext, requestJson/validateSDKResponse) and
refresh two stale "axios client" comments in use-ip-location and image.

Closes #41

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Per the /simplify pass: export `activeLocale` and use it for the
`event-titles` query key (single source of truth so the cache key can't
drift from the locale `applyRequestContext` actually sends), and drop the
unused `PayloadSDKError` export from the @payloadcms/sdk test mock.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Per the /code-review pass: export `interceptFetch` and add a test that
drives it against a spied global fetch, asserting the SDK's bracket-
serialized select/depth params survive the `new URL` round-trip while the
interceptor layers locale + auth on top — the migration's riskiest seam,
previously only covered by the live Playwright check.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying sahajatlas with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2804582
Status: ✅  Deploy successful!
Preview URL: https://69d2b51f.sahajatlas.pages.dev
Branch Preview URL: https://refactor-payload-sdk.sahajatlas.pages.dev

View logs

@Ardnived
Ardnived merged commit ecd5241 into main Jul 17, 2026
4 checks passed
@Ardnived
Ardnived deleted the refactor/payload-sdk branch July 17, 2026 17:44
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.

refactor(api): migrate data layer from axios+qs to @payloadcms/sdk

1 participant