Repository navigation
Closed
Description
Summary
Align the Atlas data layer with WeMeditateWeb by replacing the hand-rolled
axios + qs REST client with @payloadcms/sdk (PayloadSDK<Config> typed
against our synced payload-types.ts). The SDK is a fetch-based client whose
payload dependency is types-only (verified: no runtime imports of payload
in its dist JS; only qs-esm at runtime), so this drops axios (~12 kB min+gz)
and qs from the public bundle while gaining compile-time-typed find /
findByID calls (where/select/populate/draft checked against the
generated types) and one shared client pattern across both frontends.
Proposed changes
- Add
@payloadcms/sdk(match the CMS 3.x line); removeaxiosandqs. - Replace
src/config/api/client.tswith a client factory à la WeMeditateWeb's
createPayloadClient(server/payload-client.ts):new PayloadSDK<Config>
withbaseURL = ${VITE_SAHAJCLOUD_URL}/apiand
baseInit.headers.Authorization = clients API-Key …fromatlasAuth. The
per-requestlocaleparam (today added by the axios interceptor) moves to the
SDK's per-calllocaleoption, resolved from i18next at call time. - Port fetchers in
src/config/api/fetch.ts/mutate.ts:- Collection reads (
/regions,/events/:id,/clients/me) →sdk.find/
sdk.findByID/sdk.mewith the existingselect/populate/where
objects (now type-checked). - Custom endpoints (
GET /events/geojson,POST /events/:id/register) →
the SDK's rawrequesthelper (they aren't collection CRUD).
- Collection reads (
- Keep zod parsing exactly as-is. SDK types are compile-time only; the
runtime zod contract (every response parsed throughsrc/types/schemas) is
what surfaces SahajCloud shape changes as parse errors — the SDK does not
replace it. - Handle SDK does not throw errors instead returns undefined payloadcms/payload#14495 (SDK returns
undefinedon error instead of
throwing): wrap every call with avalidateSDKResponse-style guard (see
WeMeditateWebserver/payload-client.ts:182-190) so failures still throw and
reach thereact-error-boundaryfallback, preserving the current error contract. - Update
.claude/rules/data-layer.md(which currently mandates "plain axios +
zod, no Payload SDK") to document the new pattern and the bundle-size finding
that justified the change.
Acceptance criteria
- All reads/mutations go through
PayloadSDK;axiosandqsare removed frompackage.jsonand the bundle - Every fetcher still parses through its zod schema; query keys unchanged (
['geojson'],['region', slug],['event', id],['client', apiKey]) - API-key auth + locale behavior identical (verify a locale switch still refetches localized event titles)
- SDK error/undefined responses throw and render the existing
ErrorFallback(SDK does not throw errors instead returns undefined payloadcms/payload#14495 guarded) - Production bundle size is equal or smaller than before the migration (compare
pnpm buildoutput) - Lint + typecheck + unit tests + build pass;
src/config/api/fetch.test.tsboundary mocks updated to mock the SDK instead of axios -
.claude/rules/data-layer.mdupdated to reflect the SDK pattern
Files affected
src/config/api/client.ts,src/config/api/fetch.ts,src/config/api/mutate.ts,src/config/api/fetch.test.tspackage.json(add@payloadcms/sdk; dropaxios,qs).claude/rules/data-layer.md
References
- WeMeditateWeb pattern:
server/payload-client.ts(client factory,validateSDKResponse),server/cms-client.ts - SDK does not throw errors instead returns undefined payloadcms/payload#14495 — SDK returns undefined on error
- Related: live-preview issue feat(widget): add sahajcloud live preview for region and event views #40 — independent, but whichever lands second adapts the preview draft-header seam (axios interceptor vs SDK client factory)
- Related: perf(api): spike — separate region hierarchy from the event feed #32 (data-query architecture spike) — query-key restructuring is orthogonal; this ticket keeps keys unchanged
Metadata
Metadata
Assignees
Labels
No labels
Type
Fields
Hold Until
None yet
Activity
refactor(api): migrate data layer from axios+qs to @payloadcms/sdk (#56)
chore(claude): finish the accent removal and stale-doc sweep (#133)