Skip to content

Commit 66443a8

Browse files
authored
Add support for collections.list endpoint (#66)
* Add support for collections.list endpoint * Remove models field from response
1 parent 83a5025 commit 66443a8

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare module 'replicate' {
1212
name: string;
1313
slug: string;
1414
description: string;
15-
models: Model[];
15+
models?: Model[];
1616
}
1717

1818
export interface Model {
@@ -90,6 +90,7 @@ declare module 'replicate' {
9090
): Promise<Prediction>;
9191

9292
collections: {
93+
list(): Promise<Page<Collection>>;
9394
get(collection_slug: string): Promise<Collection>;
9495
};
9596

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Replicate {
4040
this.fetch = options.fetch || globalThis.fetch;
4141

4242
this.collections = {
43+
list: collections.list.bind(this),
4344
get: collections.get.bind(this),
4445
};
4546

index.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,39 @@ describe('Replicate client', () => {
3535
});
3636
});
3737

38+
describe('collections.list', () => {
39+
test('Calls the correct API route', async () => {
40+
nock(BASE_URL)
41+
.get('/collections')
42+
.reply(200, {
43+
results: [
44+
{
45+
name: 'Super resolution',
46+
slug: 'super-resolution',
47+
description: 'Upscaling models that create high-quality images from low-quality images.',
48+
},
49+
{
50+
name: 'Image classification',
51+
slug: 'image-classification',
52+
description: 'Models that classify images.',
53+
},
54+
],
55+
next: null,
56+
previous: null,
57+
});
58+
59+
const collections = await client.collections.list();
60+
expect(collections.results.length).toBe(2);
61+
});
62+
// Add more tests for error handling, edge cases, etc.
63+
});
64+
3865
describe('collections.get', () => {
3966
test('Calls the correct API route', async () => {
4067
nock(BASE_URL).get('/collections/super-resolution').reply(200, {
4168
name: 'Super resolution',
4269
slug: 'super-resolution',
43-
description:
44-
'Upscaling models that create high-quality images from low-quality images.',
70+
description: 'Upscaling models that create high-quality images from low-quality images.',
4571
models: [],
4672
});
4773

lib/collections.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@ async function getCollection(collection_slug) {
1010
});
1111
}
1212

13-
module.exports = { get: getCollection };
13+
/**
14+
* Fetch a list of model collections
15+
* @returns {Promise<object>} - Resolves with the collections data
16+
*/
17+
async function listCollections() {
18+
return this.request('/collections', {
19+
method: 'GET',
20+
});
21+
}
22+
23+
module.exports = { get: getCollection, list: listCollections };

0 commit comments

Comments
 (0)