Skip to content

Commit 328e37f

Browse files
authored
Run unit tests with TypeScript (#36)
* Fix reported return type of listPredictions * Refactor TypeScript type definitions Inline option types Fix method signatures Define Page<T> type * Run unit tests with TypeScript Fix and refactor unit tests
1 parent 11e1ffb commit 328e37f

9 files changed

+494
-204
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ end_of_line = lf
1010
# editorconfig-tools is unable to ignore longs strings or urls
1111
max_line_length = off
1212

13-
[*.js]
13+
[*.{js,ts}]
1414
quote_type = single
1515

1616
[CHANGELOG.md]

index.d.ts

+34-49
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
type Identifier = `${string}/${string}:${string}`;
2-
type WebhookEventType = "start" | "output" | "logs" | "completed";
2+
type WebhookEventType = 'start' | 'output' | 'logs' | 'completed';
33

4-
declare module "replicate" {
5-
export interface ReplicateOptions {
6-
auth: string;
7-
userAgent?: string;
8-
baseUrl?: string;
9-
}
4+
interface Page<T> {
5+
previous?: string;
6+
next?: string;
7+
results: T[];
8+
}
109

10+
declare module 'replicate' {
1111
export interface Collection {
1212
id: string;
1313
name: string;
@@ -43,39 +43,17 @@ declare module "replicate" {
4343
updated: string;
4444
}
4545

46-
export interface CollectionsGetOptions {
47-
collection_slug: string;
48-
}
49-
50-
export interface ModelsGetOptions {
51-
model_owner: string;
52-
model_name: string;
53-
}
54-
55-
export interface ModelsVersionsListOptions {
56-
model_owner: string;
57-
model_name: string;
58-
}
46+
export default class Replicate {
47+
constructor(options: {
48+
auth: string;
49+
userAgent?: string;
50+
baseUrl?: string;
51+
});
5952

60-
export interface ModelsVersionsGetOptions {
61-
model_owner: string;
62-
model_name: string;
63-
id: string;
64-
}
65-
66-
export interface PredictionsCreateOptions {
67-
version: string;
68-
input: any;
69-
webhook?: string;
70-
webhook_events_filter?: WebhookEventType[];
71-
}
72-
73-
export interface PredictionsGetOptions {
74-
predictionId: string;
75-
}
76-
77-
export class Replicate {
78-
constructor(options: ReplicateOptions);
53+
auth: string;
54+
userAgent?: string;
55+
baseUrl?: string;
56+
private instance: any;
7957

8058
run(
8159
identifier: Identifier,
@@ -87,7 +65,7 @@ declare module "replicate" {
8765
}
8866
): Promise<object>;
8967
request(route: string, parameters: any): Promise<any>;
90-
paginate<T>(endpoint: () => Promise<T>): AsyncGenerator<T[]>;
68+
paginate<T>(endpoint: () => Promise<Page<T>>): AsyncGenerator<[T]>;
9169
wait(
9270
prediction: Prediction,
9371
options: {
@@ -97,23 +75,30 @@ declare module "replicate" {
9775
): Promise<Prediction>;
9876

9977
collections: {
100-
get(options: CollectionsGetOptions): Promise<Collection>;
78+
get(collection_slug: string): Promise<Collection>;
10179
};
10280

10381
models: {
104-
get(options: ModelsGetOptions): Promise<Model>;
82+
get(model_owner: string, model_name: string): Promise<Model>;
10583
versions: {
106-
list(options: ModelsVersionsListOptions): Promise<ModelVersion[]>;
107-
get(options: ModelsVersionsGetOptions): Promise<ModelVersion>;
84+
list(model_owner: string, model_name: string): Promise<ModelVersion[]>;
85+
get(
86+
model_owner: string,
87+
model_name: string,
88+
version_id: string
89+
): Promise<ModelVersion>;
10890
};
10991
};
11092

11193
predictions: {
112-
create(options: PredictionsCreateOptions): Promise<Prediction>;
113-
get(options: PredictionsGetOptions): Promise<Prediction>;
114-
list(): Promise<Prediction[]>;
94+
create(options: {
95+
version: string;
96+
input: any;
97+
webhook?: string;
98+
webhook_events_filter?: WebhookEventType[];
99+
}): Promise<Prediction>;
100+
get(prediction_id: string): Promise<Prediction>;
101+
list(): Promise<Page<Prediction>>;
115102
};
116103
}
117-
118-
export default Replicate;
119104
}

index.test.js

-153
This file was deleted.

0 commit comments

Comments
 (0)