kwhitley / itty-router

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

Use in durable objects?? #69

Closed danboyle8637 closed 2 years ago

danboyle8637 commented 2 years ago

I saw an interview with you and you mentioned you were using itty router in a durable object class.

How?

I can't seem to get it to work... but I'm also a little brain dead at this point with dealing with all the DO issues. haha... thanks for any insight.

g45t345rt commented 2 years ago

A DurableObject has a fetch with the request as parameter. I assume you would use it the same way as with workers.

import { Router } from 'itty-router'

class YourClass {
  async fetch(request) {
    const router = Router()
    router.get('*', () => new Response('Hello'))
    return router.handle(request)
  }
}
kwhitley commented 2 years ago

You're basically on the right track!

Check out the code behind itty-durable to see what I did, but basically I embed a router (itty) into the durable, then intercept fetch requests.

Closing the issue to avoid getting dinged, but feel free to continue the dialog here! :)

g45t345rt commented 2 years ago

@kwhitley did not know about itty-durable that's great!!