grpc / grpc/grpc-web

Angular support

Open
#382 8 comments 39 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
9.3k
Forks
802
Avg merge
1d 7h
Merged PRs (30d)
5

Description

Hi guys,

it would be really nice to have a deep Angular support.

One can say it is supported, and, well, one can use it with Angular. However, it currently feels like an alien bird in the nest because

1. two-way binding and export message as "normal" JavaScript-friendly Object is not possible and sorry, but to me it is not usable, see https://github.com/grpc/grpc-web/issues/382#issuecomment-442005857
2. dependency injection is not anyhow taken into account. Ideally every service client could become a native angular service
3. the methods are not compatible with neither RX Observables / native Promises. With Promise it is quite clear it cannot be supported because of non-unary methods, but RX is fully compliant here.

This is what I came to after a couple of attempts

```ts
export function fromClient(c: T) {
return autoBind(c) as T;
}

export function fromUnary(clientMethod: (...p) => UnaryResponse, request: Message, meta?: Metadata) {
return new Observable(obs => {
const req = clientMethod(request || null, meta || null, (err: ServiceError, response: T) => {
if (err) {
obs.error(err);
}

obs.next(response);
obs.complete();
});

return () => req.cancel();
});
}
```

and usage:

```ts
const client = fromClient(new HelloServiceClient(environment.grpc));
const obs = fromUnary(client.getHello, new HelloRequest());
```

This code is far away from ideal. It is actually ugly, because I need to monkey-bind the methods and status is not returned anyhow yet (however the status part is quite solvable with pipe > filter and some boolean parameter in observable response).

Finally, the question: do you plan to support Angular in this / related repository? Should @angular/cli care of this instead? Or it is up to the end user to decide how to solve this problem?

I am ready to participate / support here (if possible).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.