I'm trying to store and retrieve private data within my hooks by setting install.private
as described here but subsequent hook requests do not include the install.private
property.
Using the below App install.json
and Worker index.js
, I'm testing this as follows:
- I click the Set the Private Value checkbox
- Hook sends
"option-change:testSetPrivate"
request to Worker - Worker sets
install.private = { value: install.options.privateIn }
in response - I click the Retrieve the Private Value checkbox
- Hook send
"option-change:testGetPrivate"
request to Worker - Worker attempts to access
install.private.value
but throws the exceptionCannot read property 'value' of undefined
Here's what the resulting UI looks like:
App install.json
{
"$schema": "http://json.schemastore.org/install",
"resources": {
"head": [],
"body": []
},
"dns": [],
"preview": {
"handlers": [
{
"options": ["_default"],
"execute": "INSTALL_SCOPE.setOptions(INSTALL_OPTIONS)"
}
]
},
"hooks": [
{
"endpoint": "...",
"events": ["option-change:testSetPrivate"],
"block": true,
"failure": {
"action": "failure",
"message": "Something went wrong"
}
},
{
"endpoint": "...",
"events": ["option-change:testGetPrivate"],
"block": true,
"failure": {
"action": "failure",
"message": "Something went wrong."
}
}
],
"options": {
"properties": {
"privateIn": {
"order": 1,
"title": "The Private Value to Set",
"type": "string",
"default": "this is private"
},
"testSetPrivate": {
"order": 2,
"title": "Set the Private Value",
"type": "boolean",
"default": false
},
"testGetPrivate": {
"order": 3,
"title": "Retrieve the Private Value",
"type": "boolean",
"default": false
},
"privateOut": {
"order": 4,
"title": "The Private Value we Got",
"type": "string"
}
}
}
}
Worker index.js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest (request) {
const data = await request.json()
let install = data.install
let errors = []
if (data.event === "option-change:testSetPrivate") {
// Write privateIn to install.private
install.private = {
value: install.options.privateIn
}
} else if (data.event === "option-change:testGetPrivate") {
// Read from install.private and write to install.options.privateOut
try {
install.options.privateOut = install.private.value
} catch (e) {
errors.push({type: "unknown", message: e.message})
}
} else {
throw `Unhandled event: ${data.event}`
}
const responseData = {
proceed: errors.length === 0,
errors,
install,
}
return new Response(JSON.stringify(responseData), {
status: responseData.proceed ? 200 : 400,
headers: { 'content-type': 'application/json' },
})
}