Skip to content

Commit 66d81af

Browse files
committed
Apply automatic formatting
1 parent 0b69c4c commit 66d81af

14 files changed

+612
-541
lines changed

index.d.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
declare module 'replicate' {
2-
type Status = 'starting' | 'processing' | 'succeeded' | 'failed' | 'canceled';
3-
type Visibility = 'public' | 'private';
4-
type WebhookEventType = 'start' | 'output' | 'logs' | 'completed';
1+
declare module "replicate" {
2+
type Status = "starting" | "processing" | "succeeded" | "failed" | "canceled";
3+
type Visibility = "public" | "private";
4+
type WebhookEventType = "start" | "output" | "logs" | "completed";
55

66
export interface ApiError extends Error {
77
request: Request;
@@ -17,15 +17,15 @@ declare module 'replicate' {
1717

1818
export interface Hardware {
1919
sku: string;
20-
name: string
20+
name: string;
2121
}
2222

2323
export interface Model {
2424
url: string;
2525
owner: string;
2626
name: string;
2727
description?: string;
28-
visibility: 'public' | 'private';
28+
visibility: "public" | "private";
2929
github_url?: string;
3030
paper_url?: string;
3131
license_url?: string;
@@ -49,7 +49,7 @@ declare module 'replicate' {
4949
version: string;
5050
input: object;
5151
output?: any;
52-
source: 'api' | 'web';
52+
source: "api" | "web";
5353
error?: any;
5454
logs?: string;
5555
metrics?: {
@@ -80,13 +80,16 @@ declare module 'replicate' {
8080
auth?: string;
8181
userAgent?: string;
8282
baseUrl?: string;
83-
fetch?: (input: Request | string, init?: RequestInit) => Promise<Response>
83+
fetch?: (
84+
input: Request | string,
85+
init?: RequestInit
86+
) => Promise<Response>;
8487
});
8588

8689
auth: string;
8790
userAgent?: string;
8891
baseUrl?: string;
89-
fetch: (input: Request | string, init?: RequestInit) => Promise<Response>
92+
fetch: (input: Request | string, init?: RequestInit) => Promise<Response>;
9093

9194
run(
9295
identifier: `${string}/${string}` | `${string}/${string}:${string}`,
@@ -100,14 +103,17 @@ declare module 'replicate' {
100103
progress?: (prediction: Prediction) => void
101104
): Promise<object>;
102105

103-
request(route: string | URL, options: {
104-
method?: string;
105-
headers?: object | Headers;
106-
params?: object;
107-
data?: object;
108-
}): Promise<Response>;
106+
request(
107+
route: string | URL,
108+
options: {
109+
method?: string;
110+
headers?: object | Headers;
111+
params?: object;
112+
data?: object;
113+
}
114+
): Promise<Response>;
109115

110-
paginate<T>(endpoint: () => Promise<Page<T>>): AsyncGenerator<[ T ]>;
116+
paginate<T>(endpoint: () => Promise<Page<T>>): AsyncGenerator<[T]>;
111117

112118
wait(
113119
prediction: Prediction,
@@ -138,8 +144,8 @@ declare module 'replicate' {
138144
};
139145

140146
hardware: {
141-
list(): Promise<Hardware[]>
142-
}
147+
list(): Promise<Hardware[]>;
148+
};
143149

144150
models: {
145151
get(model_owner: string, model_name: string): Promise<Model>;
@@ -155,7 +161,8 @@ declare module 'replicate' {
155161
paper_url?: string;
156162
license_url?: string;
157163
cover_image_url?: string;
158-
}): Promise<Model>;
164+
}
165+
): Promise<Model>;
159166
versions: {
160167
list(model_owner: string, model_name: string): Promise<ModelVersion[]>;
161168
get(

index.js

+55-51
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const ApiError = require('./lib/error');
2-
const ModelVersionIdentifier = require('./lib/identifier');
3-
const { withAutomaticRetries } = require('./lib/util');
1+
const ApiError = require("./lib/error");
2+
const ModelVersionIdentifier = require("./lib/identifier");
3+
const { withAutomaticRetries } = require("./lib/util");
44

5-
const collections = require('./lib/collections');
6-
const deployments = require('./lib/deployments');
7-
const hardware = require('./lib/hardware');
8-
const models = require('./lib/models');
9-
const predictions = require('./lib/predictions');
10-
const trainings = require('./lib/trainings');
5+
const collections = require("./lib/collections");
6+
const deployments = require("./lib/deployments");
7+
const hardware = require("./lib/hardware");
8+
const models = require("./lib/models");
9+
const predictions = require("./lib/predictions");
10+
const trainings = require("./lib/trainings");
1111

12-
const packageJSON = require('./package.json');
12+
const packageJSON = require("./package.json");
1313

1414
/**
1515
* Replicate API client library
@@ -43,7 +43,7 @@ class Replicate {
4343
this.auth = options.auth || process.env.REPLICATE_API_TOKEN;
4444
this.userAgent =
4545
options.userAgent || `replicate-javascript/${packageJSON.version}`;
46-
this.baseUrl = options.baseUrl || 'https://api.replicate.com/v1';
46+
this.baseUrl = options.baseUrl || "https://api.replicate.com/v1";
4747
this.fetch = options.fetch || globalThis.fetch;
4848

4949
this.collections = {
@@ -54,7 +54,7 @@ class Replicate {
5454
this.deployments = {
5555
predictions: {
5656
create: deployments.predictions.create.bind(this),
57-
}
57+
},
5858
};
5959

6060
this.hardware = {
@@ -131,26 +131,30 @@ class Replicate {
131131

132132
const { signal } = options;
133133

134-
prediction = await this.wait(prediction, wait || {}, async (updatedPrediction) => {
135-
// Call progress callback with the updated prediction object
136-
if (progress) {
137-
progress(updatedPrediction);
138-
}
139-
140-
if (signal && signal.aborted) {
141-
await this.predictions.cancel(updatedPrediction.id);
142-
return true; // stop polling
134+
prediction = await this.wait(
135+
prediction,
136+
wait || {},
137+
async (updatedPrediction) => {
138+
// Call progress callback with the updated prediction object
139+
if (progress) {
140+
progress(updatedPrediction);
141+
}
142+
143+
if (signal && signal.aborted) {
144+
await this.predictions.cancel(updatedPrediction.id);
145+
return true; // stop polling
146+
}
147+
148+
return false; // continue polling
143149
}
144-
145-
return false; // continue polling
146-
});
150+
);
147151

148152
// Call progress callback with the completed prediction object
149153
if (progress) {
150154
progress(prediction);
151155
}
152156

153-
if (prediction.status === 'failed') {
157+
if (prediction.status === "failed") {
154158
throw new Error(`Prediction failed: ${prediction.error}`);
155159
}
156160

@@ -177,27 +181,23 @@ class Replicate {
177181
url = route;
178182
} else {
179183
url = new URL(
180-
route.startsWith('/') ? route.slice(1) : route,
181-
baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`
184+
route.startsWith("/") ? route.slice(1) : route,
185+
baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`
182186
);
183187
}
184188

185-
const {
186-
method = 'GET',
187-
params = {},
188-
data,
189-
} = options;
189+
const { method = "GET", params = {}, data } = options;
190190

191191
for (const [key, value] of Object.entries(params)) {
192192
url.searchParams.append(key, value);
193193
}
194194

195195
const headers = new Headers();
196196
if (auth) {
197-
headers.append('Authorization', `Token ${auth}`);
197+
headers.append("Authorization", `Token ${auth}`);
198198
}
199-
headers.append('Content-Type', 'application/json');
200-
headers.append('User-Agent', userAgent);
199+
headers.append("Content-Type", "application/json");
200+
headers.append("User-Agent", userAgent);
201201
if (options.headers) {
202202
for (const [key, value] of options.headers.entries()) {
203203
headers.append(key, value);
@@ -210,22 +210,25 @@ class Replicate {
210210
body: data ? JSON.stringify(data) : undefined,
211211
};
212212

213-
const shouldRetry = method === 'GET' ?
214-
(response) => (response.status === 429 || response.status >= 500) :
215-
(response) => (response.status === 429);
213+
const shouldRetry =
214+
method === "GET"
215+
? (response) => response.status === 429 || response.status >= 500
216+
: (response) => response.status === 429;
216217

217218
// Workaround to fix `TypeError: Illegal invocation` error in Cloudflare Workers
218219
// https://github.com/replicate/replicate-javascript/issues/134
219220
const _fetch = this.fetch; // eslint-disable-line no-underscore-dangle
220-
const response = await withAutomaticRetries(async () => _fetch(url, init), { shouldRetry });
221+
const response = await withAutomaticRetries(async () => _fetch(url, init), {
222+
shouldRetry,
223+
});
221224

222225
if (!response.ok) {
223226
const request = new Request(url, init);
224227
const responseText = await response.text();
225228
throw new ApiError(
226229
`Request to ${url} failed with status ${response.status} ${response.statusText}: ${responseText}.`,
227230
request,
228-
response,
231+
response
229232
);
230233
}
231234

@@ -243,11 +246,12 @@ class Replicate {
243246
* @param {Function} endpoint - Function that returns a promise for the next page of results
244247
* @yields {object[]} Each page of results
245248
*/
246-
async * paginate(endpoint) {
249+
async *paginate(endpoint) {
247250
const response = await endpoint();
248251
yield response.results;
249252
if (response.next) {
250-
const nextPage = () => this.request(response.next, { method: 'GET' }).then((r) => r.json());
253+
const nextPage = () =>
254+
this.request(response.next, { method: "GET" }).then((r) => r.json());
251255
yield* this.paginate(nextPage);
252256
}
253257
}
@@ -271,13 +275,13 @@ class Replicate {
271275
async wait(prediction, options, stop) {
272276
const { id } = prediction;
273277
if (!id) {
274-
throw new Error('Invalid prediction');
278+
throw new Error("Invalid prediction");
275279
}
276280

277281
if (
278-
prediction.status === 'succeeded' ||
279-
prediction.status === 'failed' ||
280-
prediction.status === 'canceled'
282+
prediction.status === "succeeded" ||
283+
prediction.status === "failed" ||
284+
prediction.status === "canceled"
281285
) {
282286
return prediction;
283287
}
@@ -290,12 +294,12 @@ class Replicate {
290294
let updatedPrediction = await this.predictions.get(id);
291295

292296
while (
293-
updatedPrediction.status !== 'succeeded' &&
294-
updatedPrediction.status !== 'failed' &&
295-
updatedPrediction.status !== 'canceled'
297+
updatedPrediction.status !== "succeeded" &&
298+
updatedPrediction.status !== "failed" &&
299+
updatedPrediction.status !== "canceled"
296300
) {
297301
/* eslint-disable no-await-in-loop */
298-
if (stop && await stop(updatedPrediction) === true) {
302+
if (stop && (await stop(updatedPrediction)) === true) {
299303
break;
300304
}
301305

@@ -304,7 +308,7 @@ class Replicate {
304308
/* eslint-enable no-await-in-loop */
305309
}
306310

307-
if (updatedPrediction.status === 'failed') {
311+
if (updatedPrediction.status === "failed") {
308312
throw new Error(`Prediction failed: ${updatedPrediction.error}`);
309313
}
310314

0 commit comments

Comments
 (0)