laf/docs/guide/function/interceptor.md
nightwhite 8443c015d0
doc: add document for the interceptor (#1051)
Co-authored-by: maslow <wangfugen@126.com>
2023-04-20 13:05:26 +08:00

761 B
Raw Blame History

title
云函数拦截器

{{ $frontmatter.title }}

如果需要使用拦截器,需要创建一个云函数并且命名为 __interceptor__的云函数。

::: info __interceptor__ 为固定命名 :::

Laf云函数拦截器是在所有的云函数请求之前被请求故而也可以叫做前置拦截器。

只有拦截器的返回值为 true ,才会去请求的原本的云函数

下面是一个简单的拦截器示例,如果IP是111.111.111.111,则可以继续访问原本的云函数

export async function main(ctx: FunctionContext) {
  // 获取请求的实际IP
  const ip = ctx.headers['x-real-ip']
  if(ip == '111.111.111.111'){
    return true
  }else{
    return false
  }
}

更多用途可自由发挥!