功能配置
Configure individual function behavior. Customize authentication, dependencies, and other settings per function.
配置 #
🌐 Configuration
默认情况下,你所有的 Edge Functions 都有相同的设置。不过在实际应用中,你可能需要不同函数之间有不同的行为。
🌐 By default, all your Edge Functions have the same settings. In real applications, however, you might need different behaviors between functions.
例如:
🌐 For example:
- Stripe 网络钩子 需要可以公开访问(Stripe 没有你的用户令牌)
- 用户资料 API 应该需要身份验证
- 有些功能 可能需要特殊的依赖或不同的文件类型
要启用这些每个函数的规则,在你的项目根目录下创建 supabase/config.toml:
🌐 To enable these per-function rules, create supabase/config.toml in your project root:
1# Disables authentication for the Stripe webhook.2[functions.stripe-webhook]3verify_jwt = false45# Custom dependencies for this specific function6[functions.image-processor]7import_map = './functions/image-processor/import_map.json'89# Custom entrypoint for legacy function using JavaScript10[functions.legacy-processor]11entrypoint = './functions/legacy-processor/index.js这个配置告诉 Supabase,stripe-webhook 函数不需要有效的 JWT,image-processor 函数使用自定义导入映射,而 legacy-processor 使用自定义入口点。
🌐 This configuration tell Supabase that the stripe-webhook function doesn't require a valid JWT, the image-processor function uses a custom import map, and legacy-processor uses a custom entrypoint.
你只需设定这些规则一次,以后就不用再担心它们了。部署你的功能时,可以放心,它们的安全性和行为完全符合每个端点的需求。
🌐 You set these rules once and never worry about them again. Deploy your functions knowing that the security and behavior is exactly what each endpoint needs.
想查看更多通用的 config.toml 选项,请查看本指南。
🌐 To see more general config.toml options, check out this guide.
跳过授权检查 #
🌐 Skipping authorization checks
默认情况下,Edge Functions 需要在授权头中有一个有效的 JWT。如果你想在不进行授权检查的情况下使用 Edge Functions(通常用于 Stripe webhooks),你可以在你的 config.toml 中进行配置:
🌐 By default, Edge Functions require a valid JWT in the authorization header. If you want to use Edge Functions without Authorization checks (commonly used for Stripe webhooks), you can configure this in your config.toml:
1[functions.stripe-webhook]2verify_jwt = false你也可以在本地运行 Edge 函数时传入 --no-verify-jwt 标志:
🌐 You can also pass the --no-verify-jwt flag when serving your Edge Functions locally:
1supabase functions serve hello-world --no-verify-jwt使用这个标志时要小心,因为它会允许任何人在没有有效 JWT 的情况下调用你的 Edge Function。Supabase 客户端库会自动处理授权。
🌐 Be careful when using this flag, as it will allow anyone to invoke your Edge Function without a valid JWT. The Supabase client libraries automatically handle authorization.
自定义入口点 #
🌐 Custom entrypoints
entrypoint 仅在 Supabase CLI 版本 1.215.0 或更高版本中可用。
当你创建一个新的 Edge 函数时,它默认会使用 TypeScript。不过,你也可以使用纯 JavaScript 来编写和部署 Edge 函数。
🌐 When you create a new Edge Function, it will use TypeScript by default. However, it is possible to write and deploy Edge Functions using pure JavaScript.
将你的函数保存为一个 JavaScript 文件(例如 index.js),然后更新 supabase/config.toml:
🌐 Save your Function as a JavaScript file (e.g. index.js) update the supabase/config.toml :
1[functions.hello-world]2entrypoint = './index.js' # path must be relative to config.toml你可以使用任何 .ts、.js、.tsx、.jsx 或 .mjs 文件作为函数的入口点。
🌐 You can use any .ts, .js, .tsx, .jsx or .mjs file as the entrypoint for a Function.