Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: working on migrating instanceof examples to switch #1391

Merged
merged 6 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/deno/web-sdk/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"imports": {
"http": "https://deno.land/[email protected]/http/server.ts",
"momento": "npm:@gomomento/sdk-web@1.39.2",
"momento": "npm:@gomomento/sdk-web@1.93.0",
"xhr4sw": "npm:[email protected]",
"dotenv": "https://deno.land/std/dotenv/mod.ts"

Expand Down
45 changes: 27 additions & 18 deletions examples/deno/web-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import { serve } from 'http'
import {
CacheClient,
CacheGet,
CacheSet,
CacheDeleteResponse,
CacheGetResponse,
CacheSetResponse,
Configurations,
CredentialProvider,
} from 'momento'
Expand Down Expand Up @@ -44,28 +45,36 @@ export const handler = async (_request: Request): Promise<Response> => {
const value = 'FOO'

const setResponse = await momento.set(cacheName, key, value)
if (setResponse instanceof CacheSet.Success) {
console.log('Key stored successfully!')
} else {
console.log(`Error setting key: ${setResponse.toString()}`)
switch (setResponse.type) {
case CacheSetResponse.Success:
console.log('Key stored successfully!')
break
case CacheSetResponse.Error:
console.log(`Error setting key: ${setResponse.toString()}`)
break
}

const getResponse = await momento.get(cacheName, key)
if (getResponse instanceof CacheGet.Hit) {
console.log(`cache hit: ${getResponse.valueString()}`)
} else if (getResponse instanceof CacheGet.Miss) {
console.log('cache miss')
} else if (getResponse instanceof CacheGet.Error) {
console.log(`Error: ${getResponse.message()}`)
switch (getResponse.type) {
case CacheGetResponse.Hit:
console.log(`cache hit: ${getResponse.valueString()}`)
break
case CacheGetResponse.Miss:
console.log('cache miss')
break
case CacheGetResponse.Error:
console.log(`Error: ${getResponse.message()}`)
break
}

const deleteResponse = await momento.delete(cacheName, key)
if (deleteResponse instanceof CacheGet.Hit) {
console.log(`cache hit: ${deleteResponse.valueString()}`)
} else if (deleteResponse instanceof CacheGet.Miss) {
console.log('cache miss')
} else if (deleteResponse instanceof CacheGet.Error) {
console.log(`Error: ${deleteResponse.message()}`)
switch (deleteResponse.type) {
case CacheDeleteResponse.Success:
console.log('Key deleted successfully!')
break
case CacheDeleteResponse.Error:
console.log(`Error deleting key: ${deleteResponse.toString()}`)
break
}

return new Response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
* a second file in the future if desired.
*
*/
import {CacheClient, Configurations, CredentialProvider, CacheGet, SdkError, MomentoErrorCode} from '@gomomento/sdk';
import {
CacheClient,
Configurations,
CredentialProvider,
SdkError,
MomentoErrorCode,
CacheGetResponse,
} from '@gomomento/sdk';

/* eslint-disable @typescript-eslint/no-unused-vars */
async function example_configuration_ConstructWithNoConfig() {
Expand All @@ -31,14 +38,17 @@ async function example_configuration_ConstructWithLambdaConfig() {

async function example_configuration_ErrorHandlingHitMiss(cacheClient: CacheClient) {
const result = await cacheClient.get('test-cache', 'test-key');
if (result instanceof CacheGet.Hit) {
console.log(`Retrieved value for key 'test-key': ${result.valueString()}`);
} else if (result instanceof CacheGet.Miss) {
console.log("Key 'test-key' was not found in cache 'test-cache'");
} else if (result instanceof CacheGet.Error) {
throw new Error(
`An error occurred while attempting to get key 'test-key' from cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
switch (result.type) {
case CacheGetResponse.Hit:
console.log(`Retrieved value for key 'test-key': ${result.valueString()}`);
break;
case CacheGetResponse.Miss:
console.log("Key 'test-key' was not found in cache 'test-cache'");
break;
case CacheGetResponse.Error:
throw new Error(
`An error occurred while attempting to get key 'test-key' from cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of the changes are in the next file, which is hidden due to "large diffs" 🤦

Expand Down
Loading
Loading