Skip to content

Commit

Permalink
feat: support parsing JSON queue messages
Browse files Browse the repository at this point in the history
Queue messages can be JSON and the function runtime parses these and returns
objects to the function.
  • Loading branch information
dhensby committed Jul 10, 2023
1 parent b3a000b commit dbadd31
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/bindings/queue-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export class QueueBinding implements Binding {
}

toTrigger(): string | object {
// messages that parse as JSON are returned as objects
if (typeof this.data.queueTrigger === 'string') {
try {
return JSON.parse(this.data.queueTrigger);
} catch (e) {
// swallow error
}
}
return this.data.queueTrigger;
}

Expand Down
14 changes: 12 additions & 2 deletions test/queue-binding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,22 @@ describe('queue-binding', () => {
});
it('executes a queue trigger from object', async () => {
const functionStub = stub().resolves();
const queueBinding = QueueBinding.createFromMessageText({test: 'test'});
const queueBinding = QueueBinding.createFromMessageText({ test: 'test' });
await functionRunner(
functionStub,
[{ name: 'queueMessage', type: 'queueTrigger', direction: 'in' }],
{ queueMessage: queueBinding },
);
expect(functionStub).to.have.been.calledOnceWithExactly(match.any, {test: 'test'});
expect(functionStub).to.have.been.calledOnceWithExactly(match.any, { test: 'test' });
});
it('executes a queue trigger from JSON', async () => {
const functionStub = stub().resolves();
const queueBinding = QueueBinding.createFromMessageText(JSON.stringify({ test: true }));
await functionRunner(
functionStub,
[{ name: 'queueMessage', type: 'queueTrigger', direction: 'in' }],
{ queueMessage: queueBinding },
);
expect(functionStub).to.have.been.calledOnceWithExactly(match.any, { test: true });
});
});

0 comments on commit dbadd31

Please sign in to comment.