-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web] add client interceptors for grpc-web
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
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |