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";
2727
2828const 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.
124124import Replicate from " replicate" ;
125125import 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
134130You can override the ` fetch ` property to add custom behavior to client requests,
135131such 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
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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' ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments