Skip to content

Export files methods to client #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -239,6 +239,16 @@ declare module "replicate" {
list(): Promise<Page<Deployment>>;
};

files: {
create(
file: Blob | Buffer,
metadata?: Record<string, unknown>
): Promise<FileObject>;
list(): Promise<Page<FileObject>>;
get(file_id: string): Promise<FileObject>;
delete(file_id: string): Promise<boolean>;
};

hardware: {
list(): Promise<Hardware[]>;
};
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ const {
const accounts = require("./lib/accounts");
const collections = require("./lib/collections");
const deployments = require("./lib/deployments");
const files = require("./lib/files");
const hardware = require("./lib/hardware");
const models = require("./lib/models");
const predictions = require("./lib/predictions");
@@ -77,6 +78,13 @@ class Replicate {
},
};

this.files = {
create: files.create.bind(this),
get: files.get.bind(this),
list: files.list.bind(this),
delete: files.delete.bind(this),
};

this.hardware = {
list: hardware.list.bind(this),
};
140 changes: 115 additions & 25 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -15,6 +15,31 @@ const BASE_URL = "https://api.replicate.com/v1";

nock.disableNetConnect();

const fileTestCases = [
// Skip test case if File type is not available
...(typeof File !== "undefined"
? [
{
type: "file",
value: new File(["hello world"], "file_hello.txt", {
type: "text/plain",
}),
expected: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
},
]
: []),
{
type: "blob",
value: new Blob(["hello world"], { type: "text/plain" }),
expected: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
},
{
type: "buffer",
value: Buffer.from("hello world"),
expected: "data:application/octet-stream;base64,aGVsbG8gd29ybGQ=",
},
];

describe("Replicate client", () => {
let unmatched: any[] = [];
const handleNoMatch = (req: unknown, options: any, body: string) =>
@@ -264,31 +289,6 @@ describe("Replicate client", () => {
}
);

const fileTestCases = [
// Skip test case if File type is not available
...(typeof File !== "undefined"
? [
{
type: "file",
value: new File(["hello world"], "file_hello.txt", {
type: "text/plain",
}),
expected: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
},
]
: []),
{
type: "blob",
value: new Blob(["hello world"], { type: "text/plain" }),
expected: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
},
{
type: "buffer",
value: Buffer.from("hello world"),
expected: "data:application/octet-stream;base64,aGVsbG8gd29ybGQ=",
},
];

test.each(fileTestCases)(
"converts a $type input into a Replicate file URL",
async ({ value: data, type }) => {
@@ -1058,6 +1058,96 @@ describe("Replicate client", () => {
// Add more tests for error handling, edge cases, etc.
});

describe("files.create", () => {
test("Calls the correct API route with the correct payload", async () => {
for (const testCase of fileTestCases) {
nock(BASE_URL)
.post("/files")
.reply(200, {
id: "123",
name: "test-file",
content_type: "application/octet-stream",
size: 1024,
etag: "abc123",
checksum: "sha256:1234567890abcdef",
metadata: {},
created_at: "2023-01-01T00:00:00Z",
expires_at: null,
urls: {
get: "https://api.replicate.com/v1/files/123",
},
});
const file = await client.files.create(testCase.value);
expect(file.id).toBe("123");
expect(file.name).toBe("test-file");
}
});
});

describe("files.get", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL)
.get("/files/123")
.reply(200, {
id: "123",
name: "test-file",
content_type: "application/octet-stream",
size: 1024,
etag: "abc123",
checksum: "sha256:1234567890abcdef",
metadata: {},
created_at: "2023-01-01T00:00:00Z",
expires_at: null,
urls: {
get: "https://api.replicate.com/v1/files/123",
},
});

const file = await client.files.get("123");
expect(file.id).toBe("123");
expect(file.name).toBe("test-file");
});
});

describe("files.list", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL)
.get("/files")
.reply(200, {
next: null,
previous: null,
results: [
{
id: "123",
name: "test-file",
content_type: "application/octet-stream",
size: 1024,
etag: "abc123",
checksum: "sha256:1234567890abcdef",
metadata: {},
created_at: "2023-01-01T00:00:00Z",
expires_at: null,
urls: {
get: "https://api.replicate.com/v1/files/123",
},
},
],
});

const files = await client.files.list();
expect(files.results.length).toBe(1);
expect(files.results[0].id).toBe("123");
});
});

describe("files.delete", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL).delete("/files/123").reply(204);
const success = await client.files.delete("123");
expect(success).toBe(true);
});
});

describe("hardware.list", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL)
4 changes: 2 additions & 2 deletions lib/files.js
Original file line number Diff line number Diff line change
@@ -72,14 +72,14 @@ async function getFile(file_id) {
* Delete a file
*
* @param {string} file_id - Required. The ID of the file.
* @returns {Promise<object>} - Resolves with the deletion confirmation
* @returns {Promise<boolean>} - Resolves with true if the file was deleted
*/
async function deleteFile(file_id) {
const response = await this.request(`/files/${file_id}`, {
method: "DELETE",
});

return response.json();
return response.status === 204;
}

module.exports = {