kwhitley / itty-router

A little router.
MIT License
1.75k stars 78 forks source link

[Question] How to bundle with ES6 Module Syntax #44

Closed TorbjornHoltmon closed 3 years ago

TorbjornHoltmon commented 3 years ago

Hi!

Your router is absolutely amazing, thank you so much for taking your time to create it.

I have built an API and I am trying to change my router to use the ES6 Modules syntax. I am using esbuild to bundle my api and it works fine when using addEventListener, which makes sense.

But I can't get it working from your ES6 example, am i supposed to use the exported handler in some way?

I setup a super simple repo to demo: https://github.com/TorbjornHoltmon/itty-es6

TorbjornHoltmon commented 3 years ago

After looking into it I realize I don't need env, as I can access KV from anywhere. Would still be useful to know what I am missing though, do I need the full class described in the Cloudflare docs?

kwhitley commented 3 years ago

Hey man! Thanks for the kind words! Looks like your issue is a lack of KV binding in your wrangler.toml - should have something that looks like this:

kv-namespaces = [
  { binding = "myBinding", id = "5a55bc46fc33488095dff4cc1929dd" },
]

Then, I believe you can just access it via env.myBinding in your handler :)

TorbjornHoltmon commented 3 years ago

Thanks! I got it working 🚀

I am currently writing a implementation with tsoa using your router, to hopefully convince some enterprise clients to use Cloudflare workers. It kinda bloats your incredibly tiny package, but its coming along nicely.

kwhitley commented 3 years ago

Sounds cool! Honestly that was one of the fun benefits of code-golfing this thing down so small... people can use it inside their own feature-rich routers as the core routing bit only. Definitely glad folks like you are helping evangelize CF Workers... I'm in the same boat - when I saw what they could do, I instantly bought stock... they're going to blow apart how we create serverless apps! :)