Skip to content
Merged
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
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ class Replicate {
* @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1
* @param {Function} [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
*/
constructor(options) {
if (!options.auth) {
throw new Error('Missing required parameter: auth');
}

constructor(options = {}) {
this.auth = options.auth;
this.userAgent =
options.userAgent || `replicate-javascript/${packageJSON.version}`;
Expand Down Expand Up @@ -187,7 +183,9 @@ class Replicate {
});

const headers = new Headers();
headers.append('Authorization', `Token ${auth}`);
if (auth) {
headers.append('Authorization', `Token ${auth}`);
}
headers.append('Content-Type', 'application/json');
headers.append('User-Agent', userAgent);
if (options.headers) {
Expand Down
11 changes: 8 additions & 3 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ describe('Replicate client', () => {
expect(clientWithCustomUserAgent.userAgent).toBe('my-app/1.2.3');
});

test('Throws error if no auth token is provided', () => {
const expected = 'Missing required parameter: auth'
test('Does not throw error if auth token is not provided', () => {
expect(() => {
// @ts-expect-error
new Replicate();
}).not.toThrow();
});

test('Does not throw error if blank auth token is provided', () => {
expect(() => {
new Replicate({ auth: "" });
}).toThrow(expected);
}).not.toThrow();
});
});

Expand Down