Skip to content

Instantly share code, notes, and snippets.

@mrmeku
Last active May 31, 2018 16:38
Show Gist options
  • Save mrmeku/d01734a77d2343934e5f13b25543c35b to your computer and use it in GitHub Desktop.
Save mrmeku/d01734a77d2343934e5f13b25543c35b to your computer and use it in GitHub Desktop.
Example of how to wrap the output of ts_proto_library to create an Angular Service
import {Http, ResponseContentType} from '@angular/http';
import * as Schema from 'api_schemas_with_bazel/schema/car_ts_proto';
import {bindNodeCallback, Observable} from 'rxjs';
import {toObservableServiceMethod} from './observable-service-method';
export class CarService {
readonly getCars: (request: Schema.GetCarsRequest) => Observable<Schema.GetCarsResponse> =
toObservableServiceMethod(this.createCarServiceForMethod('getCars'), ({getCars}) => getCars);
constructor(private readonly http: Http) {}
private createCarServiceForMethod(methodName: string): Schema.CarService {
return Schema.CarService.create((_, requestData, callback) => {
const binaryBlob = new Blob([requestData]); // Ensure our request is sent as binary.
this.http
.post(
`http://localhost:8080/cars?method=${methodName}`, binaryBlob,
{responseType: ResponseContentType.ArrayBuffer})
.subscribe(
response => callback(null, new Uint8Array(response.arrayBuffer())),
error => callback(error));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment