import {
error, // creates error responses
json, // creates JSON responses
Router, // the ~440 byte router itself
withParams, // middleware: puts params directly on the Request
} from 'itty-router'
import { withDurables } from 'itty-durable'
// export the durable class, per spec
export { Posts } from './Posts'
const router = Router({ base: '' })
router
// add upstream middleware, allowing Durable access off the request
.all('*', withDurables())
// get the durable itself... returns json response, so no need to wrap
.get('/', ({ Posts }) => Posts.get('test').toJSON())
// By using { autoReturn: true } in createDurable(), this method returns the contents
.get('/increment', ({ Posts }) => Posts.get('test').increment())
// you can pass any serializable params to a method... (e.g. /counter/add/3/4 => 7)
.get('/add/:a?/:b?', withParams,
({ Posts, a, b }) => Posts.get('test').add(Number(a), Number(b))
)
// reset the durable
.get('/reset', ({ Posts }) => Posts.get('test').reset())
// 404 for everything else
.all('*', () => missing('Are you sure about that?'))
// with itty, and using ES6 module syntax (required for DO), this is all you need
// Example: Cloudflare Worker module syntax
export default {
fetch: (request, ...args) =>
router
.handle(request, ...args)
.then(json) // send as JSON
.catch(error), // catch errors
}
import {
error, // creates error responses
json, // creates JSON responses
Router, // the ~440 byte router itself
withParams, // middleware: puts params directly on the Request
} from 'itty-router'
import { withDurables } from 'itty-durable'
// export the durable class, per spec
export { Posts } from './Posts'
const router = Router({ base: '' })
router
// add upstream middleware, allowing Durable access off the request
.all('*', withDurables())
// get the durable itself... returns json response, so no need to wrap
.get('/', ({ Posts }) => Posts.get('test').toJSON())
// By using { autoReturn: true } in createDurable(), this method returns the contents
.get('/increment', ({ Posts }) => Posts.get('test').increment())
// you can pass any serializable params to a method... (e.g. /counter/add/3/4 => 7)
.get('/add/:a?/:b?', withParams,
({ Posts, a, b }) => Posts.get('test').add(Number(a), Number(b))
)
// reset the durable
.get('/reset', ({ Posts }) => Posts.get('test').reset())
// 404 for everything else
.all('*', () => missing('Are you sure about that?'))
// with itty, and using ES6 module syntax (required for DO), this is all you need
// Example: Cloudflare Worker module syntax
export default {
fetch: (request, ...args) =>
router
.handle(request, ...args)
.then(json) // send as JSON
.catch(error), // catch errors
}