Skip to content

Commit 3c04fbd

Browse files
authored
Support implicit auth using environment variable (#127)
* Default client auth to REPLICATE_API_TOKEN environment variable * Update tests * Update README * Apply suggestions from code review
1 parent 4b0d9cb commit 3c04fbd

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Replicate from "replicate";
2727

2828
const replicate = new Replicate({
2929
// get your token from https://replicate.com/account
30-
auth: process.env.REPLICATE_API_TOKEN,
30+
auth: "my api token", // defaults to process.env.REPLICATE_API_TOKEN
3131
});
3232
```
3333

@@ -124,18 +124,14 @@ and pass it to the `fetch` option in the constructor.
124124
import Replicate from "replicate";
125125
import fetch from "cross-fetch";
126126

127-
const replicate = new Replicate({
128-
// get your token from https://replicate.com/account
129-
auth: process.env.REPLICATE_API_TOKEN,
130-
fetch: fetch,
131-
});
127+
const replicate = new Replicate({ fetch });
132128
```
133129

134130
You can override the `fetch` property to add custom behavior to client requests,
135131
such as injecting headers or adding log statements.
136132

137133
```js
138-
client.fetch = (url, options) => {
134+
replicate.fetch = (url, options) => {
139135
const headers = new Headers(options && options.headers);
140136
headers.append("X-Custom-Header", "some value");
141137

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ declare module 'replicate' {
6969
}
7070

7171
export default class Replicate {
72-
constructor(options: {
73-
auth: string;
72+
constructor(options?: {
73+
auth?: string;
7474
userAgent?: string;
7575
baseUrl?: string;
7676
fetch?: Function;

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ class Replicate {
3232
* Create a new Replicate API client instance.
3333
*
3434
* @param {object} options - Configuration options for the client
35-
* @param {string} options.auth - Required. API access token
35+
* @param {string} options.auth - API access token. Defaults to the `REPLICATE_API_TOKEN` environment variable.
3636
* @param {string} options.userAgent - Identifier of your app
3737
* @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1
3838
* @param {Function} [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
3939
*/
4040
constructor(options = {}) {
41-
this.auth = options.auth;
41+
this.auth = options.auth || process.env.REPLICATE_API_TOKEN;
4242
this.userAgent =
4343
options.userAgent || `replicate-javascript/${packageJSON.version}`;
4444
this.baseUrl = options.baseUrl || 'https://api.replicate.com/v1';

index.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ describe('Replicate client', () => {
5151
});
5252

5353
test('Does not throw error if auth token is not provided', () => {
54+
process.env.REPLICATE_API_TOKEN = 'test-token';
55+
5456
expect(() => {
55-
// @ts-expect-error
56-
new Replicate();
57+
const clientWithImplicitAuth = new Replicate();
58+
59+
expect(clientWithImplicitAuth.auth).toBe('test-token');
5760
}).not.toThrow();
5861
});
5962

0 commit comments

Comments
 (0)