-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathmessage.js
59 lines (52 loc) · 1.29 KB
/
message.js
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
const MessageTrait = require('./message-trait');
const MessageTraitable = require('./message-traitable');
const Schema = require('./schema');
/**
* Implements functions to deal with a Message object.
* @class
* @alias module:@asyncapi/parser#Message
* @extends MessageTraitable
* @returns {Message}
*/
class Message extends MessageTraitable {
/**
* @returns {string}
*/
uid() {
return this.name() || this.ext('x-parser-message-name') || Buffer.from(JSON.stringify(this._json)).toString('base64');
}
/**
* @returns {Schema}
*/
payload() {
if (!this._json.payload) return null;
return new Schema(this._json.payload);
}
/**
* @returns {MessageTrait[]}
*/
traits() {
const traits = this._json['x-parser-original-traits'] || this._json.traits;
if (!traits) return [];
return traits.map(t => new MessageTrait(t));
}
/**
* @returns {boolean}
*/
hasTraits() {
return !!this._json['x-parser-original-traits'] || !!this._json.traits;
}
/**
* @returns {any}
*/
originalPayload() {
return this._json['x-parser-original-payload'] || this.payload();
}
/**
* @returns {string}
*/
originalSchemaFormat() {
return this._json['x-parser-original-schema-format'] || this.schemaFormat();
}
}
module.exports = Message;