From 2c06a00ff122feafcba192fcf646800d82d69d61 Mon Sep 17 00:00:00 2001 From: maslow Date: Mon, 4 Dec 2023 11:58:55 +0800 Subject: [PATCH] doc: deprecated the interceptor fn without next method (#1720) * doc: deprecated the interceptor fn without next method * chore: fix dead-link check --- docs/.vitepress/config.js | 17 +------- docs/guide/function/call-function.md | 54 ----------------------- docs/guide/function/interceptor.md | 65 +++++++++++++--------------- docs/guide/index.md | 2 +- 4 files changed, 32 insertions(+), 106 deletions(-) delete mode 100644 docs/guide/function/call-function.md diff --git a/docs/.vitepress/config.js b/docs/.vitepress/config.js index b56c25b41b..0f398e8f99 100644 --- a/docs/.vitepress/config.js +++ b/docs/.vitepress/config.js @@ -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: "函数市场", diff --git a/docs/guide/function/call-function.md b/docs/guide/function/call-function.md deleted file mode 100644 index ba004619cd..0000000000 --- a/docs/guide/function/call-function.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -title: 在云函数中调用 ---- - -# {{ $frontmatter.title }} - -云函数在开发完毕并发布后,可以在其他云函数中进行调用。 - -::: info -`cloud.invoke`方法已不推荐使用,可使用[引入云函数](/guide/function/use-function.html#云函数引入) -::: - -## 编写并发布待调用云函数 - -比如,我们创建一个名为 `get-user-info` 的云函数,并编写如下代码: - -```typescript -import cloud from '@lafjs/cloud' - -const db = cloud.database() - -export async function main(ctx: FunctionContext) { - - const { body: { userid } } = ctx - if (!userid) return { err: 1, errmsg: 'userid not exists' } - - const userCollection = db.collection('user') - const { data: user } = await userCollection.where({ id: userid }).get() - - if (!user) return { err: 2, errmsg: 'user not found' } - return { err: 0, data: user } -} -``` - -该函数接收一个名为 userid 的参数,并通过 id 在数据库中查找相应用户,并将 查找到的数据返回。 - -## 调用已发布云函数 - -`get-user-info` 云函数发布后,我们可以在其他云函数调用该函数。 - -```typescript -import cloud from '@lafjs/cloud' - -export async function main(ctx: FunctionContext) { - const res = await cloud.invoke('get-user-info', { ...ctx, body: { userid: 'user id' }}) - console.log(res) -} -``` - -我们通过调用 `cloud.invoke` 方法可以在云函数中调用其他云函数。 - -该方法接收两个参数:第一个参数为 待调用的函数名,第二个参数为传递的数据 - -可以看到,`body` 传入了我们请求参数,如果函数内部需要使用 ctx 中的某些属性,还可用 `...ctx` 的方式传入 ctx 以便被调用函数使用。 diff --git a/docs/guide/function/interceptor.md b/docs/guide/function/interceptor.md index 2c0f423b72..e075b9913c 100644 --- a/docs/guide/function/interceptor.md +++ b/docs/guide/function/interceptor.md @@ -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' +
-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` 分别表示是否继续执行云函数。 diff --git a/docs/guide/index.md b/docs/guide/index.md index ff743c0704..f2b9908b7f 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -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) ## 🖥 在线体验