diff --git a/packages/client-sdk-nodejs/test/integration/client-constructor-default-configs.test.ts b/packages/client-sdk-nodejs/test/integration/client-constructor-default-configs.test.ts index afb1b73a9..468743e8a 100644 --- a/packages/client-sdk-nodejs/test/integration/client-constructor-default-configs.test.ts +++ b/packages/client-sdk-nodejs/test/integration/client-constructor-default-configs.test.ts @@ -3,9 +3,14 @@ import { TopicClient, PreviewVectorIndexClient, PreviewLeaderboardClient, + CredentialProvider, } from '../../src'; import {credsProvider} from './integration-setup'; +// These tokens have valid syntax, but they don't actually have valid credentials. Just used for unit testing. +const fakeTestV1ApiKey = + 'eyJhcGlfa2V5IjogImV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwYzNNaU9pSlBibXhwYm1VZ1NsZFVJRUoxYVd4a1pYSWlMQ0pwWVhRaU9qRTJOemd6TURVNE1USXNJbVY0Y0NJNk5EZzJOVFV4TlRReE1pd2lZWFZrSWpvaUlpd2ljM1ZpSWpvaWFuSnZZMnRsZEVCbGVHRnRjR3hsTG1OdmJTSjkuOEl5OHE4NExzci1EM1lDb19IUDRkLXhqSGRUOFVDSXV2QVljeGhGTXl6OCIsICJlbmRwb2ludCI6ICJ0ZXN0Lm1vbWVudG9ocS5jb20ifQo='; + describe('default configurations', () => { it('CacheClient should be able to be constructed with a default configuration', async () => { const cacheClientViaConstructor = new CacheClient({ @@ -21,6 +26,31 @@ describe('default configurations', () => { expect(cacheClientViaFactory).toBeInstanceOf(CacheClient); }); + it('CacheClient should be able to be constructed with a simple string for env var', () => { + const cacheClientViaConstructor = new CacheClient({ + credentialProvider: + CredentialProvider.fromEnvironmentVariable('TEST_AUTH_TOKEN'), + defaultTtlSeconds: 60, + }); + expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient); + }); + + it('CacheClient should be able to be constructed with a simple string for env var, using short function name', () => { + const cacheClientViaConstructor = new CacheClient({ + credentialProvider: CredentialProvider.fromEnvVar('TEST_AUTH_TOKEN'), + defaultTtlSeconds: 60, + }); + expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient); + }); + + it('CacheClient should be able to be constructed with a simple string for fromString', () => { + const cacheClientViaConstructor = new CacheClient({ + credentialProvider: CredentialProvider.fromString(fakeTestV1ApiKey), + defaultTtlSeconds: 60, + }); + expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient); + }); + it('TopicClient should be able to be constructed with a default configuration', () => { const topicClientViaConstructor = new TopicClient({ credentialProvider: credsProvider(), diff --git a/packages/client-sdk-web/test/integration/client-constructor-default-configs.test.ts b/packages/client-sdk-web/test/integration/client-constructor-default-configs.test.ts index 36fce640e..d9cfdbed6 100644 --- a/packages/client-sdk-web/test/integration/client-constructor-default-configs.test.ts +++ b/packages/client-sdk-web/test/integration/client-constructor-default-configs.test.ts @@ -3,9 +3,14 @@ import { TopicClient, PreviewVectorIndexClient, PreviewLeaderboardClient, + CredentialProvider, } from '../../src'; import {credsProvider} from './integration-setup'; +// These tokens have valid syntax, but they don't actually have valid credentials. Just used for unit testing. +const fakeTestV1ApiKey = + 'eyJhcGlfa2V5IjogImV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwYzNNaU9pSlBibXhwYm1VZ1NsZFVJRUoxYVd4a1pYSWlMQ0pwWVhRaU9qRTJOemd6TURVNE1USXNJbVY0Y0NJNk5EZzJOVFV4TlRReE1pd2lZWFZrSWpvaUlpd2ljM1ZpSWpvaWFuSnZZMnRsZEVCbGVHRnRjR3hsTG1OdmJTSjkuOEl5OHE4NExzci1EM1lDb19IUDRkLXhqSGRUOFVDSXV2QVljeGhGTXl6OCIsICJlbmRwb2ludCI6ICJ0ZXN0Lm1vbWVudG9ocS5jb20ifQo='; + describe('default configurations', () => { it('CacheClient should be able to be constructed with a default configuration', () => { const cacheClientViaConstructor = new CacheClient({ @@ -15,6 +20,31 @@ describe('default configurations', () => { expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient); }); + it('CacheClient should be able to be constructed with a simple string for env var', () => { + const cacheClientViaConstructor = new CacheClient({ + credentialProvider: + CredentialProvider.fromEnvironmentVariable('TEST_AUTH_TOKEN'), + defaultTtlSeconds: 60, + }); + expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient); + }); + + it('CacheClient should be able to be constructed with a simple string for env var, using short function name', () => { + const cacheClientViaConstructor = new CacheClient({ + credentialProvider: CredentialProvider.fromEnvVar('TEST_AUTH_TOKEN'), + defaultTtlSeconds: 60, + }); + expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient); + }); + + it('CacheClient should be able to be constructed with a simple string for fromString', () => { + const cacheClientViaConstructor = new CacheClient({ + credentialProvider: CredentialProvider.fromString(fakeTestV1ApiKey), + defaultTtlSeconds: 60, + }); + expect(cacheClientViaConstructor).toBeInstanceOf(CacheClient); + }); + it('TopicClient should be able to be constructed with a default configuration', () => { const topicClientViaConstructor = new TopicClient({ credentialProvider: credsProvider(), diff --git a/packages/core/src/auth/credential-provider.ts b/packages/core/src/auth/credential-provider.ts index 75da9dbe7..6cd8b01b4 100644 --- a/packages/core/src/auth/credential-provider.ts +++ b/packages/core/src/auth/credential-provider.ts @@ -74,13 +74,19 @@ export abstract class CredentialProvider { abstract areEndpointsOverridden(): boolean; static fromEnvironmentVariable( - props: EnvMomentoTokenProviderProps + props: EnvMomentoTokenProviderProps | string + ): CredentialProvider { + return new EnvMomentoTokenProvider(props); + } + + static fromEnvVar( + props: EnvMomentoTokenProviderProps | string ): CredentialProvider { return new EnvMomentoTokenProvider(props); } static fromString( - props: StringMomentoTokenProviderProps + props: StringMomentoTokenProviderProps | string ): CredentialProvider { return new StringMomentoTokenProvider(props); } @@ -140,7 +146,10 @@ export class StringMomentoTokenProvider extends CredentialProviderBase { /** * @param {StringMomentoTokenProviderProps} props configuration options for the token provider */ - constructor(props: StringMomentoTokenProviderProps) { + constructor(props: StringMomentoTokenProviderProps | string) { + if (typeof props === 'string') { + props = {apiKey: props}; + } super(); let key: string; if ('authToken' in props) { @@ -238,7 +247,10 @@ export class EnvMomentoTokenProvider extends StringMomentoTokenProvider { /** * @param {EnvMomentoTokenProviderProps} props configuration options for the token provider */ - constructor(props: EnvMomentoTokenProviderProps) { + constructor(props: EnvMomentoTokenProviderProps | string) { + if (typeof props === 'string') { + props = {environmentVariableName: props}; + } const authToken = process.env[props.environmentVariableName]; if (!authToken) { throw new Error(