Skip to content

Commit

Permalink
doc: update function docs
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Dec 5, 2023
1 parent 2c06a00 commit bd3bb48
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 45 deletions.
11 changes: 2 additions & 9 deletions docs/guide/function/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ title: 在云函数中使用环境变量

环境变量添加完成后,即可在任意云函数中通过访问 `process.env` 来使用。

:::tip
`cloud.env` 已废弃
:::

:::tip
`APPID` 是系统环境变量的保留字,不可覆盖。
:::
Expand All @@ -26,10 +22,7 @@ title: 在云函数中使用环境变量
```typescript
import cloud from '@lafjs/cloud'

export async function main(ctx: FunctionContext) {

const env = process.env
console.log(env)
// 所有的环境变量都在 process.env 里面
export async function main(ctx: FunctionContext) {
console.log(process.env)
}
```
4 changes: 2 additions & 2 deletions docs/guide/function/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ function verifySignature(signature, timestamp, nonce, token) {

// 返回组装 xml
function toXML(payload, content) {
const timestamp = Date.now();
const { tousername: fromUserName, fromusername: toUserName } = payload;
const timestamp = Date.now()
const { tousername: fromUserName, fromusername: toUserName } = payload
return `
<xml>
<ToUserName><![CDATA[${toUserName}]]></ToUserName>
Expand Down
37 changes: 4 additions & 33 deletions docs/guide/function/function-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ import cloud from "@lafjs/cloud";
| --------------- | ----------------------------------------------------------------------------------- |
| `cloud.appid` | 当前 Laf 应用的 appid |
| `cloud.database()` | 当前应用的数据库对象 |
| `cloud.env` | 当前应用的环境变量,也可用 `process.env` |
| `cloud.fetch` | 可在云函数中发起 HTTP 请求,基于`axios`封装 |
| `cloud.getToken` | 生成 JWT Token |
| `cloud.parseToken` | 解密 JWT Token |
| `cloud.invoke` | 请求其他云函数 |
| `cloud.shared` | 当前应用的全局缓存 |
| `cloud.mongo` | 当前应用的原生 MongoDB 实例 |
| `cloud.sockets` | 当前应用的socket实例|
| `cloud.sockets` | 当前应用的所有 socket 连接|

## 发送网络请求

Expand Down Expand Up @@ -91,41 +89,13 @@ export async function main(ctx: FunctionContext) {
};
```

## 调用其他云函数

通过`cloud.invoke()` 调用本应用内的其他云函数。

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

```typescript
import cloud from '@lafjs/cloud'

export async function main(ctx: FunctionContext) {
// 调用 hello 云函数
await cloud.invoke('hello')
}
```

如果调用的云函数需要用到 ctx 里面的东西,我们可以通过这样的方式传入。

```typescript
import cloud from '@lafjs/cloud'

export async function main(ctx: FunctionContext) {
// 调用云函数 hello 并传入 ctx
await cloud.invoke('hello',ctx)
}
```

## 云函数鉴权

云函数鉴权可以使用 JWT Token,下方是生成和解密 JWT token 的方法

```typescript
cloud.getToken(payload); // payload 可参考下方的示例代码
cloud.parseToken(token); // token 为前端请求时 header 里的 authorization 中的 token
cloud.getToken(payload) // payload 可参考下方的示例代码
cloud.parseToken(token) // token 为前端请求时 header 里的 authorization 中的 token
```

以下实现简单的生成和解密 JWT token
Expand All @@ -142,6 +112,7 @@ export async function main(ctx: FunctionContext) {
// 生成 access_token
const access_token = cloud.getToken(payload);
console.log("云函数生成的 token:", access_token)

// ctx.user 会自动解密
console.log(ctx.user)
const authHeader = ctx.headers.authorization;
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/laf-assistant/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: laf assistant
title: laf assistant (非官方维护)
---

# {{ $frontmatter.title }}
Expand Down

0 comments on commit bd3bb48

Please sign in to comment.