Skip to content

Commit 485659d

Browse files
committed
Add support for files API endpoints
1 parent e204eff commit 485659d

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

index.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ declare module "replicate" {
1515
models?: Model[];
1616
}
1717

18+
export interface FileObject {
19+
id: string;
20+
name: string;
21+
content_type: string;
22+
size: number;
23+
etag: string;
24+
checksum: string;
25+
metadata: Record<string, any>;
26+
created_at: string;
27+
expires_at: string | null;
28+
urls: {
29+
get: string;
30+
};
31+
}
32+
1833
export interface Hardware {
1934
sku: string;
2035
name: string;
@@ -160,6 +175,16 @@ declare module "replicate" {
160175
};
161176
};
162177

178+
files: {
179+
create: (
180+
file: File | Blob,
181+
metadata?: Record<string, string | number | boolean | null>
182+
) => Promise<FileObject>;
183+
list: () => Promise<FileObject>;
184+
get: (file_id: string) => Promise<FileObject>;
185+
delete: (file_id: string) => Promise<FileObject>;
186+
};
187+
163188
hardware: {
164189
list(): Promise<Hardware[]>;
165190
};

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { withAutomaticRetries } = require("./lib/util");
55

66
const collections = require("./lib/collections");
77
const deployments = require("./lib/deployments");
8+
const files = require("./lib/files");
89
const hardware = require("./lib/hardware");
910
const models = require("./lib/models");
1011
const predictions = require("./lib/predictions");
@@ -58,6 +59,13 @@ class Replicate {
5859
},
5960
};
6061

62+
this.files = {
63+
create: files.create.bind(this),
64+
list: files.list.bind(this),
65+
get: files.get.bind(this),
66+
delete: files.delete.bind(this),
67+
};
68+
6169
this.hardware = {
6270
list: hardware.list.bind(this),
6371
};

lib/files.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const fs = require("node:fs");
2+
const path = require("node:path");
3+
4+
/**
5+
* Create a file
6+
*
7+
* @param {object} file - Required. The file object.
8+
* @param {object} metadata - Optional. User-provided metadata associated with the file.
9+
* @returns {Promise<object>} - Resolves with the file data
10+
*/
11+
async function createFile(file, metadata = {}) {
12+
const form = new FormData();
13+
14+
let fileName;
15+
let fileType;
16+
let fileStream;
17+
if (file instanceof Blob) {
18+
fileName = file.name || `blob_${Date.now()}`;
19+
fileType = file.type || "application/octet-stream";
20+
fileStream = file.stream();
21+
} else if (file instanceof File) {
22+
fileName = file.name || path.basename(file.path);
23+
fileType = file.type || "application/octet-stream";
24+
fileStream = fs.createReadStream(file.path);
25+
} else {
26+
throw new Error("Invalid file argument, must be a Blob or File");
27+
}
28+
29+
form.append("content", fs.createReadStream(file.path), {
30+
filename: fileName,
31+
type: fileType,
32+
});
33+
form.append("metadata", JSON.stringify(metadata), {
34+
type: "application/json",
35+
});
36+
37+
const response = await fetch("/files", {
38+
method: "POST",
39+
body: form,
40+
});
41+
42+
return response.json();
43+
}
44+
45+
/**
46+
* List all files
47+
*
48+
* @returns {Promise<object>} - Resolves with the files data
49+
*/
50+
async function listFiles() {
51+
const response = await fetch("/files", {
52+
method: "GET",
53+
});
54+
55+
return response.json();
56+
}
57+
58+
/**
59+
* Get a file
60+
*
61+
* @param {string} file_id - Required. The ID of the file.
62+
* @returns {Promise<object>} - Resolves with the file data
63+
*/
64+
async function getFile(file_id) {
65+
const response = await fetch(`/files/${file_id}`, {
66+
method: "GET",
67+
});
68+
69+
return response.json();
70+
}
71+
72+
/**
73+
* Delete a file
74+
*
75+
* @param {string} file_id - Required. The ID of the file.
76+
* @returns {Promise<object>} - Resolves with the deletion confirmation
77+
*/
78+
async function deleteFile(file_id) {
79+
const response = await fetch(`/files/${file_id}`, {
80+
method: "DELETE",
81+
});
82+
83+
return response.json();
84+
}
85+
86+
module.exports = {
87+
create: createFile,
88+
list: listFiles,
89+
get: getFile,
90+
delete: deleteFile,
91+
};

0 commit comments

Comments
 (0)