Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ declare module "replicate" {
response: Response;
}

export interface Account {
type: "user" | "organization";
username: string;
name: string;
github_url?: string;
}

export interface Collection {
name: string;
slug: string;
Expand Down Expand Up @@ -140,6 +147,10 @@ declare module "replicate" {
stop?: (prediction: Prediction) => Promise<boolean>
): Promise<Prediction>;

accounts: {
current(): Promise<Account>;
};

collections: {
list(): Promise<Page<Collection>>;
get(collection_slug: string): Promise<Collection>;
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ModelVersionIdentifier = require("./lib/identifier");
const { Stream } = require("./lib/stream");
const { withAutomaticRetries } = require("./lib/util");

const accounts = require("./lib/accounts");
const collections = require("./lib/collections");
const deployments = require("./lib/deployments");
const hardware = require("./lib/hardware");
Expand Down Expand Up @@ -47,6 +48,10 @@ class Replicate {
this.baseUrl = options.baseUrl || "https://api.replicate.com/v1";
this.fetch = options.fetch || globalThis.fetch;

this.accounts = {
current: accounts.current.bind(this),
};

this.collections = {
list: collections.list.bind(this),
get: collections.get.bind(this),
Expand Down
16 changes: 16 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ describe("Replicate client", () => {
});
});

describe("accounts.current", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL).get("/account").reply(200, {
type: "organization",
username: "replicate",
name: "Replicate",
github_url: "https://github.com/replicate",
});

const account = await client.accounts.current();
expect(account.type).toBe("organization");
expect(account.username).toBe("replicate");
});
// Add more tests for error handling, edge cases, etc.
});

describe("collections.list", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL)
Expand Down
16 changes: 16 additions & 0 deletions lib/accounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Get the current account
*
* @returns {Promise<object>} Resolves with the current account
*/
async function getCurrentAccount() {
const response = await this.request("/account", {
method: "GET",
});

return response.json();
}

module.exports = {
current: getCurrentAccount,
};