Skip to content

Commit

Permalink
Add missing TypeScript definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Mar 18, 2024
1 parent 64ad79a commit 207f0b9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 35 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Prediction>} - Resolves with the output of running the model
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/accounts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/** @typedef {import("./types").Account} Account */

/**
* Get the current account
*
* @returns {Promise<object>} Resolves with the current account
* @returns {Promise<Account>} Resolves with the current account
*/
async function getCurrentAccount() {
const response = await this.request("/account", {
Expand Down
4 changes: 3 additions & 1 deletion lib/deployments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** @typedef {import("./types").Deployment} Deployment */
/** @typedef {import("./types").Prediction} Prediction */
/** @typedef {import("./types").WebhookEventType} WebhookEventType */

const { transformFileInputs } = require("./util");

Expand Down Expand Up @@ -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<object>} Resolves with the deployment data
* @returns {Promise<Deployment>} Resolves with the deployment data
*/
async function getDeployment(deployment_owner, deployment_name) {
const response = await this.request(
Expand Down
3 changes: 3 additions & 0 deletions lib/predictions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
* @template T
* @typedef {import("./types").Page<T>} Page
*/

/**
* @typedef {import("./types").Prediction} Prediction
* @typedef {Object} BasePredictionOptions
* @property {unknown} input - Required. An object with the model inputs
Expand Down
2 changes: 1 addition & 1 deletion lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response>} [config.fetch] The URL to connect to.
* @param {object} [config.options] The EventSource options.
* @returns {ReadableStream<ServerSentEvent> & AsyncIterable<ServerSentEvent>}
*/
Expand Down
24 changes: 22 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
/**
/**
* @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
* @property {string} slug
* @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
Expand Down
31 changes: 8 additions & 23 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -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<boolean>} - True if the signature is valid
* @throws {Error} - If the request is missing required headers, body, or secret
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/webhooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Get the default webhook signing secret
*
* @returns {Promise<object>} 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", {
Expand Down
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 207f0b9

Please sign in to comment.