Skip to content

Commit cac159f

Browse files
committed
Generate TypeScript definitions from source
1 parent 6dd5b21 commit cac159f

19 files changed

+323
-284
lines changed

index.d.ts

-226
This file was deleted.

index.js

+60-10
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,59 @@ class Replicate {
3434
/**
3535
* Create a new Replicate API client instance.
3636
*
37-
* @param {object} options - Configuration options for the client
38-
* @param {string} options.auth - API access token. Defaults to the `REPLICATE_API_TOKEN` environment variable.
39-
* @param {string} options.userAgent - Identifier of your app
37+
* @example
38+
* // Create a new Replicate API client instance
39+
* const Replicate = require("replicate");
40+
* const replicate = new Replicate({
41+
* // get your token from https://replicate.com/account
42+
* auth: process.env.REPLICATE_API_TOKEN,
43+
* userAgent: "my-app/1.2.3"
44+
* });
45+
*
46+
* // Run a model and await the result:
47+
* const model = 'owner/model:version-id'
48+
* const input = {text: 'Hello, world!'}
49+
* const output = await replicate.run(model, { input });
50+
*
51+
* @param {Object} [options] - Configuration options for the client
52+
* @param {string} [options.auth] - API access token. Defaults to the `REPLICATE_API_TOKEN` environment variable.
53+
* @param {string} [options.userAgent] - Identifier of your app
4054
* @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1
4155
* @param {Function} [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
4256
*/
4357
constructor(options = {}) {
58+
/** @type {string} */
4459
this.auth = options.auth || process.env.REPLICATE_API_TOKEN;
60+
61+
/** @type {string} */
4562
this.userAgent =
4663
options.userAgent || `replicate-javascript/${packageJSON.version}`;
64+
65+
/** @type {string} */
4766
this.baseUrl = options.baseUrl || "https://api.replicate.com/v1";
67+
68+
/** @type {fetch} */
4869
this.fetch = options.fetch || globalThis.fetch;
4970

71+
/** @type {collections} */
5072
this.collections = {
5173
list: collections.list.bind(this),
5274
get: collections.get.bind(this),
5375
};
5476

77+
/** @type {deployments} */
5578
this.deployments = {
5679
predictions: {
5780
create: deployments.predictions.create.bind(this),
5881
},
5982
};
6083

84+
/** @type {hardware} */
6185
this.hardware = {
6286
list: hardware.list.bind(this),
6387
};
6488

89+
/** @type {models} */
6590
this.models = {
6691
get: models.get.bind(this),
6792
list: models.list.bind(this),
@@ -72,13 +97,15 @@ class Replicate {
7297
},
7398
};
7499

100+
/** @type {predictions} */
75101
this.predictions = {
76102
create: predictions.create.bind(this),
77103
get: predictions.get.bind(this),
78104
cancel: predictions.cancel.bind(this),
79105
list: predictions.list.bind(this),
80106
};
81107

108+
/** @type {trainings} */
82109
this.trainings = {
83110
create: trainings.create.bind(this),
84111
get: trainings.get.bind(this),
@@ -90,18 +117,18 @@ class Replicate {
90117
/**
91118
* Run a model and wait for its output.
92119
*
93-
* @param {string} ref - Required. The model version identifier in the format "owner/name" or "owner/name:version"
120+
* @param {`${string}/${string}` | `${string}/${string}:${string}`} ref - Required. The model version identifier in the format "owner/name" or "owner/name:version"
94121
* @param {object} options
95122
* @param {object} options.input - Required. An object with the model inputs
96123
* @param {object} [options.wait] - Options for waiting for the prediction to finish
97124
* @param {number} [options.wait.interval] - Polling interval in milliseconds. Defaults to 500
98125
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
99-
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
126+
* @param {WebhookEventType[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
100127
* @param {AbortSignal} [options.signal] - AbortSignal to cancel the prediction
101128
* @param {Function} [progress] - Callback function that receives the prediction object as it's updated. The function is called when the prediction is created, each time its updated while polling for completion, and when it's completed.
102129
* @throws {Error} If the reference is invalid
103130
* @throws {Error} If the prediction failed
104-
* @returns {Promise<object>} - Resolves with the output of running the model
131+
* @returns {Promise<Prediction>} - Resolves with the output of running the model
105132
*/
106133
async run(ref, options, progress) {
107134
const { wait, ...data } = options;
@@ -237,7 +264,7 @@ class Replicate {
237264
/**
238265
* Stream a model and wait for its output.
239266
*
240-
* @param {string} identifier - Required. The model version identifier in the format "{owner}/{name}:{version}"
267+
* @param {string} ref - Required. The model version identifier in the format "{owner}/{name}:{version}"
241268
* @param {object} options
242269
* @param {object} options.input - Required. An object with the model inputs
243270
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
@@ -285,8 +312,10 @@ class Replicate {
285312
* for await (const page of replicate.paginate(replicate.predictions.list) {
286313
* console.log(page);
287314
* }
288-
* @param {Function} endpoint - Function that returns a promise for the next page of results
289-
* @yields {object[]} Each page of results
315+
* @template T
316+
* @param {() => Promise<Page<T>>} endpoint - Function that returns a promise for the next page of results
317+
* @yields {T[]} Each page of results
318+
* @returns {AsyncGenerator<T[], void, unknown>}
290319
*/
291320
async *paginate(endpoint) {
292321
const response = await endpoint();
@@ -312,7 +341,7 @@ class Replicate {
312341
* @param {Function} [stop] - Async callback function that is called after each polling attempt. Receives the prediction object as an argument. Return false to cancel polling.
313342
* @throws {Error} If the prediction doesn't complete within the maximum number of attempts
314343
* @throws {Error} If the prediction failed
315-
* @returns {Promise<object>} Resolves with the completed prediction object
344+
* @returns {Promise<Prediction>} Resolves with the completed prediction object
316345
*/
317346
async wait(prediction, options, stop) {
318347
const { id } = prediction;
@@ -359,3 +388,24 @@ class Replicate {
359388
}
360389

361390
module.exports = Replicate;
391+
392+
// - Type Definitions
393+
394+
/**
395+
* @typedef {import("./lib/error")} ApiError
396+
* @typedef {import("./lib/types").Collection} Collection
397+
* @typedef {import("./lib/types").ModelVersion} ModelVersion
398+
* @typedef {import("./lib/types").Hardware} Hardware
399+
* @typedef {import("./lib/types").Model} Model
400+
* @typedef {import("./lib/types").Prediction} Prediction
401+
* @typedef {import("./lib/types").Training} Training
402+
* @typedef {import("./lib/types").ServerSentEvent} ServerSentEvent
403+
* @typedef {import("./lib/types").Status} Status
404+
* @typedef {import("./lib/types").Visibility} Visibility
405+
* @typedef {import("./lib/types").WebhookEventType} WebhookEventType
406+
*/
407+
408+
/**
409+
* @template T
410+
* @typedef {import("./lib/types").Page<T>} Page
411+
*/

index.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, jest, test } from "@jest/globals";
2-
import Replicate, { ApiError, Model, Prediction } from "replicate";
2+
import Replicate, { ApiError, Model, Prediction } from "./";
33
import nock from "nock";
44
import fetch from "cross-fetch";
55

@@ -838,7 +838,6 @@ describe("Replicate client", () => {
838838
});
839839

840840
test("Calls the correct API routes for a model", async () => {
841-
const firstPollingRequest = true;
842841

843842
nock(BASE_URL)
844843
.post("/models/replicate/hello-world/predictions")

0 commit comments

Comments
 (0)