1.3.0 • Published 2 years ago

ts-api-core v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

TS-API-Core SDK

一、简介

SDK 是适用于 Node.js ,基于 express 的轻量级 API 框架内核。 主要特点是可以采用传统的基于 Controller 方式和特色的基于 RouteConfig 方式开发 API 接口。 而 RouteConfig 方式的 API 接口开发具有文档即接口的特点,也正是本 SDK 所推荐的方式。

ToDo

  • 基本框架
    • 常用工具类
      • Utils
      • UtilsDB
      • PackageLoader
      • 数据库日志基础类
      • MySQL 分布式锁
      • 数据字段驼峰转化 Helper
      • 数据模型转化 ModelHelper
      • 本地内存高速数据缓存 LocalCache
    • RouteConfig 引擎
      • Raw 请求引擎 Wrapper
      • 自定义 Wrapper 执行引擎
      • GraphQL 执行引擎
      • SQL 执行引擎
    • Contoller 引擎
    • 在线 api 接口文档
      • 文档输出
      • 在线查看
      • UI css 自定义
    • MySQL 集成
      • DBConnectionManager 数据库连接池管理 (支持多数据库的管理)
      • DBManager 数据库管理器
      • DBScript 工具类
      • Context 集成
      • Wrapper 集成
      • SQL 日志
    • 三方数据源集成
      • Redis 缓存 CacheAccess
        • get, set, remove, exist, getSet, search, setNX
      • Http 集成 HttpAccess
      • 阿里云 oss 集成 OssAccess
        • move, remove, upload, uploadStream, uploadUrlDownload, uploadBuffer, getFileList
      • RabbitMQ 消息队列集成 RabbitMQAccess
        • 订阅和消费
        • 消息发送
      • 其它
    • Web Server
    • 后台服务 BackupServer
    • MVC
      • 可配置的自动扫描加载
      • 依赖注入
      • Ioc容器
      • 参数校验
        • integer
        • length
        • mobile
        • pick
        • required
    • 日志模块
      • api 日志
      • 系统日志
      • SQL 脚本日志
    • JWT 集成
    • 在线接口管理后台
      • 登录
      • 动态管理接口
  • Demo
  • SDK 文档
  • 其它 ??

1.1 推荐的项目结构

doc 项目文档
src 源代码
    controller 接口控制器目录
        ...
    model 数据模型定义目录
        ...
    route 接口路由配置目录
        ...
    server 后台服务目录
        ...
    service 数据业务服务目录(数据库操作)
        ...
    wrapper 接口业务实现目录
        ...
    app.ts 应用入口
    config.ts 程序配置管理
    consts.ts 全局常量定义
Dockerfile
package.json
README.md
tsconfig.json
tsconfig.eslint.json

1.2 使用示例

app.ts

启用服务的示例

import { App } from 'ts-api-core'

App.initialize("TS WEB 服务", {
  server: {
    port: Number(process.env.EXPOSE_PORT ?? 8080),
    controller: [],
    allowHeader: ["token"],
    // logger: new MysqlApiLogger(),
    loadOpts: { loadDir: [ "./route", "./controller" ] }
  },
  entitys: [],
  config: {
    ossConfig: Config.ossConfig,
    dbConfig: Config.dbConfig,
    rabbitmqConfig: Config.rabbitmqConfig,
    // modelHelperConfig: new ModelHumpConfig(),
    apiTimeOut: Config.API_TIME_OUT,
    loadOpts: { loadDir: ["./wrapper"] }
  }
})

1.3 自定义 Route 请求类型

1.3.1 添加 Route 请求类型

/**
 * 自定义请求处理 Wrapper
 */
class TestCustomWrapper extends BaseRouteRawWrapper {
  async executeRequest(
    context: RequestContext
  ): Promise<WrapperResult<any | undefined>> {
    return new WrapperResult({
      query: context.req.query,
      body: context.req.body,
      params: context.req.params
    })
  }
}

/**
 * 自定义 Route 请求 Controller
 */
@Service('custom')
export class TestCustomController extends BaseRouteRawController {
  protected canExecuteWrapper(context: RequestContext): boolean {
    return true
  }

  protected getWrapper(context: RequestContext): RawWrapper {
    return new TestCustomeWrapper(context)
  }
}

1.3.2 使用示例

@Route("/demo")
export class CustomRouteConfig implements RouteConfig {
 name = '自定义请求类型测试';
 data(): any {
    return this.routers
 }
 
 private readonly routers: RequestData[] = [
 {
  name: "test",
  path: "/custom/req/type",
  methed: RequestMethod.GET,
  type: 'custom',
  query: [],
  resp: {},
  config: {}
 }]
 
}
请求示例
GET http://127.0.0.1:8080/demo/custom/req/type?id=123456
响应示例
{
    "code":0,
    "data":{
        "query":{
            "id":"123456"
        },
        "body":{},
        "params":{}
    },
    "reqId":"c8ba894927c5461991da8d0d1875a9a9"
}

二、调试说明

链接到全局

将本项目链接到全局:

npm link

与调试项目建立链接

在调试项目中与本项目建立链接:

npm link ts-api-core

这时候你修改了本项目原文件里内容时,在调试项目里调试时也会跟着变。

三、发布说明

配置目标仓库

不配置会使用默认的仓库(npmjs)。

npm config set registry https://registry.npmjs.org

登录

添加用户和登录,输入命令后按提示输入用户名和密码、emial。

npm adduser
npm login

发布

// 先编译
npm run build
// 发布
cd dist
npm publish
// 发布到指定仓库
npm publish --registry https://npm.xxx.com/
1.3.0

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago