diff --git a/examples/nodejs/storage/advanced.ts b/examples/nodejs/storage/advanced.ts new file mode 100644 index 000000000..39289017f --- /dev/null +++ b/examples/nodejs/storage/advanced.ts @@ -0,0 +1,44 @@ +import { + CredentialProvider, + DefaultMomentoLoggerFactory, + DefaultMomentoLoggerLevel, + FixedTimeoutRetryStrategy, + ListStoresResponse, + MomentoLoggerFactory, + PreviewStorageClient, + StorageConfigurations, +} from '@gomomento/sdk'; + +async function main() { + // You can customize your log level or provide your own logger factory to + // integrate with your favorite logging framework + const loggerFactory: MomentoLoggerFactory = new DefaultMomentoLoggerFactory(DefaultMomentoLoggerLevel.DEBUG); + const logger = loggerFactory.getLogger('AdvancedStorageExample'); + + // You can specify the logger factory and override the default RetryStrategy + const storageClient = new PreviewStorageClient({ + configuration: StorageConfigurations.Laptop.latest(loggerFactory).withRetryStrategy( + new FixedTimeoutRetryStrategy({ + loggerFactory: loggerFactory, + responseDataReceivedTimeoutMillis: 2000, + retryDelayIntervalMillis: 200, + }) + ), + credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'), + }); + + const response = await storageClient.listStores(); + switch (response.type) { + case ListStoresResponse.Success: + logger.info('Stores:', response.stores()); + break; + case ListStoresResponse.Error: + logger.error('Error listing stores:', response.innerException()); + break; + } +} + +main().catch(e => { + console.error('An error occurred! ', e); + throw e; +}); diff --git a/examples/nodejs/storage/package.json b/examples/nodejs/storage/package.json index 258cc07d0..3bb217582 100644 --- a/examples/nodejs/storage/package.json +++ b/examples/nodejs/storage/package.json @@ -7,8 +7,9 @@ "prebuild": "eslint . --ext .ts", "build": "tsc", "basic": "tsc && node dist/basic.js", + "advanced": "tsc && node dist/advanced.js", "doc-examples": "tsc && node dist/doc-examples-js-apis.js", - "validate-examples": "tsc && node dist/doc-example-files/doc-examples-js-apis.js && node dist/doc-example-files/cheat-sheet-main.js", + "validate-examples": "tsc && node dist/doc-example-files/doc-examples-js-apis.js && node dist/doc-example-files/cheat-sheet-main.js && node dist/basic.js && node dist/advanced.js", "test": "jest", "lint": "eslint . --ext .ts", "format": "eslint . --ext .ts --fix"