Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/rabbit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,27 @@ export class Rabbit {

private parseContent(content: Buffer, content_type?: string): RabbitContentType {
let final_content: RabbitContentType = content;
if (content_type === 'application/json') {
final_content = JSON.parse(content.toString());
}
else if (content_type === 'text/string') {
final_content = content.toString();
if (content_type) {
if (content_type === 'application/json') {
final_content = JSON.parse(content.toString());
}
else if (content_type === 'text/string') {
final_content = content.toString();
}
else if (content_type === 'text/number') {
final_content = parseFloat(content.toString());
}
}
else if (content_type === 'text/number') {
final_content = parseFloat(content.toString());
else {
try {
final_content = content.toString();
final_content = JSON.parse(<string>final_content);
}
catch (e) {
// pass
}
}

return final_content;
}

Expand Down Expand Up @@ -188,7 +200,9 @@ export class Rabbit {
*/
public async publishTopic(topic: string, content: RabbitContentType = Buffer.from(''), options: amqplib.Options.Publish = {}): Promise<boolean> {
const encodedContent = this.encodeContent(content);
options.contentType = options.contentType || this.getContentType(content);
if (options.contentType === undefined) {
options.contentType = this.getContentType(content);
}
const channel = await this.pch;
await channel.checkExchange(this.exchange);
return channel.publish(this.exchange, topic, encodedContent, options);
Expand Down
17 changes: 17 additions & 0 deletions test/rabbit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,20 @@ test('rpc with replyTo', (done) => {
expect(msg).toBeNull;
});
});

test('no content-type set', (done) => {
const io = new Io({cogPath: rabbitCog});
expect(io.rabbit).toBeInstanceOf(Rabbit);
if (!io.rabbit) {
return expect(true).toBeFalsy;
}

const topicName = `test.topic.${uuidv4().replace('-', '')}`;
io.rabbit.onTopic(topicName, (msg) => {
expect(msg.properties.contentType).toBeNull;
expect(msg.content).toStrictEqual({hello: 'world'});
(<Rabbit>io.rabbit).close().then(done());
});

io.rabbit.publishTopic(topicName, {hello: 'world'}, {contentType: ''});
});