doc: add document for the interceptor (#1051)

Co-authored-by: maslow <wangfugen@126.com>
This commit is contained in:
nightwhite 2023-04-20 13:05:26 +08:00 committed by GitHub
parent 9ada3d2bb2
commit 8443c015d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 13 deletions

View File

@ -27,7 +27,7 @@ const guideSiderbarConfig = [
{
text: "概览",
link: "/guide/",
}
},
],
},
{
@ -87,6 +87,10 @@ const guideSiderbarConfig = [
text: "WebSocket 连接",
link: "/guide/function/websocket",
},
{
text: "拦截器",
link: "/guide/function/interceptor",
},
],
},
{
@ -172,14 +176,13 @@ const examplesSideBarConfig = [
text: "使用 SMTP 服务发送邮件",
link: "/examples/send-mail",
},
]
}, {
text: '前端应用',
items: [
{ text: 'Todo List', link: '/examples/todo-list', },
]
}
]
],
},
{
text: "前端应用",
items: [{ text: "Todo List", link: "/examples/todo-list" }],
},
];
export default defineConfig({
lang: "zh-CN",

View File

@ -0,0 +1,31 @@
---
title: 云函数拦截器
---
# {{ $frontmatter.title }}
如果需要使用拦截器,需要创建一个云函数并且命名为 `__interceptor__`的云函数。
::: info
`__interceptor__` 为固定命名
:::
Laf云函数拦截器是在所有的云函数请求之前被请求故而也可以叫做前置拦截器。
只有拦截器的返回值为 `true` ,才会去请求的原本的云函数
下面是一个简单的拦截器示例,如果IP是`111.111.111.111`,则可以继续访问原本的云函数
```ts
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
}
}
```
更多用途可自由发挥!

View File

@ -6,11 +6,8 @@ title: 云函数处理 WebSocket 长连接
## 特殊函数名 __websocket__
如果需要使用 WebSocket 需要创建一个云函数并且命名为 `__websocket__`这个云函数并不会运行之后销毁,会一直存在,专为 WebSocket 存在的云函数!
如果需要使用 WebSocket 需要创建一个云函数并且命名为 `__websocket__`,专为 WebSocket 存在的云函数!
::: info
`__websocket__` 为固定名称,仅有此云函数不会被销毁,会一直存在,专为 WebSocket 存在的云函数
:::
以下是云函数中处理 WebSocket 示例:
@ -51,6 +48,7 @@ wss.onclose = () => {
};
```
## 连接演示
1、替换 `__websocket__` 代码并发布