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: update readme and examples for default credential provider and new topics callbacks #1429

Merged
merged 4 commits into from
Aug 30, 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
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'),
Copy link
Contributor

Choose a reason for hiding this comment

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

configuration is actually optional now too; I just never went and updated the docs. (so thank you for being diligent about it!)

I think we should leave plenty of examples of how to explicitly set these so that people will know, but in the examples where we omit them, we should omit both. I think this file is a good place to omit both, and whatever shows up in the main README is another good spot. Maybe also in the most basic example file for each product. But probably the rest of the places (e.g. the dictionary example) I'd leave the explicit versions in.

You can use your judgment on which files to update vs leave explicit, but let's go ahead and remove the explicit configuration option from any of the ones where you remove credentialProvider.

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,

Check warning on line 3 in examples/nodejs/cache/leaderboard.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'LeaderboardConfigurations' is defined but never used

Check warning on line 3 in examples/nodejs/cache/leaderboard.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'LeaderboardConfigurations' is defined but never used

Check warning on line 3 in examples/nodejs/cache/leaderboard.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'LeaderboardConfigurations' is defined but never used
CredentialProvider,
CacheClient,
Configurations,
LeaderboardOrder,
Expand All @@ -16,12 +15,11 @@
} 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 @@
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
16 changes: 8 additions & 8 deletions examples/nodejs/cache/package-lock.json

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

2 changes: 1 addition & 1 deletion examples/nodejs/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"typescript": "4.9.5"
},
"dependencies": {
"@gomomento/sdk": "^1.97.0"
"@gomomento/sdk": "^1.98.1"
},
"engines": {
"node": ">=10.4.0"
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({});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should the props object itself become optional then?
removing the {} results in an Expected 1 arguments, but got 0 error.

Copy link
Contributor

Choose a reason for hiding this comment

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

good call, we could do that later, but I don't think it's a blocker for now.

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'),
});
16 changes: 8 additions & 8 deletions examples/nodejs/storage/package-lock.json

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

2 changes: 1 addition & 1 deletion examples/nodejs/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"typescript": "4.9.5"
},
"dependencies": {
"@gomomento/sdk": "^1.97.0"
"@gomomento/sdk": "^1.98.1"
},
"engines": {
"node": ">=10.4.0"
Expand Down
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
22 changes: 11 additions & 11 deletions examples/nodejs/topics/package-lock.json

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

2 changes: 1 addition & 1 deletion examples/nodejs/topics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"typescript": "4.9.5"
},
"dependencies": {
"@gomomento/sdk": "^1.97.0"
"@gomomento/sdk": "^1.98.1"
}
}
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 @@
TopicClient,
TopicItem,
TopicSubscribe,
CredentialProvider,
TopicConfigurations,
TopicSubscribeResponse,
TopicDiscontinuity,
TopicHeartbeat,
} from '@gomomento/sdk';
import {ensureCacheExists} from './utils/cache';

Expand All @@ -15,17 +15,16 @@
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 @@
//subscription.unsubscribe();
}

function handleDiscontinuity(discontinuity: TopicDiscontinuity) {

Check warning on line 59 in examples/nodejs/topics/topic-subscribe.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'discontinuity' is defined but never used

Check warning on line 59 in examples/nodejs/topics/topic-subscribe.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'discontinuity' is defined but never used

Check warning on line 59 in examples/nodejs/topics/topic-subscribe.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'discontinuity' is defined but never used
console.log('Discontinuity received from topic subscription');
}

function handleHeartbeat(heartbeat: TopicHeartbeat) {

Check warning on line 63 in examples/nodejs/topics/topic-subscribe.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'heartbeat' is defined but never used

Check warning on line 63 in examples/nodejs/topics/topic-subscribe.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'heartbeat' is defined but never used

Check warning on line 63 in examples/nodejs/topics/topic-subscribe.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'heartbeat' is defined but never used
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
Loading
Loading