Skip to content

Commit

Permalink
Add missing webhooks type annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Mar 19, 2024
1 parent fb22f14 commit 6b6eb15
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 94 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class Replicate {
list: trainings.list.bind(this),
};

/** @type {webhooks} */
this.webhooks = {
default: {
secret: {
Expand Down Expand Up @@ -428,6 +429,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").Deployment} Deployment
* @typedef {import("./lib/types").ModelVersion} ModelVersion
* @typedef {import("./lib/types").Hardware} Hardware
* @typedef {import("./lib/types").Model} Model
Expand Down
28 changes: 9 additions & 19 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,8 @@ describe("Replicate client", () => {
},
configuration: {
hardware: "gpu-t4",
scaling: {
min_instances: 1,
max_instances: 5,
},
min_instances: 1,
max_instances: 5,
},
},
});
Expand Down Expand Up @@ -831,10 +829,8 @@ describe("Replicate client", () => {
},
configuration: {
hardware: "gpu-t4",
scaling: {
min_instances: 1,
max_instances: 5,
},
min_instances: 1,
max_instances: 5,
},
},
});
Expand Down Expand Up @@ -877,10 +873,8 @@ describe("Replicate client", () => {
},
configuration: {
hardware: "gpu-a40-large",
scaling: {
min_instances: 3,
max_instances: 10,
},
min_instances: 3,
max_instances: 10,
},
},
});
Expand All @@ -904,12 +898,8 @@ describe("Replicate client", () => {
expect(deployment.current_release.configuration.hardware).toBe(
"gpu-a40-large"
);
expect(
deployment.current_release.configuration.scaling?.min_instances
).toBe(3);
expect(
deployment.current_release.configuration.scaling?.max_instances
).toBe(10);
expect(deployment.current_release.configuration.min_instances).toBe(3);
expect(deployment.current_release.configuration.max_instances).toBe(10);
});
// Add more tests for error handling, edge cases, etc.
});
Expand All @@ -934,7 +924,7 @@ describe("Replicate client", () => {
});

const deployments = await client.deployments.list();
expect(deployments.results.length).toBe(1)
expect(deployments.results.length).toBe(1);
});
// Add more tests for pagination, error handling, edge cases, etc.
});
Expand Down
175 changes: 113 additions & 62 deletions integration/typescript/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,135 @@
import { ApiError, Collection, Hardware, Model, ModelVersion, Page, Prediction, Status, Training, Visibility, WebhookEventType } from "replicate";
import {
Account,
ApiError,
Collection,
Deployment,
Hardware,
Model,
ModelVersion,
Page,
Prediction,
Status,
Training,
Visibility,
WebhookEventType,
} from "replicate";

export type Equals<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false;
export type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T,
>() => T extends Y ? 1 : 2
? true
: false;


type AssertFalse<A extends false> = A
type AssertFalse<A extends false> = A;

// @ts-expect-error
export type TestAssertion = AssertFalse<Equals<any, any>>

export type TestApiError = AssertFalse<Equals<ApiError, any>>
export type TestCollection = AssertFalse<Equals<Collection, any>>
export type TestHardware = AssertFalse<Equals<Hardware, any>>
export type TestModel = AssertFalse<Equals<Model, any>>
export type TestModelVersion = AssertFalse<Equals<ModelVersion, any>>
export type TestPage = AssertFalse<Equals<Page<unknown>, any>>
export type TestPrediction = AssertFalse<Equals<Prediction, any>>
export type TestStatus = AssertFalse<Equals<Status, any>>
export type TestTraining = AssertFalse<Equals<Training, any>>
export type TestVisibility = AssertFalse<Equals<Visibility, any>>
export type TestWebhookEventType = AssertFalse<Equals<WebhookEventType, any>>
export type TestAssertion = AssertFalse<Equals<any, any>>;

export type TestAccount = AssertFalse<Equals<Account, any>>;
export type TestApiError = AssertFalse<Equals<ApiError, any>>;
export type TestCollection = AssertFalse<Equals<Collection, any>>;
export type TestDeployment = AssertFalse<Equals<Deployment, any>>;
export type TestHardware = AssertFalse<Equals<Hardware, any>>;
export type TestModel = AssertFalse<Equals<Model, any>>;
export type TestModelVersion = AssertFalse<Equals<ModelVersion, any>>;
export type TestPage = AssertFalse<Equals<Page<unknown>, any>>;
export type TestPrediction = AssertFalse<Equals<Prediction, any>>;
export type TestStatus = AssertFalse<Equals<Status, any>>;
export type TestTraining = AssertFalse<Equals<Training, any>>;
export type TestVisibility = AssertFalse<Equals<Visibility, any>>;
export type TestWebhookEventType = AssertFalse<Equals<WebhookEventType, any>>;

// NOTE: We export the constants to avoid unused varaible issues.

export const collection: Collection = { name: "", slug: "", description: "", models: [] };
export const account: Account = {
type: "user",
name: "",
username: "",
github_url: "",
};
export const collection: Collection = {
name: "",
slug: "",
description: "",
models: [],
};
export const deployment: Deployment = {
owner: "",
name: "",
current_release: {
number: 1,
model: "",
version: "",
created_at: "",
created_by: {
type: "user",
username: "",
name: "",
github_url: "",
},
configuration: {
hardware: "gpu-a100",
min_instances: 0,
max_instances: 5,
},
},
};
export const status: Status = "starting";
export const visibility: Visibility = "public";
export const webhookType: WebhookEventType = "start";
export const err: ApiError = Object.assign(new Error(), {request: new Request("file://"), response: new Response()});
export const err: ApiError = Object.assign(new Error(), {
request: new Request("file://"),
response: new Response(),
});
export const hardware: Hardware = { sku: "", name: "" };
export const model: Model = {
url: "",
owner: "",
name: "",
description: "",
visibility: "public",
github_url: "",
paper_url: "",
license_url: "",
run_count: 10,
cover_image_url: "",
default_example: undefined,
latest_version: undefined,
url: "",
owner: "",
name: "",
description: "",
visibility: "public",
github_url: "",
paper_url: "",
license_url: "",
run_count: 10,
cover_image_url: "",
default_example: undefined,
latest_version: undefined,
};
export const version: ModelVersion = {
id: "",
created_at: "",
cog_version: "",
openapi_schema: "",
id: "",
created_at: "",
cog_version: "",
openapi_schema: "",
};
export const prediction: Prediction = {
id: "",
status: "starting",
model: "",
version: "",
input: {},
output: {},
source: "api",
error: undefined,
logs: "",
metrics: {
predict_time: 100,
},
webhook: "",
webhook_events_filter: [],
created_at: "",
started_at: "",
completed_at: "",
urls: {
get: "",
cancel: "",
stream: "",
},
id: "",
status: "starting",
model: "",
version: "",
input: {},
output: {},
source: "api",
error: undefined,
logs: "",
metrics: {
predict_time: 100,
},
webhook: "",
webhook_events_filter: [],
created_at: "",
started_at: "",
completed_at: "",
urls: {
get: "",
cancel: "",
stream: "",
},
};
export const training: Training = prediction;

export const page: Page<ModelVersion> = {
previous: "",
next: "",
results: [version],
previous: "",
next: "",
results: [version],
};
2 changes: 1 addition & 1 deletion lib/collections.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @typedef {import("./types").Collection} Collection */
/**
/**
* @template T
* @typedef {import("./types").Page<T>} Page
*/
Expand Down
24 changes: 14 additions & 10 deletions lib/deployments.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @template T
* @typedef {import("./types").Page<T>} Page
*/
/** @typedef {import("./types").Deployment} Deployment */
/** @typedef {import("./types").Prediction} Prediction */
/** @typedef {import("./types").WebhookEventType} WebhookEventType */
Expand Down Expand Up @@ -74,32 +78,32 @@ async function getDeployment(deployment_owner, deployment_name) {
* Create a deployment
*
* @param {DeploymentCreateRequest} config - Required. The deployment config.
* @returns {Promise<object>} Resolves with the deployment data
* @returns {Promise<Deployment>} Resolves with the deployment data
*/
async function createDeployment(deployment_config) {
async function createDeployment(config) {
const response = await this.request("/deployments", {
method: "POST",
data: deployment_config,
data: config,
});

return response.json();
}

/**
* @typedef {Object} DeploymentUpdateRequest - Request body for `deployments.update`
* @property {string} version - the 64-character string ID of the model version that you want to deploy
* @property {string} hardware - the SKU for the hardware used to run the model, via `replicate.hardware.list()`
* @property {number} min_instances - the minimum number of instances for scaling
* @property {number} max_instances - the maximum number of instances for scaling
* @property {string=} version - the 64-character string ID of the model version that you want to deploy
* @property {string=} hardware - the SKU for the hardware used to run the model, via `replicate.hardware.list()`
* @property {number=} min_instances - the minimum number of instances for scaling
* @property {number=} max_instances - the maximum number of instances for scaling
*/

/**
* Update an existing deployment
*
* @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
* @param {DeploymentUpdateRequest} deployment_config - Required. The deployment changes.
* @returns {Promise<object>} Resolves with the deployment data
* @param {DeploymentUpdateRequest | {version: string} | {hardware: string} | {min_instances: number} | {max_instance: number}} deployment_config - Required. The deployment changes.
* @returns {Promise<Deployment>} Resolves with the deployment data
*/
async function updateDeployment(
deployment_owner,
Expand All @@ -120,7 +124,7 @@ async function updateDeployment(
/**
* List all deployments
*
* @returns {Promise<object>} - Resolves with a page of deployments
* @returns {Promise<Page<Deployment>>} - Resolves with a page of deployments
*/
async function listDeployments() {
const response = await this.request("/deployments", {
Expand Down
2 changes: 1 addition & 1 deletion lib/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/** @typedef {import("./types").ModelVersion} ModelVersion */
/** @typedef {import("./types").Prediction} Prediction */
/** @typedef {import("./types").Visibility} Visibility */
/**
/**
* @template T
* @typedef {import("./types").Page<T>} Page
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/trainings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* @template T
* @typedef {import("./types").Page<T>} Page
*/
Expand Down

0 comments on commit 6b6eb15

Please sign in to comment.