diff --git a/src/api.ts b/src/api.ts index 9b1ae15..ca6c6c1 100644 --- a/src/api.ts +++ b/src/api.ts @@ -335,8 +335,6 @@ export class API { private graphqlEndpoint: string; /** @ignore */ private restEndpoint: string; - /** @ignore */ - public disabled: boolean; /** @ignore */ constructor(apiKey: string, url: string, disabled?: boolean) { @@ -345,7 +343,21 @@ export class API { this.graphqlEndpoint = `${url}/api/graphql`; this.restEndpoint = `${url}/api`; this.disabled = !!disabled; + this.checkConfig(); + } + + /** @ignore */ + private _disabled = false; + get disabled() { + return this._disabled; + } + set disabled(value: boolean) { + this._disabled = value; + this.checkConfig(); + } + private checkConfig() { + if (this.disabled) return; if (!this.apiKey) { throw new Error('LITERAL_API_KEY not set'); } diff --git a/tests/config.test.ts b/tests/config.test.ts new file mode 100644 index 0000000..4901d4c --- /dev/null +++ b/tests/config.test.ts @@ -0,0 +1,75 @@ +import { LiteralClient } from '../src'; + +describe('Literal Ai Config', () => { + let savedEnv: Record; + + beforeAll(() => { + savedEnv = { ...process.env }; + delete process.env.LITERAL_API_URL; + delete process.env.LITERAL_API_KEY; + }); + + afterAll(() => { + process.env = { ...savedEnv }; + }); + + it('should not throw when disabled', () => { + let client: LiteralClient | undefined; + expect(() => { + client = new LiteralClient(undefined, undefined, true); + }).not.toThrow(); + + expect(client).toBeDefined(); + + // HACK: This is to access private properties + const { url, apiKey, disabled } = client!.api as any; + expect(url).toBe('https://cloud.getliteral.ai'); + expect(apiKey).toBe(undefined); + expect(disabled).toBe(true); + }); + + it('should not throw when keys given', () => { + let client: LiteralClient | undefined; + expect(() => { + client = new LiteralClient('KEY', 'URL'); + }).not.toThrow(); + + expect(client).toBeDefined(); + + // HACK: This is to access private properties + const { url, apiKey, disabled } = client!.api as any; + expect(url).toBe('URL'); + expect(apiKey).toBe('KEY'); + expect(disabled).toBe(false); + }); + + it('should not throw when keys are in env', () => { + process.env.LITERAL_API_URL = 'URL'; + process.env.LITERAL_API_KEY = 'KEY'; + + let client: LiteralClient | undefined; + expect(() => { + client = new LiteralClient('KEY', 'URL'); + }).not.toThrow(); + + delete process.env.LITERAL_API_URL; + delete process.env.LITERAL_API_KEY; + + expect(client).toBeDefined(); + + // HACK: This is to access private properties + const { url, apiKey, disabled } = client!.api as any; + expect(url).toBe('URL'); + expect(apiKey).toBe('KEY'); + expect(disabled).toBe(false); + }); + + it('should throw when no keys given or found', () => { + let client: LiteralClient | undefined; + expect(() => { + client = new LiteralClient(); + }).toThrow(); + + expect(client).toBeUndefined(); + }); +});