Skip to content

Commit 826b6aa

Browse files
committed
Export singleton instance of replicate by default
This makes the library very quick to get up and running. We use a proxy instance to ensure that the library keeps working in its existing state though with a @deprecated type annotation which will show up in editors.
1 parent c26c3f0 commit 826b6aa

File tree

5 files changed

+586
-378
lines changed

5 files changed

+586
-378
lines changed

index.d.ts

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
declare module "replicate" {
2-
type Status = "starting" | "processing" | "succeeded" | "failed" | "canceled";
3-
type Visibility = "public" | "private";
4-
type WebhookEventType = "start" | "output" | "logs" | "completed";
2+
export type Status = "starting" | "processing" | "succeeded" | "failed" | "canceled";
3+
export type Visibility = "public" | "private";
4+
export type WebhookEventType = "start" | "output" | "logs" | "completed";
55

66
export interface ApiError extends Error {
77
request: Request;
@@ -82,7 +82,7 @@ declare module "replicate" {
8282
retry?: number;
8383
}
8484

85-
export default class Replicate {
85+
export class Replicate {
8686
constructor(options?: {
8787
auth?: string;
8888
userAgent?: string;
@@ -223,4 +223,74 @@ declare module "replicate" {
223223
list(): Promise<Page<Training>>;
224224
};
225225
}
226+
227+
/** @deprecated */
228+
class DeprecatedReplicate extends Replicate {
229+
/** @deprecated Use `const Replicate = require("replicate").Replicate` instead */
230+
constructor(...args: ConstructorParameters<typeof Replicate>);
231+
}
232+
233+
234+
/**
235+
* Default instance of the Replicate class that gets the access token
236+
* from the REPLICATE_API_TOKEN environment variable.
237+
*
238+
* Create a new Replicate API client instance.
239+
*
240+
* @example
241+
*
242+
* import replicate from "replicate";
243+
*
244+
* // Run a model and await the result:
245+
* const model = 'owner/model:version-id'
246+
* const input = {text: 'Hello, world!'}
247+
* const output = await replicate.run(model, { input });
248+
*
249+
* @remarks
250+
*
251+
* NOTE: Use of this object as a constructor is deprecated and will
252+
* be removed in a future version. Import the Replicate constructor
253+
* instead:
254+
*
255+
* ```
256+
* const Replicate = require("replicate").Replicate;
257+
* ```
258+
*
259+
* Or using esm:
260+
*
261+
* ```
262+
* import replicate from "replicate";
263+
* const client = new replicate.Replicate({...});
264+
* ```
265+
*
266+
* @type { Replicate & typeof DeprecatedReplicate & {ApiError: ApiError, Replicate: Replicate} }
267+
*/
268+
const replicate: Replicate & typeof DeprecatedReplicate & {
269+
/**
270+
* Create a new Replicate API client instance.
271+
*
272+
* @example
273+
* // Create a new Replicate API client instance
274+
* const Replicate = require("replicate");
275+
* const replicate = new Replicate({
276+
* // get your token from https://replicate.com/account
277+
* auth: process.env.REPLICATE_API_TOKEN,
278+
* userAgent: "my-app/1.2.3"
279+
* });
280+
*
281+
* // Run a model and await the result:
282+
* const model = 'owner/model:version-id'
283+
* const input = {text: 'Hello, world!'}
284+
* const output = await replicate.run(model, { input });
285+
*
286+
* @param {object} options - Configuration options for the client
287+
* @param {string} options.auth - API access token. Defaults to the `REPLICATE_API_TOKEN` environment variable.
288+
* @param {string} options.userAgent - Identifier of your app
289+
* @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1
290+
* @param {Function} [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
291+
*/
292+
Replicate: typeof Replicate
293+
};
294+
295+
export default replicate;
226296
}

0 commit comments

Comments
 (0)