Comments (10)
There is some work being done on getting an a typescript definition file for gRPC, see #11020. Luckily for us that contains a definition file, I copied the metadata part below.
With that definition we can type the metadata parameter:
getPage( request: logbook.getPageRequest, metadata ?: Metadata ): Observable<logbook.Page>;
Definition copied from index.d.ts
export class Metadata {
/**
* Class for storing metadata. Keys are normalized to lowercase ASCII.
*/
constructor();
/**
* Sets the given value for the given key, replacing any other values associated
* with that key. Normalizes the key.
* @param key The key to set
* @param value The value to set. Must be a buffer if and only if the normalized key ends with '-bin'
*/
set(key: string, value: string | Buffer): void;
/**
* Adds the given value for the given key. Normalizes the key.
* @param key The key to add to.
* @param value The value to add. Must be a buffer if and only if the normalized key ends with '-bin'
*/
add(key: string, value: string | Buffer): void;
/**
* Remove the given key and any associated values. Normalizes the key.
* @param key The key to remove
*/
remove(key: string): void;
/**
* Gets a list of all values associated with the key. Normalizes the key.
* @param key The key to get
* @return The values associated with that key
*/
get(key: string): (string | Buffer)[];
/**
* Get a map of each key to a single associated value. This reflects the most
* common way that people will want to see metadata.
* @return A key/value mapping of the metadata
*/
getMap(): { [index: string]: string | Buffer };
/**
* Clone the metadata object.
* @return The new cloned object
*/
clone(): Metadata;
}
from rxjs-grpc.
Thank you for the idea, I like it!
However I really don't like the any type of metadata. The whole point of this project is to be typesafe.
I have to spend some time on this the figure out a good solution. Of course in the mean time I am open to hear any idea :) It would be much easier if we would have @types/grpc. Maybe somebody? :)
from rxjs-grpc.
I agree, the any
type is less than ideal. gRPC does not include a d.ts file for its node client, so I wasn't sure what the shape of the metadata type looked like.
It seems to just be a class with a single "private" _internal_repr
property and some public methods (get
, set
, add
, remove
, getMap
, clone
).
const meta = new grpc.Metadata();
meta.set('key', 'value');
meta.set('key2', 'value2');
console.log(meta);
// prints { _internal_repr: { key: [ 'value' ], key2: [ 'value2' ] } }
https://grpc.io/grpc/node/grpc.Metadata.html for the full interface.
https://grpc.io/grpc/node/src_metadata.js.html#line50 for source around metadata.
from rxjs-grpc.
Perfect, thank you!
I will follow that PR. Then we have to upgrade grpc, so #7 looks like a dependency.
from rxjs-grpc.
@kondi The PR for gRPC Node typings is now living here--just a heads up.
from rxjs-grpc.
My comment above is already obsolete. New PR here.
from rxjs-grpc.
@kondi the typings file above has been merged in. Looks like it will be included in the next gRPC release (1.7).
from rxjs-grpc.
Hey, when will have the metadata params?
Here is a very ugly hack for the client side:
function withMetadata<Req, Res>(func: (req: Req) => rx.Observable<Res>) : (req: Req, metadata: grpc.Metadata) => rx.Observable<Res> {
type MetaFunc = (req: Req, metadata: grpc.Metadata) => rx.Observable<Res>;
return <MetaFunc>func;
}
Now you can do:
withMetadata(client.foo)(bar, metadata);
The reason why it works is that rxjs-grpc just passes the arguments downstream to grpc.
from rxjs-grpc.
@kondi the TypeScript typings for gRPC are now included in the latest release (1.7). Let me know if you'd like me to try to put together a PR.
from rxjs-grpc.
Released in v0.2.0.
from rxjs-grpc.
Related Issues (20)
- publish $protobuf import patch to npm HOT 1
- ServerBuilder/ClientBuilder for nested namespaces/packages HOT 1
- Output only contains "There are no input files to process." on OSX HOT 1
- Unable to run example with TypeScript 2.5.x HOT 3
- For service calls with streaming requests, the request should be a observable. HOT 1
- Support OS allocated random TCP port HOT 1
- Usage without RxJS? (Pure gRPC to TypeScript) HOT 4
- Enums are received as string, while the generated enum types them as number HOT 3
- Type 'ClientFactoryConstructor<{}>' cannot be converted to type 'ClientFactory'. Property 'getEmployeeRepo' is missing in type 'ClientFactoryConstructor<{}>'. HOT 2
- Type generation error
- Upgrade to RxJS 6 or add rxjs-compat dependency HOT 4
- Split generation and lib into two packages
- Create releases with proper release notes HOT 2
- Translate protobuf oneof attribute values into string literal types HOT 2
- Static generation HOT 2
- Is it no longer under maintenance? HOT 1
- Don't work in Angular HOT 1
- Unsubscribe Stream does not close connection
- Generated Enum Types Need to be Compiled
- support `protoc --proto_path=$GOPATH/src/whgo/product/proto` like go for import package HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxjs-grpc.