Skip to content

Commit

Permalink
feat: retrofit some dictionary responses with new response pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
bruuuuuuuce committed May 3, 2024
1 parent 3f9afc7 commit 6d29699
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 172 deletions.
2 changes: 0 additions & 2 deletions examples/nodejs/cache/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ async function setGetDeleteExample() {
logger.info('Key stored successfully!');
} else if (setResponse instanceof CacheSet.Error) {
logger.info(`Error setting key: ${setResponse.message()}`);
} else {
throw new Error(`Unrecognized response: ${setResponse.toString()}`);
}

const getResponse = await momento.get(cacheName, cacheKey);
Expand Down
4 changes: 0 additions & 4 deletions examples/web/cache/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ async function setGetDeleteExample() {
logger.info('Key stored successfully!');
} else if (setResponse instanceof CacheSet.Error) {
logger.info(`Error setting key: ${setResponse.message()}`);
} else {
throw new Error(`Unrecognized response: ${setResponse.toString()}`);
}

const getResponse = await momento.get(cacheName, cacheKey);
Expand All @@ -96,8 +94,6 @@ async function setGetDeleteExample() {
logger.info(`Error deleting cache key: ${deleteResponse.message()}`);
} else if (deleteResponse instanceof CacheDelete.Success) {
logger.info('Deleted key from cache');
} else {
throw new Error(`Unrecognized response: ${deleteResponse.toString()}`);
}
}

Expand Down
62 changes: 37 additions & 25 deletions packages/core/src/messages/responses/cache-dictionary-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import {
ResponseBase,
ResponseHit,
ResponseMiss,
ResponseError,
BaseResponseMiss,
BaseResponseError,
} from './response-base';
import {SdkError} from '../../errors';
import {_DictionaryFieldValuePair} from './grpc-response-types';

const TEXT_DECODER = new TextDecoder();

interface IResponse {
value(): Record<string, string> | undefined;
responseType: ResponseType;
}

export enum ResponseType {
Hit = 'Hit',
Miss = 'Miss',
Error = 'Error',
}

/**
* Parent response type for a dictionary fetch request. The
* response object is resolved to a type-safe object of one of
Expand All @@ -29,16 +39,12 @@ const TEXT_DECODER = new TextDecoder();
* }
* ```
*/
export abstract class Response extends ResponseBase {
public value(): Record<string, string> | undefined {
if (this instanceof Hit) {
return (this as Hit).value();
}
return undefined;
}
}

class _Hit extends Response {
/**
* Indicates that the requested data was successfully retrieved from the cache. Provides
* `value*` accessors to retrieve the data in the appropriate format.
*/
export class Hit extends ResponseBase implements IResponse {
private readonly items: _DictionaryFieldValuePair[];
private readonly _displayListSizeLimit = 5;

Expand Down Expand Up @@ -156,24 +162,18 @@ class _Hit extends Response {
public override toString(): string {
return `${super.toString()}: valueDictionaryStringString: ${this.truncateValueStrings()}`;
}
}

/**
* Indicates that the requested data was successfully retrieved from the cache. Provides
* `value*` accessors to retrieve the data in the appropriate format.
*/
export class Hit extends ResponseHit(_Hit) {}

class _Miss extends Response {}
responseType: ResponseType.Hit;
}

/**
* Indicates that the requested data was not available in the cache.
*/
export class Miss extends ResponseMiss(_Miss) {}
export class Miss extends BaseResponseMiss implements IResponse {
responseType: ResponseType.Miss;

class _Error extends Response {
constructor(protected _innerException: SdkError) {
super();
value(): Record<string, string> | undefined {
return undefined;
}
}

Expand All @@ -187,4 +187,16 @@ class _Error extends Response {
* - `message()` - a human-readable description of the error
* - `innerException()` - the original error that caused the failure; can be re-thrown.
*/
export class Error extends ResponseError(_Error) {}
export class Error extends BaseResponseError implements IResponse {
constructor(_innerException: SdkError) {
super(_innerException);
}

responseType = ResponseType.Error;

value(): Record<string, string> | undefined {
return undefined;
}
}

export type Response = Hit | Miss | Error;
90 changes: 36 additions & 54 deletions packages/core/src/messages/responses/cache-dictionary-get-field.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,36 @@
import {SdkError} from '../../errors';
import {
BaseResponseError,
BaseResponseMiss,
ResponseBase,
ResponseError,
ResponseHit,
ResponseMiss,
} from './response-base';
import {truncateString} from '../../internal/utils';

const TEXT_DECODER = new TextDecoder();

/**
* Parent response type for a dictionary get field request. The
* response object is resolved to a type-safe object of one of
* the following subtypes:
*
* - {Hit}
* - {Miss}
* - {Error}
*
* `instanceof` type guards can be used to operate on the appropriate subtype.
* @example
* For example:
* ```
* if (response instanceof CacheDictionaryGetField.Error) {
* // Handle error as appropriate. The compiler will smart-cast `response` to type
* // `CacheDictionaryGetField.Error` in this block, so you will have access to the properties
* // of the Error class; e.g. `response.errorCode()`.
* }
* ```
*/
export abstract class Response extends ResponseBase {
public value(): string | undefined {
if (this instanceof Hit) {
return (this as Hit).value();
}
return undefined;
}
interface IResponse {
value(): string | undefined;
responseType: ResponseType;
}

export enum ResponseType {
Hit = 'Hit',
Miss = 'Miss',
Error = 'Error',
}

class _Hit extends Response {
/**
* Indicates that the requested data was successfully retrieved from the cache. Provides
* `value*` accessors to retrieve the data in the appropriate format.
*/
export class Hit extends ResponseBase implements IResponse {
private readonly body: Uint8Array;
constructor(body: Uint8Array) {
private readonly field: Uint8Array;

constructor(body: Uint8Array, field: Uint8Array) {
super();
this.body = body;
this.field = field;
}

/**
Expand Down Expand Up @@ -73,19 +61,6 @@ class _Hit extends Response {
const display = truncateString(this.valueString());
return `${super.toString()}: ${display}`;
}
}

/**
* Indicates that the requested data was successfully retrieved from the cache. Provides
* `value*` accessors to retrieve the data in the appropriate format.
*/
export class Hit extends ResponseHit(_Hit) {
private readonly field: Uint8Array;

constructor(body: Uint8Array, field: Uint8Array) {
super(body);
this.field = field;
}

/**
* Returns the field name for the retrieved element, as a utf-8 string decoded from the underlying byte array.
Expand All @@ -102,14 +77,14 @@ export class Hit extends ResponseHit(_Hit) {
public fieldUint8Array(): Uint8Array {
return this.field;
}
}

class _Miss extends Response {}
responseType: ResponseType.Hit;
}

/**
* Indicates that the requested data was not available in the cache.
*/
export class Miss extends ResponseMiss(_Miss) {
export class Miss extends BaseResponseMiss implements IResponse {
private readonly field: Uint8Array;

constructor(field: Uint8Array) {
Expand All @@ -132,11 +107,10 @@ export class Miss extends ResponseMiss(_Miss) {
public fieldUint8Array(): Uint8Array {
return this.field;
}
}

class _Error extends Response {
constructor(protected _innerException: SdkError) {
super();
responseType: ResponseType.Miss;
value(): string | undefined {
return undefined;
}
}

Expand All @@ -150,10 +124,10 @@ class _Error extends Response {
* - `message()` - a human-readable description of the error
* - `innerException()` - the original error that caused the failure; can be re-thrown.
*/
export class Error extends ResponseError(_Error) {
export class Error extends BaseResponseError implements IResponse {
private readonly field: Uint8Array;

constructor(public _innerException: SdkError, field: Uint8Array) {
constructor(_innerException: SdkError, field: Uint8Array) {
super(_innerException);
this.field = field;
}
Expand All @@ -173,4 +147,12 @@ export class Error extends ResponseError(_Error) {
public fieldUint8Array(): Uint8Array {
return this.field;
}

responseType: ResponseType.Error;

value(): string | undefined {
return undefined;
}
}

export type Response = Hit | Miss | Error;
Loading

0 comments on commit 6d29699

Please sign in to comment.