{"id":28506189,"url":"https://github.com/Volankey/grpc-web-from-object","last_synced_at":"2025-07-05T03:32:10.970Z","repository":{"id":39712976,"uuid":"434564000","full_name":"Volankey/grpc-web-from-object","owner":"Volankey","description":"grpc-web easy fromObject","archived":false,"fork":false,"pushed_at":"2022-11-08T10:15:59.000Z","size":267,"stargazers_count":7,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-16T07:53:00.199Z","etag":null,"topics":["grpc","grpc-web","grpc-web-fromobject","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Volankey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-03T11:00:12.000Z","updated_at":"2024-12-11T17:58:54.000Z","dependencies_parsed_at":"2022-09-20T09:02:27.548Z","dependency_job_id":null,"html_url":"https://github.com/Volankey/grpc-web-from-object","commit_stats":null,"previous_names":["volankey/grpc-web-protojs-transformer"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Volankey/grpc-web-from-object","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volankey%2Fgrpc-web-from-object","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volankey%2Fgrpc-web-from-object/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volankey%2Fgrpc-web-from-object/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volankey%2Fgrpc-web-from-object/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Volankey","download_url":"https://codeload.github.com/Volankey/grpc-web-from-object/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Volankey%2Fgrpc-web-from-object/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263677002,"owners_count":23494615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grpc","grpc-web","grpc-web-fromobject","typescript"],"created_at":"2025-06-08T20:01:15.932Z","updated_at":"2025-07-05T03:32:10.960Z","avatar_url":"https://github.com/Volankey.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Happy Grpc Web FromObject\n\n## grpc-web-invoker\n\nSupport Typescript \u0026 Easy to invoke grpc method(fromObject) from web\n\n### Install\n\n```bash\nnpm install @volankey/grpc-web-invoker\n```\n\n### Usage\n\n```ts\nimport { EchoServiceClient } from '../proto/EchoServiceClientPb';\nimport { createInvoker } from 'invoker-grpc';\n\nconst client = new EchoServiceClient('http://localhost:8080', null);\n\nconst invoke = createInvoker(client).invoke;\n\ninvoke('echo', {\n  message: 'hello volankey',\n  student: {\n    favoratesList: ['basketball', 'football'],\n    name: 'volankey',\n    sex: 'man',\n  },\n  suject: {\n    liberalArtsList: [{ name: 'piano', score: '12' }],\n    scienceList: [{ name: 'math', score: '23' }],\n  },\n  keyScroeMap: [['ss', { name: 'nihao', score: '30' }]],\n  keyvalueMap: [['1', '1']],\n  oneOfSample: {\n    name: '红红火火',\n    // or\n\n    // 'subMessage':{\n    //   'name':'a1',\n    //   'score':'100'\n    // }\n  },\n})\n  .then((t) =\u003e {\n    console.log(t.resp);\n    result.value = JSON.stringify(t.resp, null, ' ');\n  })\n  .catch((e) =\u003e {});\n```\n\n#### Add MetaData\n\n```ts\nimport { EchoServiceClient } from './proto/EchoServiceClientPb';\nimport { createInvoker } from '@volankey/grpc-web-invoker';\n\nconst client = new EchoServiceClient('http://localhost:8080', null);\nenableDevTools([client]);\nconst invoker = createInvoker(client);\ninvoker.setGlobalMetaData('token', 'xxx'); // set global metadata\nconst invoke = invoker.invoke;\n\ninvoke(\n  'echo',\n  {\n    message: 'hello volankey',\n    student: {\n      favoratesList: ['basketball', 'football'],\n      name: 'volankey',\n      sex: 'man',\n    },\n    suject: {\n      liberalArtsList: [{ name: 'piano', score: '12' }],\n      scienceList: [{ name: 'math', score: '23' }],\n    },\n    keyScroeMap: [['ss', { name: 'nihao', score: '30' }]],\n    keyvalueMap: [['1', '1']],\n    oneOfSample: {\n      name: '红红火火',\n    },\n  },\n  {\n    'mock-delay': mockDelay || 0, // custom metadata\n  },\n);\n```\n\n#### Cancel Request\n\n```ts\nimport { CancelToken } from '@volankey/grpc-web-invoker';\nconst cancelToken = CancelToken.source(); // generage cancelToken\nconst timeoutAbort = new Promise((resolve, reject) =\u003e {\n  setTimeout(() =\u003e {\n    cancelToken.cancel(); // cancel request after 2000ms\n    resolve(new Error('Cancel By Client' + ', mock-delay is ' + mockDelay));\n  }, 2000);\n}).then((e) =\u003e {\n  alert((e as Error).message);\n  return Promise.reject(e);\n});\nconst invokePromise = invoke(\n  'echo',\n  {\n    message: 'hello volankey',\n    student: {\n      favoratesList: ['basketball', 'football'],\n      name: 'volankey',\n      sex: 'man',\n    },\n    suject: {\n      liberalArtsList: [{ name: 'piano', score: '12' }],\n      scienceList: [{ name: 'math', score: '23' }],\n    },\n    keyScroeMap: [['ss', { name: 'nihao', score: '30' }]],\n    keyvalueMap: [['1', '1']],\n    oneOfSample: {\n      name: '红红火火',\n    },\n  },\n  {\n    'mock-delay': 3000,\n  },\n  {\n    cancelToken: cancelToken.token, // pass the token to invoke\n  },\n);\nPromise.race([invokePromise, timeoutAbort])\n  .then((t) =\u003e {\n    const resp = (t as UnPromise\u003ctypeof invokePromise\u003e).resp;\n    console.log(resp);\n    result.value = JSON.stringify(resp, null, ' ');\n  })\n  .catch((e) =\u003e {\n    console.error(e);\n  })\n  .finally(() =\u003e {\n    sending.value = false;\n  });\n```\n\n### Get a method params type for typescript\n\n```ts\nimport {\n  createInvoker,\n  GetInvokeMethodParams,\n} from '@volankey/grpc-web-invoker';\n\ntype TEchoParams = GetInvokeMethodParams\u003cEchoServiceClient, 'echo'\u003e;\n```\n\n## grpc-web-pb-transform\n\ntransform the pb.js file which generate by protoc and grpc-web\n\n### Install\n\n```bash\nnpm install @volankey/grpc-web-pb-transform -D\n```\n\n### Usage\n\n```js\nconst {\n  pbValueTypeTransfer,\n  cjs2esm,\n  grpcWebClientTransform,\n} = require('@volankey/grpc-web-pb-transform');\n\npbValueTypeTransfer(join(__dirname, '../src/proto/echo_pb.js'));\n// if you use vite, transform to esm\ncjs2esm(\n  [\n    join(__dirname, '../src/proto/echo_pb.js'),\n    join(__dirname, '../src/proto/EchoServiceClientPb.ts'),\n  ],\n  join(__dirname, '../src/proto'),\n);\ngrpcWebClientTransform(join(__dirname, '../src/proto/EchoServiceClientPb.ts'));\n```\n\n#### How to generate protobuf\n\n```js\nconst { exec } = require('child_process');\nconst {\n  pbValueTypeTransfer,\n  cjs2esm,\n} = require('@volankey/grpc-web-pb-transform');\nconst { join } = require('path');\nconst startTime = Date.now();\nconsole.log(new Date().toLocaleString(), 'start handle pb files...');\nexec(\n  'protoc --proto_path=../ -I./ -I. --js_out=import_style=commonjs:\"./src/proto\" --grpc-web_out=import_style=typescript,mode=grpcwebtext:\"./src/proto\" \"../echo.proto\"',\n  (error, stdout, stderr) =\u003e {\n    if (error) {\n      console.log(`error: ${error.message}`);\n      return;\n    }\n    if (stderr) {\n      console.log(`stderr: ${stderr}`);\n      return;\n    }\n    pbValueTypeTransfer(join(__dirname, '../src/proto/echo_pb.js'));\n    // if you use vite, transform to esm\n    cjs2esm(\n      [\n        join(__dirname, '../src/proto/echo_pb.js'),\n        join(__dirname, '../src/proto/EchoServiceClientPb.ts'),\n      ],\n      join(__dirname, '../src/proto'),\n    );\n    console.log(\n      new Date().toLocaleString(),\n      'done in ' + (Date.now() - startTime) + ' ms',\n    );\n  },\n);\n```\n\n## CONTRIBUTE\n\n[./CONTRIBUTE.md](CONTRIBUTE.md)\n\n# TO-DO\n\n- [ ] Support CommonJs generated by grpc-web\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVolankey%2Fgrpc-web-from-object","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FVolankey%2Fgrpc-web-from-object","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVolankey%2Fgrpc-web-from-object/lists"}