Skip to content

Commit c0c5840

Browse files
committed
fix: use es6 for cloudevents.js
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
1 parent 5110ad4 commit c0c5840

File tree

1 file changed

+111
-116
lines changed

1 file changed

+111
-116
lines changed

Diff for: lib/cloudevent.js

+111-116
Original file line numberDiff line numberDiff line change
@@ -6,122 +6,117 @@ const Formatter = require("./formats/json/formatter.js");
66
*
77
* https://en.wikipedia.org/wiki/Builder_pattern
88
*/
9-
function Cloudevent(_spec, _formatter) {
10-
this.spec = (_spec) ? new _spec(Cloudevent) : new Spec(Cloudevent);
11-
this.formatter = (_formatter) ? new _formatter() : new Formatter();
9+
class CloudEvent {
10+
constructor(_spec, _formatter) {
11+
this.spec = (_spec) ? new _spec(CloudEvent) : new Spec(CloudEvent);
12+
this.formatter = (_formatter) ? new _formatter() : new Formatter();
13+
14+
// The map of extensions
15+
this.extensions = {};
16+
}
17+
18+
getFormats() {
19+
return { json: Formatter };
20+
}
21+
22+
format() {
23+
// Check the constraints
24+
this.spec.check();
25+
26+
// To run asData()
27+
this.getData();
28+
29+
// Then, format
30+
return this.formatter.format(this.spec.payload);
31+
}
32+
33+
toString() {
34+
return this.formatter.toString(this.spec.payload);
35+
}
36+
37+
type(type) {
38+
this.spec.type(type);
39+
return this;
40+
}
41+
42+
getType() {
43+
return this.spec.getType();
44+
}
45+
46+
specversion(version) {
47+
return this.spec.specversion(version);
48+
}
49+
50+
getSpecversion() {
51+
return this.spec.getSpecversion();
52+
}
53+
54+
source(_source) {
55+
this.spec.source(_source);
56+
return this;
57+
}
58+
59+
getSource() {
60+
return this.spec.getSource();
61+
}
62+
63+
id(_id) {
64+
this.spec.id(_id);
65+
return this;
66+
}
67+
68+
getId() {
69+
return this.spec.getId();
70+
}
71+
72+
time(_time) {
73+
this.spec.time(_time);
74+
return this;
75+
}
76+
77+
getTime() {
78+
return this.spec.getTime();
79+
}
80+
81+
schemaurl(_schemaurl) {
82+
this.spec.schemaurl(_schemaurl);
83+
return this;
84+
}
85+
86+
getSchemaurl() {
87+
return this.spec.getSchemaurl();
88+
}
89+
90+
dataContenttype(_contenttype) {
91+
this.spec.dataContenttype(_contenttype);
92+
return this;
93+
}
94+
95+
getDataContenttype() {
96+
return this.spec.getDataContenttype();
97+
}
98+
99+
data(_data) {
100+
this.spec.data(_data);
101+
return this;
102+
}
103+
104+
getData() {
105+
return this.spec.getData();
106+
}
107+
108+
addExtension(key, value) {
109+
this.spec.addExtension(key, value);
110+
111+
// Stores locally
112+
this.extensions[key] = value;
12113

13-
// The map of extensions
14-
this.extensions = {};
114+
return this;
115+
}
116+
117+
getExtensions() {
118+
return this.extensions;
119+
}
15120
}
16121

17-
/*
18-
* To format the payload using the formatter
19-
*/
20-
Cloudevent.prototype.format = function() {
21-
// Check the constraints
22-
this.spec.check();
23-
24-
// To run asData()
25-
this.getData();
26-
27-
// Then, format
28-
return this.formatter.format(this.spec.payload);
29-
};
30-
31-
Cloudevent.prototype.toString = function() {
32-
return this.formatter.toString(this.spec.payload);
33-
};
34-
35-
Cloudevent.prototype.type = function(type) {
36-
this.spec.type(type);
37-
return this;
38-
};
39-
40-
Cloudevent.prototype.getType = function() {
41-
return this.spec.getType();
42-
};
43-
44-
Cloudevent.prototype.specversion = function(version) {
45-
return this.spec.specversion(version);
46-
};
47-
48-
Cloudevent.prototype.getSpecversion = function() {
49-
return this.spec.getSpecversion();
50-
};
51-
52-
Cloudevent.prototype.source = function(_source) {
53-
this.spec.source(_source);
54-
return this;
55-
};
56-
57-
Cloudevent.prototype.getSource = function() {
58-
return this.spec.getSource();
59-
};
60-
61-
Cloudevent.prototype.id = function(_id) {
62-
this.spec.id(_id);
63-
return this;
64-
};
65-
66-
Cloudevent.prototype.getId = function() {
67-
return this.spec.getId();
68-
};
69-
70-
Cloudevent.prototype.time = function(_time) {
71-
this.spec.time(_time);
72-
return this;
73-
};
74-
75-
Cloudevent.prototype.getTime = function() {
76-
return this.spec.getTime();
77-
};
78-
79-
Cloudevent.prototype.schemaurl = function(_schemaurl) {
80-
this.spec.schemaurl(_schemaurl);
81-
return this;
82-
};
83-
84-
Cloudevent.prototype.getSchemaurl = function() {
85-
return this.spec.getSchemaurl();
86-
};
87-
88-
Cloudevent.prototype.dataContenttype = function(_contenttype) {
89-
this.spec.dataContenttype(_contenttype);
90-
return this;
91-
};
92-
93-
Cloudevent.prototype.getDataContenttype = function() {
94-
return this.spec.getDataContenttype();
95-
};
96-
97-
Cloudevent.prototype.data = function(_data) {
98-
this.spec.data(_data);
99-
return this;
100-
};
101-
102-
Cloudevent.prototype.getData = function() {
103-
return this.spec.getData();
104-
};
105-
106-
Cloudevent.prototype.addExtension = function(key, value) {
107-
this.spec.addExtension(key, value);
108-
109-
// Stores localy
110-
this.extensions[key] = value;
111-
112-
return this;
113-
};
114-
115-
Cloudevent.prototype.getExtensions = function() {
116-
return this.extensions;
117-
};
118-
119-
/*
120-
* Export the formats
121-
*/
122-
Cloudevent.formats = {
123-
json: Formatter,
124-
"json0.1": Formatter
125-
};
126-
127-
module.exports = Cloudevent;
122+
module.exports = CloudEvent;

0 commit comments

Comments
 (0)