Skip to content

Commit

Permalink
[web] add client interceptors for grpc-web
Browse files Browse the repository at this point in the history
Summary:
these interceptors will be used to add version and auth metadata to our RPCs.

Depends on D9671

Test Plan: https://gist.github.com/vdhanan/15ffc5810425b8a22b7d3c5bd01a8d58

Reviewers: ashoat

Reviewed By: ashoat

Subscribers: bartek, tomek, wyilio

Differential Revision: https://phab.comm.dev/D9672
  • Loading branch information
vdhanan committed Nov 8, 2023
1 parent d62b1f9 commit 77a705c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions web/grpc/interceptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// @flow

import * as grpcWeb from 'grpc-web';

import { getConfig } from 'lib/utils/config.js';

class VersionInterceptor<Request, Response> {
intercept(
request: grpcWeb.Request<Request, Response>,
invoker: (
request: grpcWeb.Request<Request, Response>,
) => Promise<grpcWeb.UnaryResponse<Request, Response>>,
): Promise<grpcWeb.UnaryResponse<Request, Response>> {
const metadata = request.getMetadata();
const config = getConfig();
const codeVersion = config.platformDetails.codeVersion;
const deviceType = config.platformDetails.platform;
if (codeVersion) {
metadata['code_version'] = codeVersion.toString();
}
metadata['device_type'] = deviceType;

return invoker(request);
}
}

class AuthInterceptor<Request, Response> {
userID: string;
deviceID: string;
accessToken: string;

constructor(userID: string, deviceID: string, accessToken: string) {
this.userID = userID;
this.deviceID = deviceID;
this.accessToken = accessToken;
}

intercept(
request: grpcWeb.Request<Request, Response>,
invoker: (
request: grpcWeb.Request<Request, Response>,
) => Promise<grpcWeb.UnaryResponse<Request, Response>>,
): Promise<grpcWeb.UnaryResponse<Request, Response>> {
const metadata = request.getMetadata();
metadata['user_id'] = this.userID;
metadata['device_id'] = this.deviceID;
metadata['access_token'] = this.accessToken;

return invoker(request);
}
}

export { VersionInterceptor, AuthInterceptor };

0 comments on commit 77a705c

Please sign in to comment.