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 1 commit
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.92.2",
"xhr4sw": "npm:[email protected]",
"dotenv": "https://deno.land/std/dotenv/mod.ts"

Expand Down
47 changes: 28 additions & 19 deletions examples/deno/web-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CacheClient,
CacheGet,
CacheSet,
CacheDelete,
Configurations,
CredentialProvider,
} from 'momento'
Expand Down Expand Up @@ -44,29 +45,37 @@ 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 CacheSet.Success:
console.log('Key stored successfully!');
break;
case CacheSet.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 CacheGet.Hit:
console.log(`cache hit: ${getResponse.valueString()}`);
break;
case CacheGet.Miss:
console.log('cache miss');
break;
case CacheGet.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 CacheDelete.Success:
console.log('Key deleted successfully!');
break;
case CacheDelete.Error:
console.log(`Error deleting key: ${deleteResponse.toString()}`);
break;
}

return new Response(
`Tested the Momento cache using: <br /> Key: ${key} | Value: ${value}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
* a second file in the future if desired.
*
*/
import {CacheClient, Configurations, CredentialProvider, CacheGet, SdkError, MomentoErrorCode} from '@gomomento/sdk';
import {
CacheClient,
Configurations,
CredentialProvider,
CacheGet,

Check warning on line 15 in examples/nodejs/cache/doc-example-files/config-and-error-handling.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'CacheGet' is defined but never used

Check warning on line 15 in examples/nodejs/cache/doc-example-files/config-and-error-handling.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'CacheGet' is defined but never used

Check warning on line 15 in examples/nodejs/cache/doc-example-files/config-and-error-handling.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'CacheGet' is defined but never used
cprice404 marked this conversation as resolved.
Show resolved Hide resolved
SdkError,
MomentoErrorCode,
CacheGetResponse,
} from '@gomomento/sdk';

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

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