Skip to content

Commit db5723f

Browse files
committed
Map properties via static config
This allows us to rename values fetched from the API semi-automatically.
1 parent d22fa98 commit db5723f

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

lib/Model.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,36 @@ import ReplicateObject from "./ReplicateObject.js";
44
import { sleep } from "./utils.js";
55

66
export default class Model extends ReplicateObject {
7-
constructor({ owner, name, version, id, ...rest }, client) {
7+
static propertyMap = {
8+
id: "version",
9+
};
10+
11+
constructor({ owner, name, version, ...rest }, client) {
812
super(rest, client);
913

10-
if (!owner) {
14+
if (owner) {
15+
this.owner = owner;
16+
}
17+
18+
if (!this.owner) {
1119
throw new ReplicateError("owner is required");
1220
}
1321

14-
this.owner = owner;
22+
if (name) {
23+
this.name = name;
24+
}
1525

16-
if (!name) {
26+
if (!this.name) {
1727
throw new ReplicateError("name is required");
1828
}
1929

20-
this.name = name;
30+
if (version) {
31+
this.version = version;
32+
}
2133

22-
if (!(version || id)) {
34+
if (!this.version) {
2335
throw new ReplicateError("version is required");
2436
}
25-
26-
// We get `id` back from the API, but we want to call it `version` here.
27-
this.version = version || id;
2837
}
2938

3039
actionForGet() {

lib/Model.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ beforeEach(() => {
2222
model = client.model("test-owner/test-name@testversion");
2323
});
2424

25+
describe("constructor()", () => {
26+
it("maps id to version", () => {
27+
const model = new Model({
28+
owner: "test-owner",
29+
name: "test-name",
30+
id: "testversion",
31+
});
32+
33+
expect(model.id).toBeUndefined();
34+
expect(model.version).toBe("testversion");
35+
});
36+
});
37+
2538
describe("load()", () => {
2639
it("makes request to get model version", async () => {
2740
jest.spyOn(client, "request").mockResolvedValue({

lib/Prediction.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ export default class Prediction extends ReplicateObject {
1313
constructor({ id, ...rest }, client) {
1414
super(rest, client);
1515

16-
if (!id) {
17-
throw new ReplicateError("id is required");
16+
if (id) {
17+
this.id = id;
1818
}
1919

20-
this.id = id;
20+
if (!this.id) {
21+
throw new ReplicateError("id is required");
22+
}
2123
}
2224

2325
actionForGet() {

lib/ReplicateObject.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ReplicateError } from "./errors.js";
22

33
export default class ReplicateObject {
4+
static propertyMap = {};
5+
46
static fromJSON(json) {
57
let props;
68
try {
@@ -15,20 +17,15 @@ export default class ReplicateObject {
1517
client;
1618

1719
constructor(props, client) {
18-
for (const key in props) {
19-
this[key] = props[key];
20-
}
21-
20+
this.#setProps(props);
2221
this.client = client;
2322
}
2423

2524
async load() {
2625
const action = this.actionForGet();
2726
const props = await this.client.request(action);
2827

29-
for (const key in props) {
30-
this[key] = props[key];
31-
}
28+
this.#setProps(props);
3229

3330
return this;
3431
}
@@ -40,4 +37,10 @@ export default class ReplicateObject {
4037
actionForGet() {
4138
throw new ReplicateError("Not implemented");
4239
}
40+
41+
#setProps(props) {
42+
for (const key in props) {
43+
this[this.constructor.propertyMap[key] || key] = props[key];
44+
}
45+
}
4346
}

0 commit comments

Comments
 (0)