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
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare module 'replicate' {
name: string;
slug: string;
description: string;
models: Model[];
models?: Model[];
}

export interface Model {
Expand Down Expand Up @@ -90,6 +90,7 @@ declare module 'replicate' {
): Promise<Prediction>;

collections: {
list(): Promise<Page<Collection>>;
get(collection_slug: string): Promise<Collection>;
};

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Replicate {
this.fetch = options.fetch || globalThis.fetch;

this.collections = {
list: collections.list.bind(this),
get: collections.get.bind(this),
};

Expand Down
30 changes: 28 additions & 2 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,39 @@ describe('Replicate client', () => {
});
});

describe('collections.list', () => {
test('Calls the correct API route', async () => {
nock(BASE_URL)
.get('/collections')
.reply(200, {
results: [
{
name: 'Super resolution',
slug: 'super-resolution',
description: 'Upscaling models that create high-quality images from low-quality images.',
},
{
name: 'Image classification',
slug: 'image-classification',
description: 'Models that classify images.',
},
],
next: null,
previous: null,
});

const collections = await client.collections.list();
expect(collections.results.length).toBe(2);
});
// Add more tests for error handling, edge cases, etc.
});

describe('collections.get', () => {
test('Calls the correct API route', async () => {
nock(BASE_URL).get('/collections/super-resolution').reply(200, {
name: 'Super resolution',
slug: 'super-resolution',
description:
'Upscaling models that create high-quality images from low-quality images.',
description: 'Upscaling models that create high-quality images from low-quality images.',
models: [],
});

Expand Down
12 changes: 11 additions & 1 deletion lib/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ async function getCollection(collection_slug) {
});
}

module.exports = { get: getCollection };
/**
* Fetch a list of model collections
* @returns {Promise<object>} - Resolves with the collections data
*/
async function listCollections() {
return this.request('/collections', {
method: 'GET',
});
}

module.exports = { get: getCollection, list: listCollections };