Skip to content

运行时小库

两个独立发布的运行时库,和装修/后台体系没有耦合,按需引入。

socket-client

@hiapi/socket-client · 1.0.2 · 源码 hiapi-fast-frame/hiapi-socket-client/ (SDK.ts / SocketClient.ts / protocol.ts,产物 dist/sdk.js)

长连接 SDK。自带心跳看门狗(startWatchdog),心跳超时会关掉 socket 触发重连。

ts
interface SDKClient {
  on(channel: 'connect',    cb: (client: SDKClient) => void): void
  on(channel: 'disconnect', cb: () => void): void
  on(channel: 'error',      cb: (err: any) => void): void

  addListener(channel: string, cb: (data: any) => void): void
  removeListener(channel: string, cb: (data: any) => void): void

  request(route: string, msg: any): Promise<any>   // 有请求 id,等响应
  notify (route: string, msg: object): void        // 单向,不等响应
}

requestnotify 的区别就是要不要等回包 —— notify 内部用请求 id 0

没有 types 字段

package.jsonmain: dist/sdk.js,没有 types、没有 exports。 TypeScript 工程引它拿不到类型提示,要自己写 .d.ts 或从源码 import。

captcha-js

@hiapi/captcha-js · 1.1.0 · 源码 hiapi-fast-frame/captcha-js/(captcha.mjs)

图形验证码前端。目前只有 hiapi-cloud-admin-ts 直接依赖, 但一般不直接用它 —— 走 admin-lib 的封装:

ts
import {captchaInit, showCaptcha, setCaptchaLocale} from '@hiapi/hiapi-cloud-admin-lib/src/utils/captcha'

const ticket = await showCaptcha()   // resolve 出票据,带给登录/注册接口

哪些接口需要验证码

账户接口按 accountType 区分:USER 跳过验证码校验 (AccountController 里的条件),merchant / channel / platform 必须过。 所以命令行给 C 端用户取 token 是可行的,给后台账号取不了。

同样没有 types