Skip to content

add fetch adapter - #1852

Merged
sachinraja merged 24 commits into
nextfrom
sachinraja/fetch-adapter
May 11, 2022
Merged

sachinraja merged 24 commits into
nextfrom
sachinraja/fetch-adapter

Conversation

@sachinraja

@sachinraja sachinraja commented May 1, 2022 •

Copy link
Copy Markdown
Member

closes #1374
part of #1597

Adds a Fetch API adapter to @trpc/server.

Also adds a Cloudflare Workers example. Right now tests and the actual router are just copied from the Express example. Preferably both examples would use the same files so an update to the code only has to happen in one place, but not sure how to make that happen unless I move the files out of the individual example directories.

Let me know if I should add myself as a codeowner.

cc @icflorescu who may be interested as the author of trpc-sveltekit (would love a review if you have time)
also cc @esamattis as the author of trpc-fetch-api-adapter (would also love a review)

Thanks to both of you for your packages! Took a lot of inspiration from them.

@vercel

vercel Bot commented May 1, 2022 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
next-prisma-starter ✅ Ready (Inspect) Visit Preview May 1, 2022 at 11:08PM (UTC)
www ✅ Ready (Inspect) Visit Preview May 1, 2022 at 11:08PM (UTC)

@KATT

KATT commented May 1, 2022

Copy link
Copy Markdown
Member

Cc @esamattis

KATT
KATT previously requested changes May 1, 2022

@KATT KATT left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks really good!

A few pieces I'd want to improve:

  • Add follow-up ticket to invert resolveHTTPResponse so that modern handling is the default and old handler
  • Docs for the adapter
  • Add example project to the list of example projects
  • Simplify example project to be just a mutation and a query?

"include": ["src", "test"],
"compilerOptions": {
"lib": ["ES2020", "DOM"],
"lib": ["ES2020", "DOM", "DOM.Iterable"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@sachinraja sachinraja May 1, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it is needed to add Symbol.iterator to the Headers type so we can use Object.fromEntries:

headers: Object.fromEntries(opts.req.headers),

Note that Node does not implement the Fetch spec.

@KATT
KATT requested a review from jlalmes May 1, 2022 09:36
Co-authored-by: Alex Johansson <[email protected]>

@KATT KATT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM - @jlalmes, what you think?

@jlalmes jlalmes 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.

Not very familiar with Cloudflare Workers, but LGTM! ✅

@sachinraja

Copy link
Copy Markdown
Member Author

Thanks for reviewing! As a side note, this is much more than just CF Workers. Deno (Deploy), SvelteKit, Astro, Next.js middleware, Remix, and others are also using the Fetch API.

@github-actions

github-actions Bot commented Oct 6, 2022

Copy link
Copy Markdown
Contributor

This pull request has been locked because it had no new activity for 30 days. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

This branch was successfully deployed

3 active deployments
Preview – todomvc — 17ed6573 Deployed May 1, 2022 by vercel[bot]
Preview – next-prisma-starter — 17ed6573 Deployed May 1, 2022 by vercel[bot]
Preview – www — 17ed6573 Deployed May 1, 2022 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decouple @trpc/server from node Feature request: Add Fetch API adapter aka "Web Platform support"

3 participants