From a32a30e547e41a840d0fcba6f2012a0311f6ae1a Mon Sep 17 00:00:00 2001 From: Brendan D Date: Thu, 27 Jun 2024 10:40:42 +0200 Subject: [PATCH 1/2] feat: client should not throw when disabled if the config is lacking --- src/api.ts | 16 +++++++++-- tests/config.test.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 tests/config.test.ts 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..c944a7a --- /dev/null +++ b/tests/config.test.ts @@ -0,0 +1,63 @@ +import { LiteralClient } from '../src'; + +describe('Literal Ai Config', () => { + 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(); + }); +}); From 3dca3a314292e093361eb4cca1f26092719e026a Mon Sep 17 00:00:00 2001 From: Brendan D Date: Thu, 27 Jun 2024 11:21:35 +0200 Subject: [PATCH 2/2] fix: safer tests on process env --- tests/config.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/config.test.ts b/tests/config.test.ts index c944a7a..4901d4c 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -1,6 +1,18 @@ 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(() => {