File tree Expand file tree Collapse file tree 4 files changed +12
-13
lines changed Expand file tree Collapse file tree 4 files changed +12
-13
lines changed Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ import Replicate from "replicate";
27
27
28
28
const replicate = new Replicate ({
29
29
// 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
31
31
});
32
32
```
33
33
@@ -124,18 +124,14 @@ and pass it to the `fetch` option in the constructor.
124
124
import Replicate from " replicate" ;
125
125
import fetch from " cross-fetch" ;
126
126
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 });
132
128
```
133
129
134
130
You can override the ` fetch ` property to add custom behavior to client requests,
135
131
such as injecting headers or adding log statements.
136
132
137
133
``` js
138
- client .fetch = (url , options ) => {
134
+ replicate .fetch = (url , options ) => {
139
135
const headers = new Headers (options && options .headers );
140
136
headers .append (" X-Custom-Header" , " some value" );
141
137
Original file line number Diff line number Diff line change @@ -69,8 +69,8 @@ declare module 'replicate' {
69
69
}
70
70
71
71
export default class Replicate {
72
- constructor ( options : {
73
- auth : string ;
72
+ constructor ( options ? : {
73
+ auth ? : string ;
74
74
userAgent ?: string ;
75
75
baseUrl ?: string ;
76
76
fetch ?: Function ;
Original file line number Diff line number Diff line change @@ -32,13 +32,13 @@ class Replicate {
32
32
* Create a new Replicate API client instance.
33
33
*
34
34
* @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.
36
36
* @param {string } options.userAgent - Identifier of your app
37
37
* @param {string } [options.baseUrl] - Defaults to https://api.replicate.com/v1
38
38
* @param {Function } [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
39
39
*/
40
40
constructor ( options = { } ) {
41
- this . auth = options . auth ;
41
+ this . auth = options . auth || process . env . REPLICATE_API_TOKEN ;
42
42
this . userAgent =
43
43
options . userAgent || `replicate-javascript/${ packageJSON . version } ` ;
44
44
this . baseUrl = options . baseUrl || 'https://api.replicate.com/v1' ;
Original file line number Diff line number Diff line change @@ -51,9 +51,12 @@ describe('Replicate client', () => {
51
51
} ) ;
52
52
53
53
test ( 'Does not throw error if auth token is not provided' , ( ) => {
54
+ process . env . REPLICATE_API_TOKEN = 'test-token' ;
55
+
54
56
expect ( ( ) => {
55
- // @ts -expect-error
56
- new Replicate ( ) ;
57
+ const clientWithImplicitAuth = new Replicate ( ) ;
58
+
59
+ expect ( clientWithImplicitAuth . auth ) . toBe ( 'test-token' ) ;
57
60
} ) . not . toThrow ( ) ;
58
61
} ) ;
59
62
You can’t perform that action at this time.
0 commit comments