Skip to content

Commit 2d10b51

Browse files
committed
Add support for files API endpoints
1 parent e204eff commit 2d10b51

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class Replicate {
5858
},
5959
};
6060

61+
this.files = {
62+
create: collections.files.create.bind(this),
63+
list: collections.files.list.bind(this),
64+
get: collections.files.get.bind(this),
65+
delete: collections.files.delete.bind(this),
66+
};
67+
6168
this.hardware = {
6269
list: hardware.list.bind(this),
6370
};

lib/file.js

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

0 commit comments

Comments
 (0)