Skip to content

refactor(cloudflare): extract list + createAndResolve helpers#2

Draft
Thanukamax wants to merge 1 commit into
BitByBit-B3:mainfrom
Thanukamax:refactor/cloudflare-idempotent-helpers
Draft

refactor(cloudflare): extract list + createAndResolve helpers#2
Thanukamax wants to merge 1 commit into
BitByBit-B3:mainfrom
Thanukamax:refactor/cloudflare-idempotent-helpers

Conversation

@Thanukamax

@Thanukamax Thanukamax commented Jul 6, 2026

Copy link
Copy Markdown

What

A targeted DRY refactor of src/cloudflare.ts — the resource-provisioning module that felt over-complicated.

Two repeated patterns are factored out:

  1. List boilerplate (×6). The verbose
    (await wranglerJson<any[]>([...], { allowFail: true })) ?? []
    appeared six times across ensureD1, ensureKv, deleteD1, deleteKv.
    Now two named helpers: listD1() and listKv().

  2. The idempotent create dance (×2). ensureD1 and ensureKv each
    hand-rolled the same "create → re-list → find the new entry → throw if
    still missing" sequence. Folded into one generic createAndResolve helper
    that names the intent.

What did not change

Behavior is identical — same wrangler invocations, same reuse-vs-create
branches, same single batched KV list per run (ensureKv still lists once
up front, so no extra API calls). The only user-visible change is error-message
wording (failed to resolve preview D1 ${name} after create vs the old
... id for ${name}) — arguably clearer.

Honest take: the rest of the codebase is already clean and modular, so I kept
this surgical rather than inventing a big rewrite. cloudflare.ts is where the
real duplication lived. Happy to take it further (e.g. the R2 create/delete
symmetry, or a bindingResourceName map) if you want a bigger pass.

Verification

  • bun run typecheck
  • bun test ✅ (9 pass)
  • bun run lint ✅ (pre-existing info in sync.ts untouched)
  • bun run build ✅ — dist/index.cjs regenerated and matches the pinned
    bun 1.3.14 bundler output CI enforces.

Draft for your review — flip to ready whenever.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4b9061c4-2ae0-4660-af10-a95bc567fd1e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

De-duplicate the resource provisioning logic in cloudflare.ts:

- Replace six copies of the verbose
  `(await wranglerJson<any[]>([...], { allowFail: true })) ?? []`
  list expression with named `listD1()` / `listKv()` helpers.
- Fold the repeated "create -> re-list -> resolve -> throw if missing"
  idempotent-create dance (duplicated across ensureD1 and ensureKv)
  into a single generic `createAndResolve` helper.

Behavior is unchanged: same wrangler calls, same reuse-vs-create paths,
same single batched KV list per run. Purely a readability/DRY refactor.
typecheck, tests, lint, and build all pass.
@Thanukamax Thanukamax force-pushed the refactor/cloudflare-idempotent-helpers branch from d2f2608 to 2670a7f Compare July 6, 2026 15:13
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.

1 participant