npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lx-frontend/grpc-shared

v0.3.4

Published

grpc 工具库

Downloads

101

Readme

@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