Repository navigation
refactor(api): migrate data layer from axios+qs to @payloadcms/sdk - #56
Merged
Merged
Conversation
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]>
Deploying sahajatlas with
|
| Latest commit: |
2804582
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://69d2b51f.sahajatlas.pages.dev |
| Branch Preview URL: | https://refactor-payload-sdk.sahajatlas.pages.dev |
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the hand-rolled
axios+qsREST client with a typedPayloadSDK<Config>(fetch-based). The SDK'spayloaddependency is types-only(its dist imports only
qs-esmat runtime — verified), so the public bundle dropsaxios+qsand gains only the SDK +qs-esm.apichunk shrank 240.8 kB → 178.0 kB. (AC: equal-or-smaller ✓)select/populate/whereare typedagainst the generated CMS
Config.every request, same error contract.
Changes
src/config/api/client.ts— onePayloadSDK<Config>whose customfetchis thesingle interceptor:
applyRequestContextattaches API-key auth + the active localeto every request, plus the live-preview secret header +
draft=trueduring a previewsession (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'sundefined-on-error) and a
requestJsonhelper oversdk.request.src/config/api/fetch.ts— collection reads → typedsdk.find(regions, eventtitles) /
sdk.findByID(event); custom endpoints (geojson, preview populate) +/clients/me→requestJson. Extracted select objects gainedas constso theirtrueliterals satisfy the generated select types.src/config/api/mutate.ts—createRegistration→requestJson.src/config/api/fetch.test.ts— mocks@payloadcms/sdkinstead of axios; theinterceptor is tested directly via
applyRequestContext, plus aninterceptFetchround-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; dropaxios,qs,@types/qs.Verification
pnpm lint)pnpm typecheck) — the type-checked selects are the main risk surfacepnpm test:run)pnpm build) — bundle smaller (see Summary)Manual / visual verification
Ran the widget against the seeded local SahajCloud (
:3000) and drove it withPlaywright — 0 console errors across the flow. Confirmed every SDK path returns
200 with the expected
select/populate/localeon the wire:/clients/me(API-key authed)requestJson/regionssdk.find/events/geojsonsdk.request/events/:idsdk.findByID/events?select[title](titles)sdk.find, re-keyed on localelocale=frCountry 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
getClientuses the rawrequesthelper, notsdk.me()— the bareme()sends no
select/populate/depth, and SahajCloud requires an explicitselecton every client read. Trade-off: that one select isn't compile-checked (inherent to
the raw path); the runtime gate is still satisfied.
@payloadcms/sdkpulls a types-onlypayload(a largeserver package) into the install/CI tree — not the shipped bundle (verified: no
runtime
payloadimport in the SDK dist; the browser bundle gains only the SDK +qs-esm). No install scripts onpayload/qs-esm. The lockfile pins the exact tree;kept the
^3.86.0range to match repo convention + the CMS 3.x line.pnpm types:cmscurrently shows unrelated CMS contract drift(locale codes,
Region.eventDefaults); left for a separate contract-sync PR so thisstays a pure transport swap.
no material findings beyond the interceptFetch test that's now added.
Closes #41
🤖 Generated with Claude Code