Skip to content

feat(sdk/invoke): export default invoke singleton + add to barrel - #103

Merged
vibe-dex merged 1 commit into
mainfrom
feat/sdk-invoke-singleton
May 1, 2026
Merged

vibe-dex merged 1 commit into
mainfrom
feat/sdk-invoke-singleton

Conversation

@vibe-dex

@vibe-dex vibe-dex commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes plan item 1.1 (framework side). createAppInvoke() already existed in src/sdk/invoke.ts but was only reachable via deep import, with no default singleton. Every migrated site ends up writing the same 3-line shim — exactly the boilerplate the migration plan flags as movable.

This PR:

  • Adds export const invoke = createAppInvoke() next to the factory. Default basePath stays /deco/invoke. Custom basePath / typed handlers still use createAppInvoke<T>().
  • Exposes createAppInvoke, invoke, and NestedFromFlat from the ./sdk barrel so they're discoverable via @decocms/start/sdk.

Site impact (after release)

Before:

// site's ~/runtime.ts (46 lines of duplicated proxy logic)
function createNestedInvokeProxy(path: string[] = []): any { ... }
export const invoke = createNestedInvokeProxy() as any;

// at call sites:
import { invoke } from "~/runtime";

After:

// no ~/runtime.ts needed
import { invoke } from "@decocms/start/sdk/invoke";

bagaggio-tanstack will be the first consumer (2 call sites + 46 LOC delete). Casa&Video (the production reference) is left untouched.

Test plan

  • 9 unit tests in src/sdk/invoke.test.ts:
    • Nested key collection (vtex.actions.checkout.addItemsToCart → /deco/invoke/vtex/actions/checkout/addItemsToCart)
    • .ts suffix fallback when first key returns 404
    • "handler not found" thrown when both 404
    • Status code + error body extraction on non-OK responses
    • Default {} body when called with no props
    • then/catch/finally trap (avoid being treated as a thenable)
    • Symbol property guard (no spurious sub-proxy on Symbol.iterator etc.)
    • Custom basePath
    • Default invoke singleton hits /deco/invoke
  • Full repo suite: 98 / 98 still passing
  • biome check clean
  • No breaking change — createAppInvoke was already in the same file with this signature

Notes for reviewers

  • The singleton is created at module load time but does no I/O — Proxies are lightweight closures, fetch only fires on actual call. SSR-safe.
  • Default /deco/invoke matches what every existing site uses; sites overriding the path were already calling createAppInvoke() themselves and aren't affected.
  • The barrel export adds NestedFromFlat because typed sites import it for createAppInvoke<Handlers>() calls.

Made with Cursor

`createAppInvoke()` was already in `src/sdk/invoke.ts` but only reachable
via the `@decocms/start/sdk/invoke` deep import — no default singleton,
no barrel export. Sites end up writing the same 3-line shim
(`export const invoke = createAppInvoke()`) in their own `~/runtime.ts`,
which is exactly the kind of duplicated boilerplate the migration plan
flags as movable into the framework.

This PR:
- Adds `export const invoke = createAppInvoke()` next to the factory.
  Default basePath stays `/deco/invoke`. Sites that need a custom
  basePath or typed handlers keep using `createAppInvoke<T>()` directly.
- Exposes `createAppInvoke`, `invoke`, and `NestedFromFlat` from the
  `./sdk` barrel so they're discoverable via `@decocms/start/sdk` too,
  matching the discoverability of `createInvokeProxy` / `batchInvoke`.

After release, sites with a `~/runtime.ts` shim can either:
1. Replace the file with `export { invoke } from "@decocms/start/sdk/invoke";`
2. Or just import `invoke` directly from the framework at call sites
   and delete `~/runtime.ts` entirely.

bagaggio-tanstack will be the first consumer; casaevideo (the production
reference) is left untouched.

Tested: 9 unit tests covering nested key collection, .ts fallback,
"handler not found" failure, error body extraction, default empty body,
thenable trap, symbol guard, custom basePath, and the singleton's
binding to /deco/invoke. Full suite still 98/98.

Made-with: Cursor
@vibe-dex
vibe-dex merged commit 9148adf into main May 1, 2026
1 check passed
@github-actions

github-actions Bot commented May 1, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 2.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

vibe-dex added a commit that referenced this pull request May 1, 2026
…checklist (#109)

Two stale areas in the deco-to-tanstack-migration skill, plus one
brand-new reference distilling battle-tested cleanup patterns.

## Changes

### `templates/vite-config.md` — drop ~80 lines of stub duplication

The old template inlined the entire `deco-server-only-stubs` plugin
(react-dom/server, node:async_hooks, node:stream, node:stream/web,
tanstack-head-scripts) — ~60 lines that exactly mirror what
`@decocms/start/vite`'s `decoVitePlugin()` already does.

The new template uses `decoVitePlugin()` and adds the production-grade
boilerplate that real sites actually need (and that the old version
omitted): VTEX dev proxy, CSP frame-ancestors header, build sourcemap,
PLUGIN_WARNING filter, react-compiler config, `pure: ["console.log"]`,
and the full `dedupe` list including `@decocms/start` + `@decocms/apps`.

### `references/vite-config/README.md` — fix "minimal" config that was broken

The "post-migration minimal config" was missing `decoVitePlugin()`
entirely. Without it, the client bundle crashes on transitively imported
server modules. Added the plugin + the minimum dedupe list, and
cross-referenced the full template.

### `references/post-migration-cleanup.md` — NEW

Dedicated reference distilling the cleanup work that surfaces on every
real migration:

1. Detect + delete unused `src/lib/*` shims (with a copy-paste detection
   script). Real-world data: baggagio-tanstack had 11 generated shims
   and 0 actual users; casaevideo had 11 generated and 1 used.
2. Drop the `site-manual-chunks` and `deco-stub-meta-gen` inline plugins
   that are now framework-provided in `@decocms/start >= 2.5.0`.
3. Drop the `runtime.ts` `invoke` shim — `@decocms/start/sdk` exports
   both `invoke` (singleton) and `createAppInvoke` (typed factory).
4. Drop site-local `withSiteGlobals` workaround — framework helper is
   now opt-in and tested.
5. Verify VTEX util imports point direct at `@decocms/apps/vtex/...`,
   not at dead `~/lib/vtex-*` shims (the migration script's stale
   `phase-cleanup.rewriteVtexUtilImports` was silently regressing
   them).
6. Search for orphan `TODO: move into framework` comments and either
   migrate, file an issue, or delete.

Plus a verification checklist.

## Why now

Validated in this session against three reference sites
(casaevideo-storefront, baggagio-tanstack, casaevideo). Each cleanup
step has a corresponding shipped or in-flight PR (#103/#106/#107/#108
on framework, #5/#6/#7 on baggagio-tanstack), so the checklist isn't
speculative.

## Scope discipline

This PR only edits skill content; no source/runtime changes. Both
`.agents/skills/` and `.cursor/skills/` copies stay in sync.

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant