1

我正在尝试通过按照此处install.private描述的设置在我的挂钩中存储和检索私有数据,但随后的挂钩请求不包括该属性。install.private

使用下面的 Appinstall.json和 Worker index.js,我正在测试如下:

  1. 我单击设置私有值复选框
  2. Hook 向"option-change:testSetPrivate"Worker 发送请求
  3. 工人设置install.private = { value: install.options.privateIn }响应
  4. 我单击检索私有值复选框
  5. Hook 向"option-change:testGetPrivate"Worker 发送请求
  6. Worker 尝试访问install.private.value但抛出异常Cannot read property 'value' of undefined

生成的 UI 如下所示:

应用程序创建者用户界面截图

应用安装.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"
      }
    }
  }
}

工人 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' },
  })
}
4

0 回答 0