Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: client should not throw when disabled if the config is lacking #36

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
75 changes: 75 additions & 0 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { LiteralClient } from '../src';

describe('Literal Ai Config', () => {
let savedEnv: Record<string, string | undefined>;

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();
});
});
Loading