diff --git a/web/grpc/interceptor.js b/web/grpc/interceptor.js new file mode 100644 index 000000000..96c5b6033 --- /dev/null +++ b/web/grpc/interceptor.js @@ -0,0 +1,53 @@ +// @flow + +import * as grpcWeb from 'grpc-web'; + +import { getConfig } from 'lib/utils/config.js'; + +class VersionInterceptor { + intercept( + request: grpcWeb.Request, + invoker: ( + request: grpcWeb.Request, + ) => Promise>, + ): Promise> { + 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 { + 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, + invoker: ( + request: grpcWeb.Request, + ) => Promise>, + ): Promise> { + 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 };