# tcb-server-sdk

> 云开发云函数轻量级类路由库，主要用于优化服务端函数处理逻辑

Latest version **1.1.3** (published 2024-09-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install tcb-server-sdk
pnpm add tcb-server-sdk
yarn add tcb-server-sdk
bun add tcb-server-sdk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2024-09-15 |
| First published | 2021-12-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 20.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | showms |
| Maintainers | showms |
| Keywords | 微信小程序云开发, 云函数路由, 微信 |

## Links

- npm: https://www.npmjs.com/package/tcb-server-sdk
- npm.io page: https://npm.io/package/tcb-server-sdk

## Dependencies (1)

- [wx-server-inherit-sdk](https://npm.io/package/wx-server-inherit-sdk.md) ^1.0.5

## Recent versions

- 1.1.3 (latest) — 2024-09-15
- 1.1.2 — 2024-09-15
- 1.1.1 — 2023-06-29
- 1.1.0 — 2023-06-28
- 1.0.9 — 2023-06-28
- 1.0.8 — 2022-10-30
- 1.0.7 — 2021-12-21
- 1.0.6 — 2021-12-21
- 1.0.5 — 2021-12-20
- 1.0.4 — 2021-12-16
- 1.0.3 — 2021-12-15
- 1.0.2 — 2021-12-15
- 1.0.1 — 2021-12-15

## README

# tcb-server-sdk

> 云开发云函数轻量级类路由库，主要用于优化服务端函数处理逻辑



##### 说明：

- 被路由的处理类要求继承RouteBase
- 云函数入口使用RouteHolder初始化路由对象，允许指定固定前缀
- RouteBase会自动使用wx-server-sdk初始化云环境

##### 其中：

- RouteBase还会初始化一些云函数所需的操作对象



## 示例

#### 云函数端

##### 目录结构

```groovy
\--- functions
	 +--- routeTest
	 	  +---- service
		  |		+---- user
		  |			  \---- Name.js
		  |		\---- Test.js
		  \---- index.js
		  \---- package.json
		  \---- package-lock.json
```



##### 入口文件

```javascript
const route = require("./tcb-server-sdk");

// 云函数入口函数
exports.main = async (event, context) => {

    console.log("入参：", event);
    //根据目录结构可以指定固定前缀为service
    //小程序端就可以使用user/Name/get访问到Name处理类里的get方法
    //如果不指定固定前缀，则需要使用service/user/Name/get才能访问到Name处理类里的get方法
    return await new route.RouteHolder("service").process(event);
};
```



##### 请求处理类 Name.js

```javascript
const route = require("./tcb-server-sdk");

class Name extends route.RouteBase {
    async index(params) {
    	console.log("这个是index方法：", params);
        return {};
    }
    
    async get(params) {
        console.log("接口请求参数：", params);
        //这里的this.wxContext来自RouteBase
        return {"idol": "kobe", "wxContext": this.wxContext};
    }
}

module.exports = Name;
```



##### 小程序端

```javascript
//执行云函数
wx.cloud.callFunction({
    // 云函数名称
    name: "routeTest",
    // 传给云函数的参数
    data: {
        env: "test",// 环境可以不填，不填就使用当前环境  
        uri: "user/Name/get",//请求URI，云函数端的RouteHolder会找到对应的处理类并调用指定方法。这里会调用Name类的get方法
        //uri: "user/Name",//这种方式会默认调用Name类的index方法
        params: {"source":"1"}
    }
}).then(res => {

}).catch(res => {

}).then(res => {

});
```



开源链接：

https://github.com/showms/tcb-server-sdk.git

---
_Source: https://npm.io/package/tcb-server-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
