Skip to content

fix: npx emdash types crashes due to schema envelope mismatch#1458

Merged
ascorbic merged 3 commits into
emdash-cms:mainfrom
mvanhorn:fix/1188-emdash-types-schema-unwrap
Jun 16, 2026
Merged

fix: npx emdash types crashes due to schema envelope mismatch#1458
ascorbic merged 3 commits into
emdash-cms:mainfrom
mvanhorn:fix/1188-emdash-types-schema-unwrap

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

What does this PR do?

npx emdash types crashes with Cannot read properties of undefined (reading 'collections') against any instance (deployed and local dev — reproduced on 0.15.0 and 0.17.2). Root cause is an envelope inconsistency. EmDashClient.request() (packages/core/src/client/index.ts:778-782) returns json.data for every response, and schemaExport() (client/index.ts:389-391) goes through request. But the /_emdash/api/schema route (packages/core/src/astro/routes/api/schema/index.ts:92-95) returns the schema un-enveloped as { ...schemaExport, version } with no data wrapper. So client.schemaExport() returns undefined, and the CLI types command (packages/core/src/cli/commands/types.ts:42-43) dereferences schema.collections.length and throws. Sibling schema endpoints (e.g. /schema/collections) do use the { success, data } envelope, confirming the JSON-schema route is the outlier.

Make the JSON /schema route consistent with the rest of the API by wrapping its body in the standard { success: true, data: { ...schemaExport, version } } envelope (keep the ?format=typescript branch and X-Schema-Version header unchanged, since schemaTypes() uses requestRaw and reads text directly). This restores the contract request() expects so schemaExport() returns the schema object. Verify no other caller depends on the un-enveloped JSON shape; the only consumers are schemaExport() (CLI) and any direct curl, and enveloping is the documented norm. If enveloping the route risks an external contract, the fallback is to switch schemaExport() to requestRaw + manual parse, but route-side enveloping is preferred for cross-endpoint consistency.

Closes #1188

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Codex (GPT-5.x)

Screenshots / test output

  • happy path: schemaExport() returns an object with a populated collections array against the enveloped route.
  • happy path: version field is preserved in the returned schema object.
  • edge: schemaTypes() (?format=typescript) still returns raw TS text, unaffected by the envelope change.
  • error path: a non-2xx schema response surfaces a clear error instead of an undefined dereference.

AI was used for assistance.

@changeset-bot

changeset-bot Bot commented Jun 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 83c2622

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

PR template validation failed

Please fix the following issues by editing your PR description:

  • Check the "I have read CONTRIBUTING.md" checkbox.

See CONTRIBUTING.md for the full contribution policy.

@github-actions github-actions Bot added area/core size/M review/needs-review No maintainer or bot review yet labels Jun 13, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1458

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1458

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1458

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1458

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1458

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1458

emdash

npm i https://pkg.pr.new/emdash@1458

create-emdash

npm i https://pkg.pr.new/create-emdash@1458

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1458

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1458

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1458

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1458

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1458

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1458

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1458

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1458

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1458

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1458

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1458

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1458

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1458

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1458

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1458

commit: 83c2622

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach is sound: the /schema JSON endpoint was the only API route returning un-enveloped JSON, which broke EmDashClient.request()'s assumption that every successful response is shaped { data: T }. Wrapping the payload with apiSuccess() fixes the npx emdash types crash and aligns the route with its siblings. The ?format=typescript branch and X-Schema-Version header are correctly preserved.

I checked the route change, the client contract, sibling schema endpoints, and the new tests. The unit and integration tests cover the route envelope, schemaExport() returning populated data, schemaTypes() remaining raw text, and error propagation. No logic bugs or regressions were found in the implementation.

The one gap is a missing changeset for the published packages/core package. CONTRIBUTING.md and AGENTS.md require one for every published-package behavior change.

@github-actions github-actions Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed review/needs-review No maintainer or bot review yet labels Jun 14, 2026
@github-actions github-actions Bot added review/needs-rereview Author pushed changes since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Jun 14, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ascorbic ascorbic merged commit 9c994ad into emdash-cms:main Jun 16, 2026
43 of 44 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core review/needs-rereview Author pushed changes since the last review size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

npx emdash types is broken against deployed (non-dev) instances

2 participants