Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add document for the interceptor #1051

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const guideSiderbarConfig = [
{
text: "概览",
link: "/guide/",
}
},
],
},
{
Expand Down Expand Up @@ -87,6 +87,10 @@ const guideSiderbarConfig = [
text: "WebSocket 连接",
link: "/guide/function/websocket",
},
{
text: "拦截器",
link: "/guide/function/interceptor",
},
],
},
{
Expand Down Expand Up @@ -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",
Expand Down
31 changes: 31 additions & 0 deletions docs/guide/function/interceptor.md
Original file line number Diff line number Diff line change
@@ -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
}
}
```

更多用途可自由发挥!
6 changes: 2 additions & 4 deletions docs/guide/function/websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ title: 云函数处理 WebSocket 长连接

## 特殊函数名 __websocket__

如果需要使用 WebSocket 需要创建一个云函数并且命名为 `__websocket__`,这个云函数并不会运行之后销毁,会一直存在,专为 WebSocket 存在的云函数!
如果需要使用 WebSocket 需要创建一个云函数并且命名为 `__websocket__`,专为 WebSocket 存在的云函数!

::: info
`__websocket__` 为固定名称,仅有此云函数不会被销毁,会一直存在,专为 WebSocket 存在的云函数
:::

以下是云函数中处理 WebSocket 示例:

Expand Down Expand Up @@ -51,6 +48,7 @@ wss.onclose = () => {
};
```


## 连接演示

1、替换 `__websocket__` 代码并发布
Expand Down