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: improved cloud function related documents #1050

Merged
merged 1 commit 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
Binary file modified docs/doc-images/edit-cloudfunction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/doc-images/function-body.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/doc-images/function-query.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/doc-images/getToken-parseToken.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions docs/guide/function/call-function-in-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: 在客户端中调用

# {{ $frontmatter.title }}

云函数编写完成并发布后,客户端可通过 `sdk` 的方式进行调用
云函数编写完成并发布后,客户端可通过 `laf-client-sdk` 进行调用

::: info
目前 SDK 暂时只支持发送 POST 请求
Expand All @@ -16,7 +16,8 @@ title: 在客户端中调用
npm i laf-client-sdk
```

## 初始化 `cloud` 对象:
## 初始化 `cloud` 对象

```typescript
import { Cloud } from "laf-client-sdk";

Expand All @@ -37,4 +38,6 @@ console.log(res) // 这里的 res 是云函数中 return 的内容

怎么样,是不是很方便, 只需简单的配置和一行代码即可实现对云函数的调用。

## laf-client-sdk 详细文档

查看详细文档:[client-sdk](/guide/client-sdk/)
18 changes: 9 additions & 9 deletions docs/guide/function/call-function-in-http.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# HTTP 调用
# HTTP 调用

云函数每个云函数都提供了 API 地址,理论上我们可以在任何能发起 http 请求的地方调用云函数。

![](../../doc-images/function-url.png)
![function-url](../../doc-images/function-url.png)

下面是用 axios 请求云函数的简单示例。
下面是用前端使用 `axios` 请求云函数的简单示例。

```ts
const { data } = await axios({
url: "<FunctionURL>",
method: "get",
});
const { data } = await axios({
url: "<FunctionURL>",
method: "get",
});

console.log(data);
```
console.log(data);
```
14 changes: 5 additions & 9 deletions docs/guide/function/call-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ title: 在云函数中调用

云函数在开发完毕并发布后,可以在其他云函数中进行调用。


## 编写并发布待调用云函数

比如,我们创建一个名为 `get-user-info` 的云函数,并编写如下代码:
Expand All @@ -22,37 +21,34 @@ export async function main(ctx: FunctionContext) {
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在数据库中查找相应用户,并将 查找到的数据返回。


## 调用已发布云函数

::: info
`cloud.invoke`方法已不推荐使用,可使用[引入云函数](/guide/function/use-function.html#云函数引入)
:::

`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以便被调用函数使用。
可以看到,`body` 传入了我们请求参数,如果函数内部需要使用ctx中的某些属性,还可用 `...ctx` 的方式传入ctx以便被调用函数使用。
6 changes: 2 additions & 4 deletions docs/guide/function/depend.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ title: 依赖管理

![](/doc-images/package-list.png)


安装完成后用户可在界面左下方 `依赖管理` 中查看已安装的依赖和版本。

## 版本
Expand All @@ -27,10 +26,9 @@ title: 依赖管理

添加依赖时,默认勾选为最新版`latest`,用户如需指定版本,可在安装时在版本下拉框中选择对应版本。


## 云函数使用

安装完成后,即可在云函数中引入并使用。例如,创建一个云函数 `hello-moment`,并修改代码如下:
安装完成后,即可在云函数中引入并使用。例如,创建一个云函数 `hello-moment`,并修改代码如下:

```typescript
import cloud from '@lafjs/cloud'
Expand All @@ -53,4 +51,4 @@ export async function main(ctx: FunctionContext) {
"now": "2023-02-08 02:14:05",
"twoHoursLater": "2023-02-08 04:14:05"
}
```
```
42 changes: 30 additions & 12 deletions docs/guide/function/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,54 @@ title: 云函数入门

# {{ $frontmatter.title }}

云函数是运行在云端的 JavaScript 代码。
## 云函数简介

云函数可使用 Typescript 编写,无需管理服务器,在开发控制台在线编写、在线调试、一键保存即可运行后端代码
`Laf云函数`是运行在云端的 `JavaScript` 代码

在你的应用中,大多数数据的获取都可在客户端直接操作数据库,但是通常业务中会使用到「非数据库操作」,如注册、登录、文件操作、事务、第三方接口等,可直接使用云函数实现。
云函数可使用 `Typescript/JavaScript` 编写,无需管理服务器,在Web开发控制台在线编写、在线调试、一键保存即可运行后端代码。

可在无需购买和管理服务器的情况下,快速开发后端代码。并且自带数据库和对象存储,极大降低后端开发难度。

每个云函数都是一个单独的 `Typescript` 文件,Laf为云函数单独封装了 `@lafjs/cloud` 模块,以便于更加方便的编写云函数。

## 创建云函数

点击页面左上角「函数」按钮,点击加号,添加云函数
创建并进入Laf应用后,点击页面左上角「函数」按钮,点击加号,添加云函数

![create-function](/doc-images/create-function.png)

**函数名** : 一般以字母或_开头,可使用`字母 数字 - _`四种,函数名不可重复

**标签** : 用来分类管理的,可通过标签名筛选云函数,可为每个云函数设置多个标签

![](/doc-images/create-function.png)
**请求方式** : 只有被勾选的请求方式才允许请求

**函数描述** : 方便后续查看云函数的功能,相当于备注

**函数模板** : 选择不同的函数模板可初始化不同的代码

## 编辑云函数

可直接在线编辑代码
Laf自带 `Web IDE`,可直接在浏览器在线编辑、运行(调试)、发布云函数

![](/doc-images/edit-cloudfunction.png)
![edit-cloudfunction](/doc-images/edit-cloudfunction.png)

## 运行云函数

云函数可直接运行调试,未发布的云函数也可以在此进行运行调试
云函数编写后可直接运行调试,未发布的云函数也可以在此进行运行调试

![run-cloudfunction](/doc-images/run-cloudfunction.png)

如在云函数中添加 `console.log` 打印日志的代码,运行后也会直接显示在Console控制台,同时也会将云函数的返回值打印在运行结果中。

![](/doc-images/run-cloudfunction.png)
可切换请求方式以及配置请求参数,默认是 `GET` 请求,此处显示的请求方式与勾选的请求方式有关。

## 发布云函数

云函数发布后,才可正式使用
云函数发布后,才是正式生效。前端才可以进行请求。

::: warning
云函数不会自动保存,发布后才会保存并生效
**云函数修改的代码,会自动在当前浏览器中缓存,只有在发布后才是真正的保存以及生效!**
:::

![](/doc-images/publish-cloudfunction.png)
![publish-cloudfunction](/doc-images/publish-cloudfunction.png)
Loading