Tobias Lins
12/19/2020, 9:18 PMTill
12/29/2020, 3:10 AMTill
12/29/2020, 3:10 AMTill
12/29/2020, 3:10 AMcatgirl
12/29/2020, 4:17 AMcatgirl
12/29/2020, 4:17 AMcatgirl
12/29/2020, 4:18 AMcatgirl
12/29/2020, 4:18 AMcatgirl
12/29/2020, 4:21 AMcatgirl
12/29/2020, 4:21 AMTill
12/29/2020, 7:24 AMkenton
12/29/2020, 3:08 PMkenton
12/29/2020, 3:08 PMTill
12/29/2020, 7:17 PMTill
12/29/2020, 7:17 PMTill
12/29/2020, 7:19 PMfetch()
method on the durable object class. Does it have to be a Request
or can I just pass any kind of string/identifier? I'm confused by the docs.Till
12/29/2020, 8:06 PMthis.storage.put();
store objects, or only text?connyay
12/29/2020, 8:11 PMkenton
12/29/2020, 8:27 PMfetch()
is explicitly HTTP-based. On the calling side it has exactly the signature of the global fetch()
function. On the receiving end, the parameter is always a Request
and it must return a Response
.kenton
12/29/2020, 8:28 PMstorage.put()
supports any value that is supported by structured clone. That should be a strict superset of things that you could JSON-stringify, FWIW.kenton
12/29/2020, 8:28 PMTill
12/29/2020, 8:58 PMTill
12/29/2020, 8:59 PMexport class Plugin {
constructor(state, env) {
this.storage = state.storage
}
async initialize() {
let stored = await this.storage.get('info')
this.value = stored || null
}
async fetch(request) {
if (! this.shouldInitialize) {
this.shouldInitialize = this.initialize()
}
await this.shouldInitialize
let currentValue = this.value
if (request.method === 'POST') {
currentValue = this.value = await request.json()
await this.storage.put('info', this.value)
}
return new Response(JSON.stringify(currentValue), {
headers: { 'Content-Type': 'application/json; charset=utf-8' },
})
}
}
Till
12/29/2020, 9:00 PMTill
12/29/2020, 9:00 PM.mjs
file as well as the metadata.json
and calling-worker.json
?Till
12/29/2020, 9:01 PMkenton
12/29/2020, 9:03 PMkenton
12/29/2020, 9:03 PMkenton
12/29/2020, 9:04 PMkenton
12/29/2020, 9:04 PM