diff --git a/index.js b/index.js index 72153bb..a54c729 100644 --- a/index.js +++ b/index.js @@ -147,7 +147,7 @@ class Replicate { * @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output * @param {WebhookEventType[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`) * @param {AbortSignal} [options.signal] - AbortSignal to cancel the prediction - * @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. + * @param {(p: Prediction) => void} [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. * @throws {Error} If the reference is invalid * @throws {Error} If the prediction failed * @returns {Promise} - Resolves with the output of running the model @@ -421,6 +421,7 @@ module.exports.parseProgressFromLogs = parseProgressFromLogs; /** * @typedef {import("./lib/error")} ApiError + * @typedef {import("./lib/types").Account} Account * @typedef {import("./lib/types").Collection} Collection * @typedef {import("./lib/types").ModelVersion} ModelVersion * @typedef {import("./lib/types").Hardware} Hardware diff --git a/lib/accounts.js b/lib/accounts.js index b3bbd9f..fda3a40 100644 --- a/lib/accounts.js +++ b/lib/accounts.js @@ -1,7 +1,9 @@ +/** @typedef {import("./types").Account} Account */ + /** * Get the current account * - * @returns {Promise} Resolves with the current account + * @returns {Promise} Resolves with the current account */ async function getCurrentAccount() { const response = await this.request("/account", { diff --git a/lib/deployments.js b/lib/deployments.js index 653ecea..29bc3a3 100644 --- a/lib/deployments.js +++ b/lib/deployments.js @@ -1,4 +1,6 @@ +/** @typedef {import("./types").Deployment} Deployment */ /** @typedef {import("./types").Prediction} Prediction */ +/** @typedef {import("./types").WebhookEventType} WebhookEventType */ const { transformFileInputs } = require("./util"); @@ -45,7 +47,7 @@ async function createPrediction(deployment_owner, deployment_name, options) { * * @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment * @param {string} deployment_name - Required. The name of the deployment - * @returns {Promise} Resolves with the deployment data + * @returns {Promise} Resolves with the deployment data */ async function getDeployment(deployment_owner, deployment_name) { const response = await this.request( diff --git a/lib/predictions.js b/lib/predictions.js index d354e77..a3ab440 100644 --- a/lib/predictions.js +++ b/lib/predictions.js @@ -1,6 +1,9 @@ /** * @template T * @typedef {import("./types").Page} Page + */ + +/** * @typedef {import("./types").Prediction} Prediction * @typedef {Object} BasePredictionOptions * @property {unknown} input - Required. An object with the model inputs diff --git a/lib/stream.js b/lib/stream.js index cd9274c..d14f48c 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -41,7 +41,7 @@ class ServerSentEvent { * * @param {object} config * @param {string} config.url The URL to connect to. - * @param {typeof fetch} [config.fetch] The URL to connect to. + * @param {(url: URL | RequestInfo, init?: RequestInit | undefined) => Promise} [config.fetch] The URL to connect to. * @param {object} [config.options] The EventSource options. * @returns {ReadableStream & AsyncIterable} */ diff --git a/lib/types.js b/lib/types.js index fd05845..9e93e3f 100644 --- a/lib/types.js +++ b/lib/types.js @@ -1,7 +1,13 @@ -/** +/** * @typedef {"starting" | "processing" | "succeeded" | "failed" | "canceled"} Status * @typedef {"public" | "private"} Visibility - * @typedef {"start" | "output" | "logs" | "completed"} WebhookEventType + * @typedef {"start" | "output" | "logs" | "completed"} WebhookEventType + * + * @typedef {Object} Account + * @property {"user" | "organization"} type + * @property {string} username + * @property {string} name + * @property {string=} github_url * * @typedef {Object} Collection * @property {string} name @@ -9,6 +15,20 @@ * @property {string} description * @property {Model[]=} models * + * @typedef {Object} Deployment + * @property {string} owner + * @property {string} name + * @property {object} current_release + * @property {number} current_release.number + * @property {string} current_release.model + * @property {string} current_release.version + * @property {string} current_release.created_at + * @property {Account} current_release.created_by + * @property {object} current_release.configuration + * @property {string} current_release.configuration.hardware + * @property {number} current_release.configuration.min_instances + * @property {number} current_release.configuration.max_instances + * * @typedef {Object} Hardware * @property {string} sku * @property {string} name diff --git a/lib/util.js b/lib/util.js index ff9dacc..47738f4 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,31 +1,16 @@ const ApiError = require("./error"); -/** - * @see {@link validateWebhook} - * @overload - * @param {object} requestData - The request data - * @param {string} requestData.id - The webhook ID header from the incoming request. - * @param {string} requestData.timestamp - The webhook timestamp header from the incoming request. - * @param {string} requestData.body - The raw body of the incoming webhook request. - * @param {string} requestData.secret - The webhook secret, obtained from `replicate.webhooks.defaul.secret` method. - * @param {string} requestData.signature - The webhook signature header from the incoming request, comprising one or more space-delimited signatures. - */ - -/** - * @see {@link validateWebhook} - * @overload - * @param {object} requestData - The request object - * @param {object} requestData.headers - The request headers - * @param {string} requestData.headers["webhook-id"] - The webhook ID header from the incoming request - * @param {string} requestData.headers["webhook-timestamp"] - The webhook timestamp header from the incoming request - * @param {string} requestData.headers["webhook-signature"] - The webhook signature header from the incoming request, comprising one or more space-delimited signatures - * @param {string} requestData.body - The raw body of the incoming webhook request - * @param {string} secret - The webhook secret, obtained from `replicate.webhooks.defaul.secret` method - */ - /** * Validate a webhook signature * + * @typedef {Object} WebhookPayload + * @property {string} id - The webhook ID header from the incoming request. + * @property {string} timestamp - The webhook timestamp header from the incoming request. + * @property {string} body - The raw body of the incoming webhook request. + * @property {string} signature - The webhook signature header from the incoming request, comprising one or more space-delimited signatures. + * + * @param {Request | WebhookPayload} requestData + * @param {string} secret - The webhook secret, obtained from `replicate.webhooks.defaul.secret` method. * @returns {Promise} - True if the signature is valid * @throws {Error} - If the request is missing required headers, body, or secret */ diff --git a/lib/webhooks.js b/lib/webhooks.js index f1324ec..e484df8 100644 --- a/lib/webhooks.js +++ b/lib/webhooks.js @@ -1,7 +1,7 @@ /** * Get the default webhook signing secret * - * @returns {Promise} Resolves with the signing secret for the default webhook + * @returns {Promise<{key: string}>} Resolves with the signing secret for the default webhook */ async function getDefaultWebhookSecret() { const response = await this.request("/webhooks/default/secret", { diff --git a/package.json b/package.json index d8c97c4..1df0fef 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "yarn": ">=1.7.0" }, "scripts": { - "build": "npm run build:types", + "build": "npm run build:types && tsc --noEmit dist/types/**/*.d.ts", "build:types": "tsc --target ES2022 --declaration --emitDeclarationOnly --allowJs --types node --outDir ./dist/types index.js", "check": "tsc", "format": "biome format . --write", @@ -39,14 +39,10 @@ "test:integration": "npm run build; for x in commonjs esm typescript; do npm --prefix integration/$x install --omit=dev && npm --prefix integration/$x test; done;", "test:all": "npm run check; npm run test; npm run test:integration" }, - "optionalDependencies": { - "readable-stream": ">=4.0.0" - }, "devDependencies": { "@biomejs/biome": "^1.4.1", "@types/jest": "^29.5.3", "@typescript-eslint/eslint-plugin": "^5.56.0", - "cross-fetch": "^3.1.5", "jest": "^29.6.2", "nock": "^14.0.0-beta.4", "publint": "^0.2.7",