Skip to content

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

Closed

Description

@Ardnived

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); remove axios and qs.
  • Replace src/config/api/client.ts with a client factory à la WeMeditateWeb's
    createPayloadClient (server/payload-client.ts): new PayloadSDK<Config>
    with baseURL = ${VITE_SAHAJCLOUD_URL}/api and
    baseInit.headers.Authorization = clients API-Key … from atlasAuth. The
    per-request locale param (today added by the axios interceptor) moves to the
    SDK's per-call locale option, 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.me with the existing select / populate / where
      objects (now type-checked).
    • Custom endpoints (GET /events/geojson, POST /events/:id/register) →
      the SDK's raw request helper (they aren't collection CRUD).
  • Keep zod parsing exactly as-is. SDK types are compile-time only; the
    runtime zod contract (every response parsed through src/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 undefined on error instead of
    throwing): wrap every call with a validateSDKResponse-style guard (see
    WeMeditateWeb server/payload-client.ts:182-190) so failures still throw and
    reach the react-error-boundary fallback, 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; axios and qs are removed from package.json and 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 build output)
  • Lint + typecheck + unit tests + build pass; src/config/api/fetch.test.ts boundary mocks updated to mock the SDK instead of axios
  • .claude/rules/data-layer.md updated 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.ts
  • package.json (add @payloadcms/sdk; drop axios, qs)
  • .claude/rules/data-layer.md

References

Activity

  1. added a commit that references this issue on Jul 17, 2026
    ecd5241
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions