From 9a5819502a0aa3de1aedc9fb4c66110f8e90fb84 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Tue, 5 Sep 2023 04:43:49 -0700 Subject: [PATCH 1/4] Default client auth to REPLICATE_API_TOKEN environment variable --- index.d.ts | 4 ++-- index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 50ebd1cd..32f279a7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -69,8 +69,8 @@ declare module 'replicate' { } export default class Replicate { - constructor(options: { - auth: string; + constructor(options?: { + auth?: string; userAgent?: string; baseUrl?: string; fetch?: Function; diff --git a/index.js b/index.js index 9662235f..c6a2cc20 100644 --- a/index.js +++ b/index.js @@ -32,13 +32,13 @@ class Replicate { * Create a new Replicate API client instance. * * @param {object} options - Configuration options for the client - * @param {string} options.auth - Required. API access token + * @param {string} options.auth - API access token. Defaults to the `REPLICATE_API_TOKEN` environment variable. * @param {string} options.userAgent - Identifier of your app * @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 = {}) { - this.auth = options.auth; + this.auth = options.auth || process.env.REPLICATE_API_TOKEN; this.userAgent = options.userAgent || `replicate-javascript/${packageJSON.version}`; this.baseUrl = options.baseUrl || 'https://api.replicate.com/v1'; From 99a483bd06c54c061103d17a097af8ca7d642ab3 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Tue, 5 Sep 2023 04:47:19 -0700 Subject: [PATCH 2/4] Update tests --- index.test.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.test.ts b/index.test.ts index a560da18..c35b41f9 100644 --- a/index.test.ts +++ b/index.test.ts @@ -51,9 +51,12 @@ describe('Replicate client', () => { }); test('Does not throw error if auth token is not provided', () => { + process.env.REPLICATE_API_TOKEN = 'test-token'; + expect(() => { - // @ts-expect-error - new Replicate(); + const clientWithImplicitAuth = new Replicate(); + + expect(clientWithImplicitAuth.auth).toBe('test-token'); }).not.toThrow(); }); From 67eccfdde65dc5460f2317c77b5d65a4c098495d Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Tue, 5 Sep 2023 04:51:07 -0700 Subject: [PATCH 3/4] Update README --- README.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 28423586..55d4f8e1 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,7 @@ Create the client: import Replicate from "replicate"; const replicate = new Replicate({ - // get your token from https://replicate.com/account - auth: process.env.REPLICATE_API_TOKEN, + auth: "my api token", // defaults to process.env.REPLICATE_API_TOKEN }); ``` @@ -124,18 +123,14 @@ and pass it to the `fetch` option in the constructor. import Replicate from "replicate"; import fetch from "cross-fetch"; -const replicate = new Replicate({ - // get your token from https://replicate.com/account - auth: process.env.REPLICATE_API_TOKEN, - fetch: fetch, -}); +const replicate = new Replicate({ fetch }); ``` You can override the `fetch` property to add custom behavior to client requests, such as injecting headers or adding log statements. ```js -client.fetch = (url, options) => { +replicate.fetch = (url, options) => { const headers = new Headers(options && options.headers); headers.append("X-Custom-Header", "some value"); From 9d01bf9a35585edcdba9c8e407d6103500e9a7c0 Mon Sep 17 00:00:00 2001 From: Mattt Date: Mon, 2 Oct 2023 03:33:47 -0700 Subject: [PATCH 4/4] Apply suggestions from code review --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 55d4f8e1..d6f02572 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Create the client: import Replicate from "replicate"; const replicate = new Replicate({ + // get your token from https://replicate.com/account auth: "my api token", // defaults to process.env.REPLICATE_API_TOKEN }); ```