-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
53 lines (48 loc) · 1.49 KB
/
index.mjs
File metadata and controls
53 lines (48 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
export class USVideoAPI {
constructor({ apiKey, baseUrl = "https://usvideoapi.com" }) {
this.apiKey = apiKey;
this.baseUrl = baseUrl.replace(/\/$/, "");
}
async request(path, { method = "GET", body } = {}) {
const response = await fetch(`${this.baseUrl}${path}`, {
method,
headers: {
Authorization: `Bearer ${this.apiKey}`,
...(body ? { "Content-Type": "application/json" } : {}),
},
...(body ? { body: JSON.stringify(body) } : {}),
});
return response.json();
}
async createVideo({
prompt,
model = "seedance-1-pro",
image_url,
size = "1080p",
duration = 5,
}) {
const payload = { prompt, model, size, duration };
if (image_url) {
payload.image_url = image_url;
}
return this.request("/v1/videos", { method: "POST", body: payload });
}
async getVideo(jobId) {
return this.request(`/v1/videos/${jobId}`);
}
async listApiKeys() {
return this.request("/v1/api-keys");
}
async waitForVideo(jobId, { pollIntervalMs = 5000, timeoutMs = 300000 } = {}) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const job = await this.getVideo(jobId);
const status = (job.status || "").toLowerCase();
if (["completed", "succeeded", "failed", "error"].includes(status)) {
return job;
}
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
}
throw new Error(`Timed out waiting for video job ${jobId}`);
}
}