From 37913d2f1157566d7db6b69f382b08534463a2f0 Mon Sep 17 00:00:00 2001 From: Bart Mont Date: Tue, 11 Apr 2023 10:47:11 +0200 Subject: [PATCH] Allow to send objects to the queue --- README.md | 6 +++--- lib/bindings/queue-binding.ts | 6 +++--- test/queue-binding.spec.ts | 10 ++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 775fa7b..d4af8f7 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ simply by providing an object that conforms to the `Binding` interface (it provi and `toContextBinding()` methods). The `toTrigger()` method returns the object/value that is passed to the Azure Function as the second parameter. -For example, queue triggers are just the message text. +For example, queue triggers are the message text or objects (in servicebus queue). The `toContextBinding()` method should return the object shape that is bound to the `bindingData` property of the context for the input binding/trigger. For example, this would be the entire queue message object for queue triggers. @@ -162,14 +162,14 @@ describe('queue triggered message', () => { ...message, messageText: Buffer.from(message.messageText, 'base64').toString(), }))); - const context = await runStubFunctionFromBindings(functionToTest, [ + const context = await functionRunner(functionToTest, [ { name: 'myInput', direction: 'in', type: 'queueTrigger' } ], { myInput: QueueBinding.createFromDequeuedMessageItem(message) }); expect(context.bindings.myInput).to.have.property('queueTrigger', 'my-message'); }); it('accepts a mocked message', async () => { const messageText = 'my-other-message'; - const context = await runStubFunctionFromBindings(functionToTest, [ + const context = await functionRunner(functionToTest, [ { name: 'myInput', direction: 'in', type: 'queueTrigger' } ], { myInput: QueueBinding.createFromMessageText('my-other-message') }); expect(context.bindings.myInput).to.have.property('queueTrigger', 'my-other-message'); diff --git a/lib/bindings/queue-binding.ts b/lib/bindings/queue-binding.ts index 81c7c41..a049889 100644 --- a/lib/bindings/queue-binding.ts +++ b/lib/bindings/queue-binding.ts @@ -5,7 +5,7 @@ import { ContextBindings } from '@azure/functions'; export type QueueBindingData = { id: string; - queueTrigger: string; + queueTrigger: string | object; dequeueCount: number; expirationTime: string; insertionTime: string; @@ -41,7 +41,7 @@ const MESSAGE_MAP: Record = { }; export class QueueBinding implements Binding { - static createFromMessageText(queueTrigger: string): QueueBinding { + static createFromMessageText(queueTrigger: string | object): QueueBinding { const now = Date.now(); return new QueueBinding({ id: uuid(), @@ -76,7 +76,7 @@ export class QueueBinding implements Binding { return this.data; } - toTrigger(): string { + toTrigger(): string | object { return this.data.queueTrigger; } diff --git a/test/queue-binding.spec.ts b/test/queue-binding.spec.ts index bc45d35..6007060 100644 --- a/test/queue-binding.spec.ts +++ b/test/queue-binding.spec.ts @@ -33,4 +33,14 @@ describe('queue-binding', () => { ); expect(functionStub).to.have.been.calledOnceWithExactly(match.any, 'test-message'); }); + it('executes a queue trigger from object', async () => { + const functionStub = stub().resolves(); + 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'}); + }); });