carsonem
05/21/2023, 9:24 PMimport { Miniflare } from 'miniflare';
const mf = new Miniflare({
modules: true,
durableObjects: { TEST_OBJECT: 'TestObject' },
script: `
export class TestObject {
constructor(state) {
this.storage = state.storage;
}
async fetch(request) {
const url = new URL(request.url);
if(url.pathname === "/put") await this.storage.put("key", 1);
return new Response((await this.storage.get("key")).toString());
}
}
export default {
async fetch(request, env) {
const stub = env.TEST_OBJECT.get(env.TEST_OBJECT.idFromName("test"));
return stub.fetch(request);
}
}
`
});
let res = await mf.dispatchFetch('http://localhost:8787/put');
console.log(await res.text()); // "1"