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 1 commit
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
}
}
```

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

# {{ $frontmatter.title }}

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

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

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

```ts
export async function main(ctx: FunctionContext) {

Expand All @@ -24,9 +26,10 @@ export async function main(ctx: FunctionContext) {
}
```

更多用法请参考: https://github.com/websockets/ws
更多用法请参考: <https://github.com/websockets/ws>

## 客户端 WebSocket 连接

```ts
const wss = new WebSocket("wss://your-own-appid.laf.run/__websocket__");

Expand All @@ -43,4 +46,4 @@ wss.onmessage = (res) => {
wss.onclose = () => {
console.log("closed");
};
```
```