Skip to content

Commit

Permalink
feat: bump momento wire types typescript to 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
bruuuuuuuce committed Oct 12, 2021
1 parent d33fa41 commit 0b9ec03
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 19 deletions.
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@types/google-protobuf": "^3.15.5",
"typescript": "^4.4.3"
},
"dependencies": {
"@grpc/grpc-js": "1.3.7",
"jwt-decode": "^3.1.2",
"@momento/wire-types-typescript": "0.1.1"
"@momento/wire-types-typescript": "0.1.2",
"google-protobuf": "^3.18.1",
"jwt-decode": "^3.1.2"
}
}
7 changes: 4 additions & 3 deletions src/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {cache, grpc} from '@momento/wire-types-typescript';
import {cache} from '@momento/wire-types-typescript';
import {authInterceptor} from "./grpc/AuthInterceptor";
import {cacheNameInterceptor} from "./grpc/CacheNameInterceptor";
import {MomentoCacheResult, momentoResultConverter} from "./messages/Result";
import {IllegalArgumentError} from "./errors/IllegalArgumentError";
import {ClientSdkError} from "./errors/ClientSdkError";
import {errorMapper} from "./errors/GrpcErrorMapper";
import {ChannelCredentials, Interceptor} from "@grpc/grpc-js";

type GetResponse = {
body: string;
Expand All @@ -21,10 +22,10 @@ export class Cache {
private readonly client: cache.cache_client.ScsClient;
private readonly textEncoder: TextEncoder;
private readonly textDecoder: TextDecoder;
private readonly interceptors: grpc.Interceptor[];
private readonly interceptors: Interceptor[];
private readonly cacheName: string;
constructor(authToken: string, cacheName: string, endpoint: string) {
this.client = new cache.cache_client.ScsClient(endpoint, grpc.ChannelCredentials.createSsl())
this.client = new cache.cache_client.ScsClient(endpoint, ChannelCredentials.createSsl());
this.textEncoder = new TextEncoder();
this.textDecoder = new TextDecoder();
this.cacheName = cacheName;
Expand Down
7 changes: 4 additions & 3 deletions src/Momento.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {control, grpc} from '@momento/wire-types-typescript';
import {control} from '@momento/wire-types-typescript';
import jwtDecode from "jwt-decode";
import {Cache} from "./Cache";
import {authInterceptor} from "./grpc/AuthInterceptor";
Expand All @@ -7,6 +7,7 @@ import {ClientSdkError} from "./errors/ClientSdkError";
import {Status} from "@grpc/grpc-js/build/src/constants";
import {CacheAlreadyExistsError} from "./errors/CacheAlreadyExistsError";
import {errorMapper} from "./errors/GrpcErrorMapper";
import {ChannelCredentials, Interceptor} from "@grpc/grpc-js";

type Claims = {
cp: string,
Expand All @@ -15,7 +16,7 @@ type Claims = {

export class Momento {
private readonly client: control.control_client.ScsControlClient;
private readonly interceptors: grpc.Interceptor[];
private readonly interceptors: Interceptor[];
private readonly controlEndpoint: string;
private readonly cacheEndpoint: string;
private readonly authToken: string;
Expand All @@ -25,7 +26,7 @@ export class Momento {
this.interceptors = [authInterceptor(authToken)]
this.controlEndpoint = endpointOverride ? `control.${endpointOverride}` : claims.cp;
this.cacheEndpoint = endpointOverride ? `cache.${endpointOverride}` : claims.c;
this.client = new control.control_client.ScsControlClient(this.controlEndpoint, grpc.ChannelCredentials.createSsl())
this.client = new control.control_client.ScsControlClient(this.controlEndpoint, ChannelCredentials.createSsl())
}

private static decodeJwt(jwt?: string): Claims {
Expand Down
4 changes: 2 additions & 2 deletions src/errors/GrpcErrorMapper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {grpc} from '@momento/wire-types-typescript';
import {ClientSdkError} from "./ClientSdkError";
import {Status} from "@grpc/grpc-js/build/src/constants";
import {PermissionDeniedError} from "./PermissionDeniedError";
import {CacheNotFoundError} from "./CacheNotFoundError";
import {IllegalArgumentError} from "./IllegalArgumentError";
import {InternalServerError} from "./InternalServerError";
import {ServiceError} from "@grpc/grpc-js";

export function errorMapper(err: grpc.ServiceError): ClientSdkError {
export function errorMapper(err: ServiceError): ClientSdkError {
if (err.code === Status.PERMISSION_DENIED) {
return new PermissionDeniedError(err.message)
}
Expand Down
7 changes: 3 additions & 4 deletions src/grpc/AuthInterceptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {grpc} from "@momento/wire-types-typescript";
import {InterceptingCall, Interceptor} from "@grpc/grpc-js";

grpc.InterceptingCall

export const authInterceptor = (token: string): grpc.Interceptor => {
export const authInterceptor = (token: string): Interceptor => {
return (options, nextCall) => {
return new grpc.InterceptingCall(nextCall(options), {
return new InterceptingCall(nextCall(options), {
start: function (metadata, listener, next) {
metadata.add("Authorization", token)
next(metadata, {})
Expand Down
8 changes: 3 additions & 5 deletions src/grpc/CacheNameInterceptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {grpc} from "@momento/wire-types-typescript";
import {InterceptingCall, Interceptor} from "@grpc/grpc-js";

grpc.InterceptingCall

export const cacheNameInterceptor = (cacheName: string): grpc.Interceptor => {
export const cacheNameInterceptor = (cacheName: string): Interceptor => {
return (options, nextCall) => {
return new grpc.InterceptingCall(nextCall(options), {
return new InterceptingCall(nextCall(options), {
start: function (metadata, listener, next) {
metadata.add("cache", cacheName)
next(metadata, {})
Expand Down

0 comments on commit 0b9ec03

Please sign in to comment.