Skip to content

Commit

Permalink
chore: update readme and examples for default credential provider and…
Browse files Browse the repository at this point in the history
… new topics callbacks (#1429)

* chore: update readme and examples for default credential provider and new topics callbacks

* fix lint errors

* revert dictionary file changes, omit optional config in basic files too

* edits in storage and leaderboards examples
  • Loading branch information
anitarua authored Aug 30, 2024
1 parent ae97ce6 commit 8f9a09e
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 63 deletions.
10 changes: 10 additions & 0 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ is best suited for a particular environment:
{% include "./examples/nodejs/cache/readme.ts" %}
```

You'll need a Momento API key to authenticate with Momento. You can get one from the [Momento Console](https://console.gomomento.com/caches).

By default, Momento clients use a `CredentialProvider` that expects an environment variable named `MOMENTO_API_KEY`

```bash
export MOMENTO_API_KEY=<your Momento API key here>
```

Note: it is best practice to put the API key into something like AWS Secret Manager or GCP Secret Manager instead of an environment variable for enhanced security. See [these docs](https://docs.momentohq.com/cache/develop#instantiating-credential-providers-using-momento-api-keys) for more information about instantiating your own `CredentialProvider`.

## Getting Started and Documentation

Documentation is available on the [Momento Docs website](https://docs.momentohq.com). For information specific to a
Expand Down
11 changes: 1 addition & 10 deletions examples/nodejs/cache/basic.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import {
CacheClient,
Configurations,
CredentialProvider,
CreateCacheResponse,
CacheSetResponse,
CacheGetResponse,
} from '@gomomento/sdk';
import {CacheClient, CreateCacheResponse, CacheSetResponse, CacheGetResponse} from '@gomomento/sdk';

async function main() {
const momento = await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});

Expand Down
4 changes: 1 addition & 3 deletions examples/nodejs/cache/doc-example-files/cheat-sheet-main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import {CacheClient, Configurations, CredentialProvider} from '@gomomento/sdk';
import {CacheClient} from '@gomomento/sdk';

async function main() {
const cacheClient = await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});
}
Expand Down
11 changes: 3 additions & 8 deletions examples/nodejs/cache/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
PreviewLeaderboardClient,
LeaderboardConfigurations,
CredentialProvider,
CacheClient,
Configurations,
LeaderboardOrder,
Expand All @@ -16,12 +15,11 @@ import {
} from '@gomomento/sdk';

async function main() {
// NOTE: trace logging to view leaderboard interactions; might want to disable in production to save log noise and
// switch to ERROR or info
// NOTE: you can use TRACE level logging to view leaderboard interactions, but you likely want to
// disable that in production to save log noise by switching to ERROR or INFO level instead.
const loggerFactory = new DefaultMomentoLoggerFactory(DefaultMomentoLoggerLevel.TRACE);
const cacheClient = await CacheClient.create({
configuration: Configurations.Laptop.v1(loggerFactory),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});

Expand All @@ -37,10 +35,7 @@ async function main() {
throw createCacheResponse.innerException();
}

const client = new PreviewLeaderboardClient({
configuration: LeaderboardConfigurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
});
const client = new PreviewLeaderboardClient({});

// Create a leaderboard with given cache and leaderboard names
const leaderboard = client.leaderboard('my-cache', 'my-leaderboard');
Expand Down
4 changes: 1 addition & 3 deletions examples/nodejs/cache/readme.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {CacheClient, CacheGetResponse, Configurations, CredentialProvider} from '@gomomento/sdk';
import {CacheClient, CacheGetResponse} from '@gomomento/sdk';

async function main() {
const cacheClient = await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});

Expand Down
14 changes: 2 additions & 12 deletions examples/nodejs/storage/basic.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import {
CreateStoreResponse,
CredentialProvider,
PreviewStorageClient,
StorageConfigurations,
StorageGetResponse,
StoragePutResponse,
} from '@gomomento/sdk';
import {CreateStoreResponse, PreviewStorageClient, StorageGetResponse, StoragePutResponse} from '@gomomento/sdk';

async function main() {
const storageClient = new PreviewStorageClient({
configuration: StorageConfigurations.Laptop.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
});
const storageClient = new PreviewStorageClient({});

const storeName = 'my-store';
const createStoreResponse = await storageClient.createStore(storeName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import {CredentialProvider, PreviewStorageClient, StorageConfigurations} from '@gomomento/sdk';
import {PreviewStorageClient, StorageConfigurations} from '@gomomento/sdk';

const storageClient = new PreviewStorageClient({
configuration: StorageConfigurations.Laptop.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {TopicClient, TopicConfigurations, CredentialProvider} from '@gomomento/s
function main() {
const cacheClient = new TopicClient({
configuration: TopicConfigurations.Default.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
});
}

Expand Down
6 changes: 3 additions & 3 deletions examples/nodejs/topics/package-lock.json

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

7 changes: 2 additions & 5 deletions examples/nodejs/topics/topic-publish.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TopicClient, CredentialProvider, TopicConfigurations, TopicPublishResponse} from '@gomomento/sdk';
import {TopicClient, TopicPublishResponse} from '@gomomento/sdk';

import {ensureCacheExists} from './utils/cache';

Expand All @@ -9,10 +9,7 @@ async function main() {
return;
}
const [cacheName, topicName, value] = clargs;
const momento = new TopicClient({
configuration: TopicConfigurations.Default.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
});
const momento = new TopicClient({});

await ensureCacheExists(cacheName);

Expand Down
19 changes: 13 additions & 6 deletions examples/nodejs/topics/topic-subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
TopicClient,
TopicItem,
TopicSubscribe,
CredentialProvider,
TopicConfigurations,
TopicSubscribeResponse,
TopicDiscontinuity,
TopicHeartbeat,
} from '@gomomento/sdk';
import {ensureCacheExists} from './utils/cache';

Expand All @@ -15,17 +15,16 @@ async function main() {
return;
}
const [cacheName, topicName] = clargs;
const momento = new TopicClient({
configuration: TopicConfigurations.Default.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
});
const momento = new TopicClient({});

await ensureCacheExists(cacheName);

console.log(`Subscribing to cacheName=${cacheName}, topicName=${topicName}`);
const response = await momento.subscribe(cacheName, topicName, {
onItem: handleItem,
onError: handleError,
onDiscontinuity: handleDiscontinuity,
onHeartbeat: handleHeartbeat,
});

switch (response.type) {
Expand Down Expand Up @@ -57,6 +56,14 @@ function handleError(error: TopicSubscribe.Error) {
//subscription.unsubscribe();
}

function handleDiscontinuity(discontinuity: TopicDiscontinuity) {
console.log('Discontinuity received from topic subscription');
}

function handleHeartbeat(heartbeat: TopicHeartbeat) {
console.log('Heartbeat received from topic subscription');
}

main()
.then(() => {
console.log('success!!');
Expand Down
11 changes: 1 addition & 10 deletions examples/web/cache/basic.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import {
CacheClient,
Configurations,
CredentialProvider,
CreateCacheResponse,
CacheSetResponse,
CacheGetResponse,
} from '@gomomento/sdk-web';
import {CacheClient, CreateCacheResponse, CacheSetResponse, CacheGetResponse} from '@gomomento/sdk-web';
import {initJSDom} from './utils/jsdom';

async function main() {
// Because the Momento Web SDK is intended for use in a browser, we use the JSDom library to set up an environment
// that will allow us to use it in a node.js program.
initJSDom();
const momento = new CacheClient({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});

Expand Down

0 comments on commit 8f9a09e

Please sign in to comment.