From 050221b6f1b7a9690647bc4183caa3ab67e459e7 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Tue, 6 Jun 2023 13:35:17 -0700 Subject: [PATCH] Throw error when constructing client without required auth parameter --- index.js | 4 ++++ index.test.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/index.js b/index.js index 390038d1..ce97fcfd 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,10 @@ class Replicate { * @param {Function} [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch` */ constructor(options) { + if (!options.auth) { + throw new Error('Missing required parameter: auth'); + } + this.auth = options.auth; this.userAgent = options.userAgent || `replicate-javascript/${packageJSON.version}`; diff --git a/index.test.ts b/index.test.ts index eab4ffec..3bcfc86a 100644 --- a/index.test.ts +++ b/index.test.ts @@ -33,6 +33,22 @@ 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' + + expect(() => { + new Replicate({ auth: undefined }); + }).toThrow(expected); + + expect(() => { + new Replicate({ auth: null }); + }).toThrow(expected); + + expect(() => { + new Replicate({ auth: "" }); + }).toThrow(expected); + }); }); describe('collections.list', () => {