Skip to content

Commit d22fa98

Browse files
committed
Support parameters for fetching models
1 parent 3e756e8 commit d22fa98

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

lib/ReplicateClient.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,33 @@ export default class ReplicateClient {
2121
}
2222
}
2323

24-
model(fullId) {
25-
const idComponents = /^([\w-]+)\/([\w-]+)@(\w+)$/.exec(fullId);
24+
model(idOrParams) {
25+
let owner, name, version;
2626

27-
if (!idComponents) {
28-
throw new ReplicateError(`Invalid ID: ${fullId}`);
29-
}
27+
if (typeof idOrParams === "string") {
28+
const idComponents = /^([\w-]+)\/([\w-]+)@(\w+)$/.exec(idOrParams);
29+
30+
if (!idComponents) {
31+
throw new ReplicateError(`Invalid ID: ${idOrParams}`);
32+
}
3033

31-
const [, owner, name, version] = idComponents;
34+
[, owner, name, version] = idComponents;
35+
} else {
36+
if (idOrParams.owner) {
37+
owner = idOrParams.owner;
38+
name = idOrParams.name;
39+
} else {
40+
const nameComponents = /^([\w-]+)\/([\w-]+)$/.exec(idOrParams.name);
41+
42+
if (!nameComponents) {
43+
throw new ReplicateError(`Invalid name: ${idOrParams.name}`);
44+
}
45+
46+
[, owner, name] = nameComponents;
47+
}
48+
49+
version = idOrParams.version;
50+
}
3251

3352
return new Model({ owner, name, version }, this);
3453
}

lib/ReplicateClient.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ describe("model()", () => {
7171

7272
expect(model).toBeInstanceOf(Model);
7373
});
74+
75+
it("parses ID string", () => {
76+
const model = client.model("test-owner/test-name@testversion");
77+
78+
expect(model.owner).toBe("test-owner");
79+
expect(model.name).toBe("test-name");
80+
expect(model.version).toBe("testversion");
81+
});
82+
83+
it("handles complete params", () => {
84+
const model = client.model({
85+
owner: "test-owner",
86+
name: "test-name",
87+
version: "testversion",
88+
});
89+
90+
expect(model.owner).toBe("test-owner");
91+
expect(model.name).toBe("test-name");
92+
expect(model.version).toBe("testversion");
93+
});
94+
95+
it("handles params with combined name", () => {
96+
const model = client.model({
97+
name: "test-owner/test-name",
98+
version: "testversion",
99+
});
100+
101+
expect(model.owner).toBe("test-owner");
102+
expect(model.name).toBe("test-name");
103+
expect(model.version).toBe("testversion");
104+
});
74105
});
75106

76107
describe("prediction()", () => {

0 commit comments

Comments
 (0)