diff --git a/README.md b/README.md index 2842358..d6f0257 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ 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 +124,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"); diff --git a/index.d.ts b/index.d.ts index 50ebd1c..32f279a 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 9662235..c6a2cc2 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'; diff --git a/index.test.ts b/index.test.ts index a560da1..c35b41f 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(); });