Git Product home page Git Product logo

Comments (10)

MaikelH avatar MaikelH commented on July 22, 2024 1

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.

kondi avatar kondi commented on July 22, 2024

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.

ksaldana1 avatar ksaldana1 commented on July 22, 2024

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.

kondi avatar kondi commented on July 22, 2024

Perfect, thank you!
I will follow that PR. Then we have to upgrade grpc, so #7 looks like a dependency.

from rxjs-grpc.

ksaldana1 avatar ksaldana1 commented on July 22, 2024

@kondi The PR for gRPC Node typings is now living here--just a heads up.

from rxjs-grpc.

ksaldana1 avatar ksaldana1 commented on July 22, 2024

My comment above is already obsolete. New PR here.

from rxjs-grpc.

ksaldana1 avatar ksaldana1 commented on July 22, 2024

@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.

derolf avatar derolf commented on July 22, 2024

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.

ksaldana1 avatar ksaldana1 commented on July 22, 2024

@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.

kondi avatar kondi commented on July 22, 2024

Released in v0.2.0.

from rxjs-grpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.