-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreator.ts
72 lines (60 loc) · 1.63 KB
/
creator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { isValidStringProperty } from "../validation/string";
import Connection from "../connection";
import {ObjectsPath} from "./path";
import {CommandBase} from "../validation/commandBase";
export default class Creator extends CommandBase {
private className?: string;
private consistencyLevel?: string
private id?: string;
private objectsPath: ObjectsPath;
private properties?: any;
private vector: any;
constructor(client: Connection, objectsPath: ObjectsPath) {
super(client)
this.objectsPath = objectsPath;
}
withVector = (vector: any) => {
this.vector = vector;
return this;
};
withClassName = (className: string) => {
this.className = className;
return this;
};
withProperties = (properties: any) => {
this.properties = properties;
return this;
};
withId = (id: string) => {
this.id = id;
return this;
};
withConsistencyLevel = (cl: string) => {
this.consistencyLevel = cl;
return this;
};
validateClassName = () => {
if (!isValidStringProperty(this.className)) {
this.addError("className must be set - set with .withClassName(className)")
}
};
payload = () => ({
vector: this.vector,
properties: this.properties,
class: this.className,
id: this.id,
});
validate = () => {
this.validateClassName();
};
do = () => {
this.validate();
if (this.errors.length > 0) {
return Promise.reject(
new Error("invalid usage: " + this.errors.join(", "))
);
}
return this.objectsPath.buildCreate(this.consistencyLevel)
.then((path: string) => this.client.post(path, this.payload()))
};
}