Skip to content

refactor(server): delete the payload#14495 scaffolding — @payloadcms/sdk throws since 3.88.0 - #87

Merged
Ardnived merged 3 commits into
mainfrom
claude/refactor-payload-sdk-scaffolding
Sep 7, 2026
Merged

Ardnived merged 3 commits into
mainfrom
claude/refactor-payload-sdk-scaffolding

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Changes

  • server/payload-client.ts — validateSDKResponse deleted; the file-header bug note deleted; fetchWithErrorDetails kept, its docblock corrected.
  • server/cms-client.ts — the one remaining call site (getWebConfig), plus two stale header claims.
  • server/payload-client.test.ts — the guard's 11 unit tests replaced by two that pin the upstream contract.
  • docs/rules/rest-api.md — the recipe returns result.docs[0] ?? null and says to let SDK errors propagate.

Preview

No rendered output changes, so there is nothing new to look at. Both branch aliases are up, for a sanity check that the Worker still boots and reads the CMS:

Test results

  • Lean gate: ✓ pnpm lint (0 errors), pnpm typecheck, pnpm test:run — 49 files, 502 tests
  • Targeted specs: server/payload-client.test.ts 7 passed
  • CI: ✓ green on b64249a — Lint/Typecheck/Unit, Smoke (web), Smoke (ladle), Workers build, Pages build
  • Build: ✓ via the Cloudflare Workers build

Both new assertions were confirmed failing first, at the site that causes them: the stub was made to answer 200 with a null body — the exact shape of payload#14495 — and both went red (rejects.toBeInstanceOf and .status), then green on restore.

Manual verification

Nothing. No rendered output changes, and the one behaviour change (which error copy a CMS failure produces) is asserted in the specs.

Notes for reviewer

  • The judgement call is deleting the null half of the guard, not the undefined half. findGlobal returns response.json() only on a 2xx, and the globals endpoint answers with the document, never a bare null. The one SDK path that returns null deliberately is findByID({ disableErrors: true }), which this repo never calls.
  • One review observation, recorded rather than acted on. If the CMS ever answered 200 with a null body, getWebConfig would now throw a bare TypeError from partitionPublishedPages instead of a named error. Retry behaviour is identical — both classify as UNKNOWN, neither retries — so this is diagnostics, not correctness.
  • fetchWithErrorDetails stays and is not dead. Its [PayloadCMS] <method> <url> → <status> line is the debugging workflow AGENTS.md documents, and it logs the full error body where the thrown error carries only errors[0].message.
  • The third commit is a one-line JSDoc fix the security pass turned up in the same file: previewSecret said "passed via URL parameter" while the code sends a header.
Detail

The evidence for the premise. PayloadSDK.request (node_modules/@payloadcms/sdk/dist/index.js, the !response.ok branch) reads the error payload and throws PayloadSDKError({ errors, message, response, status }). Every collection and global helper routes through that one method — dist/globals/findOne.js and dist/collections/find.js both call sdk.request(...) then response.json() — so no read path can resolve undefined on a failure.

Why deleting the guard improves error handling rather than weakening it. validateSDKResponse threw a bare Error whose message matched none of error-utils.ts's patterns, so detectErrorType could only return UNKNOWN and withRetry never retried it. readStatus reads error.response?.status then error.status; PayloadSDKError sets both. The second new spec asserts exactly that path end to end.

Three commits, one behaviour each:

  • 142bd30 — the deletion, its call site, its tests, and the docs step.
  • 8a820b1 — /simplify pass: cms-client.ts's header claimed error-utils classifies an SDK error by matching its message. Status is the rule, message the fallback.
  • b64249a — docs sync: the previewSecret JSDoc.

Review passes. /simplify across four angles (reuse, simplification, efficiency, altitude): one finding, the cms-client.ts header, fixed in 8a820b1. /code-review: no findings, one non-blocking observation, recorded above. /security-review (triggered by ^server/): no findings at confidence 8+ — it confirmed the logging wrapper never touches init.headers, so neither the API key nor the preview secret can reach a log line.

Not run: pnpm types:cms. This change reads no new CMS field, and running it would pull unrelated schema movement from SahajCloud main into a refactor PR.

Fixture pre-mortem. The new spec mocks ./cms-context, because createPayloadClient calls getCmsContext() unconditionally and that throws outside a request. The shape { apiKey, baseURL, kv } was checked against server/cms-context.ts's CmsContext interface, and matches what server/cms-client.test.ts already uses. The fetch stub returns a real Response, not a plain object, because fetchWithErrorDetails calls response.clone().

Closes #85

🤖 Written by the sydevs autonomous loop — see what this is

🤖 Generated with Claude Code

https://claude.ai/code/session_01QiKyVWeb6VXFQom2DDUfHC

`server/payload-client.ts` was built around one upstream bug: `@payloadcms/sdk`
resolved `undefined` on an error response instead of throwing. #83 moved this
repo to `@payloadcms/[email protected]`, where `PayloadSDK.request`
(`dist/index.js:120-137`) checks `!response.ok`, reads the error payload, and
throws a `PayloadSDKError` carrying `status` and `errors`. Every collection and
global helper routes through that one method, so no read path can resolve
`undefined` on a failure any more.

Removed: `validateSDKResponse`, its one remaining call site (`getWebConfig`),
its unit tests, the `cms-client.test.ts` mock entry, the file-header note, and
the step `docs/rules/rest-api.md` wrote into the recipe for every future query
function.

The guard also rejected `null`. Nothing can reach it: `findGlobal`
(`dist/globals/findOne.js`) returns `response.json()` on a 2xx only, and the
globals endpoint answers with the document, never a bare `null`. The one SDK
path that returns `null` deliberately is `findByID({ disableErrors: true })`,
which this repo never calls.

Deleting the guard improves error classification rather than weakening it. It
threw a bare `Error`, which `detectErrorType` could only read as UNKNOWN;
`PayloadSDKError` carries `status`, which `readStatus` reads first, so a 4xx
now surfaces the CLIENT copy and a 5xx the SERVER copy.

`fetchWithErrorDetails` stays. Its `[PayloadCMS] <method> <url> -> <status>`
line is the debugging workflow AGENTS.md documents, and it logs the full error
body, where the thrown error carries only `errors[0].message`. Its docblock no
longer claims the SDK swallows the status.

Tests: two specs pin the upstream contract, stubbing a non-OK response and
asserting the throw, the `status` it carries, and the `ErrorType` that follows.
Both were confirmed failing first, against a stub returning a 200 with a `null`
body — the exact shape of the bug this scaffolding existed for.

Closes #85

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QiKyVWeb6VXFQom2DDUfHC
The `cms-client.ts` header claimed error-utils classifies an SDK error by
matching its message. `detectErrorType` reads `readStatus()` first, and
`PayloadSDKError` carries `status`, so the status branch is the rule and
message matching the fallback. The claim was already imprecise; deleting the
guard made it misleading.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QiKyVWeb6VXFQom2DDUfHC
…eter

`PayloadClientConfig.previewSecret`'s JSDoc said "passed via URL parameter".
`createPayloadClient` sets it as the `x-sahajcloud-preview-secret` header
(`payload-client.ts:32`, `:145`). The security pass flagged the mismatch while
reviewing this file's other comment fixes.

Docs swept for the deleted guard: `docs/rules/rest-api.md` was the only stale
surface, corrected in the first commit. `docs/rules/zod-validation.md` still
describes `payloadConfigSchema` and `PayloadConfigError` accurately, and
`AGENTS.md`'s `[PayloadCMS] GET <url> -> <status>` debugging line still holds.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QiKyVWeb6VXFQom2DDUfHC
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying wemeditate-design with  Cloudflare Pages  Cloudflare Pages

Latest commit: b64249a
Status: ✅  Deploy successful!
Preview URL: https://a29bb3cb.wm-design.pages.dev
Branch Preview URL: https://claude-refactor-payload-sdk.wm-design.pages.dev

View logs

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
wemeditate-web b64249a Commit Preview URL

Branch Preview URL
Sep 07 2026, 11:19 PM

@sydevs-bot
sydevs-bot marked this pull request as ready for review September 7, 2026 23:21
@Ardnived
Ardnived merged commit 5c9f818 into main Sep 7, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in SYDevelopers Technical Roadmap Sep 7, 2026
@Ardnived
Ardnived deleted the claude/refactor-payload-sdk-scaffolding branch September 7, 2026 23:27
@Ardnived Ardnived removed the awaiting label Sep 7, 2026
Ardnived added a commit that referenced this pull request Nov 2, 2026
* test: drop leftovers from the KV cache removal and an unused smoke helper

- `kv: undefined` in two sahajcloud-context mocks: the context lost its
  kv field in #107, so the mocks described a shape that no longer exists.
- Three retry tests titled "…since the KV layer used to supply that":
  the KV layer is gone, so the titles now state only the behaviour.
- `headTags()` in the smoke preview helper: exported in #28 and never
  called.

The audit found no migration-era test files left; #87, #99 and #107
already removed them with their code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(server): drop the stale KV binding claim

`getSahajCloudContext()` returns `{ apiKey, baseURL }`, and
`SahajCloudEnv.Bindings` carries no KV namespace — the binding went with
`server/kv-cache.ts` in #98. The docblock still promised a reader that the
helper resolves one, which is the same leftover this PR deletes from the
file's own test mock. Wording now matches `sitemap-routes.ts`.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjXde9tn99Z82seiuKd

* fix(components): call the related fetch edge-cached

Nothing in this Worker holds persistent state (server/CACHING.md). The
related endpoints are cached by the Cloudflare zone in front of SahajCloud,
which sets `s-maxage` and `Cache-Tag` through `publicReadCacheHeaders`. The
KV namespace these docblocks named went away in #98, so "KV-cached" pointed
a reader at a cache they could neither inspect nor purge.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjXde9tn99Z82seiuKd

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

refactor(server): delete the payload#14495 scaffolding — @payloadcms/sdk throws since 3.88.0

3 participants