grpc-modern
Advanced tools
Comparing version
@@ -1,6 +0,10 @@ | ||
import { Client } from "grpc"; | ||
import { ChannelCredentials, Client as BaseClient } from "grpc"; | ||
import { GrpcMethod, PromisifiedGrpcMethod } from "./promisify"; | ||
export declare type ModernClient<C extends Client> = { | ||
[key in Exclude<keyof C, keyof Client>]: C[key] extends GrpcMethod<infer M, infer R> ? PromisifiedGrpcMethod<M, R> : never; | ||
export declare type ModernClient<C extends BaseClient> = { | ||
[key in Exclude<keyof C, keyof BaseClient>]: C[key] extends GrpcMethod<infer M, infer R> ? PromisifiedGrpcMethod<M, R> : never; | ||
}; | ||
export declare function makeModernClient<C extends Client>(client: C): ModernClient<C>; | ||
export declare function makeModernClient<C extends BaseClient>(Client: new (address: string, credentials: ChannelCredentials, options?: object) => C, options: { | ||
address: string; | ||
credentials: ChannelCredentials; | ||
options?: object; | ||
}): ModernClient<C>; |
@@ -12,3 +12,3 @@ "use strict"; | ||
function makeModernClient(client) { | ||
function makeModernClient(Client, options) { | ||
const _baseClient = new _grpc.Client( | ||
@@ -18,11 +18,11 @@ /* dummy */ | ||
const _client = client; | ||
const _client = new Client(options.address, options.credentials, options.options); | ||
for (const key in client) { | ||
for (const key in _client) { | ||
if (key.indexOf("$") === -1 && !_baseClient[key] && _client[key]) { | ||
const Request = _client.$method_definitions[key].requestType; | ||
const fn = _client[key].bind(client); | ||
const method = _client[key].bind(_client); | ||
_client[key] = (0, _promisify.promisify)(Request, fn); | ||
_client[key] = (0, _promisify.promisify)(Request, method); | ||
} | ||
@@ -29,0 +29,0 @@ } |
{ | ||
"name": "grpc-modern", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "module": "lib/index.mjs", |
@@ -19,12 +19,16 @@ # gRPC Modern | ||
```typescript | ||
import { GetSomethingReq, SomethingClient } from '../stubs/something/...' | ||
import { credentials } from "grpc"; | ||
import { GetSomethingReq, SomethingClient } from "../stubs/something/..."; | ||
const client = new SomethingClient(...) | ||
const req = new GetSomethingReq() | ||
req.setId('...') | ||
req.setSomeOption(false) | ||
const client = new SomethingClient( | ||
"example.com:80", | ||
credentials.createInsecure() | ||
); | ||
const req = new GetSomethingReq(); | ||
req.setId("..."); | ||
req.setSomeOption(false); | ||
client.getSomething(req, (error, response) => { | ||
console.log(response) | ||
}) | ||
console.log(response); | ||
}); | ||
``` | ||
@@ -35,13 +39,17 @@ | ||
```typescript | ||
import { makeModernClient } from 'grpc-modern' | ||
import { SomethingClient } from '../stubs/something/...' | ||
import { credentials } from "grpc"; | ||
import { makeModernClient } from "grpc-modern"; | ||
import { SomethingClient } from "../stubs/something/..."; | ||
const client = makeModernClient(new SomethingClient(...)) | ||
const client = makeModernClient(SomethingClient, { | ||
address: "example.com:80", | ||
credentials: credentials.createInsecure(), | ||
}); | ||
const [error, response] = await client.getSomething({ | ||
Id: '...', | ||
Id: "...", | ||
SomeOption: false, | ||
}) | ||
}); | ||
console.log(response) | ||
console.log(response); | ||
``` | ||
@@ -52,15 +60,21 @@ | ||
```typescript | ||
import { Int64Value } from 'google-protobuf/google/protobuf/wrappers_pb' | ||
import { makeModernClient, set } from 'grpc-modern' | ||
import { SomethingClient } from '../stubs/something/...' | ||
import { Int64Value } from "google-protobuf/google/protobuf/wrappers_pb"; | ||
import { credentials } from "grpc"; | ||
import { makeModernClient, set } from "grpc-modern"; | ||
import { SomethingClient } from "../stubs/something/..."; | ||
const client = makeModernClient(new SomethingClient(...)) | ||
const client = makeModernClient(SomethingClient, { | ||
address: "example.com:80", | ||
credentials: credentials.createInsecure(), | ||
}); | ||
const [error, response] = await client.getSomething({ | ||
Ids: [1, 2, 3].map((i) => set(Int64Value, { | ||
Value: i, | ||
})), | ||
}) | ||
Ids: [1, 2, 3].map((i) => | ||
set(Int64Value, { | ||
Value: i, | ||
}) | ||
), | ||
}); | ||
console.log(response) | ||
console.log(response); | ||
``` |
Sorry, the diff of this file is not supported yet
9400
8.25%183
2.23%78
21.88%