Skip to content

Commit

Permalink
feat: client should not throw when disabled if the config is lacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Granipouss committed Jun 27, 2024
1 parent 0d474b6 commit a32a30e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
}
Expand Down
63 changes: 63 additions & 0 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit a32a30e

Please sign in to comment.