Skip to content

Commit 6b6eb15

Browse files
committed
Add missing webhooks type annotation
1 parent fb22f14 commit 6b6eb15

File tree

7 files changed

+141
-94
lines changed

7 files changed

+141
-94
lines changed

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class Replicate {
131131
list: trainings.list.bind(this),
132132
};
133133

134+
/** @type {webhooks} */
134135
this.webhooks = {
135136
default: {
136137
secret: {
@@ -428,6 +429,7 @@ module.exports.parseProgressFromLogs = parseProgressFromLogs;
428429
* @typedef {import("./lib/error")} ApiError
429430
* @typedef {import("./lib/types").Account} Account
430431
* @typedef {import("./lib/types").Collection} Collection
432+
* @typedef {import("./lib/types").Deployment} Deployment
431433
* @typedef {import("./lib/types").ModelVersion} ModelVersion
432434
* @typedef {import("./lib/types").Hardware} Hardware
433435
* @typedef {import("./lib/types").Model} Model

index.test.ts

+9-19
Original file line numberDiff line numberDiff line change
@@ -790,10 +790,8 @@ describe("Replicate client", () => {
790790
},
791791
configuration: {
792792
hardware: "gpu-t4",
793-
scaling: {
794-
min_instances: 1,
795-
max_instances: 5,
796-
},
793+
min_instances: 1,
794+
max_instances: 5,
797795
},
798796
},
799797
});
@@ -831,10 +829,8 @@ describe("Replicate client", () => {
831829
},
832830
configuration: {
833831
hardware: "gpu-t4",
834-
scaling: {
835-
min_instances: 1,
836-
max_instances: 5,
837-
},
832+
min_instances: 1,
833+
max_instances: 5,
838834
},
839835
},
840836
});
@@ -877,10 +873,8 @@ describe("Replicate client", () => {
877873
},
878874
configuration: {
879875
hardware: "gpu-a40-large",
880-
scaling: {
881-
min_instances: 3,
882-
max_instances: 10,
883-
},
876+
min_instances: 3,
877+
max_instances: 10,
884878
},
885879
},
886880
});
@@ -904,12 +898,8 @@ describe("Replicate client", () => {
904898
expect(deployment.current_release.configuration.hardware).toBe(
905899
"gpu-a40-large"
906900
);
907-
expect(
908-
deployment.current_release.configuration.scaling?.min_instances
909-
).toBe(3);
910-
expect(
911-
deployment.current_release.configuration.scaling?.max_instances
912-
).toBe(10);
901+
expect(deployment.current_release.configuration.min_instances).toBe(3);
902+
expect(deployment.current_release.configuration.max_instances).toBe(10);
913903
});
914904
// Add more tests for error handling, edge cases, etc.
915905
});
@@ -934,7 +924,7 @@ describe("Replicate client", () => {
934924
});
935925

936926
const deployments = await client.deployments.list();
937-
expect(deployments.results.length).toBe(1)
927+
expect(deployments.results.length).toBe(1);
938928
});
939929
// Add more tests for pagination, error handling, edge cases, etc.
940930
});

integration/typescript/types.test.ts

+113-62
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,135 @@
1-
import { ApiError, Collection, Hardware, Model, ModelVersion, Page, Prediction, Status, Training, Visibility, WebhookEventType } from "replicate";
1+
import {
2+
Account,
3+
ApiError,
4+
Collection,
5+
Deployment,
6+
Hardware,
7+
Model,
8+
ModelVersion,
9+
Page,
10+
Prediction,
11+
Status,
12+
Training,
13+
Visibility,
14+
WebhookEventType,
15+
} from "replicate";
216

3-
export type Equals<X, Y> =
4-
(<T>() => T extends X ? 1 : 2) extends
5-
(<T>() => T extends Y ? 1 : 2) ? true : false;
17+
export type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
18+
T,
19+
>() => T extends Y ? 1 : 2
20+
? true
21+
: false;
622

7-
8-
type AssertFalse<A extends false> = A
23+
type AssertFalse<A extends false> = A;
924

1025
// @ts-expect-error
11-
export type TestAssertion = AssertFalse<Equals<any, any>>
12-
13-
export type TestApiError = AssertFalse<Equals<ApiError, any>>
14-
export type TestCollection = AssertFalse<Equals<Collection, any>>
15-
export type TestHardware = AssertFalse<Equals<Hardware, any>>
16-
export type TestModel = AssertFalse<Equals<Model, any>>
17-
export type TestModelVersion = AssertFalse<Equals<ModelVersion, any>>
18-
export type TestPage = AssertFalse<Equals<Page<unknown>, any>>
19-
export type TestPrediction = AssertFalse<Equals<Prediction, any>>
20-
export type TestStatus = AssertFalse<Equals<Status, any>>
21-
export type TestTraining = AssertFalse<Equals<Training, any>>
22-
export type TestVisibility = AssertFalse<Equals<Visibility, any>>
23-
export type TestWebhookEventType = AssertFalse<Equals<WebhookEventType, any>>
26+
export type TestAssertion = AssertFalse<Equals<any, any>>;
2427

28+
export type TestAccount = AssertFalse<Equals<Account, any>>;
29+
export type TestApiError = AssertFalse<Equals<ApiError, any>>;
30+
export type TestCollection = AssertFalse<Equals<Collection, any>>;
31+
export type TestDeployment = AssertFalse<Equals<Deployment, any>>;
32+
export type TestHardware = AssertFalse<Equals<Hardware, any>>;
33+
export type TestModel = AssertFalse<Equals<Model, any>>;
34+
export type TestModelVersion = AssertFalse<Equals<ModelVersion, any>>;
35+
export type TestPage = AssertFalse<Equals<Page<unknown>, any>>;
36+
export type TestPrediction = AssertFalse<Equals<Prediction, any>>;
37+
export type TestStatus = AssertFalse<Equals<Status, any>>;
38+
export type TestTraining = AssertFalse<Equals<Training, any>>;
39+
export type TestVisibility = AssertFalse<Equals<Visibility, any>>;
40+
export type TestWebhookEventType = AssertFalse<Equals<WebhookEventType, any>>;
2541

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

28-
export const collection: Collection = { name: "", slug: "", description: "", models: [] };
44+
export const account: Account = {
45+
type: "user",
46+
name: "",
47+
username: "",
48+
github_url: "",
49+
};
50+
export const collection: Collection = {
51+
name: "",
52+
slug: "",
53+
description: "",
54+
models: [],
55+
};
56+
export const deployment: Deployment = {
57+
owner: "",
58+
name: "",
59+
current_release: {
60+
number: 1,
61+
model: "",
62+
version: "",
63+
created_at: "",
64+
created_by: {
65+
type: "user",
66+
username: "",
67+
name: "",
68+
github_url: "",
69+
},
70+
configuration: {
71+
hardware: "gpu-a100",
72+
min_instances: 0,
73+
max_instances: 5,
74+
},
75+
},
76+
};
2977
export const status: Status = "starting";
3078
export const visibility: Visibility = "public";
3179
export const webhookType: WebhookEventType = "start";
32-
export const err: ApiError = Object.assign(new Error(), {request: new Request("file://"), response: new Response()});
80+
export const err: ApiError = Object.assign(new Error(), {
81+
request: new Request("file://"),
82+
response: new Response(),
83+
});
3384
export const hardware: Hardware = { sku: "", name: "" };
3485
export const model: Model = {
35-
url: "",
36-
owner: "",
37-
name: "",
38-
description: "",
39-
visibility: "public",
40-
github_url: "",
41-
paper_url: "",
42-
license_url: "",
43-
run_count: 10,
44-
cover_image_url: "",
45-
default_example: undefined,
46-
latest_version: undefined,
86+
url: "",
87+
owner: "",
88+
name: "",
89+
description: "",
90+
visibility: "public",
91+
github_url: "",
92+
paper_url: "",
93+
license_url: "",
94+
run_count: 10,
95+
cover_image_url: "",
96+
default_example: undefined,
97+
latest_version: undefined,
4798
};
4899
export const version: ModelVersion = {
49-
id: "",
50-
created_at: "",
51-
cog_version: "",
52-
openapi_schema: "",
100+
id: "",
101+
created_at: "",
102+
cog_version: "",
103+
openapi_schema: "",
53104
};
54105
export const prediction: Prediction = {
55-
id: "",
56-
status: "starting",
57-
model: "",
58-
version: "",
59-
input: {},
60-
output: {},
61-
source: "api",
62-
error: undefined,
63-
logs: "",
64-
metrics: {
65-
predict_time: 100,
66-
},
67-
webhook: "",
68-
webhook_events_filter: [],
69-
created_at: "",
70-
started_at: "",
71-
completed_at: "",
72-
urls: {
73-
get: "",
74-
cancel: "",
75-
stream: "",
76-
},
106+
id: "",
107+
status: "starting",
108+
model: "",
109+
version: "",
110+
input: {},
111+
output: {},
112+
source: "api",
113+
error: undefined,
114+
logs: "",
115+
metrics: {
116+
predict_time: 100,
117+
},
118+
webhook: "",
119+
webhook_events_filter: [],
120+
created_at: "",
121+
started_at: "",
122+
completed_at: "",
123+
urls: {
124+
get: "",
125+
cancel: "",
126+
stream: "",
127+
},
77128
};
78129
export const training: Training = prediction;
79130

80131
export const page: Page<ModelVersion> = {
81-
previous: "",
82-
next: "",
83-
results: [version],
132+
previous: "",
133+
next: "",
134+
results: [version],
84135
};

lib/collections.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @typedef {import("./types").Collection} Collection */
2-
/**
2+
/**
33
* @template T
44
* @typedef {import("./types").Page<T>} Page
55
*/

lib/deployments.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @template T
3+
* @typedef {import("./types").Page<T>} Page
4+
*/
15
/** @typedef {import("./types").Deployment} Deployment */
26
/** @typedef {import("./types").Prediction} Prediction */
37
/** @typedef {import("./types").WebhookEventType} WebhookEventType */
@@ -74,32 +78,32 @@ async function getDeployment(deployment_owner, deployment_name) {
7478
* Create a deployment
7579
*
7680
* @param {DeploymentCreateRequest} config - Required. The deployment config.
77-
* @returns {Promise<object>} Resolves with the deployment data
81+
* @returns {Promise<Deployment>} Resolves with the deployment data
7882
*/
79-
async function createDeployment(deployment_config) {
83+
async function createDeployment(config) {
8084
const response = await this.request("/deployments", {
8185
method: "POST",
82-
data: deployment_config,
86+
data: config,
8387
});
8488

8589
return response.json();
8690
}
8791

8892
/**
8993
* @typedef {Object} DeploymentUpdateRequest - Request body for `deployments.update`
90-
* @property {string} version - the 64-character string ID of the model version that you want to deploy
91-
* @property {string} hardware - the SKU for the hardware used to run the model, via `replicate.hardware.list()`
92-
* @property {number} min_instances - the minimum number of instances for scaling
93-
* @property {number} max_instances - the maximum number of instances for scaling
94+
* @property {string=} version - the 64-character string ID of the model version that you want to deploy
95+
* @property {string=} hardware - the SKU for the hardware used to run the model, via `replicate.hardware.list()`
96+
* @property {number=} min_instances - the minimum number of instances for scaling
97+
* @property {number=} max_instances - the maximum number of instances for scaling
9498
*/
9599

96100
/**
97101
* Update an existing deployment
98102
*
99103
* @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment
100104
* @param {string} deployment_name - Required. The name of the deployment
101-
* @param {DeploymentUpdateRequest} deployment_config - Required. The deployment changes.
102-
* @returns {Promise<object>} Resolves with the deployment data
105+
* @param {DeploymentUpdateRequest | {version: string} | {hardware: string} | {min_instances: number} | {max_instance: number}} deployment_config - Required. The deployment changes.
106+
* @returns {Promise<Deployment>} Resolves with the deployment data
103107
*/
104108
async function updateDeployment(
105109
deployment_owner,
@@ -120,7 +124,7 @@ async function updateDeployment(
120124
/**
121125
* List all deployments
122126
*
123-
* @returns {Promise<object>} - Resolves with a page of deployments
127+
* @returns {Promise<Page<Deployment>>} - Resolves with a page of deployments
124128
*/
125129
async function listDeployments() {
126130
const response = await this.request("/deployments", {

lib/models.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** @typedef {import("./types").ModelVersion} ModelVersion */
33
/** @typedef {import("./types").Prediction} Prediction */
44
/** @typedef {import("./types").Visibility} Visibility */
5-
/**
5+
/**
66
* @template T
77
* @typedef {import("./types").Page<T>} Page
88
*/

lib/trainings.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* @template T
33
* @typedef {import("./types").Page<T>} Page
44
*/

0 commit comments

Comments
 (0)