@lx-frontend/grpc-shared
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

@lx-frontend/grpc-shared

grpc 相关工具函数

Description

grpc 调用相关封装函数。

Usage

分页请求封装方法

把生产分页token和filter、sort集成到一个函数内,简化调用逻辑

传入 serviceFn、页数、条数,过滤条件(可选),排序(可选)即可完成分页请求。

import { getPageData } from '@lx-frontend/grpc-shared';

function listRoles(data: ListRolesRequest.AsObject) {
  return grpcPromise({
    method: 'listRoles',
    data
  })
}

// 简单使用,传递页数和条数即可。
getPageData(listRoles, 1, 10).then((response) => {
  console.log('response', response)
})

// 传入了 查询字段 与 排序字段
getPageData(listRoles, 1, 10, [
  Has('name', 'mind'),
  Eq('status', 1)
], [
  {Field: "timestamp", Direction: "asc"},
  {Field: "id", Direction: "desc"}
]).then((response) => {
  console.log('response', response)
})

单独使用

  1. object2Request 把 js 对象转换成 grpc request 实例。支持多层对象
import { GreeterPromiseClient } from './dist/helloworld_grpc_web_pb';
import { HelloRequest, UserInfo, Store } from './dist/helloworld_pb';
import { object2Request } from '@lx-frontend/grpc-shared';

const client = new GreeterPromiseClient('http://localhost:50051')

const helloRequest2 = object2Request(HelloRequest, {
  name: "123",
  userinfo: {
    id: 123,
    name: 'mind',
    age: 20,
    store: {
      sid: 12,
      sname: 'sname'
    }
  }
}, {
  userinfo: UserInfo,
  store: Store,
})

client.sayHello(helloRequest2, {}).then((res) => {
  const result = res.toObject()
  console.log('result', result)
})
  1. 支持对层对象字段是数组集合
import { SetRolePermsRequest } from "./dos-boss-bff-sdk/lixin/dos/boss-bff/v1/svc_pb";
import { Permission } from "./dos-boss-bff-sdk/lixin/dos/boss-bff/v1/msg_pb";
import { object2Request } from '@lx-frontend/grpc-shared';

const setRolePermsData = {
  name: 'role/id/1',
  permissionsList: [
    {
      name: 'name-0',
      displayName: 'displayName-0',
      code: 'code-0',
      chirdrenList: []
    },
    {
      name: 'name-1',
      displayName: 'displayName-1',
      code: 'code-1',
      chirdrenList: []
    },
    {
      name: 'name-2',
      displayName: 'displayName-2',
      code: 'code-2',
      chirdrenList: []
    }
  ]
}

// 得到请求 request 数据
const setRolePermsRequest = object2Request(SetRolePermsRequest, setRolePermsData, {
  permissionsList: Permission,
  // 注意,多层对象,有几个,则需要传递对应 requestClass 类进去。
  // 多层对象有,permissionsList、chirdrenList 所以都需要传递,他们指向的 requestClass 都是 Permission
  chirdrenList: Permission
})

filter Query 构建

用与生成后台 gprc filter 参数构建,可以用来控制后台 grpc 服务返回字段,按需使用。

使用说明:

  1. 为了简化使用,Filter 函数的值,需要来自于 Eq, Gte, Gt, Has, Lt, Lte, Ne 这些函数的返回值。
  2. 更多使用方式,可以查看 测试用例

简单示例代码

import { NewQuery, Eq, Gte, Gt, Has, Lt, Lte, Ne } from "./lib";

async function demo() {
  const query = NewQuery()
    .Filter(Eq('name', "李四"))
    .Filter(Gte('age', 1))
    .Filter(Gt('id', 2))
    .Filter(Has('ddd', "123"))
    .Filter(Lt('pw', 123))
    .Filter(Lte('lll', [222, 33]))
    .Filter(Ne('neKey', "www"))

  const filterStr = query.String()

  console.log(filterStr) // name = "李四" AND age >= 1 AND id > 2 AND ddd:"123" AND pw < 123 AND lll <= (222, 33) AND neKey != www

  // 传递给后台,过滤参数。
  const res = await client.GetCarModelRequest({
    Name: "123",
    Filter: filterStr,
  })
}

sort 构建

构建 sort 字符串

import { SortBy } from "./lib";

// 可以是单个参数,也可以是多个参数
const sort = SortBy({Field: 'timestamp', Direction: "asc"}, {Field: "id", Direction: "desc"})

const sortStr = sort.String() // timestamp asc, id desc

Readme

Keywords

none

Package Sidebar

Install

npm i @lx-frontend/grpc-shared

Weekly Downloads

89

Version

0.3.1

License

ISC

Unpacked Size

83.2 kB

Total Files

38

Last publish

Collaborators

  • ruqimobile
  • haiyulu
  • erica.liuyj
  • pok.h
  • chenzian
  • azumia
  • jeely
  • lichao.franklee
  • mind29
  • yuki.liu
  • lixinleon
  • frontbot
  • owen.huang
  • tiny.tu
  • simba.wang