Skip to content

Commit

Permalink
doc: deprecated the interceptor fn without next method (#1720)
Browse files Browse the repository at this point in the history
* doc: deprecated the interceptor fn without next method
* chore: fix dead-link check
  • Loading branch information
maslow committed Dec 4, 2023
1 parent be0cb05 commit 2c06a00
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 106 deletions.
17 changes: 2 additions & 15 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,8 @@ const guideSiderbarConfig = [
],
},
{
text: "调用云函数",
items: [
{
text: "在客户端中调用",
link: "/guide/function/call-function-in-client",
},
{
text: "在云函数中调用",
link: "/guide/function/call-function",
},
{
text: "HTTP 调用",
link: "/guide/function/call-function-in-http",
},
],
text: "HTTP 调用云函数",
link: "/guide/function/call-function-in-http",
},
{
text: "函数市场",
Expand Down
54 changes: 0 additions & 54 deletions docs/guide/function/call-function.md

This file was deleted.

65 changes: 29 additions & 36 deletions docs/guide/function/interceptor.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,46 @@ title: 云函数拦截器
`__interceptor__` 为固定命名,其他名称无效
:::

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

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

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


```typescript
export default async function(ctx: FunctionContext) {
// 获取请求的实际 IP
import cloud from '@lafjs/cloud'

export default async function (ctx: FunctionContext, next: Function) {
const ip = ctx.headers['x-forwarded-for']
if(ip === '111.111.111.111'){
return true
}else{
return false
}
if(ip === '111.111.111.111') {
ctx.response.status(403).send('Permission Denied')
return
}

// 继续执行云函数
return await next(ctx)
}
```

## 新版写法

```typescript
import cloud from '@lafjs/cloud'
<br />

export default async function (ctx: FunctionContext, next) {
let res = null

// 拦截逻辑
// ...
// 返回错误信息
// return {
// code: 400,
// data: e.message
// }

// 请求实际云函数
res = await next(ctx)
return {
code: 200,
data: res
}
}
### 旧版写法

// 兼容旧版写法
::: danger
DEPRECATED: 旧版用法已废弃,不推荐使用,仅作为兼容旧版的云函数。
:::

// import cloud from '@lafjs/cloud'

// export default async function (ctx: FunctionContext) {
// return true
// }
```typescript
export default async function(ctx: FunctionContext) {
const ip = ctx.headers['x-forwarded-for']
if(ip === '111.111.111.111'){
return true
}else{
return false
}
}
```

> 旧版本写法无需使用 `next` 参数,直接返回 `true``false` 分别表示是否继续执行云函数。
2 changes: 1 addition & 1 deletion docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ laf 是开源的云开发平台,提供云函数、云数据库、云存储等
## 🚀 Quick Start

[三分钟体验使用 laf 写一个自己的 ChatGPT (开发到上线)](https://icloudnative.io/posts/build-chatgpt-web-using-laf/)
[三分钟体验使用 laf 开发一个简单的「Todo List」](./docs/guide/quick-start/Todo.md)
[三分钟体验使用 laf 开发一个简单的「Todo List」](./quick-start/Todo.md)

## 🖥 在线体验

Expand Down

0 comments on commit 2c06a00

Please sign in to comment.