# @lx-frontend/grpc-shared

> grpc 工具库

Latest version **0.4.0** (published 2026-07-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install @lx-frontend/grpc-shared
pnpm add @lx-frontend/grpc-shared
yarn add @lx-frontend/grpc-shared
bun add @lx-frontend/grpc-shared
```

## Health

**Score 55/100 (C)** — status: active.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2026-07-01 |
| First published | 2023-07-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 110.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | mind.wang |
| Maintainers | liuzihao0413, erica.liuyj, chenzian, azumia, jeely, lichao.franklee, mind29, ruqimobile, yuki.liu, lixinleon, frontbot, owen.huang, tiny.tu, simba.wang, pok.h, furinalu |

## Links

- npm: https://www.npmjs.com/package/@lx-frontend/grpc-shared
- Repository: git@gitlab.lixinio.com:frontend/lx-basic
- npm.io page: https://npm.io/package/@lx-frontend/grpc-shared

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.5.3
- [crypto-js](https://npm.io/package/crypto-js.md) ^4.1.1

## Recent versions

- 0.4.0 (latest) — 2026-07-01
- 0.4.0-beta.1 (beta) — 2026-06-24
- 0.4.0-beta.0 — 2026-06-24
- 0.3.9 — 2026-06-24
- 0.3.8 — 2026-06-23
- 0.3.8-beta.0 — 2026-06-23
- 0.3.7 — 2025-12-23
- 0.3.6 — 2025-02-12
- 0.3.6-beta.1 — 2025-02-06
- 0.3.6-beta.0 — 2025-02-06
- 0.3.5-beta.4 — 2025-01-10
- 0.3.5-beta.3 — 2025-01-10
- 0.3.5-beta.2 — 2025-01-10
- 0.3.5-beta.1 — 2025-01-10
- 0.3.5-beta.0 — 2024-11-07
- … 38 more at https://npm.io/package/@lx-frontend/grpc-shared/versions

## README

# `@lx-frontend/grpc-shared`

> grpc 相关工具函数

## Description

grpc 调用相关封装函数。

## Usage

### 分页请求封装方法

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

传入 `serviceFn`、页数、条数，过滤条件（可选），排序（可选）即可完成分页请求。

```ts
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 实例。支持多层对象

```ts
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)
})
```

2. 支持对层对象字段是数组集合

```ts
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. 更多使用方式，可以查看 [测试用例](./tests/query/query.test.ts)

简单示例代码

```typescript
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 字符串

```typescript
import { SortBy } from './lib'

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

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

---
_Source: https://npm.io/package/@lx-frontend/grpc-shared · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
