跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

绑定 API

最后更新 查看 MarkdownAgent 设置

Workers 通过您添加到 Wrangler 配置文件中的绑定来访问 Flagship。binding 字段设置您在 Worker 代码中使用的变量名称。

{
    "flagship": [
        {
            "binding": "FLAGS",
            "app_id": "<APP_ID>",
        },
    ],
}
[[flagship]]
binding = "FLAGS"
app_id = "<APP_ID>"

将 <APP_ID> 替换为您 Flagship 应用程序中的应用程序 ID。如果您还没有创建应用程序,请参阅入门指南。通过此配置,该绑定可作为 env.FLAGS 使用。有关绑定到多个应用程序等附加选项,请参阅配置。

该绑定提供了类型安全的评估功能标志的方法。如果评估失败或找不到标志,该方法将返回您提供的默认值。

export default {
	async fetch(request, env) {
		const enabled = await env.FLAGS.getBooleanValue("new-feature", false, {
			userId: "user-42",
			profile: { plan: "enterprise" },
			tags: ["beta"],
		});
		return new Response(enabled ? "Feature on" : "Feature off");
	},
};
export default {
    async fetch(request: Request, env: Env): Promise<Response> {
        const enabled = await env.FLAGS.getBooleanValue("new-feature", false, {
            userId: "user-42",
            profile: { plan: "enterprise" },
            tags: ["beta"],
        });
        return new Response(enabled ? "Feature on" : "Feature off");
    },
};

此绑定具有来自 @cloudflare/workers-types 包的 Flagship 类型。

这篇文档对您有帮助吗?