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

feat: web sdk example with topic client that refreshes disposable tokens #1437

Merged
merged 13 commits into from
Sep 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
150 changes: 90 additions & 60 deletions examples/nodejs/token-vending-machine/infrastructure/package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@types/jest": "^29.4.0",
"@types/node": "18.11.18",
"@typescript-eslint/eslint-plugin": "5.59.11",
"aws-cdk": "2.85.0",
"aws-cdk": "2.158.0",
"eslint": "8.42.0",
"eslint-config-prettier": "8.8.0",
"eslint-plugin-import": "2.27.5",
Expand All @@ -31,7 +31,7 @@
"typescript": "~4.9.4"
},
"dependencies": {
"aws-cdk-lib": "^2.85.0",
"aws-cdk-lib": "^2.158.0",
"constructs": "^10.0.0",
"source-map-support": "^0.5.21"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPr
console.log(err);
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: 'An error occurred!',
}),
Expand Down
38 changes: 38 additions & 0 deletions examples/web/cache/README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ MOMENTO_API_KEY=<YOUR API KEY> npm run dictionary

Example Code: [dictionary.ts](dictionary.ts)

## Running the TopicClient example with auto-refreshing disposable tokens

This example implements TokenRefreshingTopicClient, an example wrapper class around the TopicClient that refreshes disposable tokens before they expire. Getting a new disposable token requires creating a new TopicClient that accepts a CredentialProvider that uses the new token. After the new TopicClient is created, existing subscriptions must be transferred to the new client. All of this occurs within the TokenRefreshingTopicClient.

If you run the example using the `localTokenVendingMachine()` method passed to the wrapped client (this is the default for this example):

```typescript
const wrappedTopicClient = await TokenRefreshingTopicClient.create({
refreshBeforeExpiryMs: 10_000, // 10 seconds before token expires, refresh it.
getDisposableToken: localTokenVendingMachine,
});
```

Run the example using:

```bash
# Run example code
MOMENTO_API_KEY=<YOUR API KEY> npm run tokens
```

If you have deployed a token vending machine to generate disposable tokens like so:

```typescript
const wrappedTopicClient = await TokenRefreshingTopicClient.create({
refreshBeforeExpiryMs: 10_000, // 10 seconds before token expires, refresh it.
getDisposableToken: tokenVendingMachine,
});
```

Run the example using:

```bash
# Run example code
TVM_ENDPOINT=<YOUR TVM URL> npm run tokens
```

Example Code: [refresh-disposable-tokens.ts](refresh-disposable-tokens.ts)

If you have questions or need help experimenting further, please reach out to us!

{{ ossFooter }}
Loading
Loading