Skip to content

feat: read an api spec from a url, and transform it before nimbus reads it - #424

Merged
MohamedH1998 merged 8 commits into
mainfrom
feat/api-spec-sources-transform
Nov 23, 2026
Merged

MohamedH1998 merged 8 commits into
mainfrom
feat/api-spec-sources-transform

Conversation

@MohamedH1998

@MohamedH1998 MohamedH1998 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

A content source can now be one file at a URL, and an API collection can prepare its spec in Node before Nimbus reads it. A site whose spec is a published file (a release asset, say) no longer needs its own download, cache and preview code, and a heavy preparation step stays out of the server bundle.

Before → after: a URL that redirects to another file

A source at a "latest" URL that redirects to a versioned file (/latest → /v1.yaml, then /v2.yaml), where both files send the same ETag and answer 304 to any conditional request. Three builds on a fresh site, on this branch's previous head and on its current one:

Build Previous head (d34e9d1) Current head
1: /latest → /v1.yaml /v1.yaml fetched; page shows v1 /v1.yaml fetched; page shows v1
2: unchanged /v1.yaml asked conditionally, 304; v1 /v1.yaml asked conditionally, 304; v1
3: /latest → /v2.yaml /v2.yaml asked with v1's ETag, 304; page still shows v1 /v2.yaml asked plainly; page shows v2

The cache headers now go only to the URL that sent them: the saved record keeps the SHA-256 of that URL (not the URL, whose query string can hold a token).

With the server down, the fallback warning names the reason: the request failed (ECONNREFUSED), where it said the request failed ().

What changed

// before: the site downloads and prepares the spec itself, then hands Nimbus a file
nimbus(defineConfig({ api: [await prepareSpecToFile()] }))

// after
nimbus(
  defineConfig({
    sources: { spec: { url: "https://example.com/openapi.json", integrity: "sha256-..." } },
    api: [{ collection: "api", spec: { source: "spec" } }],
  }),
  { api: { api: { transform: (document, { collection, version }) => prepare(document) } } },
)
Surface Before After
sources.<name> A Git repository Also { url, integrity? }, one file
api[].spec, api[].versions[].spec { source, path } path is left out for a URL source
nimbus() options No spec hook api: { <collection>: { transform } }, Node only, once per spec
nimbus-docs check A URL source without integrity reports nimbus/source-floating-ref
astro preview Serves what the last build produced: no request, no transform

A URL source behaves like a Git source: saved under .nimbus/sources, pinned or floating, revalidated with ETag / Last-Modified, and read from the saved file when the server is down. Sites that use neither feature are unchanged. No starter or user-owned file changes.

How to review

  1. packages/nimbus-docs/src/types.ts, src/_internal/validate.ts: the config shape.
  2. src/_internal/source-repo.ts: fetching one file, redirects, the saved snapshot.
  3. src/_internal/content-sources.ts, src/content.ts: a URL source in the source store.
  4. src/_internal/api/spec-transform.ts: running the transform, writing and restoring its result.
  5. src/integration.ts, src/_internal/virtual-config.ts: where both are wired in.
  6. src/check/sources.ts: the floating source check.
  7. test/content-sources-url.test.ts, test/api-spec-transform.test.ts.
  8. apps/www/src/content/docs/api-reference/openapi/spec-sources.mdx and the three updated docs pages.

Try it

cd packages/nimbus-docs
node --import tsx --test test/content-sources-url.test.ts
node --import tsx --test test/api-spec-transform.test.ts
node --import tsx --test test/content-sources.test.ts

Checks

  • tsc --noEmit clean; the three test files above pass (5, 4 and 13 tests). The transform test is a real Astro build.
  • CI is green.
  • On a fresh site from the preview release: first fetch, a 304 on the next build, a new file fetched when it changes, the saved file used when the server is down, a 404 failing the build, a pinned file making no request and a wrong hash failing, nimbus-docs check reporting the floating source, the transform on rendered pages, and astro preview making no request.
  • The transform example in the docs type-checks.
  • The Cloudflare API site (4,182 API pages, rendered on request) built on this PR's preview release, with its own download and preview code replaced by a URL source and a transform, against the same site on main:
main This PR
8,372 URLs (every page and its Markdown twin) Identical, no 5xx
Build, empty cache 43 to 49 s 41 to 44 s
Build, second run 27 s 22 s (one conditional request instead of a 27 MB download)
Page request, median / p90 12.3 / 25.0 ms 12.3 / 22.6 ms
Deployed assets 116,676 KB, 108 page files Same
Worker code 1,784 KB 1,778 KB, no transform code

@MohamedH1998 MohamedH1998 added the pr preview Publish a pkg.pr.new preview for this PR label Nov 23, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://feat-api-spec-sources-transform.nimbus-docs.com (commit 816b98d)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://fafb2e3e.nimbus-docs.com 816b98d 2026-11-23T14:38:40.287Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://429749aa.nimbus-docs.com e111739 2026-11-23T12:02:23.261Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://f07809b0.nimbus-docs.com d34e9d1 2026-11-23T08:38:44.683Z Visit the dashboard ↗

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://feat-api-spec-sources-transform.demo.nimbus-docs.com (commit 816b98d)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://89a2761e.demo.nimbus-docs.com 816b98d 2026-11-23T14:38:18.241Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://e0e79022.demo.nimbus-docs.com e111739 2026-11-23T12:02:36.906Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://14fb5ea9.demo.nimbus-docs.com d34e9d1 2026-11-23T08:38:20.333Z Visit the dashboard ↗

@github-actions github-actions Bot removed the pr preview Publish a pkg.pr.new preview for this PR label Nov 23, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
pnpm add https://pkg.pr.new/@cloudflare/create-nimbus-docs@424
pnpm add https://pkg.pr.new/@cloudflare/nimbus-docs@424

commit: 816b98d

@MohamedH1998 MohamedH1998 added the pr preview Publish a pkg.pr.new preview for this PR label Nov 23, 2026
@github-actions github-actions Bot removed the pr preview Publish a pkg.pr.new preview for this PR label Nov 23, 2026
@MohamedH1998 MohamedH1998 added the pr preview Publish a pkg.pr.new preview for this PR label Nov 23, 2026
@github-actions github-actions Bot removed the pr preview Publish a pkg.pr.new preview for this PR label Nov 23, 2026
@MohamedH1998 MohamedH1998 added the pr preview Publish a pkg.pr.new preview for this PR label Nov 23, 2026
@github-actions github-actions Bot removed the pr preview Publish a pkg.pr.new preview for this PR label Nov 23, 2026
@MohamedH1998
MohamedH1998 marked this pull request as ready for review November 23, 2026 15:04
@MohamedH1998
MohamedH1998 merged commit f7649b8 into main Nov 23, 2026
62 of 64 checks passed
@MohamedH1998
MohamedH1998 deleted the feat/api-spec-sources-transform branch November 23, 2026 15:38

This branch was successfully deployed

1 active deployment
cloudflare — 816b98d6 Deployed Nov 23, 2026 by MohamedH1998 via Delete closed PR Preview #160
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