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: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```

Expand Down Expand Up @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 5 additions & 2 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down